nir: Refactor nir_src_as_* constant functions
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 26 Jun 2019 01:33:46 +0000 (20:33 -0500)
committerJason Ekstrand <jason@jlekstrand.net>
Wed, 10 Jul 2019 00:20:59 +0000 (00:20 +0000)
Now that we have the nir_const_value_as_* helpers, every one of these
functions is effectively the same except for the suffix they use so we
can easily define them with a repeated macro.  This also means that
they're inline and the fact that the nir_src is being passed by-value
should no longer really hurt anything.

Reviewed-by: Timothy Arceri <tarceri@itsqueeze.com>
src/compiler/nir/nir.c
src/compiler/nir/nir.h

index e0fcc17526c2c5eadb6e41d0563cfe0043d34244..c7413aa64b0c6c04736ccad25266c3d72b557537 100644 (file)
@@ -1239,98 +1239,6 @@ nir_const_value_as_float(nir_const_value value, unsigned bit_size)
    }
 }
 
-int64_t
-nir_src_comp_as_int(nir_src src, unsigned comp)
-{
-   assert(nir_src_is_const(src));
-   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
-
-   assert(comp < load->def.num_components);
-   switch (load->def.bit_size) {
-   /* int1_t uses 0/-1 convention */
-   case 1:  return -(int)load->value[comp].b;
-   case 8:  return load->value[comp].i8;
-   case 16: return load->value[comp].i16;
-   case 32: return load->value[comp].i32;
-   case 64: return load->value[comp].i64;
-   default:
-      unreachable("Invalid bit size");
-   }
-}
-
-uint64_t
-nir_src_comp_as_uint(nir_src src, unsigned comp)
-{
-   assert(nir_src_is_const(src));
-   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
-
-   assert(comp < load->def.num_components);
-   switch (load->def.bit_size) {
-   case 1:  return load->value[comp].b;
-   case 8:  return load->value[comp].u8;
-   case 16: return load->value[comp].u16;
-   case 32: return load->value[comp].u32;
-   case 64: return load->value[comp].u64;
-   default:
-      unreachable("Invalid bit size");
-   }
-}
-
-bool
-nir_src_comp_as_bool(nir_src src, unsigned comp)
-{
-   int64_t i = nir_src_comp_as_int(src, comp);
-
-   /* Booleans of any size use 0/-1 convention */
-   assert(i == 0 || i == -1);
-
-   return i;
-}
-
-double
-nir_src_comp_as_float(nir_src src, unsigned comp)
-{
-   assert(nir_src_is_const(src));
-   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
-
-   assert(comp < load->def.num_components);
-   switch (load->def.bit_size) {
-   case 16: return _mesa_half_to_float(load->value[comp].u16);
-   case 32: return load->value[comp].f32;
-   case 64: return load->value[comp].f64;
-   default:
-      unreachable("Invalid bit size");
-   }
-}
-
-int64_t
-nir_src_as_int(nir_src src)
-{
-   assert(nir_src_num_components(src) == 1);
-   return nir_src_comp_as_int(src, 0);
-}
-
-uint64_t
-nir_src_as_uint(nir_src src)
-{
-   assert(nir_src_num_components(src) == 1);
-   return nir_src_comp_as_uint(src, 0);
-}
-
-bool
-nir_src_as_bool(nir_src src)
-{
-   assert(nir_src_num_components(src) == 1);
-   return nir_src_comp_as_bool(src, 0);
-}
-
-double
-nir_src_as_float(nir_src src)
-{
-   assert(nir_src_num_components(src) == 1);
-   return nir_src_comp_as_float(src, 0);
-}
-
 nir_const_value *
 nir_src_as_const_value(nir_src src)
 {
index 5983da2e2fe663c2837ba654bbe01581ab9a3968..7a8d4425e92d8d0f5b4b03efd950f5237b924006 100644 (file)
@@ -807,15 +807,6 @@ nir_src_is_const(nir_src src)
           src.ssa->parent_instr->type == nir_instr_type_load_const;
 }
 
-int64_t nir_src_as_int(nir_src src);
-uint64_t nir_src_as_uint(nir_src src);
-bool nir_src_as_bool(nir_src src);
-double nir_src_as_float(nir_src src);
-int64_t nir_src_comp_as_int(nir_src src, unsigned component);
-uint64_t nir_src_comp_as_uint(nir_src src, unsigned component);
-bool nir_src_comp_as_bool(nir_src src, unsigned component);
-double nir_src_comp_as_float(nir_src src, unsigned component);
-
 static inline unsigned
 nir_dest_bit_size(nir_dest dest)
 {
@@ -1983,6 +1974,34 @@ NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
                 nir_parallel_copy_instr, instr,
                 type, nir_instr_type_parallel_copy)
 
+
+#define NIR_DEFINE_SRC_AS_CONST(type, suffix)               \
+static inline type                                          \
+nir_src_comp_as_##suffix(nir_src src, unsigned comp)        \
+{                                                           \
+   assert(nir_src_is_const(src));                           \
+   nir_load_const_instr *load =                             \
+      nir_instr_as_load_const(src.ssa->parent_instr);       \
+   assert(comp < load->def.num_components);                 \
+   return nir_const_value_as_##suffix(load->value[comp],    \
+                                      load->def.bit_size);  \
+}                                                           \
+                                                            \
+static inline type                                          \
+nir_src_as_##suffix(nir_src src)                            \
+{                                                           \
+   assert(nir_src_num_components(src) == 1);                \
+   return nir_src_comp_as_##suffix(src, 0);                 \
+}
+
+NIR_DEFINE_SRC_AS_CONST(int64_t,    int)
+NIR_DEFINE_SRC_AS_CONST(uint64_t,   uint)
+NIR_DEFINE_SRC_AS_CONST(bool,       bool)
+NIR_DEFINE_SRC_AS_CONST(double,     float)
+
+#undef NIR_DEFINE_SRC_AS_CONST
+
+
 /*
  * Control flow
  *