1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "agx_pack.h"
7
8 #include <gtest/gtest.h>
9
10 const struct {
11 float f;
12 uint16_t encoded;
13 bool inexact;
14 } lod_cases[] = {
15 /* Lower bound clamp */
16 {-INFINITY, 0x00, true},
17 {-0.1, 0x00, true},
18 {-0.0, 0x00, true},
19
20 /* Exact bounds */
21 {0.0, 0x00},
22 {14.0, 0x380},
23
24 /* Upper bound clamp */
25 {14.1, 0x380, true},
26 {18.1, 0x380, true},
27 {INFINITY, 0x380, true},
28 };
29
30 const struct {
31 uint32_t group_size;
32 uint32_t length;
33 uint32_t value;
34 uint32_t encoded;
35 } group_cases[] = {
36 /* Groups of 16 in a 4-bit word */
37 {16, 4, 0, 0x1}, {16, 4, 1, 0x1}, {16, 4, 16, 0x1}, {16, 4, 17, 0x2},
38 {16, 4, 31, 0x2}, {16, 4, 32, 0x2}, {16, 4, 33, 0x3}, {16, 4, 239, 0xF},
39 {16, 4, 240, 0xF}, {16, 4, 241, 0x0}, {16, 4, 255, 0x0}, {16, 4, 256, 0x0},
40 };
41
TEST(LODClamp,Encode)42 TEST(LODClamp, Encode)
43 {
44 for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i)
45 ASSERT_EQ(__gen_pack_lod(lod_cases[i].f, 0, 9), lod_cases[i].encoded);
46 }
47
TEST(LODClamp,Decode)48 TEST(LODClamp, Decode)
49 {
50 for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i) {
51 if (lod_cases[i].inexact)
52 continue;
53
54 uint32_t cl;
55 memcpy(&cl, &lod_cases[i].encoded, sizeof(lod_cases[i].encoded));
56
57 ASSERT_EQ(__gen_unpack_lod(&cl, 0, 10), lod_cases[i].f);
58 }
59 }
60
TEST(Groups,Encode)61 TEST(Groups, Encode)
62 {
63 for (unsigned i = 0; i < ARRAY_SIZE(group_cases); ++i) {
64 ASSERT_EQ(__gen_to_groups(group_cases[i].value, group_cases[i].group_size,
65 group_cases[i].length),
66 group_cases[i].encoded);
67 }
68 }
69
TEST(Groups,Decode)70 TEST(Groups, Decode)
71 {
72 for (unsigned i = 0; i < ARRAY_SIZE(group_cases); ++i) {
73 unsigned expected =
74 ALIGN_POT(group_cases[i].value, group_cases[i].group_size);
75
76 /* Clamp to minimum encodable */
77 if (group_cases[i].value == 0)
78 expected = group_cases[i].group_size;
79
80 ASSERT_EQ(
81 __gen_from_groups(group_cases[i].encoded, group_cases[i].group_size,
82 group_cases[i].length),
83 expected);
84 }
85 }
86