shithub: orca

Download patch

ref: 463ff0324c2f01b427476727130bb839e52acac4
parent: 3fa787ada88132cd60c68cffbc4dd2120cc89978
author: cancel <cancel@cancel.fm>
date: Fri Nov 30 08:57:24 EST 2018

Add smarter array countof macro for gcc/clang

--- a/sim.c
+++ b/sim.c
@@ -111,6 +111,18 @@
   return val;
 }
 
+#if defined(__GNUC__) || defined(__clang__)
+#define ORCA_ASSERT_IS_ARRAY(_array)                                           \
+  (sizeof(char[1 - 2 * __builtin_types_compatible_p(                           \
+                           __typeof(_array), __typeof(&(_array)[0]))]) -       \
+   1)
+#define ORCA_ARRAY_COUNTOF(_array)                                             \
+  (sizeof(_array) / sizeof((_array)[0]) + ORCA_ASSERT_IS_ARRAY(_array))
+#else
+// pray
+#define ORCA_ARRAY_COUNTOF(_array) (sizeof(_array) / sizeof(_array[0]))
+#endif
+
 #define ORCA_EXPAND_SOLO_OPER_CHARS(_oper_char, _oper_name)                    \
   Orca_oper_char_##_oper_name = _oper_char,
 #define ORCA_EXPAND_DUAL_OPER_CHARS(_upper_oper_char, _lower_oper_char,        \
@@ -186,9 +198,11 @@
                                  _delta_x, Mark_flag_lock)
 
 #define STORE(_i32_array)                                                      \
-  oper_bank_store(bank_params, width, y, x, _i32_array, sizeof(_i32_array) / sizeof(I32))
+  oper_bank_store(bank_params, width, y, x, _i32_array,                        \
+                  ORCA_ARRAY_COUNTOF(_i32_array))
 #define LOAD(_i32_array)                                                       \
-  oper_bank_load(bank_params, width, y, x, _i32_array, sizeof(_i32_array) / sizeof(I32))
+  oper_bank_load(bank_params, width, y, x, _i32_array,                         \
+                 ORCA_ARRAY_COUNTOF(_i32_array))
 
 #define IN Mark_flag_input
 #define OUT Mark_flag_output