short_macros.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef __c3dlas__short_macros_h__
  2. #define __c3dlas__short_macros_h__
  3. #ifndef PP_NARG
  4. #define PP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, N, ...) N
  5. #define PP_RSEQ_N() 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  6. #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
  7. #define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
  8. #endif
  9. typedef uint8_t bool;
  10. typedef int8_t s8;
  11. typedef int16_t s16;
  12. typedef int32_t s32;
  13. typedef int64_t s64;
  14. typedef uint8_t u8;
  15. typedef uint16_t u16;
  16. typedef uint32_t u32;
  17. typedef uint64_t u64;
  18. typedef float f32;
  19. typedef double f64;
  20. typedef Vector4 quat;
  21. typedef Matrix Matrix4;
  22. typedef Matrix4 mat4;
  23. typedef Matrix3 mat3;
  24. typedef Vector2 vec2;
  25. typedef Vector3 vec3;
  26. typedef Vector4 vec4;
  27. typedef Vector2d vec2d;
  28. typedef Vector3d vec3d;
  29. typedef Vector4d vec4d;
  30. typedef Vector2i vec2i;
  31. typedef Vector3i vec3i;
  32. typedef Vector4i vec4i;
  33. typedef Vector2l vec2l;
  34. typedef Vector3l vec3l;
  35. typedef Vector4l vec4l;
  36. // __attribute__((always_inline)) forces gcc to inline these trivial functions even during -O0
  37. #define _GETTER_FN(type1, type2, contentsx, contentsy, contentsz, contentsw) \
  38. static inline type2 __attribute__((always_inline)) _##type1##_getx(type1 a, type2 c) { return contentsx; } \
  39. static inline type2 __attribute__((always_inline)) _##type1##_gety(type1 a, type2 c) { return contentsy; } \
  40. static inline type2 __attribute__((always_inline)) _##type1##_getz(type1 a, type2 c) { return contentsz; } \
  41. static inline type2 __attribute__((always_inline)) _##type1##_getw(type1 a, type2 c) { return contentsw; } \
  42. #define _VECTOR_GETTERS(X, ...) \
  43. X(__VA_ARGS__ __VA_OPT__(,) int, int, a, a, a, a ) \
  44. X(__VA_ARGS__ __VA_OPT__(,) float, float, a, a, a, a ) \
  45. X(__VA_ARGS__ __VA_OPT__(,) long, long, a, a, a, a ) \
  46. X(__VA_ARGS__ __VA_OPT__(,) double, double, a, a, a, a ) \
  47. X(__VA_ARGS__ __VA_OPT__(,) vec2, float, a.x, a.y, c, c ) \
  48. X(__VA_ARGS__ __VA_OPT__(,) vec3, float, a.x, a.y, a.z, c ) \
  49. X(__VA_ARGS__ __VA_OPT__(,) vec4, float, a.x, a.y, a.z, a.w) \
  50. X(__VA_ARGS__ __VA_OPT__(,) vec2d, double, a.x, a.y, c, c ) \
  51. X(__VA_ARGS__ __VA_OPT__(,) vec3d, double, a.x, a.y, a.z, c ) \
  52. X(__VA_ARGS__ __VA_OPT__(,) vec4d, double, a.x, a.y, a.z, a.w) \
  53. X(__VA_ARGS__ __VA_OPT__(,) vec2l, s64, a.x, a.y, c, c ) \
  54. X(__VA_ARGS__ __VA_OPT__(,) vec3l, s64, a.x, a.y, a.z, c ) \
  55. X(__VA_ARGS__ __VA_OPT__(,) vec4l, s64, a.x, a.y, a.z, a.w) \
  56. X(__VA_ARGS__ __VA_OPT__(,) vec2i, s32, a.x, a.y, c, c ) \
  57. X(__VA_ARGS__ __VA_OPT__(,) vec3i, s32, a.x, a.y, a.z, c ) \
  58. X(__VA_ARGS__ __VA_OPT__(,) vec4i, s32, a.x, a.y, a.z, a.w)
  59. _VECTOR_GETTERS(_GETTER_FN)
  60. #ifndef CAT
  61. #define CAT(a,b) a##b
  62. #endif
  63. #define CAT4(a,b,c,d) a##b##c##d
  64. #define V__N(a, narg, ...) CAT(a, narg)(__VA_ARGS__)
  65. #define _V_HELPER(a, b, ...) b: CAT4(_, b, _get, a),
  66. #define _V_GET(a, b, c) _Generic(a, \
  67. _VECTOR_GETTERS(_V_HELPER, b) \
  68. default: _float_getx \
  69. )(a, c)
  70. #define V2(...) V__N(V2_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  71. #define V2_1(a) ((Vector2){.x=_V_GET(a, x, 0),.y=_V_GET(a, y, 0)})
  72. #define V2_2(_x,_y) ((Vector2){.x=(_x),.y=(_y)})
  73. #define V2_3(_x,_y,...) ((Vector2){.x=(_x),.y=(_y)})
  74. #define V3(...) V__N(V3_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  75. #define V3_1(a) ((Vector3){.x=_V_GET(a, x, 0),.y=_V_GET(a, y, 0),.z=_V_GET(a, z, 0)})
  76. #define V3_2(a, _z) ((Vector3){.x=_V_GET(a, x, 0),.y=_V_GET(a, y, 0),.z=_z})
  77. #define V3_3(_x,_y,_z) ((Vector3){.x=(_x),.y=(_y),.z=(_z)})
  78. #define V3_4(_x,_y,_z,...) ((Vector3){.x=(_x),.y=(_y),.z=(_z)})
  79. #define V4(...) V__N(V4_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  80. #define V4_1(a) ((Vector4){.x=_V_GET(a, x, 0),.y=_V_GET(a, y, 0),.z=_V_GET(a, z, 0),.w=_V_GET(a, w, 1)})
  81. #define V4_2(a, b) ((Vector4){.x=_V_GET(a, x, 0),.y=_V_GET(a, y, 0),.z=_V_GET(a, z, 0),b})
  82. #define V4_3(_x,_y,_z) ((Vector4){.x=(_x),.y=(_y),.z=(_z),.w=1})
  83. #define V4_4(_x,_y,_z,_w) ((Vector4){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  84. #define V4_5(_x,_y,_z,_w,...) ((Vector4){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  85. #define _Vi_GET(a, b, c) _Generic(a, \
  86. _VECTOR_GETTERS(_V_HELPER, b) \
  87. default: _int_getx \
  88. )(a, c)
  89. #define V2i(...) V__N(V2i_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  90. #define V2i_1(a) ((Vector2i){.x=_Vi_GET(a, x, 0),.y=_Vi_GET(a, y, 0)})
  91. #define V2i_2(_x,_y) ((Vector2i){.x=(_x),.y=(_y)})
  92. #define V2i_3(_x,_y,...) ((Vector2i){.x=(_x),.y=(_y)})
  93. #define V3i(...) V__N(V3i_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  94. #define V3i_1(a) ((Vector3i){.x=_Vi_GET(a, x, 0),.y=_Vi_GET(a, y, 0),.z=_Vi_GET(a, z, 0)})
  95. #define V3i_2(a, _z) ((Vector3i){.x=_Vi_GET(a, x, 0),.y=_Vi_GET(a, y, 0),.z=_z})
  96. #define V3i_3(_x,_y,_z) ((Vector3i){.x=(_x),.y=(_y),.z=(_z)})
  97. #define V3i_4(_x,_y,_z,...) ((Vector3i){.x=(_x),.y=(_y),.z=(_z)})
  98. #define V4i(...) V__N(V4i_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  99. #define V4i_1(a) ((Vector4i){.x=_Vi_GET(a, x, 0),.y=_Vi_GET(a, y, 0),.z=_Vi_GET(a, z, 0),.w=_Vi_GET(a, w, 1)})
  100. #define V4i_2(a, b) ((Vector4i){.x=_Vi_GET(a, x, 0),.y=_Vi_GET(a, y, 0),.z=_Vi_GET(a, z, 0),b})
  101. #define V4i_3(_x,_y,_z) ((Vector4i){.x=(_x),.y=(_y),.z=(_z),.w=1})
  102. #define V4i_4(_x,_y,_z,_w) ((Vector4i){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  103. #define V4i_5(_x,_y,_z,_w,...) ((Vector4i){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  104. #define _Vl_GET(a, b, c) _Generic(a, \
  105. _VECTOR_GETTERS(_V_HELPER, b) \
  106. default: _long_getx \
  107. )(a, c)
  108. #define V2l(...) V__N(V2l_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  109. #define V2l_1(a) ((Vector2l){.x=_Vl_GET(a, x, 0),.y=_Vl_GET(a, y, 0)})
  110. #define V2l_2(_x,_y) ((Vector2l){.x=(_x),.y=(_y)})
  111. #define V2l_3(_x,_y,...) ((Vector2l){.x=(_x),.y=(_y)})
  112. #define V3l(...) V__N(V3l_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  113. #define V3l_1(a) ((Vector3l){.x=_Vl_GET(a, x, 0),.y=_Vl_GET(a, y, 0),.z=_Vl_GET(a, z, 0)})
  114. #define V3l_2(a, _z) ((Vector3l){.x=_Vl_GET(a, x, 0),.y=_Vl_GET(a, y, 0),.z=_z})c,
  115. #define V3l_3(_x,_y,_z) ((Vector3l){.x=(_x),.y=(_y),.z=(_z)})
  116. #define V3l_4(_x,_y,_z,...) ((Vector3l){.x=(_x),.y=(_y),.z=(_z)})
  117. #define V4l(...) V__N(V4l_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  118. #define V4l_1(a) ((Vector4l){.x=_Vl_GET(a, x, 0),.y=_Vl_GET(a, y, 0),.z=_Vl_GET(a, z, 0),.w=_Vl_GET(a, w, 1)})
  119. #define V4l_2(a, b) ((Vector4l){.x=_Vl_GET(a, x, 0),.y=_Vl_GET(a, y, 0),.z=_Vl_GET(a, z, 0),b})
  120. #define V4l_3(_x,_y,_z) ((Vector4l){.x=(_x),.y=(_y),.z=(_z),.w=1})
  121. #define V4l_4(_x,_y,_z,_w) ((Vector4l){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  122. #define V4l_5(_x,_y,_z,_w,...) ((Vector4l){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  123. #define _Vd_GET(a, b, c) _Generic(a, \
  124. _VECTOR_GETTERS(_V_HELPER, b) \
  125. default: _double_getx \
  126. )(a, c)
  127. #define V2d(...) V__N(V2d_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  128. #define V2d_1(a) ((Vector2d){.x=_Vd_GET(a, x, 0),.y=_Vd_GET(a, y, 0)})
  129. #define V2d_2(_x,_y) ((Vector2d){.x=(_x),.y=(_y)})
  130. #define V2d_3(_x,_y,...) ((Vector2d){.x=(_x),.y=(_y)})
  131. #define V3d(...) V__N(V3d_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  132. #define V3d_1(a) ((Vector3d){.x=_Vd_GET(a, x, 0),.y=_Vd_GET(a, y, 0),.z=_Vd_GET(a, z, 0)})
  133. #define V3d_2(a, _z) ((Vector3d){.x=_Vd_GET(a, x, 0),.y=_Vd_GET(a, y, 0),.z=_z})c,
  134. #define V3d_3(_x,_y,_z) ((Vector3d){.x=(_x),.y=(_y),.z=(_z)})
  135. #define V3d_4(_x,_y,_z,...) ((Vector3d){.x=(_x),.y=(_y),.z=(_z)})
  136. #define V4d(...) V__N(V4d_, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  137. #define V4d_1(a) ((Vector4d){.x=_Vd_GET(a, x, 0),.y=_Vd_GET(a, y, 0),.z=_Vd_GET(a, z, 0),.w=_Vd_GET(a, w, 1)})
  138. #define V4d_2(a, b) ((Vector4d){.x=_Vd_GET(a, x, 0),.y=_Vd_GET(a, y, 0),.z=_Vd_GET(a, z, 0),b})
  139. #define V4d_3(_x,_y,_z) ((Vector4d){.x=(_x),.y=(_y),.z=(_z),.w=1})
  140. #define V4d_4(_x,_y,_z,_w) ((Vector4d){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  141. #define V4d_5(_x,_y,_z,_w,...) ((Vector4d){.x=(_x),.y=(_y),.z=(_z),.w=(_w)})
  142. #endif // __c3dlas__short_macros_h__