intrinsics.c (2299B)
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 static FORCE_INLINE u32 8 clz_u32(u32 a) 9 { 10 u32 result = 32; 11 if (a) result = __builtin_clz(a); 12 return result; 13 } 14 15 static FORCE_INLINE u32 16 ctz_u32(u32 a) 17 { 18 u32 result = 32; 19 if (a) result = __builtin_ctz(a); 20 return result; 21 } 22 23 #ifdef __ARM_ARCH_ISA_A64 24 /* TODO? debuggers just loop here forever and need a manual PC increment (step over) */ 25 #define debugbreak() asm volatile ("brk 0xf000") 26 27 /* NOTE(rnp): we are only doing a handful of f32x4 operations so we will just use NEON and do 28 * the macro renaming thing. If you are implementing a serious wide vector operation you should 29 * use SVE(2) instead. The semantics are different however and the code will be written for an 30 * arbitrary vector bit width. In that case you will also need x86_64 code for determining 31 * the supported vector width (ideally at runtime though that may not be possible). 32 */ 33 #include <arm_neon.h> 34 typedef float32x4_t f32x4; 35 typedef int32x4_t i32x4; 36 37 #define cvt_i32x4_f32x4(a) vcvtq_f32_s32(a) 38 #define cvt_f32x4_i32x4(a) vcvtq_s32_f32(a) 39 #define dup_f32x4(f) vdupq_n_f32(f) 40 #define load_f32x4(a) vld1q_f32(a) 41 #define load_i32x4(a) vld1q_s32(a) 42 #define mul_f32x4(a, b) vmulq_f32(a, b) 43 #define set_f32x4(a, b, c, d) vld1q_f32((f32 []){d, c, b, a}) 44 #define sqrt_f32x4(a) vsqrtq_f32(a) 45 #define store_f32x4(a, o) vst1q_f32(o, a) 46 #define store_i32x4(a, o) vst1q_s32(o, a) 47 48 #elif __x86_64__ 49 #include <immintrin.h> 50 typedef __m128 f32x4; 51 typedef __m128i i32x4; 52 53 #define cvt_i32x4_f32x4(a) _mm_cvtepi32_ps(a) 54 #define cvt_f32x4_i32x4(a) _mm_cvtps_epi32(a) 55 #define dup_f32x4(f) _mm_set1_ps(f) 56 #define load_f32x4(a) _mm_loadu_ps(a) 57 #define load_i32x4(a) _mm_loadu_si128((i32x4 *)a) 58 #define mul_f32x4(a, b) _mm_mul_ps(a, b) 59 #define set_f32x4(a, b, c, d) _mm_set_ps(a, b, c, d) 60 #define sqrt_f32x4(a) _mm_sqrt_ps(a) 61 #define store_f32x4(a, o) _mm_storeu_ps(o, a) 62 #define store_i32x4(a, o) _mm_storeu_si128((i32x4 *)o, a) 63 64 #define debugbreak() asm volatile ("int3; nop") 65 66 #endif