[virglrenderer-devel] [PATCH 1/2] Simple futurize python srcipts

mimi.vx at gmail.com mimi.vx at gmail.com
Mon Nov 13 09:17:01 UTC 2017


From: Ondřej Súkup <mimi.vx at gmail.com>

---
 src/gallium/auxiliary/util/u_format_pack.py  | 159 ++++++++++++++-------------
 src/gallium/auxiliary/util/u_format_table.py |  85 +++++++-------
 2 files changed, 123 insertions(+), 121 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_format_pack.py b/src/gallium/auxiliary/util/u_format_pack.py
index 37ab07a..890745c 100644
--- a/src/gallium/auxiliary/util/u_format_pack.py
+++ b/src/gallium/auxiliary/util/u_format_pack.py
@@ -35,6 +35,7 @@
  * @author Jose Fonseca <jfonseca at vmware.com>
  */
 '''
+from __future__ import print_function
 
 
 from u_format_parse import *
@@ -54,11 +55,11 @@ def print_channels(format, func):
     if format.nr_channels() <= 1:
         func(format.le_channels, format.le_swizzles)
     else:
-        print '#ifdef PIPE_ARCH_BIG_ENDIAN'
+        print('#ifdef PIPE_ARCH_BIG_ENDIAN')
         func(format.be_channels, format.be_swizzles)
-        print '#else'
+        print('#else')
         func(format.le_channels, format.le_swizzles)
-        print '#endif'
+        print('#endif')
 
 def generate_format_type(format):
     '''Generate a structure that describes the format.'''
@@ -69,18 +70,18 @@ def generate_format_type(format):
         for channel in channels:
             if channel.type == VOID:
                 if channel.size:
-                    print '      unsigned %s:%u;' % (channel.name, channel.size)
+                    print('      unsigned %s:%u;' % (channel.name, channel.size))
             elif channel.type == UNSIGNED:
-                print '      unsigned %s:%u;' % (channel.name, channel.size)
+                print('      unsigned %s:%u;' % (channel.name, channel.size))
             elif channel.type in (SIGNED, FIXED):
-                print '      int %s:%u;' % (channel.name, channel.size)
+                print('      int %s:%u;' % (channel.name, channel.size))
             elif channel.type == FLOAT:
                 if channel.size == 64:
-                    print '      double %s;' % (channel.name)
+                    print('      double %s;' % (channel.name))
                 elif channel.size == 32:
-                    print '      float %s;' % (channel.name)
+                    print('      float %s;' % (channel.name))
                 else:
-                    print '      unsigned %s:%u;' % (channel.name, channel.size)
+                    print('      unsigned %s:%u;' % (channel.name, channel.size))
             else:
                 assert 0
 
@@ -89,41 +90,41 @@ def generate_format_type(format):
             assert channel.size % 8 == 0 and is_pot(channel.size)
             if channel.type == VOID:
                 if channel.size:
-                    print '      uint%u_t %s;' % (channel.size, channel.name)
+                    print('      uint%u_t %s;' % (channel.size, channel.name))
             elif channel.type == UNSIGNED:
-                print '      uint%u_t %s;' % (channel.size, channel.name)
+                print('      uint%u_t %s;' % (channel.size, channel.name))
             elif channel.type in (SIGNED, FIXED):
-                print '      int%u_t %s;' % (channel.size, channel.name)
+                print('      int%u_t %s;' % (channel.size, channel.name))
             elif channel.type == FLOAT:
                 if channel.size == 64:
-                    print '      double %s;' % (channel.name)
+                    print('      double %s;' % (channel.name))
                 elif channel.size == 32:
-                    print '      float %s;' % (channel.name)
+                    print('      float %s;' % (channel.name))
                 elif channel.size == 16:
-                    print '      uint16_t %s;' % (channel.name)
+                    print('      uint16_t %s;' % (channel.name))
                 else:
                     assert 0
             else:
                 assert 0
 
-    print 'union util_format_%s {' % format.short_name()
+    print('union util_format_%s {' % format.short_name())
     
     if format.block_size() in (8, 16, 32, 64):
-        print '   uint%u_t value;' % (format.block_size(),)
+        print('   uint%u_t value;' % (format.block_size(),))
 
     use_bitfields = False
     for channel in format.le_channels:
         if channel.size % 8 or not is_pot(channel.size):
             use_bitfields = True
 
-    print '   struct {'
+    print('   struct {')
     if use_bitfields:
         print_channels(format, generate_bitfields)
     else:
         print_channels(format, generate_full_fields)
-    print '   } chan;'
-    print '};'
-    print
+    print('   } chan;')
+    print('};')
+    print()
 
 
 def is_format_supported(format):
@@ -445,15 +446,15 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
 
     def unpack_from_bitmask(channels, swizzles):
         depth = format.block_size()
-        print '         uint%u_t value = *(const uint%u_t *)src;' % (depth, depth) 
+        print('         uint%u_t value = *(const uint%u_t *)src;' % (depth, depth)) 
 
         # Declare the intermediate variables
         for i in range(format.nr_channels()):
             src_channel = channels[i]
             if src_channel.type == UNSIGNED:
-                print '         uint%u_t %s;' % (depth, src_channel.name)
+                print('         uint%u_t %s;' % (depth, src_channel.name))
             elif src_channel.type == SIGNED:
-                print '         int%u_t %s;' % (depth, src_channel.name)
+                print('         int%u_t %s;' % (depth, src_channel.name))
 
         # Compute the intermediate unshifted values 
         for i in range(format.nr_channels()):
@@ -480,7 +481,7 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
                 value = None
                 
             if value is not None:
-                print '         %s = %s;' % (src_channel.name, value)
+                print('         %s = %s;' % (src_channel.name, value))
                 
         # Convert, swizzle, and store final values
         for i in range(4):
@@ -504,11 +505,11 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
                 value = '0'
             else:
                 assert False
-            print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
+            print('         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
         
     def unpack_from_union(channels, swizzles):
-        print '         union util_format_%s pixel;' % format.short_name()
-        print '         memcpy(&pixel, src, sizeof pixel);'
+        print('         union util_format_%s pixel;' % format.short_name())
+        print('         memcpy(&pixel, src, sizeof pixel);')
     
         for i in range(4):
             swizzle = swizzles[i]
@@ -531,7 +532,7 @@ def generate_unpack_kernel(format, dst_channel, dst_native_type):
                 value = '0'
             else:
                 assert False
-            print '         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i])
+            print('         dst[%u] = %s; /* %s */' % (i, value, 'rgba'[i]))
     
     if format.is_bitmask():
         print_channels(format, unpack_from_bitmask)
@@ -552,7 +553,7 @@ def generate_pack_kernel(format, src_channel, src_native_type):
         inv_swizzle = inv_swizzles(swizzles)
 
         depth = format.block_size()
-        print '         uint%u_t value = 0;' % depth 
+        print('         uint%u_t value = 0;' % depth) 
 
         for i in range(4):
             dst_channel = channels[i]
@@ -578,14 +579,14 @@ def generate_pack_kernel(format, src_channel, src_native_type):
                 else:
                     value = None
                 if value is not None:
-                    print '         value |= %s;' % (value)
+                    print('         value |= %s;' % (value))
                 
-        print '         *(uint%u_t *)dst = value;' % depth 
+        print('         *(uint%u_t *)dst = value;' % depth) 
 
     def pack_into_union(channels, swizzles):
         inv_swizzle = inv_swizzles(swizzles)
 
-        print '         union util_format_%s pixel;' % format.short_name()
+        print('         union util_format_%s pixel;' % format.short_name())
     
         for i in range(4):
             dst_channel = channels[i]
@@ -601,9 +602,9 @@ def generate_pack_kernel(format, src_channel, src_native_type):
                                     dst_channel, dst_native_type, 
                                     value, 
                                     dst_colorspace = dst_colorspace)
-            print '         pixel.chan.%s = %s;' % (dst_channel.name, value)
+            print('         pixel.chan.%s = %s;' % (dst_channel.name, value))
     
-        print '         memcpy(dst, &pixel, sizeof pixel);'
+        print('         memcpy(dst, &pixel, sizeof pixel);')
     
     if format.is_bitmask():
         print_channels(format, pack_into_bitmask)
@@ -616,28 +617,28 @@ def generate_format_unpack(format, dst_channel, dst_native_type, dst_suffix):
 
     name = format.short_name()
 
-    print 'static inline void'
-    print 'util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type)
-    print '{'
+    print('static inline void')
+    print('util_format_%s_unpack_%s(%s *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, dst_suffix, dst_native_type))
+    print('{')
 
     if is_format_supported(format):
-        print '   unsigned x, y;'
-        print '   for(y = 0; y < height; y += %u) {' % (format.block_height,)
-        print '      %s *dst = dst_row;' % (dst_native_type)
-        print '      const uint8_t *src = src_row;'
-        print '      for(x = 0; x < width; x += %u) {' % (format.block_width,)
+        print('   unsigned x, y;')
+        print('   for(y = 0; y < height; y += %u) {' % (format.block_height,))
+        print('      %s *dst = dst_row;' % (dst_native_type))
+        print('      const uint8_t *src = src_row;')
+        print('      for(x = 0; x < width; x += %u) {' % (format.block_width,))
         
         generate_unpack_kernel(format, dst_channel, dst_native_type)
     
-        print '         src += %u;' % (format.block_size() / 8,)
-        print '         dst += 4;'
-        print '      }'
-        print '      src_row += src_stride;'
-        print '      dst_row += dst_stride/sizeof(*dst_row);'
-        print '   }'
-
-    print '}'
-    print
+        print('         src += %u;' % (format.block_size() / 8,))
+        print('         dst += 4;')
+        print('      }')
+        print('      src_row += src_stride;')
+        print('      dst_row += dst_stride/sizeof(*dst_row);')
+        print('   }')
+
+    print('}')
+    print()
     
 
 def generate_format_pack(format, src_channel, src_native_type, src_suffix):
@@ -645,28 +646,28 @@ def generate_format_pack(format, src_channel, src_native_type, src_suffix):
 
     name = format.short_name()
 
-    print 'static inline void'
-    print 'util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type)
-    print '{'
+    print('static inline void')
+    print('util_format_%s_pack_%s(uint8_t *dst_row, unsigned dst_stride, const %s *src_row, unsigned src_stride, unsigned width, unsigned height)' % (name, src_suffix, src_native_type))
+    print('{')
     
     if is_format_supported(format):
-        print '   unsigned x, y;'
-        print '   for(y = 0; y < height; y += %u) {' % (format.block_height,)
-        print '      const %s *src = src_row;' % (src_native_type)
-        print '      uint8_t *dst = dst_row;'
-        print '      for(x = 0; x < width; x += %u) {' % (format.block_width,)
+        print('   unsigned x, y;')
+        print('   for(y = 0; y < height; y += %u) {' % (format.block_height,))
+        print('      const %s *src = src_row;' % (src_native_type))
+        print('      uint8_t *dst = dst_row;')
+        print('      for(x = 0; x < width; x += %u) {' % (format.block_width,))
     
         generate_pack_kernel(format, src_channel, src_native_type)
             
-        print '         src += 4;'
-        print '         dst += %u;' % (format.block_size() / 8,)
-        print '      }'
-        print '      dst_row += dst_stride;'
-        print '      src_row += src_stride/sizeof(*src_row);'
-        print '   }'
+        print('         src += 4;')
+        print('         dst += %u;' % (format.block_size() / 8,))
+        print('      }')
+        print('      dst_row += dst_stride;')
+        print('      src_row += src_stride/sizeof(*src_row);')
+        print('   }')
         
-    print '}'
-    print
+    print('}')
+    print()
     
 
 def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
@@ -674,15 +675,15 @@ def generate_format_fetch(format, dst_channel, dst_native_type, dst_suffix):
 
     name = format.short_name()
 
-    print 'static inline void'
-    print 'util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type)
-    print '{'
+    print('static inline void')
+    print('util_format_%s_fetch_%s(%s *dst, const uint8_t *src, unsigned i, unsigned j)' % (name, dst_suffix, dst_native_type))
+    print('{')
 
     if is_format_supported(format):
         generate_unpack_kernel(format, dst_channel, dst_native_type)
 
-    print '}'
-    print
+    print('}')
+    print()
 
 
 def is_format_hand_written(format):
@@ -690,10 +691,10 @@ def is_format_hand_written(format):
 
 
 def generate(formats):
-    print
-    print '#include "pipe/p_compiler.h"'
-    print '#include "u_math.h"'
-    print '#include "u_half.h"'
-    print '#include "u_format.h"'
-    print
+    print()
+    print('#include "pipe/p_compiler.h"')
+    print('#include "u_math.h"')
+    print('#include "u_half.h"')
+    print('#include "u_format.h"')
+    print()
 
diff --git a/src/gallium/auxiliary/util/u_format_table.py b/src/gallium/auxiliary/util/u_format_table.py
index 50a78ef..50146b2 100755
--- a/src/gallium/auxiliary/util/u_format_table.py
+++ b/src/gallium/auxiliary/util/u_format_table.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+from __future__ import print_function
 CopyRight = '''
 /**************************************************************************
  *
@@ -80,18 +81,18 @@ swizzle_map = {
 
 
 def write_format_table(formats):
-    print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
-    print
+    print('/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */')
+    print()
     # This will print the copyright message on the top of this file
-    print CopyRight.strip()
-    print
-    print '#include "u_format.h"'
-    print
+    print(CopyRight.strip())
+    print()
+    print('#include "u_format.h"')
+    print()
     
     u_format_pack.generate(formats)
     
     def do_channel_array(channels, swizzles):
-        print "   {"
+        print("   {")
         for i in range(4):
             channel = channels[i]
             if i < 3:
@@ -99,13 +100,13 @@ def write_format_table(formats):
             else:
                 sep = ""
             if channel.size:
-                print "      {%s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, channel.shift, sep, "xyzw"[i], channel.name)
+                print("      {%s, %s, %s, %u, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, channel.shift, sep, "xyzw"[i], channel.name))
             else:
-                print "      {0, 0, 0, 0, 0}%s" % (sep,)
-        print "   },"
+                print("      {0, 0, 0, 0, 0}%s" % (sep,))
+        print("   },")
 
     def do_swizzle_array(channels, swizzles):
-        print "   {"
+        print("   {")
         for i in range(4):
             swizzle = swizzles[i]
             if i < 3:
@@ -116,43 +117,43 @@ def write_format_table(formats):
                 comment = colorspace_channels_map[format.colorspace][i]
             except (KeyError, IndexError):
                 comment = 'ignored'
-            print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
-        print "   },"
+            print("      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment))
+        print("   },")
 
     for format in formats:
-        print 'const struct util_format_description'
-        print 'util_format_%s_description = {' % (format.short_name(),)
-        print "   %s," % (format.name,)
-        print "   \"%s\"," % (format.name,)
-        print "   \"%s\"," % (format.short_name(),)
-        print "   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
-        print "   %s," % (layout_map(format.layout),)
-        print "   %u,\t/* nr_channels */" % (format.nr_channels(),)
-        print "   %s,\t/* is_array */" % (bool_map(format.is_array()),)
-        print "   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
-        print "   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
+        print('const struct util_format_description')
+        print('util_format_%s_description = {' % (format.short_name(),))
+        print("   %s," % (format.name,))
+        print("   \"%s\"," % (format.name,))
+        print("   \"%s\"," % (format.short_name(),))
+        print("   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size()))
+        print("   %s," % (layout_map(format.layout),))
+        print("   %u,\t/* nr_channels */" % (format.nr_channels(),))
+        print("   %s,\t/* is_array */" % (bool_map(format.is_array()),))
+        print("   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),))
+        print("   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),))
         u_format_pack.print_channels(format, do_channel_array)
         u_format_pack.print_channels(format, do_swizzle_array)
-        print "   %s," % (colorspace_map(format.colorspace),)
-        print "};"
-        print
+        print("   %s," % (colorspace_map(format.colorspace),))
+        print("};")
+        print()
         
-    print "const struct util_format_description *"
-    print "util_format_description(enum pipe_format format)"
-    print "{"
-    print "   if (format >= PIPE_FORMAT_COUNT) {"
-    print "      return NULL;"
-    print "   }"
-    print
-    print "   switch (format) {"
+    print("const struct util_format_description *")
+    print("util_format_description(enum pipe_format format)")
+    print("{")
+    print("   if (format >= PIPE_FORMAT_COUNT) {")
+    print("      return NULL;")
+    print("   }")
+    print()
+    print("   switch (format) {")
     for format in formats:
-        print "   case %s:" % format.name
-        print "      return &util_format_%s_description;" % (format.short_name(),)
-    print "   default:"
-    print "      return NULL;"
-    print "   }"
-    print "}"
-    print
+        print("   case %s:" % format.name)
+        print("      return &util_format_%s_description;" % (format.short_name(),))
+    print("   default:")
+    print("      return NULL;")
+    print("   }")
+    print("}")
+    print()
 
 
 def main():
-- 
2.15.0



More information about the virglrenderer-devel mailing list