Advertisement
OswaldHurlem

One Weird Trick

Feb 2nd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <stddef.h>
  2.  
  3. inline unsigned char SaturatedAdd(unsigned char A, unsigned char B)
  4. {
  5. unsigned char C = A + B;
  6. if (C < A) { C = 255; }
  7.  
  8. return C;
  9. }
  10.  
  11. /*[[[cog
  12. import cog
  13. import math
  14. import sys
  15.  
  16. baseName = "3" # In case you want 2.78 to be referred to as E for some reason
  17. base = 3.0
  18. bytesPerPower = 16
  19. cog.outl("unsigned char Lookup_{1}Log{0}OfSumOfExp{0}_NOver{1}[256] = {{".format(baseName, bytesPerPower))
  20. maxExponent = math.log(sys.float_info.max, base)
  21. verboseLUT = False # Generated code is more informative
  22.  
  23. for i in range(0, 256):
  24. if (i % 16 == 0) or verboseLUT:
  25. cog.out(" ")
  26. exponent = float(i)/bytesPerPower
  27. if (exponent > maxExponent):
  28. if verboseLUT:
  29. cog.outl("{0}, // error too small to measure".format(i))
  30. else:
  31. cog.out("{0}, ".format(i))
  32. else:
  33. magicNumber = bytesPerPower*math.log(base**exponent + 1, base)
  34. magicNumberByte = int(round(magicNumber))
  35. magicNumberByte = sorted((magicNumberByte, 0, 255))[1]
  36. error = magicNumber - magicNumberByte
  37.  
  38. if (abs(error) > 0.5):
  39. cog.msg("WARNING: Magic number at index {0} has value {1}, error of {2} when converting to byte"
  40. .format(i, magicNumber, error))
  41. if verboseLUT:
  42. cog.out("{0}, // ind {1}, error {2:.3f}".format(magicNumberByte, i, error))
  43. else:
  44. cog.out("{0}, ".format(magicNumberByte))
  45. if (i % 16 == 15) or verboseLUT:
  46. cog.out("\n")
  47. cog.outl("};\n")
  48. cog.outl("#define LOOKUP Lookup_{1}Log{0}OfSumOfExp{0}_NOver{1}".format(baseName, bytesPerPower))
  49. cog.outl("// computes {1}*log{0}(pow({0},A/{1}) + pow({0},B/{1})), where log{0} is base-{0} log"
  50. .format(baseName, bytesPerPower))
  51. cog.outl("unsigned char Get_{1}Log{0}OfSumOfExp{0}_NOver{1}(unsigned char A, unsigned char B)"
  52. .format(baseName, bytesPerPower))
  53. ]]]*/
  54. unsigned char Lookup_16Log3OfSumOfExp3_NOver16[256] = {
  55. 10, 11, 11, 12, 12, 13, 13, 14, 15, 15, 16, 17, 17, 18, 19, 19,
  56. 20, 21, 22, 22, 23, 24, 25, 26, 27, 27, 28, 29, 30, 31, 32, 33,
  57. 34, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  58. 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  59. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  60. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  61. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  62. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  63. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  64. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  65. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  66. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  67. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  68. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  69. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  70. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
  71. };
  72.  
  73. #define LOOKUP Lookup_16Log3OfSumOfExp3_NOver16
  74. // computes 16*log3(pow(3,A/16) + pow(3,B/16)), where log3 is base-3 log
  75. unsigned char Get_16Log3OfSumOfExp3_NOver16(unsigned char A, unsigned char B)
  76. //[[[end]]]
  77. {
  78. size_t Max = A > B ? A : B;
  79. size_t Min = A < B ? A : B;
  80. return SaturatedAdd(LOOKUP[Max-Min],Min);
  81. }
  82. #undef LOOKUP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement