Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created January 18, 2013 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springmeyer/4569109 to your computer and use it in GitHub Desktop.
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
#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