PolyFEM
Loading...
Searching...
No Matches
Tools.h
Go to the documentation of this file.
1
41#ifndef JIXIE_SVD_TOOLS_H
42#define JIXIE_SVD_TOOLS_H
43
44#pragma GCC diagnostic push
45#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
46#include <Eigen/Dense>
47#include <Eigen/Core>
48#include <Eigen/SVD>
49#pragma GCC diagnostic pop
50
51#if !defined(__APPLE__) || defined(__i386__) || defined(__x86_64__)
52#include <mmintrin.h>
53#include <xmmintrin.h>
54#elif defined(__ARM_NEON) || defined(__ARM_NEON__)
55#include <arm_neon.h>
56#endif
57#include <cmath>
58#include <random>
59#include <chrono>
60#include <iostream>
61#include <iomanip>
62
63namespace JIXIE
64{
65
66 template <bool B, class T = void>
67 using enable_if_t = typename std::enable_if<B, T>::type;
68
69 namespace MATH_TOOLS
70 {
71
78 inline float approx_rsqrt(float a)
79 {
80 // return 1.0f / std::sqrt(a);
81#if !defined(__APPLE__) || defined(__i386__) || defined(__x86_64__)
82 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(a)));
83#else
84 return vgetq_lane_f32(vrsqrteq_f32(vld1q_dup_f32(&a)), 0);
85#endif
86 }
87
92 inline float rsqrt(float a)
93 {
94 return (float)1.0f / std::sqrt(a);
95
96 // float b = approx_rsqrt(a);
97 // // Newton step with f(x) = a - 1/x^2
98 // b = 0.5f * b * (3.0f - a * (b * b));
99 // return b;
100 }
101
106 inline double rsqrt(double a)
107 {
108 using std::sqrt;
109 return 1 / sqrt(a);
110 }
111 } // namespace MATH_TOOLS
112
113 namespace INTERNAL
114 {
115 using namespace std;
116 template <class T, class Enable = void>
118 {
119 using type = typename T::Scalar;
120 };
121 template <class T>
122 struct ScalarTypeHelper<T, enable_if_t<is_arithmetic<T>::value>>
123 {
124 using type = T;
125 };
126 } // namespace INTERNAL
127
128 template <class T>
130
131 template <class MatrixType>
132 constexpr bool isSize(int m, int n)
133 {
134 return MatrixType::RowsAtCompileTime == m && MatrixType::ColsAtCompileTime == n;
135 }
136
137} // namespace JIXIE
138#endif
float approx_rsqrt(float a)
Approximate inverse square root.
Definition Tools.h:78
float rsqrt(float a)
Inverse square root computed from approx_rsqrt and one newton step.
Definition Tools.h:92
Copyright (c) 2016 Theodore Gast, Chuyuan Fu, Chenfanfu Jiang, Joseph Teran.
typename std::enable_if< B, T >::type enable_if_t
Definition Tools.h:67
typename INTERNAL::ScalarTypeHelper< T >::type ScalarType
Definition Tools.h:129
constexpr bool isSize(int m, int n)
Definition Tools.h:132
typename T::Scalar type
Definition Tools.h:119