00-e4m4.c (2531B)
1 #include <inttypes.h> 2 #include <limits.h> 3 4 #include "_tap.c" 5 #include "src/sfinx.c" 6 7 static void check_u32(uint32_t got, uint32_t want, const char *what) { 8 tap(got == want, what, "got %" PRIu32 ", want %" PRIu32, got, want); 9 } 10 11 static void check_encode(uint32_t value, uint8_t want, const char *what) { 12 uint8_t got = 0xaa; 13 if (_sfinx_e4m4_encode(value, &got)) { 14 tap(got == want, what, "got 0x%02x, want 0x%02x", got, want); 15 } else { 16 tap(0, what, "encode(%" PRIu32 ") not representable", value); 17 } 18 } 19 20 int main(void) { 21 uint8_t scratch = 0; 22 uint32_t round_ok = 1; 23 int bad = -1; 24 25 tap_begin("sfinx E4M4"); 26 27 check_u32(_sfinx_e4m4_decode(0x00), 0, "decode 0x00"); 28 check_u32(_sfinx_e4m4_decode(0x0f), 15, "decode 0x0f"); 29 check_u32(_sfinx_e4m4_decode(0x10), 16, "decode 0x10"); 30 check_u32(_sfinx_e4m4_decode(0x1c), 28, "decode 0x1c"); 31 check_u32(_sfinx_e4m4_decode(0x1f), 31, "decode 0x1f"); 32 check_u32(_sfinx_e4m4_decode(0x20), 32, "decode 0x20"); 33 check_u32(_sfinx_e4m4_decode(0x28), 48, "decode 0x28"); 34 check_u32(_sfinx_e4m4_decode(0x2f), 62, "decode 0x2f"); 35 check_u32(_sfinx_e4m4_decode(0x30), 64, "decode 0x30"); 36 check_u32(_sfinx_e4m4_decode(0x3f), 124, "decode 0x3f"); 37 check_u32(_sfinx_e4m4_decode(0x40), 128, "decode 0x40"); 38 check_u32(_sfinx_e4m4_decode(0xff), 507904, "decode 0xff"); 39 40 check_encode(0, 0x00, "encode 0"); 41 check_encode(15, 0x0f, "encode 15"); 42 check_encode(16, 0x10, "encode 16"); 43 check_encode(28, 0x1c, "encode 28 (L 224)"); 44 check_encode(32, 0x20, "encode 32 (N default)"); 45 check_encode(48, 0x28, "encode 48 (L 384)"); 46 check_encode(64, 0x30, "encode 64 (L 512)"); 47 check_encode(507904, 0xff, "encode max"); 48 49 check_u32(_sfinx_e4m4_encode(33, &scratch), 0, "reject 33"); 50 check_u32(_sfinx_e4m4_encode(63, &scratch), 0, "reject 63"); 51 check_u32(_sfinx_e4m4_encode(125, &scratch), 0, "reject 125"); 52 check_u32(_sfinx_e4m4_encode(507905, &scratch), 0, "reject max + 1"); 53 check_u32(_sfinx_e4m4_encode(524288, &scratch), 0, "reject 16 << 15"); 54 check_u32(_sfinx_e4m4_encode(INT_MAX, &scratch), 0, "reject INT_MAX"); 55 check_u32(_sfinx_e4m4_encode(UINT32_MAX, &scratch), 0, "reject UINT32_MAX"); 56 57 for (int i = 0; i < 256; i++) { 58 uint8_t round = 0; 59 uint32_t value = _sfinx_e4m4_decode((uint8_t)i); 60 if (!_sfinx_e4m4_encode(value, &round) || round != (uint8_t)i) { 61 round_ok = 0; 62 bad = i; 63 break; 64 } 65 } 66 tap(round_ok, "every byte round trips", "byte 0x%02x did not", (unsigned)bad); 67 68 return tap_plan(); 69 }