Created
July 30, 2019 04:03
-
-
Save sh1boot/1bd6400d240fdeab0443f3dcdf8a2f10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
uint8_t Y(uint8_t r, uint8_t g, uint8_t b) { return ((r * 66 + g * 129 + b * 25 + 128) >> 8) + 16; } | |
uint8_t U(uint8_t r, uint8_t g, uint8_t b) { return ((r * -38 + g * -74 + b * 112 + 128) >> 8) + 128; } | |
uint8_t V(uint8_t r, uint8_t g, uint8_t b) { return ((r * 112 + g * -94 + b * -18 + 128) >> 8) + 128; } | |
uint32_t ref(uint8_t const* p) { | |
uint8_t lr = p[0], lg = p[1], lb = p[2], rr = p[3], rg = p[4], rb = p[5]; | |
uint8_t b0 = Y(lr, lg, lb); | |
uint8_t b1 = (U(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
uint8_t b2 = Y(rr, rg, rb); | |
uint8_t b3 = (V(lr, lg, lb) + U(rr, rg, rb)) >> 1; | |
return (uint32_t)b3 << 24 | (uint32_t)b2 << 16 | (uint32_t)b1 << 8 | b0; | |
} | |
uint32_t t0[256], t1[256], t2[256], t3[256], t4[256], t5[256]; | |
void dut_setup() { | |
uint8_t rgbrgb[11] = { 0 }; | |
uint32_t base = ref(rgbrgb); | |
for (int i = 0; i < 256; ++i) { | |
rgbrgb[5] = i; | |
t0[i] = ref(rgbrgb + 5) - base; | |
t1[i] = ref(rgbrgb + 4) - base; | |
t2[i] = ref(rgbrgb + 3) - base; | |
t3[i] = ref(rgbrgb + 2) - base; | |
t4[i] = ref(rgbrgb + 1) - base; | |
t5[i] = ref(rgbrgb + 0) - base; | |
t0[i] += base; | |
} | |
} | |
uint32_t dut(uint8_t const* p) { | |
return t0[p[0]] + t1[p[1]] + t2[p[2]] + t3[p[3]] + t4[p[4]] + t5[p[5]]; | |
} | |
uint64_t hash24(uint64_t x) { | |
x = ((x ^ (x >> 12)) * 0x46e963) & 0xffffff; | |
x = ((x ^ (x >> 10)) * 0x6da653) & 0xffffff; | |
return x ^ (x >> 12); | |
} | |
int main(void) { | |
dut_setup(); | |
for (int i = 0; i < 65536; ++i) { | |
for (int j = 0; j < 65536; ++j) { | |
int ii = hash24(i), jj = hash24(j); | |
uint8_t test[6] = { ii & 255, (ii >> 8) & 255, (ii >> 16) & 255, | |
jj & 255, (jj >> 8) & 255, (jj >> 16) & 255 }; | |
uint32_t r = ref(test); | |
uint32_t d = dut(test); | |
uint32_t map = 0; | |
int e = 0; | |
for (int k = 0; k < 32; k += 8) { | |
int diff = abs((int)((r >> k) & 255) - (int)((d >> k) & 255)); | |
if (e < diff) e = diff; | |
map |= diff << k; | |
} | |
if (e > 4) printf("%06x,%06x: %08x != %08x %08x %08x\n", ii, jj, d, r, d ^ r, map); | |
} | |
if ((i & 255) == 0) printf("%d\n", i >> 8); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment