| 1 |
/* Debugging assertions and traps |
| 2 |
* Portable Snippets - https://gitub.com/nemequ/portable-snippets |
| 3 |
* Created by Evan Nemerson <[email protected]> |
| 4 |
* |
| 5 |
* To the extent possible under law, the authors have waived all |
| 6 |
* copyright and related or neighboring rights to this code. For |
| 7 |
* details, see the Creative Commons Zero 1.0 Universal license at |
| 8 |
* https://creativecommons.org/publicdomain/zero/1.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: CC0-1.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
#if !defined(SIMDE_DEBUG_TRAP_H) |
| 14 |
#define SIMDE_DEBUG_TRAP_H |
| 15 |
|
| 16 |
#if !defined(SIMDE_NDEBUG) && defined(NDEBUG) && !defined(SIMDE_DEBUG) |
| 17 |
# define SIMDE_NDEBUG 1 |
| 18 |
#endif |
| 19 |
|
| 20 |
#if defined(__has_builtin) && !defined(__ibmxl__) |
| 21 |
# if __has_builtin(__builtin_debugtrap) |
| 22 |
# define simde_trap() __builtin_debugtrap() |
| 23 |
# elif __has_builtin(__debugbreak) |
| 24 |
# define simde_trap() __debugbreak() |
| 25 |
# endif |
| 26 |
#endif |
| 27 |
#if !defined(simde_trap) |
| 28 |
# if defined(_MSC_VER) || defined(__INTEL_COMPILER) |
| 29 |
# define simde_trap() __debugbreak() |
| 30 |
# elif defined(__ARMCC_VERSION) |
| 31 |
# define simde_trap() __breakpoint(42) |
| 32 |
# elif defined(__ibmxl__) || defined(__xlC__) |
| 33 |
# include <builtins.h> |
| 34 |
# define simde_trap() __trap(42) |
| 35 |
# elif defined(__DMC__) && defined(_M_IX86) |
| 36 |
static inline void simde_trap(void) { __asm int 3h; } |
| 37 |
# elif defined(__i386__) || defined(__x86_64__) |
| 38 |
static inline void simde_trap(void) { __asm__ __volatile__("int $03"); } |
| 39 |
# elif defined(__thumb__) |
| 40 |
static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xde01"); } |
| 41 |
# elif defined(__aarch64__) |
| 42 |
static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xd4200000"); } |
| 43 |
# elif defined(__arm__) |
| 44 |
static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xe7f001f0"); } |
| 45 |
# elif defined (__alpha__) && !defined(__osf__) |
| 46 |
static inline void simde_trap(void) { __asm__ __volatile__("bpt"); } |
| 47 |
# elif defined(_54_) |
| 48 |
static inline void simde_trap(void) { __asm__ __volatile__("ESTOP"); } |
| 49 |
# elif defined(_55_) |
| 50 |
static inline void simde_trap(void) { __asm__ __volatile__(";\n .if (.MNEMONIC)\n ESTOP_1\n .else\n ESTOP_1()\n .endif\n NOP"); } |
| 51 |
# elif defined(_64P_) |
| 52 |
static inline void simde_trap(void) { __asm__ __volatile__("SWBP 0"); } |
| 53 |
# elif defined(_6x_) |
| 54 |
static inline void simde_trap(void) { __asm__ __volatile__("NOP\n .word 0x10000000"); } |
| 55 |
# elif defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 0) && defined(__GNUC__) |
| 56 |
# define simde_trap() __builtin_trap() |
| 57 |
# else |
| 58 |
# include <signal.h> |
| 59 |
# if defined(SIGTRAP) |
| 60 |
# define simde_trap() raise(SIGTRAP) |
| 61 |
# else |
| 62 |
# define simde_trap() raise(SIGABRT) |
| 63 |
# endif |
| 64 |
# endif |
| 65 |
#endif |
| 66 |
|
| 67 |
#if defined(HEDLEY_LIKELY) |
| 68 |
# define SIMDE_DBG_LIKELY(expr) HEDLEY_LIKELY(expr) |
| 69 |
#elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 70 |
# define SIMDE_DBG_LIKELY(expr) __builtin_expect(!!(expr), 1) |
| 71 |
#else |
| 72 |
# define SIMDE_DBG_LIKELY(expr) (!!(expr)) |
| 73 |
#endif |
| 74 |
|
| 75 |
#if !defined(SIMDE_NDEBUG) || (SIMDE_NDEBUG == 0) |
| 76 |
# define simde_dbg_assert(expr) do { \ |
| 77 |
if (!SIMDE_DBG_LIKELY(expr)) { \ |
| 78 |
simde_trap(); \ |
| 79 |
} \ |
| 80 |
} while (0) |
| 81 |
#else |
| 82 |
# define simde_dbg_assert(expr) |
| 83 |
#endif |
| 84 |
|
| 85 |
#endif /* !defined(SIMDE_DEBUG_TRAP_H) */ |