panfrost: gen_pack: Add an align() modifier

Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6797>
diff --git a/src/panfrost/lib/gen_pack.py b/src/panfrost/lib/gen_pack.py
index fed4dc1..e2647ea 100644
--- a/src/panfrost/lib/gen_pack.py
+++ b/src/panfrost/lib/gen_pack.py
@@ -47,6 +47,7 @@
 #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))
@@ -213,7 +214,7 @@
         assert(not num_str.startswith('0') and 'octals numbers not allowed')
         return int(num_str)
 
-MODIFIERS = ["shr", "minus"]
+MODIFIERS = ["shr", "minus", "align"]
 
 def parse_modifier(modifier):
     if modifier is None:
@@ -221,7 +222,13 @@
 
     for mod in MODIFIERS:
         if modifier[0:len(mod)] == mod and modifier[len(mod)] == '(' and modifier[-1] == ')':
-            return [mod, int(modifier[(len(mod) + 1):-1])]
+            ret = [mod, int(modifier[(len(mod) + 1):-1])]
+            if ret[0] == 'align':
+                align = ret[1]
+                # Make sure the alignment is a power of 2
+                assert(align > 0 and not(align & (align - 1)));
+
+            return ret
 
     print("Invalid modifier")
     assert(False)
@@ -444,6 +451,8 @@
                         value = "{} >> {}".format(value, field.modifier[1])
                     elif field.modifier[0] == "minus":
                         value = "{} - {}".format(value, field.modifier[1])
+                    elif field.modifier[0] == "align":
+                        value = "ALIGN_POT({}, {})".format(value, field.modifier[1])
 
                 if field.type == "uint" or field.type == "address":
                     s = "__gen_uint(%s, %d, %d)" % \
@@ -549,6 +558,9 @@
             decoded = '{}({}){}'.format(convert, ', '.join(args), suffix)
 
             print('   values->{} = {};'.format(field.name, decoded))
+            if field.modifier and field.modifier[0] == "align":
+                mask = hex(field.modifier[1] - 1)
+                print('   assert(!(values->{} & {}));'.format(field.name, mask))
 
     def emit_print_function(self):
         for field in self.fields: