repo
stringlengths
1
152
file
stringlengths
14
221
code
stringlengths
501
25k
file_length
int64
501
25k
avg_line_length
float64
20
99.5
max_line_length
int64
21
134
extension_type
stringclasses
2 values
XNNPACK
XNNPACK-master/src/math/f32-f16-cvt-sse2.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_f16_cvt__sse2( size_t n, const float* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); const __m128 vnonsign_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF)); const __m128i vexp_bias = _mm_set1_epi32(0x07800000); const __m128 vscale_to_inf = _mm_set1_ps(0x1.0p+112f); const __m128i vexpw_max = _mm_set1_epi32(0x7F800000); const __m128 vscale_to_zero = _mm_set1_ps(0x1.0p-110f); const __m128i vbias_min = _mm_set1_epi32(0x40008000); const __m128i vmanth_mask = _mm_set1_epi32(0x0FFF); const __m128i vexph_mask = _mm_set1_epi32(0x7C00); const __m128i vnanh = _mm_set1_epi16(0x7E00); uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { const __m128 vx_lo = _mm_loadu_ps(input); const __m128 vx_hi = _mm_loadu_ps(input + 4); input += 8; const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask); const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask); const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo); const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi); __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias); __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias); __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf); __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf); const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max); const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max); vbias_lo = _mm_and_si128(vbias_lo, vexpw_max); vbias_hi = _mm_and_si128(vbias_hi, vexpw_max); vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero); vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero); const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi); const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi)); vbias_lo = _mm_max_epi16(vbias_lo, vbias_min); vbias_hi = _mm_max_epi16(vbias_hi, vbias_min); __m128i vh = _mm_and_si128(vnanh, vnanmaskh); vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo)); vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi)); vh = _mm_or_si128(vh, vsignh); __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13); __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13); const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask); const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask); vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask); vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask); const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo); const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi); const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi); vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh)); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
3,246
37.654762
101
c
XNNPACK
XNNPACK-master/src/math/f32-f16-cvt-sse41.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <smmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_f16_cvt__sse41( size_t n, const float* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); const __m128 vnonsign_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF)); const __m128i vexp_bias = _mm_set1_epi32(0x07800000); const __m128 vscale_to_inf = _mm_set1_ps(0x1.0p+112f); const __m128i vexpw_max = _mm_set1_epi32(0x7F800000); const __m128 vscale_to_zero = _mm_set1_ps(0x1.0p-110f); const __m128i vbias_min = _mm_set1_epi32(0x40008000); const __m128i vmanth_mask = _mm_set1_epi32(0x0FFF); const __m128i vexph_mask = _mm_set1_epi32(0x7C00); const __m128i vnanh = _mm_set1_epi16(0x7E00); uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { const __m128 vx_lo = _mm_loadu_ps(input); const __m128 vx_hi = _mm_loadu_ps(input + 4); input += 8; const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask); const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask); const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo); const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi); __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias); __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias); __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf); __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf); const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max); const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max); vbias_lo = _mm_and_si128(vbias_lo, vexpw_max); vbias_hi = _mm_and_si128(vbias_hi, vexpw_max); vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero); vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero); const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi); const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi)); vbias_lo = _mm_max_epi16(vbias_lo, vbias_min); vbias_hi = _mm_max_epi16(vbias_hi, vbias_min); vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo)); vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi)); __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13); __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13); const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask); const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask); vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask); vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask); const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo); const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi); const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi); const __m128i vabsh = _mm_blendv_epi8(vnonsignh, vnanh, vnanmaskh); const __m128i vh = _mm_or_si128(vabsh, vsignh); _mm_storeu_si128((__m128i*) o, vh); o += 8; } }
3,220
37.345238
101
c
XNNPACK
XNNPACK-master/src/math/f32-f16-cvt-wasmsimd.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_f16_cvt__wasmsimd( size_t n, const float* input, void* output) { assert(n % (8 * sizeof(uint16_t)) == 0); const v128_t vscale_to_inf = wasm_f32x4_const_splat(0x1.0p+112f); const v128_t vscale_to_zero = wasm_f32x4_const_splat(0x1.0p-110f); const v128_t vexp_bias = wasm_i32x4_const_splat(0x07800000); const v128_t vexpw_max = wasm_i32x4_const_splat(0x7F800000); const v128_t vbias_min = wasm_i32x4_const_splat(0x40008000); const v128_t vexph_mask = wasm_i32x4_const_splat(0x7C00); const v128_t vmanth_mask = wasm_i32x4_const_splat(0x0FFF); const v128_t vnanh = wasm_i16x8_const_splat(0x7E00); uint16_t* o = (uint16_t*) output; for (; n != 0; n -= 8 * sizeof(uint16_t)) { const v128_t vx_lo = wasm_v128_load(input); const v128_t vx_hi = wasm_v128_load(input + 4); input += 8; const v128_t vabsx_lo = wasm_f32x4_abs(vx_lo); const v128_t vabsx_hi = wasm_f32x4_abs(vx_hi); const v128_t vsignx_lo = wasm_v128_xor(vx_lo, vabsx_lo); const v128_t vsignx_hi = wasm_v128_xor(vx_hi, vabsx_hi); v128_t vbias_lo = wasm_i32x4_add(vabsx_lo, vexp_bias); v128_t vbias_hi = wasm_i32x4_add(vabsx_hi, vexp_bias); v128_t vf_lo = wasm_f32x4_mul(vabsx_lo, vscale_to_inf); v128_t vf_hi = wasm_f32x4_mul(vabsx_hi, vscale_to_inf); const v128_t vnanmaskw_lo = wasm_i32x4_gt(vabsx_lo, vexpw_max); const v128_t vnanmaskw_hi = wasm_i32x4_gt(vabsx_hi, vexpw_max); vbias_lo = wasm_v128_and(vbias_lo, vexpw_max); vbias_hi = wasm_v128_and(vbias_hi, vexpw_max); vf_lo = wasm_f32x4_mul(vf_lo, vscale_to_zero); vf_hi = wasm_f32x4_mul(vf_hi, vscale_to_zero); const v128_t vnanmaskh = wasm_i16x8_narrow_i32x4(vnanmaskw_lo, vnanmaskw_hi); const v128_t vsignh = wasm_i16x8_narrow_i32x4(vsignx_lo, vsignx_hi); vbias_lo = wasm_i16x8_max(vbias_lo, vbias_min); vbias_hi = wasm_i16x8_max(vbias_hi, vbias_min); vf_lo = wasm_f32x4_add(vf_lo, vbias_lo); vf_hi = wasm_f32x4_add(vf_hi, vbias_hi); v128_t vexpw_lo = wasm_i32x4_shr(vf_lo, 13); v128_t vexpw_hi = wasm_i32x4_shr(vf_hi, 13); const v128_t vmantw_lo = wasm_v128_and(vf_lo, vmanth_mask); const v128_t vmantw_hi = wasm_v128_and(vf_hi, vmanth_mask); vexpw_lo = wasm_v128_and(vexpw_lo, vexph_mask); vexpw_hi = wasm_v128_and(vexpw_hi, vexph_mask); const v128_t vnonsignw_lo = wasm_i32x4_add(vmantw_lo, vexpw_lo); const v128_t vnonsignw_hi = wasm_i32x4_add(vmantw_hi, vexpw_hi); const v128_t vnonsignh = wasm_i16x8_narrow_i32x4(vnonsignw_lo, vnonsignw_hi); const v128_t vabsh = wasm_v128_bitselect(vnanh, vnonsignh, vnanmaskh); const v128_t vh = wasm_v128_or(vabsh, vsignh); wasm_v128_store(o, vh); o += 8; } }
3,012
35.301205
81
c
XNNPACK
XNNPACK-master/src/math/f32-qs8-cvt-neon.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qs8_cvt__neon( size_t n, const float* input, int8_t* output, int8_t output_zero_point) { assert(n % (8 * sizeof(int8_t)) == 0); const float32x4_t vfmagic = vdupq_n_f32(12582912.0f); const int32x4_t vimagic = vdupq_n_s32(INT32_C(0x4B400000) - (int32_t) output_zero_point); for (; n != 0; n -= 8 * sizeof(int8_t)) { float32x4_t vx_lo = vld1q_f32(input); input += 4; float32x4_t vx_hi = vld1q_f32(input); input += 4; vx_lo = vaddq_f32(vx_lo, vfmagic); vx_hi = vaddq_f32(vx_hi, vfmagic); int32x4_t vy_lo = vreinterpretq_s32_f32(vx_lo); int32x4_t vy_hi = vreinterpretq_s32_f32(vx_hi); vy_lo = vqsubq_s32(vy_lo, vimagic); vy_hi = vqsubq_s32(vy_hi, vimagic); const int16x8_t vy = vcombine_s16(vqmovn_s32(vy_lo), vqmovn_s32(vy_hi)); const int8x8_t vout = vqmovn_s16(vy); vst1_s8(output, vout); output += 8; } }
1,180
25.840909
91
c
XNNPACK
XNNPACK-master/src/math/f32-qs8-cvt-neonv8.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/intrinsics-polyfill.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qs8_cvt__neonv8( size_t n, const float* input, int8_t* output, int8_t output_zero_point) { assert(n % (8 * sizeof(int8_t)) == 0); const int16x8_t voutput_zero_point = vdupq_n_s16((int16_t) output_zero_point); for (; n != 0; n -= 8 * sizeof(int8_t)) { const float32x4_t vx_lo = vld1q_f32(input); input += 4; const float32x4_t vx_hi = vld1q_f32(input); input += 4; const int32x4_t vy_lo = vcvtnq_s32_f32(vx_lo); const int32x4_t vy_hi = vcvtnq_s32_f32(vx_hi); const int16x8_t vy = vqaddq_s16(vcombine_s16(vqmovn_s32(vy_lo), vqmovn_s32(vy_hi)), voutput_zero_point); const int8x8_t vout = vqmovn_s16(vy); vst1_s8(output, vout); output += 8; } }
1,038
26.342105
108
c
XNNPACK
XNNPACK-master/src/math/f32-qs8-cvt-wasmsimd.c
// Copyright 2022 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qs8_cvt__wasmsimd( size_t n, const float* input, int8_t* output, int8_t output_zero_point) { assert(n % (16 * sizeof(int8_t)) == 0); const v128_t vmin = wasm_f32x4_splat(12582912.0f - 128.0f - (float) output_zero_point); const v128_t vfmagic = wasm_f32x4_const_splat(12582912.0f); const v128_t vimagic = wasm_i32x4_splat(INT32_C(0x4B400000) - (int32_t) output_zero_point); for (; n != 0; n -= 16 * sizeof(int8_t)) { const v128_t vx_ll = wasm_v128_load(input); const v128_t vx_lh = wasm_v128_load(input + 4); const v128_t vx_hl = wasm_v128_load(input + 8); const v128_t vx_hh = wasm_v128_load(input + 12); input += 16; v128_t vy_ll = wasm_f32x4_add(vx_ll, vfmagic); v128_t vy_lh = wasm_f32x4_add(vx_lh, vfmagic); v128_t vy_hl = wasm_f32x4_add(vx_hl, vfmagic); v128_t vy_hh = wasm_f32x4_add(vx_hh, vfmagic); vy_ll = wasm_i32x4_max(vy_ll, vmin); vy_lh = wasm_i32x4_max(vy_lh, vmin); vy_hl = wasm_i32x4_max(vy_hl, vmin); vy_hh = wasm_i32x4_max(vy_hh, vmin); vy_ll = wasm_i32x4_sub(vy_ll, vimagic); vy_lh = wasm_i32x4_sub(vy_lh, vimagic); vy_hl = wasm_i32x4_sub(vy_hl, vimagic); vy_hh = wasm_i32x4_sub(vy_hh, vimagic); const v128_t vy_lo = wasm_i16x8_narrow_i32x4(vy_ll, vy_lh); const v128_t vy_hi = wasm_i16x8_narrow_i32x4(vy_hl, vy_hh); const v128_t vout = wasm_i8x16_narrow_i16x8(vy_lo, vy_hi); wasm_v128_store(output, vout); output += 16; } }
1,769
30.607143
93
c
XNNPACK
XNNPACK-master/src/math/f32-qu8-cvt-neon.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qu8_cvt__neon( size_t n, const float* input, uint8_t* output, uint8_t output_zero_point) { assert(n % (8 * sizeof(uint8_t)) == 0); const float32x4_t vfmagic = vdupq_n_f32(12582912.0f); const int32x4_t vimagic = vdupq_n_s32(INT32_C(0x4B400000) - (int32_t) output_zero_point); for (; n != 0; n -= 8 * sizeof(uint8_t)) { float32x4_t vx_lo = vld1q_f32(input); input += 4; float32x4_t vx_hi = vld1q_f32(input); input += 4; vx_lo = vaddq_f32(vx_lo, vfmagic); vx_hi = vaddq_f32(vx_hi, vfmagic); int32x4_t vy_lo = vreinterpretq_s32_f32(vx_lo); int32x4_t vy_hi = vreinterpretq_s32_f32(vx_hi); vy_lo = vqsubq_s32(vy_lo, vimagic); vy_hi = vqsubq_s32(vy_hi, vimagic); const int16x8_t vy = vcombine_s16(vqmovn_s32(vy_lo), vqmovn_s32(vy_hi)); const uint8x8_t vout = vqmovun_s16(vy); vst1_u8(output, vout); output += 8; } }
1,186
25.977273
91
c
XNNPACK
XNNPACK-master/src/math/f32-qu8-cvt-neonv8.c
// Copyright 2021 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/intrinsics-polyfill.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qu8_cvt__neonv8( size_t n, const float* input, uint8_t* output, uint8_t output_zero_point) { assert(n % (8 * sizeof(int8_t)) == 0); const int16x8_t voutput_zero_point = vdupq_n_s16((int16_t) (uint16_t) output_zero_point); for (; n != 0; n -= 8 * sizeof(int8_t)) { const float32x4_t vx_lo = vld1q_f32(input); input += 4; const float32x4_t vx_hi = vld1q_f32(input); input += 4; const int32x4_t vy_lo = vcvtnq_s32_f32(vx_lo); const int32x4_t vy_hi = vcvtnq_s32_f32(vx_hi); const int16x8_t vy = vqaddq_s16(vcombine_s16(vqmovn_s32(vy_lo), vqmovn_s32(vy_hi)), voutput_zero_point); const uint8x8_t vout = vqmovun_s16(vy); vst1_u8(output, vout); output += 8; } }
1,053
26.736842
108
c
XNNPACK
XNNPACK-master/src/math/f32-qu8-cvt-wasmsimd.c
// Copyright 2022 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_qu8_cvt__wasmsimd( size_t n, const float* input, uint8_t* output, uint8_t output_zero_point) { assert(n % (16 * sizeof(uint8_t)) == 0); const v128_t vmin = wasm_f32x4_splat(12582912.0f - (float) (int32_t) output_zero_point); const v128_t vfmagic = wasm_f32x4_const_splat(12582912.0f); const v128_t vimagic = wasm_i32x4_splat(INT32_C(0x4B400000) - (int32_t) output_zero_point); for (; n != 0; n -= 16 * sizeof(uint8_t)) { const v128_t vx_ll = wasm_v128_load(input); const v128_t vx_lh = wasm_v128_load(input + 4); const v128_t vx_hl = wasm_v128_load(input + 8); const v128_t vx_hh = wasm_v128_load(input + 12); input += 16; v128_t vy_ll = wasm_f32x4_add(vx_ll, vfmagic); v128_t vy_lh = wasm_f32x4_add(vx_lh, vfmagic); v128_t vy_hl = wasm_f32x4_add(vx_hl, vfmagic); v128_t vy_hh = wasm_f32x4_add(vx_hh, vfmagic); vy_ll = wasm_i32x4_max(vy_ll, vmin); vy_lh = wasm_i32x4_max(vy_lh, vmin); vy_hl = wasm_i32x4_max(vy_hl, vmin); vy_hh = wasm_i32x4_max(vy_hh, vmin); vy_ll = wasm_i32x4_sub(vy_ll, vimagic); vy_lh = wasm_i32x4_sub(vy_lh, vimagic); vy_hl = wasm_i32x4_sub(vy_hl, vimagic); vy_hh = wasm_i32x4_sub(vy_hh, vimagic); const v128_t vy_lo = wasm_i16x8_narrow_i32x4(vy_ll, vy_lh); const v128_t vy_hi = wasm_i16x8_narrow_i32x4(vy_hl, vy_hh); const v128_t vout = wasm_u8x16_narrow_i16x8(vy_lo, vy_hi); wasm_v128_store(output, vout); output += 16; } }
1,774
30.696429
93
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-neon-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__neon_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign bit of a floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const uint32x4_t vone = vmovq_n_u32(UINT32_C(0x3F800000)); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float32x4_t vabsx = vabsq_f32(vx); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain. const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask); // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const float32x4_t vrndx = vbslq_f32(vrndmask, vx, vrndabsx); // Adjust x rounded towards nearest-even to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vy = vsubq_f32(vrndx, vreinterpretq_f32_u32(vandq_u32(vcgtq_f32(vrndx, vx), vone))); vst1q_f32(output, vy); output += 4; } }
3,120
49.33871
119
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-neon-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__neon_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float32x4_t vintegral_threshold = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const uint32x4_t vone = vmovq_n_u32(UINT32_C(0x3F800000)); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float32x4_t vprerndx = vcvtq_f32_s32(vcvtq_s32_f32(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const uint32x4_t vrndmask = vbicq_u32(vcaltq_f32(vx, vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const float32x4_t vrndx = vbslq_f32(vrndmask, vprerndx, vx); // Adjust x rounded towards nearest-even to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vy = vsubq_f32(vrndx, vreinterpretq_f32_u32(vandq_u32(vcgtq_f32(vrndx, vx), vone))); vst1q_f32(output, vy); output += 4; } }
2,424
43.090909
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-scalar-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__scalar_addsub( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float vmagic_number = 0x1.000000p+23f; // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float vabsx = fabsf(vx); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float vprerndabsx = (vabsx + vmagic_number) - vmagic_number; // Select between the abs(x) rounded using addition-subtraction trick and the abs(x) value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick. // For abs(x) >= 2**23, the result is abs(x) itself (already an integer). // For NaN inputs, the result is abs(x) converted to QNaN as a side-effect of addition-subtraction. const float vrndabsx = XNN_UNPREDICTABLE(vabsx >= vmagic_number) ? vabsx : vprerndabsx; // Restore the sign of the rounded value. const float vrndx = copysignf(vrndabsx, vx); // Adjust x rounded to nearest-even to get x rounded down. const float vy = XNN_UNPREDICTABLE(vrndx > vx) ? vrndx - vone : vrndx; *output++ = vy; } }
2,402
43.5
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-scalar-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <stdint.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__scalar_cvt( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float vintegral_threshold = 0x1.000000p+23f; // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-towards-zero operation. const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float vprerndx = (float) (int32_t) vx; // Compute abs(x) to check if the FP->INT->FP conversion result is valid. const float vabsx = fabsf(vx); // Select between the x rounded via FP->INT->FP conversion and the original x value. const float vrndx = XNN_UNPREDICTABLE(vabsx < vintegral_threshold) ? vprerndx : vx; // Restore the sign of -0.0f lost in the FP->INT->FP conversion. const float vadjrndx = copysignf(vrndx, vx); // Adjust x rounded towards zero to get x rounded down. // Note: addition implicitly converts SNaN inputs to QNaNs. const float vy = XNN_UNPREDICTABLE(vrndx <= vx) ? vadjrndx : vrndx - vone; *output++ = vy; } }
1,765
34.32
110
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-sse-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__sse_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for all bits of a floating-point number except the sign bit. const __m128 vnonsign_mask = _mm_set1_ps(math_nonsign_mask_f32()); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const __m128 vmagic_number = _mm_set1_ps(0x1.000000p+23f); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const __m128 vone = _mm_set1_ps(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const __m128 vabsx = _mm_and_ps(vx, vnonsign_mask); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const __m128 vrndx = _mm_or_ps(_mm_and_ps(vrndabsx, vrndmask), _mm_andnot_ps(vrndmask, vx)); // Adjust x rounded to nearest-even to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vy = _mm_sub_ps(vrndx, _mm_and_ps(_mm_cmpgt_ps(vrndx, vx), vone)); _mm_store_ps(output, vy); output += 4; } }
3,010
46.793651
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-sse2-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__sse2_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // This magic number serves two purposes: // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask. // 2. Check if the input to CVTTPS2DQ (_mm_cvttps_epi32) is out-of-range, which results in 0x80000000 output. const __m128i vmagic = _mm_set1_epi32(0x80000000); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-towards-zero operation. const __m128 vone = _mm_set1_ps(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero. // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000). const __m128i vintx = _mm_cvttps_epi32(vx); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If x is out-of-range for CVTTPS2DQ, we want all bits from x. // If x is in-range for CVTTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x. const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); // Convert integer back to floating-point. // We binary OR the result with the sign of x to restore the sign of negative zero. const __m128 vprerndx = _mm_cvtepi32_ps(vintx); // Combine x rounded via conversion to integer and the initial x value. // For -2**31 < x < 2**31, the result is x rounded via conversion to integer. // Otherwise (including NaN inputs), the result is x itself. const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx)); // Adjust x rounded towards zero to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vy = _mm_sub_ps(vrndx, _mm_and_ps(_mm_cmpgt_ps(vrndx, vx), vone)); _mm_store_ps(output, vy); output += 4; } }
2,329
39.172414
111
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-wasmsimd-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__wasmsimd_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for the sign bit of a floating-point number. const v128_t vsign_mask = wasm_i32x4_const_splat(INT32_C(0x80000000)); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const v128_t vmagic_number = wasm_f32x4_const_splat(0x1.000000p+23f); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const v128_t vone = wasm_f32x4_const_splat(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x). // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x). // Otherwise (abs(x) >= 2**23), we want all bits from x. const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_ge(vabsx, vmagic_number)); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask); // Adjust x rounded towards nearest-even to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vy = wasm_f32x4_sub(vrndx, wasm_v128_and(wasm_f32x4_gt(vrndx, vx), vone)); wasm_v128_store(output, vy); output += 4; } }
3,019
46.936508
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundd-wasmsimd-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundd__wasmsimd_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const v128_t vintegral_threshold = wasm_f32x4_const_splat(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const v128_t vsign_mask = wasm_f32x4_const_splat(-0.0f); // Unit constant to decrement results rounded "wrong way" (i.e. up) in the round-to-nearest-even operation. const v128_t vone = wasm_f32x4_const_splat(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const v128_t vprerndx = wasm_f32x4_convert_i32x4(wasm_i32x4_trunc_sat_f32x4(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(wasm_f32x4_abs(vx), vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const v128_t vrndx = wasm_v128_bitselect(vprerndx, vx, vrndmask); // Adjust x rounded towards nearest-even to get x rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vy = wasm_f32x4_sub(vrndx, wasm_v128_and(wasm_f32x4_gt(vrndx, vx), vone)); wasm_v128_store(output, vy); output += 4; } }
2,466
42.280702
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundne-neon-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundne__neon_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign bit of a floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float32x4_t vabsx = vabsq_f32(vx); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain. const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const float32x4_t vy = vbslq_f32(vrndmask, vx, vrndabsx); vst1q_f32(output, vy); output += 4; } }
2,687
47
119
c
XNNPACK
XNNPACK-master/src/math/f32-roundne-scalar-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundne__scalar_addsub( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float vmagic_number = 0x1.000000p+23f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float vabsx = fabsf(vx); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float vrndabsx = (vabsx + vmagic_number) - vmagic_number; // Select between the abs(x) rounded using addition-subtraction trick and the abs(x) value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick. // For abs(x) >= 2**23, the result is abs(x) itself (already an integer). // For NaN inputs, the result is abs(x) converted to QNaN as a side-effect of addition-subtraction. const float vabsy = XNN_UNPREDICTABLE(vabsx >= vmagic_number) ? vabsx : vrndabsx; // Restore the sign of the rounded value. const float vy = copysignf(vabsy, vx); *output++ = vy; } }
2,112
42.122449
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundne-sse-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundne__sse_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for all bits of a floating-point number except the sign bit. const __m128 vnonsign_mask = _mm_set1_ps(math_nonsign_mask_f32()); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const __m128 vmagic_number = _mm_set1_ps(0x1.000000p+23f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const __m128 vabsx = _mm_and_ps(vx, vnonsign_mask); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const __m128 vy = _mm_or_ps(_mm_and_ps(vrndabsx, vrndmask), _mm_andnot_ps(vrndmask, vx)); _mm_store_ps(output, vy); output += 4; } }
2,642
45.368421
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundne-sse2-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundne__sse2_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // This magic number serves two purposes: // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask. // 2. Check if the input to CVTPS2DQ (_mm_cvtps_epi32) is out-of-range, which results in 0x80000000 output. const __m128i vmagic = _mm_set1_epi32(0x80000000); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Convert floating-point value x to integer, with default rounding (to nearest-even). // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000). const __m128i vintx = _mm_cvtps_epi32(vx); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If x is out-of-range for CVTPS2DQ, we want all bits from x. // If x is in-range for CVTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x. const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); // Convert integer back to floating-point. // We binary OR the result with the sign of x to restore the sign of negative zero. const __m128 vrndx = _mm_cvtepi32_ps(vintx); // Combine x rounded via conversion to integer and the initial x value. // For -2**31 < x < 2**31, the result is x rounded via conversion to integer. // Otherwise (including NaN inputs), the result is x itself. const __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx)); _mm_store_ps(output, vy); output += 4; } }
1,969
36.884615
110
c
XNNPACK
XNNPACK-master/src/math/f32-roundne-wasmsimd-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundne__wasmsimd_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for the sign bit of a floating-point number. const v128_t vsign_mask = wasm_i32x4_const_splat(INT32_C(0x80000000)); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const v128_t vmagic_number = wasm_f32x4_const_splat(0x1.000000p+23f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x). // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x). // Otherwise (abs(x) >= 2**23), we want all bits from x. const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_gt(vabsx, vmagic_number)); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const v128_t vy = wasm_v128_bitselect(vx, vrndabsx, vrndmask); wasm_v128_store(output, vy); output += 4; } }
2,628
45.122807
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-neon-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__neon_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign bit of a floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation. const float32x4_t vone = vmovq_n_f32(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float32x4_t vabsx = vabsq_f32(vx); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain. const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask); // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const float32x4_t vrndx = vbslq_f32(vrndmask, vx, vrndabsx); // Compute bitmask for the bits to copy from the adjusted rounded x. Other bits will be copied from rounded x. // If rounded x < x, we want all but the sign bit from the adjusted rounded x and the sign bit from rounded x (same // as the sign bit of x). // If rounded x >= x or rounded x is NaN (implies x is NaN), we want all bits from rounded x. const uint32x4_t vadjmask = vbicq_u32(vcltq_f32(vrndx, vx), vsign_mask); // Adjust the rounded x value. // The adjusted value is a unit above the rounded-to-nearest-even x value, but is used only if the rounded value is // below x. In these cases, the adjusted value is x rounded up. const float32x4_t vadjrndx = vaddq_f32(vrndx, vone); // Combine the adjusted rounded x and the original rounded to nearest-even x. // For rounded x < x, the result is the absolute value of adjusted rounded-to-nearest-even x with the sign of // rounded-to-nearest-even x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-to-nearest-even x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of addition-subtraction). const float32x4_t vy = vbslq_f32(vadjmask, vadjrndx, vrndx); vst1q_f32(output, vy); output += 4; } }
4,264
55.118421
119
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-neon-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__neon_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float32x4_t vintegral_threshold = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation. const float32x4_t vone = vmovq_n_f32(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float32x4_t vprerndx = vcvtq_f32_s32(vcvtq_s32_f32(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const uint32x4_t vrndmask = vbicq_u32(vcaltq_f32(vx, vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const float32x4_t vrndx = vbslq_f32(vrndmask, vprerndx, vx); // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x. // If rounded x >= x, we want all bits from rounded x. // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded // x and the sign bit from rounded x (same as the sign bit of x). const uint32x4_t vadjmask = vorrq_u32(vcgeq_f32(vrndx, vx), vsign_mask); // Adjust the rounded x value. // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is // below x. In these cases, the adjusted value is x rounded up. // Note: addition implicitly converts SNaN inputs to QNaNs. const float32x4_t vadjrndx = vaddq_f32(vrndx, vone); // Combine the adjusted rounded x and the original rounded towards zero x. // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-towards-zero x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment). const float32x4_t vy = vbslq_f32(vadjmask, vrndx, vadjrndx); vst1q_f32(output, vy); output += 4; } }
3,603
50.485714
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-scalar-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__scalar_addsub( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float vmagic_number = 0x1.000000p+23f; // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation. const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const float vabsx = fabsf(vx); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float vprerndabsx = (vabsx + vmagic_number) - vmagic_number; // Select between the abs(x) rounded using addition-subtraction trick and the abs(x) value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick. // For abs(x) >= 2**23, the result is abs(x) itself (already an integer). // For NaN inputs, the result is abs(x) converted to QNaN as a side-effect of addition-subtraction. const float vrndabsx = XNN_UNPREDICTABLE(vabsx >= vmagic_number) ? vabsx : vprerndabsx; // Restore the sign of the rounded value. const float vrndx = copysignf(vrndabsx, vx); // Adjust x rounded to nearest-even to get x rounded up. const float vprey = XNN_UNPREDICTABLE(vrndx < vx) ? vrndx + vone : vrndx; // Restore the sign of the adjusted value. // This second restoration of the sign is important to produce negative zero for -1.0 < x < -0.5 inputs, where // otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. const float vy = copysignf(vprey, vrndx); *output++ = vy; } }
2,687
45.344828
117
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-scalar-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <stdint.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__scalar_cvt( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float vintegral_threshold = 0x1.000000p+23f; // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation. const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float vprerndx = (float) (int32_t) vx; // Compute abs(x) to check if the FP->INT->FP conversion result is valid. const float vabsx = fabsf(vx); // Select between the x rounded via FP->INT->FP conversion and the original x value. const float vrndx = XNN_UNPREDICTABLE(vabsx < vintegral_threshold) ? vprerndx : vx; // Adjust x rounded towards zero to get x rounded up. // Note: addition implicitly converts SNaN inputs to QNaNs. const float vprey = XNN_UNPREDICTABLE(vrndx >= vx) ? vrndx : vrndx + vone; // Restore the sign of -0.0f lost in the FP->INT->FP conversion and adjustment. const float vy = copysignf(vprey, vx); *output++ = vy; } }
1,773
35.204082
110
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-sse-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__sse_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for all bits of a floating-point number except the sign bit. const __m128 vnonsign_mask = _mm_set1_ps(math_nonsign_mask_f32()); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const __m128 vmagic_number = _mm_set1_ps(0x1.000000p+23f); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation. const __m128 vone = _mm_set1_ps(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const __m128 vabsx = _mm_and_ps(vx, vnonsign_mask); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const __m128 vrndx = _mm_or_ps(_mm_and_ps(vrndabsx, vrndmask), _mm_andnot_ps(vrndmask, vx)); // Compute bitmask for the bits to copy from the adjusted rounded x. Other bits will be copied from rounded x. // If rounded x < x, we want all but the sign bit from the adjusted rounded x and the sign bit from rounded x (same // as the sign bit of x). // If rounded x >= x or rounded x is NaN (implies x is NaN), we want all bits from rounded x. const __m128 vadjmask = _mm_and_ps(_mm_cmplt_ps(vrndx, vx), vnonsign_mask); // Compute adjusted rounded x value. // The adjusted value is a unit above the rounded-to-nearest-even x value, but is used only if the rounded value is // below x. In this cases, the adjusted value is x rounded up. const __m128 vadjrndx = _mm_add_ps(vrndx, vone); // Combine the adjusted rounded x and the original rounded to nearest-even x. // For rounded x < x, the result is the absolute value of adjusted rounded-to-nearest-even x with the sign of // rounded-to-nearest-even x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-to-nearest-even x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of addition-subtraction). const __m128 vy = _mm_or_ps(_mm_and_ps(vadjrndx, vadjmask), _mm_andnot_ps(vadjmask, vrndx)); _mm_store_ps(output, vy); output += 4; } }
4,233
53.987013
119
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-sse2-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__sse2_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // This magic number serves two purposes: // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask. // 2. Check if the input to CVTTPS2DQ (_mm_cvttps_epi32) is out-of-range, which results in 0x80000000 output. const __m128i vmagic = _mm_set1_epi32(0x80000000); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation. const __m128 vone = _mm_set1_ps(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero. // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000). const __m128i vintx = _mm_cvttps_epi32(vx); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If x is out-of-range for CVTTPS2DQ, we want all bits from x. // If x is in-range for CVTTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x. const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); // Convert integer back to floating-point. // We binary OR the result with the sign of x to restore the sign of negative zero. const __m128 vprerndx = _mm_cvtepi32_ps(vintx); // Combine x rounded via conversion to integer and the initial x value. // For -2**31 < x < 2**31, the result is x rounded via conversion to integer. // Otherwise (including NaN inputs), the result is x itself. const __m128 vrndx = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vprerndx)); // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x. // If rounded x >= x, we want all bits from rounded x. // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded // x and the sign bit from rounded x (same as the sign bit of x). const __m128 vadjmask = _mm_or_ps(_mm_cmpge_ps(vrndx, vx), _mm_castsi128_ps(vmagic)); // Adjust the rounded x value. // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is // below x. In these cases, the adjusted value is x rounded up. // Note: addition implicitly converts SNaN inputs to QNaNs. const __m128 vadjrndx = _mm_add_ps(vrndx, vone); // Combine the adjusted rounded x and the original rounded towards zero x. // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-towards-zero x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment). const __m128 vy = _mm_or_ps(_mm_and_ps(vrndx, vadjmask), _mm_andnot_ps(vadjmask, vadjrndx)); _mm_store_ps(output, vy); output += 4; } }
3,598
48.30137
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-wasmsimd-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__wasmsimd_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for the sign bit of a floating-point number. const v128_t vsign_mask = wasm_i32x4_const_splat(INT32_C(0x80000000)); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const v128_t vmagic_number = wasm_f32x4_const_splat(0x1.000000p+23f); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-to-nearest-even operation. const v128_t vone = wasm_f32x4_const_splat(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-to-nearest-even because it is an odd function. const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x). // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x). // Otherwise (abs(x) >= 2**23), we want all bits from x. const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_ge(vabsx, vmagic_number)); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); // Combine abs(x) rounded via addition-subtraction trick and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction. // For abs(x) >= 2**23, the result is x itself. const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask); // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x. // If rounded x >= x, we want all bits from rounded x. // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded // x and the sign bit from rounded x (same as the sign bit of x). const v128_t vadjmask = wasm_v128_or(wasm_f32x4_ge(vrndx, vx), vsign_mask); // Adjust the rounded x value. // The adjusted value is a unit above the rounded-to-nearest-even x value, but is used only if the rounded value is // below x. In these cases, the adjusted value is x rounded up. // Note: addition implicitly converts SNaN inputs to QNaNs. const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone); // Combine the adjusted rounded x and the original rounded towards zero x. // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-towards-zero x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment). const v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask); wasm_v128_store(output, vy); output += 4; } }
4,242
53.397436
119
c
XNNPACK
XNNPACK-master/src/math/f32-roundu-wasmsimd-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundu__wasmsimd_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const v128_t vintegral_threshold = wasm_f32x4_const_splat(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const v128_t vsign_mask = wasm_f32x4_const_splat(-0.0f); // Unit constant to increment results rounded "wrong way" (i.e. down) in the round-towards-zero operation. const v128_t vone = wasm_f32x4_const_splat(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const v128_t vprerndx = wasm_f32x4_convert_i32x4(wasm_i32x4_trunc_sat_f32x4(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(wasm_f32x4_abs(vx), vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const v128_t vrndx = wasm_v128_bitselect(vprerndx, vx, vrndmask); // Compute bitmask for the bits to copy from the rounded x. Other bits will be copied from the adjusted rounded x. // If rounded x >= x, we want all bits from rounded x. // If rounded x < x or rounded x is NaN (implies x is NaN), we want all but the sign bit from the adjusted rounded // x and the sign bit from rounded x (same as the sign bit of x). const v128_t vadjmask = wasm_v128_or(wasm_f32x4_ge(vrndx, vx), vsign_mask); // Adjust the rounded x value. // The adjusted value is a unit above the rounded-towards-zero x value, but is used only if the rounded value is // below x. In these cases, the adjusted value is x rounded up. // Note: addition implicitly converts SNaN inputs to QNaNs. const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone); // Combine the adjusted rounded x and the original rounded towards zero x. // For rounded x < x, the result is the absolute value of adjusted rounded-towards-zero x with the sign of // rounded-towards x (same as sign of x). Propagating the sign of x is important to produce negative zero // for -1.0 < x < -0.5 inputs, where otherwise we would get -1.0 (rounded x) + 1.0 (adjustment) = +0.0. // For rounded x >= x, the result is the rounded-towards-zero x. // For NaN inputs, the result is rounded x (same as x converted to QNaN as a side-effect of adjustment). const v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask); wasm_v128_store(output, vy); output += 4; } }
3,683
50.166667
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-neon-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__neon_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float32x4_t vmagic_number = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign bit of a floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); // Unit constant to decrement absolute values rounded "wrong way" (i.e. away from zero) in the round-to-nearest-even // operation. const uint32x4_t vone = vmovq_n_u32(UINT32_C(0x3F800000)); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-towards-zero because it is an odd function. const float32x4_t vabsx = vabsq_f32(vx); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. // Note: we do vcaltq_f32(vmagic_number, vx) instead of vcltq_f32(vmagic_number, vabsx) to reduce dependency chain. const uint32x4_t vrndmask = vorrq_u32(vcaltq_f32(vmagic_number, vx), vsign_mask); // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vrndabsx = vsubq_f32(vaddq_f32(vabsx, vmagic_number), vmagic_number); // Compute adjustment to be subtracted from the rounded-to-nearest-even abs(x) value. // Adjustment is one if the rounded value is greater than the abs(x) value and zero otherwise (including NaN input). const float32x4_t vadjustment = vreinterpretq_f32_u32(vandq_u32(vone, vcgtq_f32(vrndabsx, vabsx))); // Adjust abs(x) rounded to nearest-even via the addition-subtraction trick to get abs(x) rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const float32x4_t vflrabsx = vsubq_f32(vrndabsx, vadjustment); // Combine abs(x) rounded via addition-subtraction trick with adjustment and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction and adjustment. // For abs(x) >= 2**23, the result is x itself. const float32x4_t vy = vbslq_f32(vrndmask, vx, vflrabsx); vst1q_f32(output, vy); output += 4; } }
3,485
51.818182
120
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-neon-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__neon_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float32x4_t vintegral_threshold = vmovq_n_f32(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const uint32x4_t vsign_mask = vmovq_n_u32(UINT32_C(0x80000000)); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float32x4_t vrndx = vcvtq_f32_s32(vcvtq_s32_f32(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const uint32x4_t vrndmask = vbicq_u32(vcaltq_f32(vx, vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const float32x4_t vy = vbslq_f32(vrndmask, vrndx, vx); vst1q_f32(output, vy); output += 4; } }
2,001
39.857143
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-scalar-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__scalar_addsub( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const float vmagic_number = 0x1.000000p+23f; // Unit constant to decrement absolute values rounded "wrong way" (i.e. away from zero) in the round-to-nearest-even // operation. const float vone = 1.0f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-towards-zero because it is an odd function. const float vabsx = fabsf(vx); // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const float vrndabsx = (vabsx + vmagic_number) - vmagic_number; // Adjust abs(x) rounded to nearest-even via the addition-subtraction trick to get abs(x) rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const float vflrabsx = XNN_UNPREDICTABLE(vrndabsx <= vabsx) ? vrndabsx : vrndabsx - vone; // Select between the abs(x) rounded down using addition-subtraction trick with adjustment and the abs(x) value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick. // For abs(x) >= 2**23, the result is abs(x) itself (already an integer). // For NaN inputs, the result is abs(x) converted to QNaN as a side-effect of addition-subtraction. const float vabsy = XNN_UNPREDICTABLE(vabsx >= vmagic_number) ? vabsx : vflrabsx; // Restore the sign of the rounded value. const float vy = copysignf(vabsy, vx); *output++ = vy; } }
2,578
45.053571
118
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-scalar-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <math.h> #include <stddef.h> #include <stdint.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__scalar_cvt( size_t n, const float* input, float* output) { assert(n % sizeof(float) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const float vintegral_threshold = 0x1.000000p+23f; for (; n != 0; n -= sizeof(float)) { const float vx = *input++; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const float vrndx = (float) (int32_t) vx; // Compute abs(x) to check if the FP->INT->FP conversion result is valid. const float vabsx = fabsf(vx); // Select between the x rounded via FP->INT->FP conversion and the original x value. const float vprey = XNN_UNPREDICTABLE(vabsx < vintegral_threshold) ? vrndx : vx; // Restore the sign of -0.0f lost in the FP->INT->FP conversion. const float vy = copysignf(vprey, vx); *output++ = vy; } }
1,414
31.906977
110
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-sse-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <xmmintrin.h> #include <xnnpack/math.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__sse_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for all bits of a floating-point number except the sign bit. const __m128 vnonsign_mask = _mm_set1_ps(math_nonsign_mask_f32()); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const __m128 vmagic_number = _mm_set1_ps(0x1.000000p+23f); // Unit constant to decrement absolute values rounded "wrong way" (i.e. away from zero) in the round-to-nearest-even // operation. const __m128 vone = _mm_set1_ps(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-towards-zero because it is an odd function. const __m128 vabsx = _mm_and_ps(vx, vnonsign_mask); // Compute bitmask for the bits we want to copy from the rounded abs(x). Other bits will be copied from x. // If abs(x) >= 2**23, we want all bits from x. // If abs(x) < 2**23 or x is NaN, we want all but the sign bit from the rounded abs(x) and the sign bit from x. const __m128 vrndmask = _mm_andnot_ps(_mm_cmpge_ps(vabsx, vmagic_number), vnonsign_mask); // Addition-subtraction trick with the magic number to cause rounding to the nearest-even integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vrndabsx = _mm_sub_ps(_mm_add_ps(vabsx, vmagic_number), vmagic_number); // Compute adjustment to be subtracted from the rounded-to-nearest-even abs(x) value. // Adjustment is one if the rounded value is greater than the abs(x) value and zero otherwise (including NaN input). const __m128 vadjustment = _mm_and_ps(vone, _mm_cmpgt_ps(vrndabsx, vabsx)); // Adjust abs(x) rounded to nearest-even via the addition-subtraction trick to get abs(x) rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const __m128 vflrabsx = _mm_sub_ps(vrndabsx, vadjustment); // Combine abs(x) rounded down via addition-subtraction trick with adjustment and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction and adjustment. // For abs(x) >= 2**23, the result is x itself. const __m128 vy = _mm_or_ps(_mm_and_ps(vflrabsx, vrndmask), _mm_andnot_ps(vrndmask, vx)); _mm_store_ps(output, vy); output += 4; } }
3,397
49.716418
120
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-sse2-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <emmintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__sse2_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // This magic number serves two purposes: // 1. Set the bit corresponding to the sign of a floating-point number in a bitmask. // 2. Check if the input to CVTTPS2DQ (_mm_cvttps_epi32) is out-of-range, which results in 0x80000000 output. const __m128i vmagic = _mm_set1_epi32(0x80000000); for (; n != 0; n -= 4 * sizeof(float)) { const __m128 vx = _mm_load_ps(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero. // If x is beyond [-2**31, 2**31-1] range or x is NaN, the result is -2**31 (0x80000000). const __m128i vintx = _mm_cvttps_epi32(vx); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If x is out-of-range for CVTTPS2DQ, we want all bits from x. // If x is in-range for CVTTPS2DQ, we want all but the sign bit from the rounded x and the sign bit from x. const __m128 vrndmask = _mm_castsi128_ps(_mm_or_si128(vmagic, _mm_cmpeq_epi32(vintx, vmagic))); // Convert integer back to floating-point. // We binary OR the result with the sign of x to restore the sign of negative zero. const __m128 vrndx = _mm_cvtepi32_ps(vintx); // Combine x rounded via conversion to integer and the initial x value. // For -2**31 < x < 2**31, the result is x rounded via conversion to integer. // Otherwise (including NaN inputs), the result is x itself. const __m128 vy = _mm_or_ps(_mm_and_ps(vx, vrndmask), _mm_andnot_ps(vrndmask, vrndx)); _mm_store_ps(output, vy); output += 4; } }
1,960
36.711538
111
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-wasmsimd-addsub.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__wasmsimd_addsub( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Mask for the sign bit of a floating-point number. const v128_t vsign_mask = wasm_i32x4_const_splat(INT32_C(0x80000000)); // Addition of this number to a floating-point number x cause rounding of the result to an integer. Then this magic // number is subtracted back from the result to get original x rounded to integer. This trick works only for // 0 <= x < 2**24, but all numbers in 2**23 <= x < 2**24 range are integers, so we can further restrict it to // 0 <= x < 2**23. Then the upper bound of the validity interval is conveniently the same as the magic number. const v128_t vmagic_number = wasm_f32x4_const_splat(0x1.000000p+23f); // Unit constant to decrement absolute values rounded "wrong way" (i.e. away from zero) in the round-to-nearest-even // operation. const v128_t vone = wasm_f32x4_const_splat(1.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // The rounding trick works only for x >= 0, so we compute absolute value of x, round it, and restore the sign in // the end. This method works for round-toward-zero because it is an odd function. const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); // Compute bitmask for the bits we want to copy from x. Other bits will be copied from the rounded abs(x). // If abs(x) < 2**23 or x is NaN, we want the sign bit from x and the rest from the rounded abs(x). // Otherwise (abs(x) >= 2**23), we want all bits from x. const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_ge(vabsx, vmagic_number)); // Addition-subtraction trick with the magic number to cause rounding to integer for abs(x). // Note: the result is valid only for 0 <= abs(x) < 2**23. // Note: addition-subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); // Compute adjustment to be subtracted from the rounded-to-nearest-even abs(x) value. // Adjustment is one if the rounded value is greater than the abs(x) value and zero otherwise (including NaN input). const v128_t vadjustment = wasm_v128_and(wasm_f32x4_gt(vrndabsx, vabsx), vone); // Adjust abs(x) rounded to nearest-even via the addition-subtraction trick to get abs(x) rounded down. // Note: subtraction implicitly converts SNaN inputs to QNaNs. const v128_t vflrabsx = wasm_f32x4_sub(vrndabsx, vadjustment); // Combine abs(x) rounded down via addition-subtraction trick with adjustment and the input x value. // For abs(x) < 2**23, the result is abs(x) rounded via addition-subtraction trick with the sign of x. // For NaN inputs, the result is x converted to QNaN as a side-effect of addition-subtraction and adjustment. // For abs(x) >= 2**23, the result is x itself. const v128_t vy = wasm_v128_bitselect(vx, vflrabsx, vrndmask); wasm_v128_store(output, vy); output += 4; } }
3,383
49.507463
120
c
XNNPACK
XNNPACK-master/src/math/f32-roundz-wasmsimd-cvt.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <stdint.h> #include <wasm_simd128.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_roundz__wasmsimd_cvt( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Threshold of non-integral values in single-precision floating-point representation. // All inputs above this threshold (by absolute value) are integer numbers. const v128_t vintegral_threshold = wasm_f32x4_const_splat(0x1.000000p+23f); // Mask for the sign of a single-precision floating-point number. const v128_t vsign_mask = wasm_f32x4_const_splat(-0.0f); for (; n != 0; n -= 4 * sizeof(float)) { const v128_t vx = wasm_v128_load(input); input += 4; // Convert floating-point value x to integer, with rounding towards zero, and then back to floating-point. // Note: the result is valid only for abs(x) < 2**31, but we further restrict its use to 2**23. const v128_t vrndx = wasm_f32x4_convert_i32x4(wasm_i32x4_trunc_sat_f32x4(vx)); // Compute bitmask for the bits we want to copy from the rounded x. Other bits will be copied from x. // If abs(x) is below the integral threshold, use all but the sign bit from the rounded x and the sign bit from x. // If x is guaranteed integral or NaN, use all bits from x. const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(wasm_f32x4_abs(vx), vintegral_threshold), vsign_mask); // Combine x rounded towardz zero via FP->INT->FP conversion and the input x value. // For 0.0 <= x < 2**23, the result is x rounded via FP->INT->FP conversion. // For -2**23 < x <= -0.0, the result is abs(x) rounded via FP->INT->FP conversion with the sign of x. // For abs(x) >= 2**23 or NaN inputs, the result is x itself. const v128_t vy = wasm_v128_bitselect(vrndx, vx, vrndmask); wasm_v128_store(output, vy); output += 4; } }
2,067
39.54902
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr1-lut2048-p1-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__aarch64_neonfma_rr1_lut2048_p1_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
5,706
48.198276
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr1-lut64-p2-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__aarch64_neonfma_rr1_lut64_p2_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
5,751
48.162393
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr1-p5-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__aarch64_neonfma_rr1_p5_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,225
41.686869
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr2-lut2048-p1-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__aarch64_neonfma_rr2_lut2048_p1_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
5,916
48.722689
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr2-lut64-p2-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__aarch64_neonfma_rr2_lut64_p2_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
5,961
48.683333
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-aarch64-neonfma-rr2-p5-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__aarch64_neonfma_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vdivq_f32(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,435
42.490196
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx-rr2-p5-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23)); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_div_ps(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,847
43.888889
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx-rr2-p5-nr1.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx_rr2_p5_nr1( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); const __m256 vtwo = _mm256_set1_ps(2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23)); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_mul_ps(vr, _mm256_sub_ps(vtwo, _mm256_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,213
44.33913
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx-rr2-p5-nr2.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx_rr2_p5_nr2( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Last 7 bits are zeroes const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E400p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(-0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); const __m256 vtwo = _mm256_set1_ps(2.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside // of [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the // result for such inputs at the very end of the algorithm. __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23)); const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23)); const __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz); vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc5, vt), vc4); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2); vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_add_ps(_mm256_mul_ps(vt, vp), vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_mul_ps(vr, _mm256_sub_ps(vtwo, _mm256_mul_ps(vr, vd))); vr = _mm256_mul_ps(vr, _mm256_sub_ps(vtwo, _mm256_mul_ps(vr, vd))); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,286
44.577586
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-lut64-p2-gather-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr1_lut64_p2_gather_div( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_div_ps(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,273
44.86087
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-lut64-p2-gather-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr1_lut64_p2_gather_nr1fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,592
45.22314
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-lut64-p2-gather-nr2fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr1_lut64_p2_gather_nr2fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,659
45.393443
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-lut64-p2-gather-nr2fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr1_lut64_p2_gather_nr2fma1adj( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m256 vf = _mm256_mul_ps(vy, vr); vf = _mm256_fmadd_ps(_mm256_fnmadd_ps(vf, vd, vy), vr, vf); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,773
45.943089
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-p5-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr1_p5_div( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_div_ps(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,366
41.398058
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-p5-nr1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr1_p5_nr1fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,685
41.990826
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr1-p5-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr1_p5_nr2fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2 = _mm256_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,752
42.209091
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-lut64-p2-gather-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr2_lut64_p2_gather_div( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_div_ps(vy, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,492
45.550847
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-lut64-p2-gather-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr2_lut64_p2_gather_nr1fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,811
45.870968
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-lut64-p2-gather-nr2fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr2_lut64_p2_gather_nr2fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,878
46.032
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-lut64-p2-gather-nr2fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__avx2_rr2_lut64_p2_gather_nr2fma1adj( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == exp2(-6) const __m256 vmagic_bias = _mm256_set1_ps(0x1.800000p17f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m256 vindex_mask = _mm256_castsi256_ps(_mm256_set1_epi32(INT32_C(0x3F))); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m256 vc2 = _mm256_set1_ps(0x1.FFFF0Ap-2f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(z) is normalized, // i.e. -87.33642 <= z <= 0. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for -87.33642 <= z <= 0 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). __m256i ve = _mm256_slli_epi32(_mm256_castps_si256(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const __m256i vidx = _mm256_castps_si256(_mm256_and_ps(vn, vindex_mask)); const __m256i vl = _mm256_i32gather_epi32((const int*) xnn_table_exp2minus_k_over_64, vidx, sizeof(float)); // Adjust exponent of the value l fetched from the table to get the final s value. const __m256 vs = _mm256_castsi256_ps(_mm256_add_epi32(vl, ve)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) = 1 + p __m256 vp = _mm256_mul_ps(vt, vc2); vp = _mm256_fmadd_ps(vt, vp, vt); // Reconstruct the exp(z) value: // e = s * (1 + t * (1 + t * c2)) // = s * (1 + p) // = s + s * p const __m256 vy = _mm256_fmadd_ps(vs, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m256 vf = _mm256_mul_ps(vy, vr); vf = _mm256_fmadd_ps(_mm256_fnmadd_ps(vf, vd, vy), vr, vf); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
5,992
46.563492
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-p5-div.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr2_p5_div( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_div_ps(ve, vd); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,591
42.320755
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-p5-nr1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr2_p5_nr1fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,910
42.848214
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx2-rr2-p5-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx2_rr2_p5_nr2fma( size_t n, const float* input, float* output) { assert(n % (8 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m256 vsign_mask = _mm256_set1_ps(-0.0f); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f); const __m256 vlog2e = _mm256_set1_ps(0x1.715476p0f); const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E43p-1f); const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m256 vc5 = _mm256_set1_ps(0x1.0F9F9Cp-7f); const __m256 vc4 = _mm256_set1_ps(0x1.573A1Ap-5f); const __m256 vc3 = _mm256_set1_ps(0x1.555A80p-3f); const __m256 vc2 = _mm256_set1_ps(0x1.FFFDC6p-2f); const __m256 vc1 = _mm256_set1_ps(0x1.FFFFF6p-1f); const __m256 vone = _mm256_set1_ps(1.0f); // The smallest x for which sigmoidf(x) is normalized. // This number is also the smallest x for which expf(x) is normalized. const __m256 vdenorm_cutoff = _mm256_set1_ps(-0x1.5D589Ep+6f); for (; n != 0; n -= 8 * sizeof(float)) { const __m256 vx = _mm256_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m256 vz = _mm256_or_ps(vx, vsign_mask); // Compute reduced argument n := round(z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The addition is combined with multiplication by log2e into a single FMA instruction. The // trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**22, i.e. // |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.33642 <= z <= 0.0, and -126 <= n <= 0 accordingly. const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23)); // Subtract the large number back to get the final n := round(z / log(2)) as a floating-point number. vn = _mm256_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm256_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4); vp = _mm256_fmadd_ps(vp, vt, vc3); vp = _mm256_fmadd_ps(vp, vt, vc2); vp = _mm256_fmadd_ps(vp, vt, vc1); // Reconstruct the exp(z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = _mm256_mul_ps(vt, vs); const __m256 ve = _mm256_fmadd_ps(vt, vp, vs); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m256 vd = _mm256_add_ps(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m256 vr = _mm256_rcp_ps(vd); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); vr = _mm256_fmadd_ps(_mm256_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m256 vf = _mm256_mul_ps(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx); _mm256_storeu_ps(output, vf); input += 8; output += 8; } }
4,977
43.053097
118
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut16-p3-perm-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut16_p3_perm_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,939
40.041667
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut16-p3-perm-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut16_p3_perm_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,260
40.77451
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut16-p3-perm-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut16_p3_perm_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,374
41.475728
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut32-p2-perm2-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut32_p2_perm2_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,203
41.04
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut32-p2-perm2-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut32_p2_perm2_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,524
41.688679
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut32-p2-perm2-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_lut32_p2_perm2_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,638
42.35514
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut64-p2-gather-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr1_lut64_p2_gather_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62e43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,814
39.585106
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut64-p2-gather-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr1_lut64_p2_gather_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62e43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,135
40.36
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-lut64-p2-gather-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr1_lut64_p2_gather_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62e43p-1f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). const __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,249
41.079208
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-p5-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_p5_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
2,729
34
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-p5-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_p5_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,050
35.321429
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr1-p5-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr1_p5_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,164
36.235294
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut16-p3-perm-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut16_p3_perm_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,164
41.070707
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut16-p3-perm-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut16_p3_perm_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,485
41.72381
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut16-p3-perm-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut16_p3_perm_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-4) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 16) values, k = 0..15 const __m512 vtable = _mm512_set_ps( 0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f, 0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f, 0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f, 0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (1 + t * (c2 + t * c3)) on [-log(2)/32, log(2)/32] const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f); const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 4). // We do it by adding a large number (magic bias), which cause rounding of the result to 4 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**18, // i.e. |z| <= 0x1.62E43p+17 = 181704.375), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 4 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable); // Subtract the large number back to get the final n := round(z / log(2), 4) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-3 polynomial approximation for exp(t) on [-log(2)/32, log(2)/32]. // P(t) = 1 + t * (1 + t * (c2 + t * c3)) // p = l * P(t) // = l + l * (t + t * (t * (c2 + t * c3))) __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2); vp = _mm512_mul_ps(vp, vt); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,599
42.396226
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut32-p2-perm2-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut32_p2_perm2_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,428
42
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut32-p2-perm2-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut32_p2_perm2_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,749
42.577982
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut32-p2-perm2-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_lut32_p2_perm2_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-5) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p18f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Table of exp2(k / 32) values, k = 0..31 const __m512 vtable_hi = _mm512_set_ps( 0x1.F50766p+0f, 0x1.EA4AFAp+0f, 0x1.DFC974p+0f, 0x1.D5818Ep+0f, 0x1.CB720Ep+0f, 0x1.C199BEp+0f, 0x1.B7F770p+0f, 0x1.AE89FAp+0f, 0x1.A5503Cp+0f, 0x1.9C4918p+0f, 0x1.93737Cp+0f, 0x1.8ACE54p+0f, 0x1.82589Ap+0f, 0x1.7A1148p+0f, 0x1.71F75Ep+0f, 0x1.6A09E6p+0f); const __m512 vtable_lo = _mm512_set_ps( 0x1.6247ECp+0f, 0x1.5AB07Ep+0f, 0x1.5342B6p+0f, 0x1.4BFDAEp+0f, 0x1.44E086p+0f, 0x1.3DEA64p+0f, 0x1.371A74p+0f, 0x1.306FE0p+0f, 0x1.29E9E0p+0f, 0x1.2387A6p+0f, 0x1.1D4874p+0f, 0x1.172B84p+0f, 0x1.11301Ep+0f, 0x1.0B5586p+0f, 0x1.059B0Ep+0f, 0x1.000000p+0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * c2) on [-log(2)/64, log(2)/64] const __m512 vc2 = _mm512_set1_ps(0x1.000000p-1f); const __m512 vc1 = _mm512_set1_ps(0x1.0000F6p-0f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 5). // We do it by adding a large number (magic bias), which cause rounding of the result to 5 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**17, // i.e. |z| <= 0x1.62E43p+16 = 90852.1875), but that is acceptable, because inputs x outside of // [-87.336544, 17.328678] (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result // for such inputs at the very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 5 bits of n (as integer) for table lookup. const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi); // Subtract the large number back to get the final n := round(z / log(2), 5) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/64, log(2)/64]. // P(t) = 1 + t * (c1 + t * c2) // p = l * P(t) // = l + l * t * (c1 + t * c2) __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1); vt = _mm512_mul_ps(vt, vl); vp = _mm512_fmadd_ps(vt, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,863
43.218182
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut64-p2-gather-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr2_lut64_p2_gather_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,033
40.587629
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut64-p2-gather-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr2_lut64_p2_gather_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,354
41.281553
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-lut64-p2-gather-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values, k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64]; void xnn_math_f32_sigmoid__avx512f_rr2_lut64_p2_gather_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); // Large number such that ulp(magic bias) == exp2(-6) const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); // Mask for the lowest 6 bits const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F)); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f); // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e. // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678] // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the // very end of the algorithm. __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias); // Use the low 6 bits of n (as integer) for table lookup. const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask); const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float)); // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number. vn = _mm512_sub_ps(vn, vmagic_bias); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2)) // p = l * P(t) // = l + l * (t + t * (t * c2)) __m512 vp = _mm512_mul_ps(vt, vc2); vp = _mm512_fmadd_ps(vt, vp, vt); vp = _mm512_fmadd_ps(vl, vp, vl); // Reconstruct the exp(z) value: e = exp2(floor(n)) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
4,468
41.971154
119
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-p5-scalef-div.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_p5_scalef_div( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_div_ps(ve, vd); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
2,954
35.481481
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-p5-scalef-nr1fma.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_p5_scalef_nr1fma( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) __m512 vf = _mm512_mul_ps(ve, vr); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,275
36.655172
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-avx512f-rr2-p5-scalef-nr1fma1adj.c
// Copyright 2020 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <immintrin.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__avx512f_rr2_p5_scalef_nr1fma1adj( size_t n, const float* input, float* output) { assert(n % (16 * sizeof(float)) == 0); // Floating-point mask with only the sign bit set const __m512i vsign_mask = _mm512_set1_epi32(0x80000000); const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f); const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f); const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f); // Coefficient of polynomial approximation of // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f); const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f); const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f); const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f); const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f); const __m512 vone = _mm512_set1_ps(1.0f); for (; n != 0; n -= 16 * sizeof(float)) { const __m512 vx = _mm512_loadu_ps(input); // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0. const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask)); // Compute reduced argument n := round(z / log(2)). const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0); // Compute reduced argument t := z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz); vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt); // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4); vp = _mm512_fmadd_ps(vp, vt, vc3); vp = _mm512_fmadd_ps(vp, vt, vc2); vp = _mm512_fmadd_ps(vp, vt, vc1); vp = _mm512_fmadd_ps(vp, vt, vone); // Reconstruct the exp(z) value: e = exp2(n) * p. const __m512 ve = _mm512_scalef_ps(vp, vn); // Denominator of the sigmoid fraction: 1.0 + exp(z) const __m512 vd = _mm512_add_ps(ve, vone); // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. __m512 vr = _mm512_rcp14_ps(vd); vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr); // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z)) with adjustment to match IEEE division result __m512 vf = _mm512_mul_ps(ve, vr); vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z) vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf); _mm512_storeu_ps(output, vf); input += 16; output += 16; } }
3,389
37.522727
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neon-rr2-lut2048-p1-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neon_rr2_lut2048_p1_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); // Last 18 bits are zeroes const float32x4_t vln2_hi = vmovq_n_f32(0x1.600000p-1f); const float32x4_t vln2_lo = vmovq_n_f32(0x1.7217F8p-8f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vmlaq_f32(vz, vn, vln2_hi); vt = vmlaq_f32(vt, vn, vln2_lo); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vmlaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,283
48.480315
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neon-rr2-lut64-p2-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neon_rr2_lut64_p2_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); // Last 13 bits are zeroes const float32x4_t vln2_hi = vmovq_n_f32(0x1.630000p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.BD0106p-13f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vmlaq_f32(vz, vn, vln2_hi); vt = vmlaq_f32(vt, vn, vln2_lo); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vmlsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vmlsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,330
48.460938
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neon-rr2-p5-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neon_rr2_p5_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); // Last 7 bits are zeroes const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E400p-1f); const float32x4_t vln2_lo = vmovq_n_f32(0x1.7F7D1Cp-20f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vmlaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vmlaq_f32(vz, vn, vln2_hi); vt = vmlaq_f32(vt, vn, vln2_lo); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) float32x4_t vp = vmlaq_f32(vc4, vc5, vt); vp = vmlaq_f32(vc3, vp, vt); vp = vmlaq_f32(vc2, vp, vt); vp = vmlaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vmlaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,829
43.311927
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut2048-p1-nr1recps1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr1_lut2048_p1_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,059
48.268293
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut2048-p1-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr1_lut2048_p1_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,061
48.284553
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut2048-p1-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr1_lut2048_p1_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,047
48.170732
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut64-p2-nr1recps1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr1_lut64_p2_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,104
48.233871
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut64-p2-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr1_lut64_p2_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,106
48.25
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-lut64-p2-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 64) values decremented (as integer) by (k << 17), k = 0..63 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64]; void xnn_math_f32_sigmoid__neonfma_rr1_lut64_p2_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-6) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p17f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 6 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F)); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128] const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 6). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**16, i.e. |z| <= 0x1.62E43p+15 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 6 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 6:14 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 17); // Use bits 0:6 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 6) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-2 polynomial approximation for exp(-t) on [-log(2)/128, log(2)/128]. // P(t) = 1 + t * (-1 + t * c2) = 1 - (t - t * (t * c2)) = 1 - p float32x4_t vp = vmulq_f32(vt, vc2); vp = vfmsq_f32(vt, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (-1 + t * c2)) // = s * (1 - p) // = s - s * p const float32x4_t vy = vfmsq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,092
48.137097
125
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-p5-nr1recps1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr1_p5_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,578
42.198113
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-p5-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr1_p5_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,580
42.216981
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr1-p5-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/math-stubs.h> void xnn_math_f32_sigmoid__neonfma_rr1_p5_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22. const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f); const float32x4_t vln2 = vmovq_n_f32(0x1.62E43p-1f); // Coefficient of polynomial approximation of // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2] const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f); const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f); const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f); const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f); const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2)). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+22 = 5814540.0), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly. const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23)); // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2). float32x4_t vt = vfmaq_f32(vz, vn, vln2); // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]: // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p float32x4_t vp = vfmaq_f32(vc4, vc5, vt); vp = vfmaq_f32(vc3, vp, vt); vp = vfmaq_f32(vc2, vp, vt); vp = vfmaq_f32(vc1, vp, vt); // Reconstruct the exp(-z) value: // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) // = s * (1 + t * p) // = s + (t * s) * p vt = vmulq_f32(vt, vs); float32x4_t ve = vfmaq_f32(vs, vp, vt); // Denominator of the sigmoid fraction: 1.0 + exp(-z) float32x4_t vd = vaddq_f32(ve, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(ve, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
4,566
42.084906
117
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-lut2048-p1-nr1recps1fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr2_lut2048_p1_nr1recps1fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,269
48.761905
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-lut2048-p1-nr2fma.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr2_lut2048_p1_nr2fma( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,271
48.777778
127
c
XNNPACK
XNNPACK-master/src/math/f32-sigmoid-neonfma-rr2-lut2048-p1-nr2recps.c
// Copyright 2019 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. #include <assert.h> #include <stddef.h> #include <arm_neon.h> #include <xnnpack/common.h> #include <xnnpack/math-stubs.h> // Table of exp2(k / 2048) values decremented (as integer) by (k << 12), k = 0..2048 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_2048[2048]; void xnn_math_f32_sigmoid__neonfma_rr2_lut2048_p1_nr2recps( size_t n, const float* input, float* output) { assert(n % (4 * sizeof(float)) == 0); // Large number such that ulp(magic bias) == exp2(-11) const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p12f); const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p0f); // Mask for the lowest 11 bits const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x7FF)); const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f); const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f); // Coefficient of polynomial approximation of exp(-t) ~ 1 + t * c1 on [-log(2)/2048, log(2)/2048] const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFFEp-1f); const float32x4_t vone = vmovq_n_f32(1.0f); // The largest z for which sigmoidf(-z) is normalized. // This number is also the largest z for which expf(-z) is normalized. const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep+6f); for (; n != 0; n -= 4 * sizeof(float)) { const float32x4_t vx = vld1q_f32(input); input += 4; // General structure of the algorithm: // // / exp(x) / (1 + exp(x)) if x <= 0 // f[x] := // \ 1 - f[-x] if x >= 0 // // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x), // then replace result with 1 - f[-z] if x >= 0. const float32x4_t vz = vabsq_f32(vx); // Compute reduced argument n := round(-z / log(2), 11). // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing // the large number back. The trick with adding large number is valid only within certain bounds // (|-z / log(2)| <= 2**11, i.e. |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup // the result for such inputs at the very end of the algorithm. float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e); // Create a floating-point number s (scale) such that s := 2**n for such inputs that sigmoidf(-z) is normalized, // i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**n = 2**int(n) * 2**frac(n). We create s // in two steps: // 1. Fetch 2**frac(n) from the table using the 11 low bits of n, as integer. Note that the fetched values are in // the [1.0, 2.0) range, i.e. their floating-point exponent is 0. // 2. Adjust fecthed value by addition of int(n) to its floating-point exponent. The result is always a normalized // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(z) is normalized) we have // -126 <= int(n) <= 0, and thus the adjusted exponent is not lower than -126. // // Shift bits 11:19 into 23:31 (position of floating-point exponent). const int32x4_t ve = vshlq_n_s32(vreinterpretq_s32_f32(vn), 12); // Use bits 0:11 of n, as integer, as an index for table lookup of l := 2**frac(n). const uint64x2_t vidx = vreinterpretq_u64_s32(vshlq_n_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask), 2)); const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0); const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1); float32x2_t vl_lo = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_lo)); float32x2_t vl_hi = vld1_dup_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) vidx_hi)); vl_lo = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_lo >> 32)), vl_lo, 1); vl_hi = vld1_lane_f32((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_2048 + (uint32_t) (vidx_hi >> 32)), vl_hi, 1); const float32x4_t vl = vcombine_f32(vl_lo, vl_hi); // Adjust exponent of the value l fetched from the table to get the final s value. const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve)); // Subtract the large number back to get the final n := round(-z / log(2), 11) as a floating-point number. vn = vsubq_f32(vn, vmagic_bias); // Compute reduced argument t := (z + n * log(2)). Note that -t = -z - n * log(2). // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi); vt = vfmaq_f32(vt, vn, vln2_lo); // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/2048, log(2)/2048]: // P(t) = 1 + t * c1 = 1 + p const float32x4_t vp = vmulq_f32(vt, vc1); // Reconstruct the exp(-z) value: // e = s * (1 + t * c1) // = s * (1 + p) // = s + s * p const float32x4_t vy = vfmaq_f32(vs, vs, vp); // Denominator of the sigmoid fraction: 1.0 + exp(-z) const float32x4_t vd = vaddq_f32(vy, vone); // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator. // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0. // Thus the reciprocal of the denominator never overflows. float32x4_t vr = vrecpeq_f32(vd); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); vr = vmulq_f32(vr, vrecpsq_f32(vr, vd)); // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z)) float32x4_t vf = vmulq_f32(vy, vr); // For inputs below denormal cutoff, replace output with +0.0f. // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff))); // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z) const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f)); vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf)); vst1q_f32(output, vf); output += 4; } }
6,257
48.666667
127
c