Created
January 18, 2013 22:14
-
-
Save springmeyer/4569109 to your computer and use it in GitHub Desktop.
test rounding to 16 and 32 bit integer boundaries - output on os x 10.7: https://gist.github.com/4f5eea309fc4656b5261
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* | |
clang++ -o test-bit-rounding test-bit-rounding.cpp -O3 -g -Wconversion -Wsign-conversion;./test-bit-rounding | |
https://github.com/mapnik/mapnik/commit/e7ae5121a2551763e81aa19fe8a4db66226e829e | |
*/ | |
/* | |
warning: operand of ? changes signedness: 'int' to 'unsigned int' [-Wsign-conversion] | |
return width > 7 ? (int(0.125*width) + 1)&~1 : 1; | |
~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~^~~ | |
*/ | |
unsigned old_16(unsigned width) { | |
return width > 7 ? (int(0.125*width) + 1)&~1 : 1; | |
} | |
unsigned new_16(unsigned width) { | |
return ((width + 15) >> 3) & ~1U; | |
} | |
unsigned old_32(unsigned width) { | |
return width > 3 ? (int(0.5*width) + 3)&~3 : 4; | |
} | |
unsigned new_32(unsigned width) { | |
return ((width + 7) >> 1) & ~3U; | |
} | |
int main() { | |
for (unsigned i=0;i<640;++i) { | |
if (new_16(i) != old_16(i)) { | |
std::clog << new_16(i) << " != " << old_16(i) << " for (" << i << " width will crash)\n"; | |
} else { | |
std::clog << new_16(i) << " == " << old_16(i) << " for (" << i << " width is okay)\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment