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