PolyFEM
Loading...
Searching...
No Matches
Tools.h
Go to the documentation of this file.
1
45#ifndef JIXIE_SVD_TOOLS_H
46#define JIXIE_SVD_TOOLS_H
47
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
50#include <Eigen/Dense>
51#include <Eigen/Core>
52#include <Eigen/SVD>
53#pragma GCC diagnostic pop
54
55#if !defined(__APPLE__) || defined(__i386__) || defined(__x86_64__)
56#include <mmintrin.h>
57#include <xmmintrin.h>
58#endif
59#include <cmath>
60#include <random>
61#include <chrono>
62#include <iostream>
63#include <iomanip>
64
65namespace JIXIE {
66
67template <bool B, class T = void>
68using enable_if_t = typename std::enable_if<B, T>::type;
69
70namespace MATH_TOOLS {
71
78inline 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
92inline 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
106inline double rsqrt(double a)
107{
108 using std::sqrt;
109 return 1 / sqrt(a);
110}
111} // namespace MATH_TOOLS
112
113namespace INTERNAL {
114using namespace std;
115template <class T, class Enable = void>
117 using type = typename T::Scalar;
118};
119template <class T>
120struct ScalarTypeHelper<T, enable_if_t<is_arithmetic<T>::value>> {
121 using type = T;
122};
123} // namespace INTERNAL
124
125template <class T>
127
128template <class MatrixType>
129constexpr bool isSize(int m, int n)
130{
131 return MatrixType::RowsAtCompileTime == m && MatrixType::ColsAtCompileTime == n;
132}
133
134} // namespace JIXIE
135#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:68
typename INTERNAL::ScalarTypeHelper< T >::type ScalarType
Definition Tools.h:126
constexpr bool isSize(int m, int n)
Definition Tools.h:129
typename T::Scalar type
Definition Tools.h:117