intrinsics.c (2670B)
1 #define FORCE_INLINE inline __attribute__((always_inline)) 2 3 /* TODO(rnp): msvc probably won't build this but there are other things preventing that as well */ 4 #define sqrt_f32(a) __builtin_sqrtf(a) 5 #define atan2_f32(y, x) __builtin_atan2f(y, x) 6 7 #define atomic_store(ptr, n) __atomic_store_n(ptr, n, __ATOMIC_RELEASE) 8 #define atomic_load(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE) 9 #define atomic_and(ptr, n) __atomic_and_fetch(ptr, n, __ATOMIC_RELEASE) 10 #define atomic_add(ptr, n) __atomic_add_fetch(ptr, n, __ATOMIC_RELEASE) 11 #define atomic_inc(ptr, n) __atomic_fetch_add(ptr, n, __ATOMIC_ACQ_REL) 12 13 static FORCE_INLINE u32 14 clz_u32(u32 a) 15 { 16 u32 result = 32; 17 if (a) result = __builtin_clz(a); 18 return result; 19 } 20 21 static FORCE_INLINE u32 22 ctz_u32(u32 a) 23 { 24 u32 result = 32; 25 if (a) result = __builtin_ctz(a); 26 return result; 27 } 28 29 #ifdef __ARM_ARCH_ISA_A64 30 /* TODO? debuggers just loop here forever and need a manual PC increment (step over) */ 31 #define debugbreak() asm volatile ("brk 0xf000") 32 33 /* NOTE(rnp): we are only doing a handful of f32x4 operations so we will just use NEON and do 34 * the macro renaming thing. If you are implementing a serious wide vector operation you should 35 * use SVE(2) instead. The semantics are different however and the code will be written for an 36 * arbitrary vector bit width. In that case you will also need x86_64 code for determining 37 * the supported vector width (ideally at runtime though that may not be possible). 38 */ 39 #include <arm_neon.h> 40 typedef float32x4_t f32x4; 41 typedef int32x4_t i32x4; 42 43 #define cvt_i32x4_f32x4(a) vcvtq_f32_s32(a) 44 #define cvt_f32x4_i32x4(a) vcvtq_s32_f32(a) 45 #define dup_f32x4(f) vdupq_n_f32(f) 46 #define load_f32x4(a) vld1q_f32(a) 47 #define load_i32x4(a) vld1q_s32(a) 48 #define mul_f32x4(a, b) vmulq_f32(a, b) 49 #define set_f32x4(a, b, c, d) vld1q_f32((f32 []){d, c, b, a}) 50 #define sqrt_f32x4(a) vsqrtq_f32(a) 51 #define store_f32x4(a, o) vst1q_f32(o, a) 52 #define store_i32x4(a, o) vst1q_s32(o, a) 53 54 #elif __x86_64__ 55 #include <immintrin.h> 56 typedef __m128 f32x4; 57 typedef __m128i i32x4; 58 59 #define cvt_i32x4_f32x4(a) _mm_cvtepi32_ps(a) 60 #define cvt_f32x4_i32x4(a) _mm_cvtps_epi32(a) 61 #define dup_f32x4(f) _mm_set1_ps(f) 62 #define load_f32x4(a) _mm_loadu_ps(a) 63 #define load_i32x4(a) _mm_loadu_si128((i32x4 *)a) 64 #define mul_f32x4(a, b) _mm_mul_ps(a, b) 65 #define set_f32x4(a, b, c, d) _mm_set_ps(a, b, c, d) 66 #define sqrt_f32x4(a) _mm_sqrt_ps(a) 67 #define store_f32x4(a, o) _mm_storeu_ps(o, a) 68 #define store_i32x4(a, o) _mm_storeu_si128((i32x4 *)o, a) 69 70 #define debugbreak() asm volatile ("int3; nop") 71 72 #endif