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