MCPcopy Create free account
hub / github.com/numpy/numpy / npyv_trunc_f64

Function npyv_trunc_f64

numpy/core/src/common/simd/sse/math.h:391–410  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

389 return npyv_select_f32(_mm_or_si128(nfinite_mask, overflow_mask), a, trunc);
390 }
391 NPY_FINLINE npyv_f64 npyv_trunc_f64(npyv_f64 a)
392 {
393 const __m128d one = _mm_set1_pd(1.0);
394 const __m128d szero = _mm_set1_pd(-0.0);
395 const __m128d two_power_52 = _mm_set1_pd(0x10000000000000);
396 __m128d nan_mask = _mm_cmpunord_pd(a, a);
397 // eliminate nans to avoid invalid fp errors within cmpge
398 __m128d abs_x = npyv_abs_f64(_mm_xor_pd(nan_mask, a));
399 // round by add magic number 2^52
400 // assuming that MXCSR register is set to rounding
401 __m128d abs_round = _mm_sub_pd(_mm_add_pd(two_power_52, abs_x), two_power_52);
402 __m128d subtrahend = _mm_and_pd(_mm_cmpgt_pd(abs_round, abs_x), one);
403 __m128d trunc = _mm_sub_pd(abs_round, subtrahend);
404 // copysign
405 trunc = _mm_or_pd(trunc, _mm_and_pd(a, szero));
406 // a if |a| >= 2^52 or a == NaN
407 __m128d mask = _mm_cmpge_pd(abs_x, two_power_52);
408 mask = _mm_or_pd(mask, nan_mask);
409 return npyv_select_f64(_mm_castpd_si128(mask), a, trunc);
410 }
411#endif
412
413// floor

Callers

nothing calls this directly

Calls 2

npyv_select_f64Function · 0.85
npyv_abs_f64Function · 0.70

Tested by

no test coverage detected