fork(7) download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. #define RANGE 256
  7. uint32_t log2_lut[RANGE];
  8.  
  9. uint16_t fast_gamma_16b(uint32_t* l2_lut, uint32_t g_q24, int n) {
  10. if (n == 0) return 0;
  11.  
  12. uint32_t p_q24 = ((uint64_t)l2_lut[n] * g_q24) >> 24;
  13. int32_t c_fixed = -(int32_t)p_q24;
  14. int i = c_fixed >> 24;
  15. uint32_t f_31 = (c_fixed & 0xFFFFFF) << 7;
  16.  
  17. const uint32_t c[5] = {3676939, 6937762, 16216082, 23252085, 16777278};
  18. uint64_t res = c[0];
  19. for (int k = 1; k < 5; k++) {
  20. res = ((res * f_31) >> 32) + c[k];
  21. }
  22.  
  23. int shift = 8 - i;
  24. uint32_t result = 0;
  25. if (shift < 32) {
  26. uint32_t round = (shift == 0) ? 0 : (1u << (shift - 1));
  27. result = (res + round) >> shift;
  28. }
  29. return (result > 65535) ? 65535 : (uint16_t)result;
  30. }
  31.  
  32. void calc_log2_lut(uint32_t* lut, int range) {
  33. lut[0] = 0;
  34. for (int n = 1; n < range; n++) {
  35. lut[n] = (uint32_t)(-log2(n / ((range - 1) * 1.0)) * 16777216.0 + 0.5);
  36. }
  37. }
  38.  
  39. int main() {
  40. const float gamma = 2.2f;
  41. calc_log2_lut(log2_lut, RANGE);
  42.  
  43. int max_err = 0;
  44. uint32_t g_q24 = (uint32_t)(gamma * 16777216.0);
  45.  
  46. for (int n = 0; n < RANGE; n++) {
  47. uint16_t fast_int = fast_gamma_16b(log2_lut, g_q24, n);
  48. uint16_t standard = (uint16_t)(pow((double)n / (RANGE - 1), gamma) * 65535.0 + 0.5);
  49. int err = (int)fast_int - (int)standard;
  50. if (abs(err) > abs(max_err)) max_err = err;
  51.  
  52. if (err != 0 || n == 0 || n == RANGE - 1) {
  53. printf("%4d | %6d | %6d | %d\n", n, standard, fast_int, err);
  54. }
  55. }
  56. printf(">> Max Error: %d\n", max_err);
  57. return 0;
  58. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
   0 |      0 |      0 | 0
  69 |   3694 |   3695 | 1
  80 |   5115 |   5116 | 1
 109 |  10102 |  10103 | 1
 110 |  10307 |  10308 | 1
 115 |  11366 |  11367 | 1
 126 |  13896 |  13897 | 1
 130 |  14885 |  14886 | 1
 131 |  15138 |  15139 | 1
 138 |  16975 |  16976 | 1
 140 |  17521 |  17522 | 1
 149 |  20095 |  20096 | 1
 150 |  20393 |  20394 | 1
 152 |  20996 |  20997 | 1
 153 |  21301 |  21302 | 1
 159 |  23182 |  23183 | 1
 160 |  23504 |  23505 | 1
 166 |  25487 |  25488 | 1
 167 |  25826 |  25827 | 1
 177 |  29351 |  29352 | 1
 178 |  29717 |  29718 | 1
 181 |  30830 |  30831 | 1
 182 |  31206 |  31207 | 1
 185 |  32349 |  32350 | 1
 186 |  32735 |  32736 | 1
 188 |  33514 |  33515 | 1
 191 |  34702 |  34703 | 1
 192 |  35103 |  35104 | 1
 195 |  36321 |  36322 | 1
 196 |  36732 |  36733 | 1
 198 |  37562 |  37563 | 1
 201 |  38825 |  38826 | 1
 203 |  39680 |  39681 | 1
 206 |  40982 |  40983 | 1
 207 |  41421 |  41422 | 1
 208 |  41862 |  41863 | 1
 209 |  42306 |  42307 | 1
 210 |  42753 |  42754 | 1
 211 |  43202 |  43203 | 1
 212 |  43654 |  43655 | 1
 213 |  44108 |  44109 | 1
 214 |  44565 |  44566 | 1
 217 |  45951 |  45952 | 1
 218 |  46418 |  46419 | 1
 219 |  46888 |  46889 | 1
 220 |  47360 |  47361 | 1
 221 |  47835 |  47836 | 1
 224 |  49275 |  49276 | 1
 227 |  50739 |  50740 | 1
 228 |  51232 |  51233 | 1
 230 |  52226 |  52227 | 1
 232 |  53230 |  53231 | 1
 233 |  53736 |  53737 | 1
 234 |  54245 |  54246 | 1
 235 |  54756 |  54757 | 1
 236 |  55270 |  55271 | 1
 237 |  55787 |  55788 | 1
 238 |  56306 |  56307 | 1
 239 |  56828 |  56829 | 1
 240 |  57352 |  57353 | 1
 241 |  57879 |  57880 | 1
 242 |  58409 |  58410 | 1
 243 |  58941 |  58942 | 1
 244 |  59476 |  59477 | 1
 245 |  60014 |  60015 | 1
 246 |  60554 |  60555 | 1
 247 |  61097 |  61098 | 1
 248 |  61642 |  61643 | 1
 249 |  62190 |  62191 | 1
 250 |  62741 |  62742 | 1
 251 |  63295 |  63296 | 1
 252 |  63851 |  63852 | 1
 253 |  64410 |  64411 | 1
 254 |  64971 |  64972 | 1
 255 |  65535 |  65535 | 0
>> Max Error: 1