Skip to content

Commit 0822b6b

Browse files
committed
More and better accuracy trig function
1 parent 718e4fb commit 0822b6b

File tree

7 files changed

+701
-54
lines changed

7 files changed

+701
-54
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@
3838
/old
3939

4040
emsdk
41+
/*build*/
42+
/.*

include/FastSIMD/ToolSet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace FastSIMD
2121
} // namespace FastSIMD
2222

2323
#include "ToolSet/Generic/Functions.h"
24+
#include "ToolSet/Generic/Trig.h"
2425

2526
#include "ToolSet/Generic/Scalar.h"
2627

include/FastSIMD/ToolSet/Generic/Functions.h

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace FS
5151
Store( ptr + N / 2, a.v1 );
5252
}
5353

54+
// Convert a mask into a uint bitmask of appropriate size for register element count
5455
template<std::size_t S, bool F, std::size_t N, FastSIMD::FeatureSet SIMD>
5556
FS_FORCEINLINE BitStorage<N> BitMask( const Register<Mask<S, F>, N, SIMD>& a )
5657
{
@@ -531,50 +532,7 @@ namespace FS
531532
return Register<T, N, SIMD>{ Sqrt( a.v0 ), Sqrt( a.v1 ) };
532533
}
533534

534-
template<std::size_t N, FastSIMD::FeatureSet SIMD>
535-
FS_FORCEINLINE Register<float, N, SIMD> Cos( const Register<float, N, SIMD>& a )
536-
{
537-
if constexpr( IsNativeV<Register<float, N, SIMD>> )
538-
{
539-
using RegisterF = Register<float, N, SIMD>;
540-
using RegisterI = Register<std::int32_t, N, SIMD>;
541-
542-
RegisterF value = Abs( a );
543-
value -= Floor( value * RegisterF( 0.1591549f ) ) * RegisterF( 6.283185f );
544-
545-
auto geHalfPi = value >= RegisterF( 1.570796f );
546-
auto geHalfPi2 = value >= RegisterF( 3.141593f );
547-
auto geHalfPi3 = value >= RegisterF( 4.7123889f );
548535

549-
RegisterF cosAngle;
550-
cosAngle = value ^ Masked( geHalfPi, value ^ ( RegisterF( 3.141593f ) - value ) );
551-
cosAngle = cosAngle ^ Masked( geHalfPi2, Cast<float>( RegisterI( 0x80000000 ) ) );
552-
cosAngle = cosAngle ^ Masked( geHalfPi3, cosAngle ^ ( RegisterF( 6.283185f ) - value ) );
553-
554-
cosAngle *= cosAngle;
555-
556-
cosAngle = FMulAdd( cosAngle, FMulAdd( cosAngle, RegisterF( 0.03679168f ), RegisterF( -0.49558072f ) ), RegisterF( 0.99940307f ) );
557-
558-
return cosAngle ^ Masked( BitwiseAndNot( geHalfPi, geHalfPi3 ), Cast<float>( RegisterI( 0x80000000 ) ) );
559-
}
560-
else
561-
{
562-
return Register<float, N, SIMD>{ Cos( a.v0 ), Cos( a.v1 ) };
563-
}
564-
}
565-
566-
template<typename T, std::size_t N, FastSIMD::FeatureSet SIMD>
567-
FS_FORCEINLINE Register<T, N, SIMD> Sin( const Register<T, N, SIMD>& a )
568-
{
569-
if constexpr( IsNativeV<Register<T, N, SIMD>> )
570-
{
571-
return Cos( Register<T, N, SIMD>( (T)1.57079632679 ) - a );
572-
}
573-
else
574-
{
575-
return Register<T, N, SIMD>{ Sin( a.v0 ), Sin( a.v1 ) };
576-
}
577-
}
578536

579537
template<std::size_t N, FastSIMD::FeatureSet SIMD>
580538
FS_FORCEINLINE Register<float, N, SIMD> Exp( const Register<float, N, SIMD>& a )
@@ -693,7 +651,14 @@ namespace FS
693651
template<typename T, std::size_t N, FastSIMD::FeatureSet SIMD>
694652
FS_FORCEINLINE Register<T, N, SIMD> Pow( const Register<T, N, SIMD>& value, const Register<T, N, SIMD>& pow )
695653
{
696-
return Exp( pow * Log( value ) );
654+
if constexpr( IsNativeV<Register<T, N, SIMD>> )
655+
{
656+
return Exp( pow * Log( value ) );
657+
}
658+
else
659+
{
660+
return Register<T, N, SIMD>{ Pow( value.v0, pow.v0 ), Pow( value.v1, pow.v1 ) };
661+
}
697662
}
698663

699664
// Any Mask

include/FastSIMD/ToolSet/Generic/Scalar/f32x1.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,76 @@ namespace FS
127127
{
128128
return std::sqrt( a.native.f );
129129
}
130+
131+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
132+
FS_FORCEINLINE f32<1, SIMD> Sin( const f32<1, SIMD>& a )
133+
{
134+
return std::sin( a.native.f );
135+
}
136+
137+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
138+
FS_FORCEINLINE f32<1, SIMD> Cos( const f32<1, SIMD>& a )
139+
{
140+
return std::cos( a.native.f );
141+
}
142+
143+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
144+
FS_FORCEINLINE f32<1, SIMD> Log( const f32<1, SIMD>& a )
145+
{
146+
return std::log( a.native.f );
147+
}
148+
149+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
150+
FS_FORCEINLINE f32<1, SIMD> Exp( const f32<1, SIMD>& a )
151+
{
152+
return std::exp( a.native.f );
153+
}
154+
155+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
156+
FS_FORCEINLINE f32<1, SIMD> Pow( const f32<1, SIMD>& a, const f32<1, SIMD>& b )
157+
{
158+
return std::pow( a.native.f, b.native.f );
159+
}
160+
161+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
162+
FS_FORCEINLINE f32<1, SIMD> ACos( const f32<1, SIMD>& a )
163+
{
164+
return std::acos( a.native.f );
165+
}
166+
167+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
168+
FS_FORCEINLINE f32<1, SIMD> ASin( const f32<1, SIMD>& a )
169+
{
170+
return std::asin( a.native.f );
171+
}
172+
173+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
174+
FS_FORCEINLINE f32<1, SIMD> ATan( const f32<1, SIMD>& a )
175+
{
176+
return std::atan( a.native.f );
177+
}
178+
179+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
180+
FS_FORCEINLINE f32<1, SIMD> ATan2( const f32<1, SIMD>& y, const f32<1, SIMD>& x )
181+
{
182+
return std::atan2( y.native.f, x.native.f );
183+
}
184+
185+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
186+
FS_FORCEINLINE f32<1, SIMD> Tan( const f32<1, SIMD>& a )
187+
{
188+
return std::tan( a.native.f );
189+
}
190+
191+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
192+
FS_FORCEINLINE f32<1, SIMD> Log2( const f32<1, SIMD>& a )
193+
{
194+
return std::log2( a.native.f );
195+
}
196+
197+
template<FastSIMD::FeatureSet SIMD, typename = EnableIfNative<f32<1, SIMD>>, typename = EnableIfRelaxed<SIMD>>
198+
FS_FORCEINLINE f32<1, SIMD> Exp2( const f32<1, SIMD>& a )
199+
{
200+
return std::exp2( a.native.f );
201+
}
130202
}

0 commit comments

Comments
 (0)