debug-trap.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Debugging assertions and traps
  2. * Portable Snippets - https://github.com/nemequ/portable-snippets
  3. * Created by Evan Nemerson <evan@nemerson.com>
  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. #if !defined(SIMDE_DEBUG_TRAP_H)
  13. #define SIMDE_DEBUG_TRAP_H
  14. #if !defined(SIMDE_NDEBUG) && defined(NDEBUG) && !defined(SIMDE_DEBUG)
  15. # define SIMDE_NDEBUG 1
  16. #endif
  17. #if defined(__has_builtin) && !defined(__ibmxl__)
  18. # if __has_builtin(__builtin_debugtrap)
  19. # define simde_trap() __builtin_debugtrap()
  20. # elif __has_builtin(__debugbreak)
  21. # define simde_trap() __debugbreak()
  22. # endif
  23. #endif
  24. #if !defined(simde_trap)
  25. # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
  26. # define simde_trap() __debugbreak()
  27. # elif defined(__ARMCC_VERSION)
  28. # define simde_trap() __breakpoint(42)
  29. # elif defined(__ibmxl__) || defined(__xlC__)
  30. # include <builtins.h>
  31. # define simde_trap() __trap(42)
  32. # elif defined(__DMC__) && defined(_M_IX86)
  33. static inline void simde_trap(void) { __asm int 3h; }
  34. # elif defined(__i386__) || defined(__x86_64__)
  35. static inline void simde_trap(void) { __asm__ __volatile__("int $03"); }
  36. # elif defined(__thumb__)
  37. static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xde01"); }
  38. # elif defined(__aarch64__)
  39. static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xd4200000"); }
  40. # elif defined(__arm__)
  41. static inline void simde_trap(void) { __asm__ __volatile__(".inst 0xe7f001f0"); }
  42. # elif defined (__alpha__) && !defined(__osf__)
  43. static inline void simde_trap(void) { __asm__ __volatile__("bpt"); }
  44. # elif defined(_54_)
  45. static inline void simde_trap(void) { __asm__ __volatile__("ESTOP"); }
  46. # elif defined(_55_)
  47. static inline void simde_trap(void) { __asm__ __volatile__(";\n .if (.MNEMONIC)\n ESTOP_1\n .else\n ESTOP_1()\n .endif\n NOP"); }
  48. # elif defined(_64P_)
  49. static inline void simde_trap(void) { __asm__ __volatile__("SWBP 0"); }
  50. # elif defined(_6x_)
  51. static inline void simde_trap(void) { __asm__ __volatile__("NOP\n .word 0x10000000"); }
  52. # elif defined(__STDC_HOSTED__) && (__STDC_HOSTED__ == 0) && defined(__GNUC__)
  53. # define simde_trap() __builtin_trap()
  54. # else
  55. # include <signal.h>
  56. # if defined(SIGTRAP)
  57. # define simde_trap() raise(SIGTRAP)
  58. # else
  59. # define simde_trap() raise(SIGABRT)
  60. # endif
  61. # endif
  62. #endif
  63. #if defined(HEDLEY_LIKELY)
  64. # define SIMDE_DBG_LIKELY(expr) HEDLEY_LIKELY(expr)
  65. #elif defined(__GNUC__) && (__GNUC__ >= 3)
  66. # define SIMDE_DBG_LIKELY(expr) __builtin_expect(!!(expr), 1)
  67. #else
  68. # define SIMDE_DBG_LIKELY(expr) (!!(expr))
  69. #endif
  70. #if !defined(SIMDE_NDEBUG) || (SIMDE_NDEBUG == 0)
  71. # define simde_dbg_assert(expr) do { \
  72. if (!SIMDE_DBG_LIKELY(expr)) { \
  73. simde_trap(); \
  74. } \
  75. } while (0)
  76. #else
  77. # define simde_dbg_assert(expr)
  78. #endif
  79. #endif /* !defined(SIMDE_DEBUG_TRAP_H) */