6 int main(
int argc,
char **argv) {
10 for (i = 0; i < 256; i++) {
11 for (j = 0; j < 256; j++) {
12 for (alpha = 0; alpha < 256; alpha++) {
13 uint32_t value = (i * alpha + j * (255 - alpha));
14 #if 0 // sloppy, just dividing by 256: 8546790/16777216 = 50.942838% incorrect
17 #elif 0 // better: 446670/16777216 = 2.662361%
18 int a = (value * 0x101) >> 16;
20 #elif 0 // better: 446670/16777216 = 2.662361%
21 int a = (value * 0x4040) >> 22;
23 #elif 0 // rounding, /256: 8323072/16777216 = 49.609375%
24 int a = (value + (1 << 7)) >> 8;
25 int b = round(value / 255.);
26 #elif 0 // rounding: 16224/16777216 = 0.096703%
27 int a = (value * 0x101 + (1 << 15)) >> 16;
28 int b = round(value / 255.);
29 #elif 0 // rounding: 15948/16777216 = 0.095057%
30 int a = (value * 0x8081 + (1 << 22)) >> 23;
31 int b = round(value / 255.);
32 #elif 0 // exact rounding: 0/16777216 = 0.000000%
33 int a = ((value + (1 << 7)) * 0x101) >> 16;
34 int b = round(value / 255.);
35 #elif 0 // exact: 0/16777216 = 0.000000%
36 int a = (value * 0x8081) >> 23;
38 #else // exact: 0/16777216 = 0.000000%
39 int a = (value * 257 + i + j) >> 16;
44 assert(abs(a - b) <= 1);
52 printf(
"%ld/%ld = %f%%\n", err, tot, 100. * err / tot);