[Bf-blender-cvs] [910431e3826] functions: start taking alignment of types into account when constructing tuples

Jacques Lucke noreply at git.blender.org
Mon Jul 29 17:58:02 CEST 2019


Commit: 910431e3826083b914ed5949de6170fa533ccacc
Author: Jacques Lucke
Date:   Mon Jul 29 17:53:12 2019 +0200
Branches: functions
https://developer.blender.org/rB910431e3826083b914ed5949de6170fa533ccacc

start taking alignment of types into account when constructing tuples

===================================================================

M	source/blender/blenlib/BLI_math_base.h
M	source/blender/blenlib/intern/math_base_inline.c
M	source/blender/functions/backends/tuple/cpp_types.hpp
M	source/blender/functions/backends/tuple/tuple.cpp

===================================================================

diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h
index 177a1a84b16..da8c4a0a6ad 100644
--- a/source/blender/blenlib/BLI_math_base.h
+++ b/source/blender/blenlib/BLI_math_base.h
@@ -155,6 +155,8 @@ MINLINE int power_of_2_min_i(int n);
 MINLINE unsigned int power_of_2_max_u(unsigned int x);
 MINLINE unsigned int power_of_2_min_u(unsigned int x);
 
+MINLINE unsigned int pad_up(unsigned int x, unsigned int alignment);
+
 MINLINE int divide_round_i(int a, int b);
 MINLINE int mod_i(int i, int n);
 
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c
index 8f30255a08b..1b3689a8a71 100644
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@ -225,6 +225,12 @@ MINLINE unsigned power_of_2_min_u(unsigned x)
   return x - (x >> 1);
 }
 
+MINLINE unsigned int pad_up(unsigned int x, unsigned int alignment)
+{
+  BLI_assert(is_power_of_2_i((int)alignment));
+  return (x + alignment - 1) & ~(alignment - 1);
+}
+
 /* rounding and clamping */
 
 #define _round_clamp_fl_impl(arg, ty, min, max) \
diff --git a/source/blender/functions/backends/tuple/cpp_types.hpp b/source/blender/functions/backends/tuple/cpp_types.hpp
index 93edc2e7250..a9ba15e15de 100644
--- a/source/blender/functions/backends/tuple/cpp_types.hpp
+++ b/source/blender/functions/backends/tuple/cpp_types.hpp
@@ -25,6 +25,11 @@ class CPPTypeInfo : public TypeExtension {
    */
   virtual uint size_of_type() const = 0;
 
+  /**
+   * Get the alignment requirements for this type.
+   */
+  virtual uint alignment() const = 0;
+
   /**
    * Construct a default version of that type at the given pointer.
    */
@@ -72,6 +77,11 @@ template<typename T> class CPPTypeInfoForType : public CPPTypeInfo {
     return sizeof(T);
   }
 
+  uint alignment() const override
+  {
+    return std::alignment_of<T>::value;
+  }
+
   void construct_default(void *ptr) const override
   {
     new (ptr) T();
diff --git a/source/blender/functions/backends/tuple/tuple.cpp b/source/blender/functions/backends/tuple/tuple.cpp
index eecc46f571e..78b7160cba3 100644
--- a/source/blender/functions/backends/tuple/tuple.cpp
+++ b/source/blender/functions/backends/tuple/tuple.cpp
@@ -8,6 +8,8 @@ TupleMeta::TupleMeta(ArrayRef<SharedType> types) : m_types(types)
   m_size__data = 0;
   for (const SharedType &type : types) {
     CPPTypeInfo &info = type->extension<CPPTypeInfo>();
+    uint alignment = info.alignment();
+    m_size__data = pad_up(m_size__data, alignment);
     m_offsets.append(m_size__data);
     m_type_info.append(&info);
     m_size__data += info.size_of_type();



More information about the Bf-blender-cvs mailing list