asahi: Use util/bitpack_helpers.h
authorJason Ekstrand <jason.ekstrand@collabora.com>
Mon, 15 Aug 2022 14:17:37 +0000 (09:17 -0500)
committerMarge Bot <emma+marge@anholt.net>
Tue, 30 Aug 2022 04:28:34 +0000 (04:28 +0000)
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18062>

src/asahi/lib/gen_pack.py
src/asahi/lib/meson.build
src/asahi/lib/tests/test-lod-clamps.cpp

index 6f6f351..430daeb 100644 (file)
@@ -42,44 +42,11 @@ pack_header = """
 #define AGX_PACK_H
 
 #include <stdio.h>
-#include <stdint.h>
-#include <stdbool.h>
-#include <assert.h>
-#include <math.h>
 #include <inttypes.h>
-#include "util/macros.h"
-#include "util/u_math.h"
 
-#define __gen_unpack_float(x, y, z) uif(__gen_unpack_uint(x, y, z))
-
-static inline uint64_t
-__gen_uint(uint64_t v, uint32_t start, uint32_t end)
-{
-#ifndef NDEBUG
-   const int width = end - start + 1;
-   if (width < 64) {
-      const uint64_t max = (1ull << width) - 1;
-      assert(v <= max);
-   }
-#endif
-
-   return v << start;
-}
+#include "util/bitpack_helpers.h"
 
-static inline uint32_t
-__gen_sint(int32_t v, uint32_t start, uint32_t end)
-{
-#ifndef NDEBUG
-   const int width = end - start + 1;
-   if (width < 64) {
-      const int64_t max = (1ll << (width - 1)) - 1;
-      const int64_t min = -(1ll << (width - 1));
-      assert(min <= v && v <= max);
-   }
-#endif
-
-   return (((uint32_t) v) << start) & ((2ll << end) - 1);
-}
+#define __gen_unpack_float(x, y, z) uif(__gen_unpack_uint(x, y, z))
 
 static inline uint64_t
 __gen_unpack_uint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
@@ -100,9 +67,10 @@ __gen_unpack_uint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
  * avoid undefined behaviour for out-of-bounds inputs like +/- infinity.
  */
 static inline uint32_t
-__float_to_lod(float f)
+__gen_pack_lod(float f, uint32_t start, uint32_t end)
 {
-    return (uint32_t) CLAMP(f * (1 << 6), 0 /* 0.0 */, 0x380 /* 14.0 */);
+    uint32_t fixed = CLAMP(f * (1 << 6), 0 /* 0.0 */, 0x380 /* 14.0 */);
+    return util_bitpack_uint(fixed, start, end);
 }
 
 static inline float
@@ -435,23 +403,23 @@ class Group(object):
                         value = "util_logbase2({})".format(value)
 
                 if field.type in ["uint", "hex", "Pixel Format", "address"]:
-                    s = "__gen_uint(%s, %d, %d)" % \
+                    s = "util_bitpack_uint(%s, %d, %d)" % \
                         (value, start, end)
                 elif field.type in self.parser.enums:
-                    s = "__gen_uint(%s, %d, %d)" % \
+                    s = "util_bitpack_uint(%s, %d, %d)" % \
                         (value, start, end)
                 elif field.type == "int":
-                    s = "__gen_sint(%s, %d, %d)" % \
+                    s = "util_bitpack_sint(%s, %d, %d)" % \
                         (value, start, end)
                 elif field.type == "bool":
-                    s = "__gen_uint(%s, %d, %d)" % \
+                    s = "util_bitpack_uint(%s, %d, %d)" % \
                         (value, start, end)
                 elif field.type == "float":
                     assert(start == 0 and end == 31)
-                    s = "__gen_uint(fui({}), 0, 32)".format(value)
+                    s = "util_bitpack_float({})".format(value)
                 elif field.type == "lod":
                     assert(end - start + 1 == 10)
-                    s = "__gen_uint(__float_to_lod(%s), %d, %d)" % (value, start, end)
+                    s = "__gen_pack_lod(%s, %d, %d)" % (value, start, end)
                 else:
                     s = "#error unhandled field {}, type {}".format(contributor.path, field.type)
 
index 856304c..e73a2cf 100644 (file)
@@ -42,6 +42,7 @@ agx_pack = custom_target(
 
 idep_agx_pack = declare_dependency(
   sources : [agx_pack],
+  dependencies : dep_valgrind,
   include_directories : include_directories('.'),
 )
 
@@ -51,7 +52,7 @@ libasahi_lib = static_library(
   include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_asahi],
   c_args : [no_override_init_args],
   gnu_symbol_visibility : 'hidden',
-  dependencies: [dep_libdrm, idep_nir, dep_iokit],
+  dependencies: [dep_libdrm, dep_valgrind, idep_nir, dep_iokit],
   build_by_default : false,
 )
 
@@ -59,7 +60,7 @@ libasahi_decode = static_library(
   'asahi_decode',
   [libasahi_decode_files, agx_pack],
   include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_asahi],
-  dependencies : idep_mesautil,
+  dependencies : [dep_valgrind, idep_mesautil],
   c_args : [no_override_init_args],
   gnu_symbol_visibility : 'hidden',
   build_by_default : false,
index 3bee31f..f222403 100644 (file)
@@ -51,7 +51,7 @@ class LODClamp : public testing::Test {
 TEST_F(LODClamp, Encode)
 {
    for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i)
-      ASSERT_EQ(__float_to_lod(lod_cases[i].f), lod_cases[i].encoded);
+      ASSERT_EQ(__gen_pack_lod(lod_cases[i].f, 0, 9), lod_cases[i].encoded);
 }
 
 TEST_F(LODClamp, Decode)