simde-align.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* Alignment
  2. * Created by Evan Nemerson <evan@nemerson.com>
  3. *
  4. * To the extent possible under law, the authors have waived all
  5. * copyright and related or neighboring rights to this code. For
  6. * details, see the Creative Commons Zero 1.0 Universal license at
  7. * <https://creativecommons.org/publicdomain/zero/1.0/>
  8. *
  9. * SPDX-License-Identifier: CC0-1.0
  10. *
  11. **********************************************************************
  12. *
  13. * This is portability layer which should help iron out some
  14. * differences across various compilers, as well as various versions of
  15. * C and C++.
  16. *
  17. * It was originally developed for SIMD Everywhere
  18. * (<https://github.com/simd-everywhere/simde>), but since its only
  19. * dependency is Hedley (<https://nemequ.github.io/hedley>, also CC0)
  20. * it can easily be used in other projects, so please feel free to do
  21. * so.
  22. *
  23. * If you do use this in your project, please keep a link to SIMDe in
  24. * your code to remind you where to report any bugs and/or check for
  25. * updated versions.
  26. *
  27. * # API Overview
  28. *
  29. * The API has several parts, and most macros have a few variations.
  30. * There are APIs for declaring aligned fields/variables, optimization
  31. * hints, and run-time alignment checks.
  32. *
  33. * Briefly, macros ending with "_TO" take numeric values and are great
  34. * when you know the value you would like to use. Macros ending with
  35. * "_LIKE", on the other hand, accept a type and are used when you want
  36. * to use the alignment of a type instead of hardcoding a value.
  37. *
  38. * Documentation for each section of the API is inline.
  39. *
  40. * True to form, MSVC is the main problem and imposes several
  41. * limitations on the effectiveness of the APIs. Detailed descriptions
  42. * of the limitations of each macro are inline, but in general:
  43. *
  44. * * On C11+ or C++11+ code written using this API will work. The
  45. * ASSUME macros may or may not generate a hint to the compiler, but
  46. * that is only an optimization issue and will not actually cause
  47. * failures.
  48. * * If you're using pretty much any compiler other than MSVC,
  49. * everything should basically work as well as in C11/C++11.
  50. */
  51. #if !defined(SIMDE_ALIGN_H)
  52. #define SIMDE_ALIGN_H
  53. #include "hedley.h"
  54. /* I know this seems a little silly, but some non-hosted compilers
  55. * don't have stddef.h, so we try to accommodate them. */
  56. #if !defined(SIMDE_ALIGN_SIZE_T_)
  57. #if defined(__SIZE_TYPE__)
  58. #define SIMDE_ALIGN_SIZE_T_ __SIZE_TYPE__
  59. #elif defined(__SIZE_T_TYPE__)
  60. #define SIMDE_ALIGN_SIZE_T_ __SIZE_TYPE__
  61. #elif defined(__cplusplus)
  62. #include <cstddef>
  63. #define SIMDE_ALIGN_SIZE_T_ size_t
  64. #else
  65. #include <stddef.h>
  66. #define SIMDE_ALIGN_SIZE_T_ size_t
  67. #endif
  68. #endif
  69. #if !defined(SIMDE_ALIGN_INTPTR_T_)
  70. #if defined(__INTPTR_TYPE__)
  71. #define SIMDE_ALIGN_INTPTR_T_ __INTPTR_TYPE__
  72. #elif defined(__PTRDIFF_TYPE__)
  73. #define SIMDE_ALIGN_INTPTR_T_ __PTRDIFF_TYPE__
  74. #elif defined(__PTRDIFF_T_TYPE__)
  75. #define SIMDE_ALIGN_INTPTR_T_ __PTRDIFF_T_TYPE__
  76. #elif defined(__cplusplus)
  77. #include <cstddef>
  78. #define SIMDE_ALIGN_INTPTR_T_ ptrdiff_t
  79. #else
  80. #include <stddef.h>
  81. #define SIMDE_ALIGN_INTPTR_T_ ptrdiff_t
  82. #endif
  83. #endif
  84. #if defined(SIMDE_ALIGN_DEBUG)
  85. #if defined(__cplusplus)
  86. #include <cstdio>
  87. #else
  88. #include <stdio.h>
  89. #endif
  90. #endif
  91. /* SIMDE_ALIGN_OF(Type)
  92. *
  93. * The SIMDE_ALIGN_OF macro works like alignof, or _Alignof, or
  94. * __alignof, or __alignof__, or __ALIGNOF__, depending on the compiler.
  95. * It isn't defined everywhere (only when the compiler has some alignof-
  96. * like feature we can use to implement it), but it should work in most
  97. * modern compilers, as well as C11 and C++11.
  98. *
  99. * If we can't find an implementation for SIMDE_ALIGN_OF then the macro
  100. * will not be defined, so if you can handle that situation sensibly
  101. * you may need to sprinkle some ifdefs into your code.
  102. */
  103. #if \
  104. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
  105. (0 && HEDLEY_HAS_FEATURE(c_alignof))
  106. #define SIMDE_ALIGN_OF(Type) _Alignof(Type)
  107. #elif \
  108. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  109. (0 && HEDLEY_HAS_FEATURE(cxx_alignof))
  110. #define SIMDE_ALIGN_OF(Type) alignof(Type)
  111. #elif \
  112. HEDLEY_GCC_VERSION_CHECK(2,95,0) || \
  113. HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  114. HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  115. HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
  116. HEDLEY_TINYC_VERSION_CHECK(0,9,24) || \
  117. HEDLEY_PGI_VERSION_CHECK(19,10,0) || \
  118. HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
  119. HEDLEY_TI_ARMCL_VERSION_CHECK(16,9,0) || \
  120. HEDLEY_TI_CL2000_VERSION_CHECK(16,9,0) || \
  121. HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  122. HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  123. HEDLEY_TI_CL430_VERSION_CHECK(16,9,0) || \
  124. HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,2) || \
  125. HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
  126. defined(__IBM__ALIGNOF__) || \
  127. defined(__clang__)
  128. #define SIMDE_ALIGN_OF(Type) __alignof__(Type)
  129. #elif \
  130. HEDLEY_IAR_VERSION_CHECK(8,40,0)
  131. #define SIMDE_ALIGN_OF(Type) __ALIGNOF__(Type)
  132. #elif \
  133. HEDLEY_MSVC_VERSION_CHECK(19,0,0)
  134. /* Probably goes back much further, but MS takes down their old docs.
  135. * If you can verify that this works in earlier versions please let
  136. * me know! */
  137. #define SIMDE_ALIGN_OF(Type) __alignof(Type)
  138. #endif
  139. /* SIMDE_ALIGN_MAXIMUM:
  140. *
  141. * This is the maximum alignment that the compiler supports. You can
  142. * define the value prior to including SIMDe if necessary, but in that
  143. * case *please* submit an issue so we can add the platform to the
  144. * detection code.
  145. *
  146. * Most compilers are okay with types which are aligned beyond what
  147. * they think is the maximum, as long as the alignment is a power
  148. * of two. Older versions of MSVC is the exception, so we need to cap
  149. * the alignment requests at values that the implementation supports.
  150. *
  151. * XL C/C++ will accept values larger than 16 (which is the alignment
  152. * of an AltiVec vector), but will not reliably align to the larger
  153. * value, so so we cap the value at 16 there.
  154. *
  155. * If the compiler accepts any power-of-two value within reason then
  156. * this macro should be left undefined, and the SIMDE_ALIGN_CAP
  157. * macro will just return the value passed to it. */
  158. #if !defined(SIMDE_ALIGN_MAXIMUM)
  159. #if defined(HEDLEY_MSVC_VERSION)
  160. #if HEDLEY_MSVC_VERSION_CHECK(19, 16, 0)
  161. // Visual studio 2017 and newer does not need a max
  162. #else
  163. #if defined(_M_IX86) || defined(_M_AMD64)
  164. #if HEDLEY_MSVC_VERSION_CHECK(19,14,0)
  165. #define SIMDE_ALIGN_PLATFORM_MAXIMUM 64
  166. #elif HEDLEY_MSVC_VERSION_CHECK(16,0,0)
  167. /* VS 2010 is really a guess based on Wikipedia; if anyone can
  168. * test with old VS versions I'd really appreciate it. */
  169. #define SIMDE_ALIGN_PLATFORM_MAXIMUM 32
  170. #else
  171. #define SIMDE_ALIGN_PLATFORM_MAXIMUM 16
  172. #endif
  173. #elif defined(_M_ARM) || defined(_M_ARM64)
  174. #define SIMDE_ALIGN_PLATFORM_MAXIMUM 8
  175. #endif
  176. #endif
  177. #elif defined(HEDLEY_IBM_VERSION)
  178. #define SIMDE_ALIGN_PLATFORM_MAXIMUM 16
  179. #endif
  180. #endif
  181. /* You can mostly ignore these; they're intended for internal use.
  182. * If you do need to use them please let me know; if they fulfill
  183. * a common use case I'll probably drop the trailing underscore
  184. * and make them part of the public API. */
  185. #if defined(SIMDE_ALIGN_PLATFORM_MAXIMUM)
  186. #if SIMDE_ALIGN_PLATFORM_MAXIMUM >= 64
  187. #define SIMDE_ALIGN_64_ 64
  188. #define SIMDE_ALIGN_32_ 32
  189. #define SIMDE_ALIGN_16_ 16
  190. #define SIMDE_ALIGN_8_ 8
  191. #elif SIMDE_ALIGN_PLATFORM_MAXIMUM >= 32
  192. #define SIMDE_ALIGN_64_ 32
  193. #define SIMDE_ALIGN_32_ 32
  194. #define SIMDE_ALIGN_16_ 16
  195. #define SIMDE_ALIGN_8_ 8
  196. #elif SIMDE_ALIGN_PLATFORM_MAXIMUM >= 16
  197. #define SIMDE_ALIGN_64_ 16
  198. #define SIMDE_ALIGN_32_ 16
  199. #define SIMDE_ALIGN_16_ 16
  200. #define SIMDE_ALIGN_8_ 8
  201. #elif SIMDE_ALIGN_PLATFORM_MAXIMUM >= 8
  202. #define SIMDE_ALIGN_64_ 8
  203. #define SIMDE_ALIGN_32_ 8
  204. #define SIMDE_ALIGN_16_ 8
  205. #define SIMDE_ALIGN_8_ 8
  206. #else
  207. #error Max alignment expected to be >= 8
  208. #endif
  209. #else
  210. #define SIMDE_ALIGN_64_ 64
  211. #define SIMDE_ALIGN_32_ 32
  212. #define SIMDE_ALIGN_16_ 16
  213. #define SIMDE_ALIGN_8_ 8
  214. #endif
  215. /**
  216. * SIMDE_ALIGN_CAP(Alignment)
  217. *
  218. * Returns the minimum of Alignment or SIMDE_ALIGN_MAXIMUM.
  219. */
  220. #if defined(SIMDE_ALIGN_MAXIMUM)
  221. #define SIMDE_ALIGN_CAP(Alignment) (((Alignment) < (SIMDE_ALIGN_PLATFORM_MAXIMUM)) ? (Alignment) : (SIMDE_ALIGN_PLATFORM_MAXIMUM))
  222. #else
  223. #define SIMDE_ALIGN_CAP(Alignment) (Alignment)
  224. #endif
  225. /* SIMDE_ALIGN_TO(Alignment)
  226. *
  227. * SIMDE_ALIGN_TO is used to declare types or variables. It basically
  228. * maps to the align attribute in most compilers, the align declspec
  229. * in MSVC, or _Alignas/alignas in C11/C++11.
  230. *
  231. * Example:
  232. *
  233. * struct i32x4 {
  234. * SIMDE_ALIGN_TO(16) int32_t values[4];
  235. * }
  236. *
  237. * Limitations:
  238. *
  239. * MSVC requires that the Alignment parameter be numeric; you can't do
  240. * something like `SIMDE_ALIGN_TO(SIMDE_ALIGN_OF(int))`. This is
  241. * unfortunate because that's really how the LIKE macros are
  242. * implemented, and I am not aware of a way to get anything like this
  243. * to work without using the C11/C++11 keywords.
  244. *
  245. * It also means that we can't use SIMDE_ALIGN_CAP to limit the
  246. * alignment to the value specified, which MSVC also requires, so on
  247. * MSVC you should use the `SIMDE_ALIGN_TO_8/16/32/64` macros instead.
  248. * They work like `SIMDE_ALIGN_TO(SIMDE_ALIGN_CAP(Alignment))` would,
  249. * but should be safe to use on MSVC.
  250. *
  251. * All this is to say that, if you want your code to work on MSVC, you
  252. * should use the SIMDE_ALIGN_TO_8/16/32/64 macros below instead of
  253. * SIMDE_ALIGN_TO(8/16/32/64).
  254. */
  255. #if \
  256. HEDLEY_HAS_ATTRIBUTE(aligned) || \
  257. HEDLEY_GCC_VERSION_CHECK(2,95,0) || \
  258. HEDLEY_CRAY_VERSION_CHECK(8,4,0) || \
  259. HEDLEY_IBM_VERSION_CHECK(11,1,0) || \
  260. HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
  261. HEDLEY_PGI_VERSION_CHECK(19,4,0) || \
  262. HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  263. HEDLEY_TINYC_VERSION_CHECK(0,9,24) || \
  264. HEDLEY_TI_ARMCL_VERSION_CHECK(16,9,0) || \
  265. HEDLEY_TI_CL2000_VERSION_CHECK(16,9,0) || \
  266. HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
  267. HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
  268. HEDLEY_TI_CL430_VERSION_CHECK(16,9,0) || \
  269. HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,2)
  270. #define SIMDE_ALIGN_TO(Alignment) __attribute__((__aligned__(SIMDE_ALIGN_CAP(Alignment))))
  271. #elif \
  272. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L))
  273. #define SIMDE_ALIGN_TO(Alignment) _Alignas(SIMDE_ALIGN_CAP(Alignment))
  274. #elif \
  275. (defined(__cplusplus) && (__cplusplus >= 201103L))
  276. #define SIMDE_ALIGN_TO(Alignment) alignas(SIMDE_ALIGN_CAP(Alignment))
  277. #elif \
  278. defined(HEDLEY_MSVC_VERSION)
  279. #define SIMDE_ALIGN_TO(Alignment) __declspec(align(Alignment))
  280. /* Unfortunately MSVC can't handle __declspec(align(__alignof(Type)));
  281. * the alignment passed to the declspec has to be an integer. */
  282. #define SIMDE_ALIGN_OF_UNUSABLE_FOR_LIKE
  283. #endif
  284. #define SIMDE_ALIGN_TO_64 SIMDE_ALIGN_TO(SIMDE_ALIGN_64_)
  285. #define SIMDE_ALIGN_TO_32 SIMDE_ALIGN_TO(SIMDE_ALIGN_32_)
  286. #define SIMDE_ALIGN_TO_16 SIMDE_ALIGN_TO(SIMDE_ALIGN_16_)
  287. #define SIMDE_ALIGN_TO_8 SIMDE_ALIGN_TO(SIMDE_ALIGN_8_)
  288. /* SIMDE_ALIGN_ASSUME_TO(Pointer, Alignment)
  289. *
  290. * SIMDE_ALIGN_ASSUME_TO is semantically similar to C++20's
  291. * std::assume_aligned, or __builtin_assume_aligned. It tells the
  292. * compiler to assume that the provided pointer is aligned to an
  293. * `Alignment`-byte boundary.
  294. *
  295. * If you define SIMDE_ALIGN_DEBUG prior to including this header then
  296. * SIMDE_ALIGN_ASSUME_TO will turn into a runtime check. We don't
  297. * integrate with NDEBUG in this header, but it may be a good idea to
  298. * put something like this in your code:
  299. *
  300. * #if !defined(NDEBUG)
  301. * #define SIMDE_ALIGN_DEBUG
  302. * #endif
  303. * #include <.../simde-align.h>
  304. */
  305. #if \
  306. HEDLEY_HAS_BUILTIN(__builtin_assume_aligned) || \
  307. HEDLEY_GCC_VERSION_CHECK(4,7,0)
  308. #define SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment) \
  309. HEDLEY_REINTERPRET_CAST(__typeof__(Pointer), __builtin_assume_aligned(HEDLEY_CONST_CAST(void*, HEDLEY_REINTERPRET_CAST(const void*, Pointer)), Alignment))
  310. #elif HEDLEY_INTEL_VERSION_CHECK(13,0,0)
  311. #define SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment) (__extension__ ({ \
  312. __typeof__(v) simde_assume_aligned_t_ = (Pointer); \
  313. __assume_aligned(simde_assume_aligned_t_, Alignment); \
  314. simde_assume_aligned_t_; \
  315. }))
  316. #elif defined(__cplusplus) && (__cplusplus > 201703L)
  317. #include <memory>
  318. #define SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment) std::assume_aligned<Alignment>(Pointer)
  319. #else
  320. #if defined(__cplusplus)
  321. template<typename T> HEDLEY_ALWAYS_INLINE static T* simde_align_assume_to_unchecked(T* ptr, const size_t alignment)
  322. #else
  323. HEDLEY_ALWAYS_INLINE static void* simde_align_assume_to_unchecked(void* ptr, const size_t alignment)
  324. #endif
  325. {
  326. HEDLEY_ASSUME((HEDLEY_REINTERPRET_CAST(size_t, (ptr)) % SIMDE_ALIGN_CAP(alignment)) == 0);
  327. return ptr;
  328. }
  329. #if defined(__cplusplus)
  330. #define SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment) simde_align_assume_to_unchecked((Pointer), (Alignment))
  331. #else
  332. #define SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment) simde_align_assume_to_unchecked(HEDLEY_CONST_CAST(void*, HEDLEY_REINTERPRET_CAST(const void*, Pointer)), (Alignment))
  333. #endif
  334. #endif
  335. #if !defined(SIMDE_ALIGN_DEBUG)
  336. #define SIMDE_ALIGN_ASSUME_TO(Pointer, Alignment) SIMDE_ALIGN_ASSUME_TO_UNCHECKED(Pointer, Alignment)
  337. #else
  338. #include <stdio.h>
  339. #if defined(__cplusplus)
  340. template<typename T>
  341. static HEDLEY_ALWAYS_INLINE
  342. T*
  343. simde_align_assume_to_checked_uncapped(T* ptr, const size_t alignment, const char* file, int line, const char* ptrname)
  344. #else
  345. static HEDLEY_ALWAYS_INLINE
  346. void*
  347. simde_align_assume_to_checked_uncapped(void* ptr, const size_t alignment, const char* file, int line, const char* ptrname)
  348. #endif
  349. {
  350. if (HEDLEY_UNLIKELY((HEDLEY_REINTERPRET_CAST(SIMDE_ALIGN_INTPTR_T_, (ptr)) % HEDLEY_STATIC_CAST(SIMDE_ALIGN_INTPTR_T_, SIMDE_ALIGN_CAP(alignment))) != 0)) {
  351. fprintf(stderr, "%s:%d: alignment check failed for `%s' (%p %% %u == %u)\n",
  352. file, line, ptrname, HEDLEY_REINTERPRET_CAST(const void*, ptr),
  353. HEDLEY_STATIC_CAST(unsigned int, SIMDE_ALIGN_CAP(alignment)),
  354. HEDLEY_STATIC_CAST(unsigned int, HEDLEY_REINTERPRET_CAST(SIMDE_ALIGN_INTPTR_T_, (ptr)) % HEDLEY_STATIC_CAST(SIMDE_ALIGN_INTPTR_T_, SIMDE_ALIGN_CAP(alignment))));
  355. }
  356. return ptr;
  357. }
  358. #if defined(__cplusplus)
  359. #define SIMDE_ALIGN_ASSUME_TO(Pointer, Alignment) simde_align_assume_to_checked_uncapped((Pointer), (Alignment), __FILE__, __LINE__, #Pointer)
  360. #else
  361. #define SIMDE_ALIGN_ASSUME_TO(Pointer, Alignment) simde_align_assume_to_checked_uncapped(HEDLEY_CONST_CAST(void*, HEDLEY_REINTERPRET_CAST(const void*, Pointer)), (Alignment), __FILE__, __LINE__, #Pointer)
  362. #endif
  363. #endif
  364. /* SIMDE_ALIGN_LIKE(Type)
  365. * SIMDE_ALIGN_LIKE_#(Type)
  366. *
  367. * The SIMDE_ALIGN_LIKE macros are similar to the SIMDE_ALIGN_TO macros
  368. * except instead of an integer they take a type; basically, it's just
  369. * a more convenient way to do something like:
  370. *
  371. * SIMDE_ALIGN_TO(SIMDE_ALIGN_OF(Type))
  372. *
  373. * The versions with a numeric suffix will fall back on using a numeric
  374. * value in the event we can't use SIMDE_ALIGN_OF(Type). This is
  375. * mainly for MSVC, where __declspec(align()) can't handle anything
  376. * other than hard-coded numeric values.
  377. */
  378. #if defined(SIMDE_ALIGN_OF) && defined(SIMDE_ALIGN_TO) && !defined(SIMDE_ALIGN_OF_UNUSABLE_FOR_LIKE)
  379. #define SIMDE_ALIGN_LIKE(Type) SIMDE_ALIGN_TO(SIMDE_ALIGN_OF(Type))
  380. #define SIMDE_ALIGN_LIKE_64(Type) SIMDE_ALIGN_LIKE(Type)
  381. #define SIMDE_ALIGN_LIKE_32(Type) SIMDE_ALIGN_LIKE(Type)
  382. #define SIMDE_ALIGN_LIKE_16(Type) SIMDE_ALIGN_LIKE(Type)
  383. #define SIMDE_ALIGN_LIKE_8(Type) SIMDE_ALIGN_LIKE(Type)
  384. #else
  385. #define SIMDE_ALIGN_LIKE_64(Type) SIMDE_ALIGN_TO_64
  386. #define SIMDE_ALIGN_LIKE_32(Type) SIMDE_ALIGN_TO_32
  387. #define SIMDE_ALIGN_LIKE_16(Type) SIMDE_ALIGN_TO_16
  388. #define SIMDE_ALIGN_LIKE_8(Type) SIMDE_ALIGN_TO_8
  389. #endif
  390. /* SIMDE_ALIGN_ASSUME_LIKE(Pointer, Type)
  391. *
  392. * This is similar to SIMDE_ALIGN_ASSUME_TO, except that it takes a
  393. * type instead of a numeric value. */
  394. #if defined(SIMDE_ALIGN_OF) && defined(SIMDE_ALIGN_ASSUME_TO)
  395. #define SIMDE_ALIGN_ASSUME_LIKE(Pointer, Type) SIMDE_ALIGN_ASSUME_TO(Pointer, SIMDE_ALIGN_OF(Type))
  396. #endif
  397. /* SIMDE_ALIGN_CAST(Type, Pointer)
  398. *
  399. * SIMDE_ALIGN_CAST is like C++'s reinterpret_cast, but it will try
  400. * to silence warnings that some compilers may produce if you try
  401. * to assign to a type with increased alignment requirements.
  402. *
  403. * Note that it does *not* actually attempt to tell the compiler that
  404. * the pointer is aligned like the destination should be; that's the
  405. * job of the next macro. This macro is necessary for stupid APIs
  406. * like _mm_loadu_si128 where the input is a __m128i* but the function
  407. * is specifically for data which isn't necessarily aligned to
  408. * _Alignof(__m128i).
  409. */
  410. #if HEDLEY_HAS_WARNING("-Wcast-align") || defined(__clang__) || HEDLEY_GCC_VERSION_CHECK(3,4,0)
  411. #define SIMDE_ALIGN_CAST(Type, Pointer) (__extension__({ \
  412. HEDLEY_DIAGNOSTIC_PUSH \
  413. _Pragma("GCC diagnostic ignored \"-Wcast-align\"") \
  414. Type simde_r_ = HEDLEY_REINTERPRET_CAST(Type, Pointer); \
  415. HEDLEY_DIAGNOSTIC_POP \
  416. simde_r_; \
  417. }))
  418. #else
  419. #define SIMDE_ALIGN_CAST(Type, Pointer) HEDLEY_REINTERPRET_CAST(Type, Pointer)
  420. #endif
  421. /* SIMDE_ALIGN_ASSUME_CAST(Type, Pointer)
  422. *
  423. * This is sort of like a combination of a reinterpret_cast and a
  424. * SIMDE_ALIGN_ASSUME_LIKE. It uses SIMDE_ALIGN_ASSUME_LIKE to tell
  425. * the compiler that the pointer is aligned like the specified type
  426. * and casts the pointer to the specified type while suppressing any
  427. * warnings from the compiler about casting to a type with greater
  428. * alignment requirements.
  429. */
  430. #define SIMDE_ALIGN_ASSUME_CAST(Type, Pointer) SIMDE_ALIGN_ASSUME_LIKE(SIMDE_ALIGN_CAST(Type, Pointer), Type)
  431. #endif /* !defined(SIMDE_ALIGN_H) */