Loading [MathJax]/extensions/tex2jax.js
PolyFEM
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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#endif
55#include <cmath>
56#include <random>
57#include <chrono>
58#include <iostream>
59#include <iomanip>
60
61namespace JIXIE
62{
63
64 template <bool B, class T = void>
65 using enable_if_t = typename std::enable_if<B, T>::type;
66
67 namespace MATH_TOOLS
68 {
69
76 inline float approx_rsqrt(float a)
77 {
78 // return 1.0f / std::sqrt(a);
79#if !defined(__APPLE__) || defined(__i386__) || defined(__x86_64__)
80 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(a)));
81#else
82 return vgetq_lane_f32(vrsqrteq_f32(vld1q_dup_f32(&a)), 0);
83#endif
84 }
85
90 inline float rsqrt(float a)
91 {
92 return (float)1.0f / std::sqrt(a);
93
94 // float b = approx_rsqrt(a);
95 // // Newton step with f(x) = a - 1/x^2
96 // b = 0.5f * b * (3.0f - a * (b * b));
97 // return b;
98 }
99
104 inline double rsqrt(double a)
105 {
106 using std::sqrt;
107 return 1 / sqrt(a);
108 }
109 } // namespace MATH_TOOLS
110
111 namespace INTERNAL
112 {
113 using namespace std;
114 template <class T, class Enable = void>
116 {
117 using type = typename T::Scalar;
118 };
119 template <class T>
120 struct ScalarTypeHelper<T, enable_if_t<is_arithmetic<T>::value>>
121 {
122 using type = T;
123 };
124 } // namespace INTERNAL
125
126 template <class T>
128
129 template <class MatrixType>
130 constexpr bool isSize(int m, int n)
131 {
132 return MatrixType::RowsAtCompileTime == m && MatrixType::ColsAtCompileTime == n;
133 }
134
135} // namespace JIXIE
136#endif
float approx_rsqrt(float a)
Approximate inverse square root.
Definition Tools.h:76
float rsqrt(float a)
Inverse square root computed from approx_rsqrt and one newton step.
Definition Tools.h:90
Copyright (c) 2016 Theodore Gast, Chuyuan Fu, Chenfanfu Jiang, Joseph Teran.
typename std::enable_if< B, T >::type enable_if_t
Definition Tools.h:65
typename INTERNAL::ScalarTypeHelper< T >::type ScalarType
Definition Tools.h:127
constexpr bool isSize(int m, int n)
Definition Tools.h:130
typename T::Scalar type
Definition Tools.h:117