batmat develop
Batched linear algebra routines
Loading...
Searching...
No Matches
sqrt.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <batmat/simd.hpp>
4
5namespace batmat::ops {
6
7/// @addtogroup topic-low-level-ops
8/// @{
9
10/// @name Square root
11/// @{
12
13/// Square root.
14template <std::floating_point T>
15T sqrt(T x) {
16 using std::sqrt;
17 return sqrt(x);
18}
19
20#ifdef __SSE2__
21
22/// Square root implementation that avoids setting errno.
23inline float sqrt(float x) {
24 __m128 input = _mm_set_ss(x);
25 __m128 result = _mm_sqrt_ss(input);
26 return _mm_cvtss_f32(result);
27}
28
29/// Square root implementation that avoids setting errno.
30inline double sqrt(double x) {
31 __m128d input = _mm_set_sd(x);
32 __m128d result = _mm_sqrt_sd(input, input);
33 return _mm_cvtsd_f64(result);
34}
35
36#elif defined(__aarch64__) && defined(__GNUC__)
37
38/// Square root implementation that avoids setting errno.
39inline float sqrt(float x) {
40 float result;
41 __asm__("fsqrt %s0, %s1" : "=w"(result) : "w"(x));
42 return result;
43}
44
45/// Square root implementation that avoids setting errno.
46inline double sqrt(double x) {
47 double result;
48 __asm__("fsqrt %d0, %d1" : "=w"(result) : "w"(x));
49 return result;
50}
51
52#endif
53
54/// @}
55
56/// @}
57
58} // namespace batmat::ops
T sqrt(T x)
Square root.
Definition sqrt.hpp:15