| 1 | /* |
| 2 | sse_mathfun_extension.h - zlib license |
| 3 | Written by Tolga Mizrak 2016 |
| 4 | Extension of sse_mathfun.h, which is written by Julien Pommier |
| 5 | |
| 6 | Based on the corresponding algorithms of the cephes math library |
| 7 | |
| 8 | This is written as an extension to sse_mathfun.h instead of modifying it, just because I didn't want |
| 9 | to maintain a modified version of the original library. This way switching to a newer version of the |
| 10 | library won't be a hassle. |
| 11 | |
| 12 | Note that non SSE2 implementations of tan_ps, atan_ps, cot_ps and atan2_ps are not implemented yet. |
| 13 | As such, currently you need to #define USE_SSE2 to compile. |
| 14 | |
| 15 | With tan_ps, cot_ps you get good precision on input ranges that are further away from the domain |
| 16 | borders (-PI/2, PI/2 for tan and 0, 1 for cot). See the results on the deviations for these |
| 17 | functions on my machine: |
| 18 | checking tan on [-0.25*Pi, 0.25*Pi] |
| 19 | max deviation from tanf(x): 1.19209e-07 at 0.250000006957*Pi, max deviation from cephes_tan(x): |
| 20 | 5.96046e-08 |
| 21 | ->> precision OK for the tan_ps <<- |
| 22 | |
| 23 | checking tan on [-0.49*Pi, 0.49*Pi] |
| 24 | max deviation from tanf(x): 3.8147e-06 at -0.490000009841*Pi, max deviation from cephes_tan(x): |
| 25 | 9.53674e-07 |
| 26 | ->> precision OK for the tan_ps <<- |
| 27 | |
| 28 | checking cot on [0.2*Pi, 0.7*Pi] |
| 29 | max deviation from cotf(x): 1.19209e-07 at 0.204303119606*Pi, max deviation from cephes_cot(x): |
| 30 | 1.19209e-07 |
| 31 | ->> precision OK for the cot_ps <<- |
| 32 | |
| 33 | checking cot on [0.01*Pi, 0.99*Pi] |
| 34 | max deviation from cotf(x): 3.8147e-06 at 0.987876517942*Pi, max deviation from cephes_cot(x): |
| 35 | 9.53674e-07 |
| 36 | ->> precision OK for the cot_ps <<- |
| 37 | |
| 38 | With atan_ps and atan2_ps you get pretty good precision, atan_ps max deviation is < 2e-7 and |
| 39 | atan2_ps max deviation is < 2.5e-7 |
| 40 | */ |
| 41 | |
| 42 | /* Copyright (C) 2016 Tolga Mizrak |
| 43 | |
| 44 | This software is provided 'as-is', without any express or implied |
| 45 | warranty. In no event will the authors be held liable for any damages |
| 46 | arising from the use of this software. |
| 47 | |
| 48 | Permission is granted to anyone to use this software for any purpose, |
| 49 | including commercial applications, and to alter it and redistribute it |
| 50 | freely, subject to the following restrictions: |
| 51 | |
| 52 | 1. The origin of this software must not be misrepresented; you must not |
| 53 | claim that you wrote the original software. If you use this software |
| 54 | in a product, an acknowledgment in the product documentation would be |
| 55 | appreciated but is not required. |
| 56 | 2. Altered source versions must be plainly marked as such, and must not be |
| 57 | misrepresented as being the original software. |
| 58 | 3. This notice may not be removed or altered from any source distribution. |
| 59 | |
| 60 | (this is the zlib license) |
| 61 | */ |
| 62 | |
| 63 | #pragma once |
| 64 | |
| 65 | #ifndef _SSE_MATHFUN_EXTENSION_H_INCLUDED_ |
| 66 | #define _SSE_MATHFUN_EXTENSION_H_INCLUDED_ |
| 67 | |
| 68 | #ifndef USE_SSE2 |
| 69 | #error sse1 & mmx version not implemented |
| 70 | #endif |
| 71 | |
| 72 | #ifdef _MSC_VER |
| 73 | #pragma warning( push ) |
| 74 | /* warning C4838: conversion from 'double' to 'const float' requires a narrowing conversion */ |
| 75 | #pragma warning( disable : 4838 ) |
| 76 | /* warning C4305: 'initializing': truncation from 'double' to 'const float' */ |
| 77 | #pragma warning( disable : 4305 ) |
| 78 | #endif |
| 79 | |
| 80 | #include "sse_mathfun.h" |
| 81 | |
| 82 | _PS_CONST( 0, 0 ); |
| 83 | _PS_CONST( 2, 2 ); |
| 84 | _PI32_CONST( neg1, 1 ); |
| 85 | |
| 86 | _PS_CONST( tancof_p0, 9.38540185543E-3 ); |
| 87 | _PS_CONST( tancof_p1, 3.11992232697E-3 ); |
| 88 | _PS_CONST( tancof_p2, 2.44301354525E-2 ); |
| 89 | _PS_CONST( tancof_p3, 5.34112807005E-2 ); |
| 90 | _PS_CONST( tancof_p4, 1.33387994085E-1 ); |
| 91 | _PS_CONST( tancof_p5, 3.33331568548E-1 ); |
| 92 | |
| 93 | _PS_CONST( tancot_eps, 1.0e-4 ); |
| 94 | |
| 95 | v4sf tancot_ps( v4sf x, int cotFlag ) |
| 96 | { |
| 97 | v4sf xmm1, xmm2 = _mm_setzero_ps(), xmm3, sign_bit, y; |
| 98 | |
| 99 | #ifdef USE_SSE2 |
| 100 | v4si emm2; |
| 101 | #else |
| 102 | #endif |
| 103 | sign_bit = x; |
| 104 | /* take the absolute value */ |
| 105 | x = _mm_and_ps( x, *(v4sf*)_ps_inv_sign_mask ); |
| 106 | /* extract the sign bit (upper one) */ |
| 107 | sign_bit = _mm_and_ps( sign_bit, *(v4sf*)_ps_sign_mask ); |
| 108 | |
| 109 | /* scale by 4/Pi */ |
| 110 | y = _mm_mul_ps( x, *(v4sf*)_ps_cephes_FOPI ); |
| 111 | |
| 112 | #ifdef USE_SSE2 |
| 113 | /* store the integer part of y in mm0 */ |
| 114 | emm2 = _mm_cvttps_epi32( y ); |
| 115 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 116 | emm2 = _mm_add_epi32( emm2, *(v4si*)_pi32_1 ); |
| 117 | emm2 = _mm_and_si128( emm2, *(v4si*)_pi32_inv1 ); |
| 118 | y = _mm_cvtepi32_ps( emm2 ); |
| 119 | |
| 120 | emm2 = _mm_and_si128( emm2, *(v4si*)_pi32_2 ); |
| 121 | emm2 = _mm_cmpeq_epi32( emm2, _mm_setzero_si128() ); |
| 122 | |
| 123 | v4sf poly_mask = _mm_castsi128_ps( emm2 ); |
| 124 | #else |
| 125 | #endif |
| 126 | /* The magic pass: "Extended precision modular arithmetic" |
| 127 | x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 128 | xmm1 = *(v4sf*)_ps_minus_cephes_DP1; |
| 129 | xmm2 = *(v4sf*)_ps_minus_cephes_DP2; |
| 130 | xmm3 = *(v4sf*)_ps_minus_cephes_DP3; |
| 131 | xmm1 = _mm_mul_ps( y, xmm1 ); |
| 132 | xmm2 = _mm_mul_ps( y, xmm2 ); |
| 133 | xmm3 = _mm_mul_ps( y, xmm3 ); |
| 134 | v4sf z = _mm_add_ps( x, xmm1 ); |
| 135 | z = _mm_add_ps( z, xmm2 ); |
| 136 | z = _mm_add_ps( z, xmm3 ); |
| 137 | |
| 138 | v4sf zz = _mm_mul_ps( z, z ); |
| 139 | |
| 140 | y = *(v4sf*)_ps_tancof_p0; |
| 141 | y = _mm_mul_ps( y, zz ); |
| 142 | y = _mm_add_ps( y, *(v4sf*)_ps_tancof_p1 ); |
| 143 | y = _mm_mul_ps( y, zz ); |
| 144 | y = _mm_add_ps( y, *(v4sf*)_ps_tancof_p2 ); |
| 145 | y = _mm_mul_ps( y, zz ); |
| 146 | y = _mm_add_ps( y, *(v4sf*)_ps_tancof_p3 ); |
| 147 | y = _mm_mul_ps( y, zz ); |
| 148 | y = _mm_add_ps( y, *(v4sf*)_ps_tancof_p4 ); |
| 149 | y = _mm_mul_ps( y, zz ); |
| 150 | y = _mm_add_ps( y, *(v4sf*)_ps_tancof_p5 ); |
| 151 | y = _mm_mul_ps( y, zz ); |
| 152 | y = _mm_mul_ps( y, z ); |
| 153 | y = _mm_add_ps( y, z ); |
| 154 | |
| 155 | v4sf y2; |
| 156 | if( cotFlag ) { |
| 157 | y2 = _mm_xor_ps( y, *(v4sf*)_ps_sign_mask ); |
| 158 | /* y = _mm_rcp_ps( y ); */ |
| 159 | /* using _mm_rcp_ps here loses on way too much precision, better to do a div */ |
| 160 | y = _mm_div_ps( *(v4sf*)_ps_1, y ); |
| 161 | } else { |
| 162 | /* y2 = _mm_rcp_ps( y ); */ |
| 163 | /* using _mm_rcp_ps here loses on way too much precision, better to do a div */ |
| 164 | y2 = _mm_div_ps( *(v4sf*)_ps_1, y ); |
| 165 | y2 = _mm_xor_ps( y2, *(v4sf*)_ps_sign_mask ); |
| 166 | } |
| 167 | |
| 168 | /* select the correct result from the two polynoms */ |
| 169 | xmm3 = poly_mask; |
| 170 | y = _mm_and_ps( xmm3, y ); |
| 171 | y2 = _mm_andnot_ps( xmm3, y2 ); |
| 172 | y = _mm_or_ps( y, y2 ); |
| 173 | |
| 174 | /* update the sign */ |
| 175 | y = _mm_xor_ps( y, sign_bit ); |
| 176 | |
| 177 | return y; |
| 178 | } |
| 179 | |
| 180 | v4sf tan_ps( v4sf x ) { return tancot_ps( x, 0 ); } |
| 181 | |
| 182 | v4sf cot_ps( v4sf x ) { return tancot_ps( x, 1 ); } |
| 183 | |
| 184 | _PS_CONST( atanrange_hi, 2.414213562373095 ); |
| 185 | _PS_CONST( atanrange_lo, 0.4142135623730950 ); |
| 186 | const float PIF = 3.141592653589793238; |
| 187 | const float PIO2F = 1.5707963267948966192; |
| 188 | _PS_CONST( cephes_PIF, 3.141592653589793238 ); |
| 189 | _PS_CONST( cephes_PIO2F, 1.5707963267948966192 ); |
| 190 | _PS_CONST( cephes_PIO4F, 0.7853981633974483096 ); |
| 191 | |
| 192 | _PS_CONST( atancof_p0, 8.05374449538e-2 ); |
| 193 | _PS_CONST( atancof_p1, 1.38776856032E-1 ); |
| 194 | _PS_CONST( atancof_p2, 1.99777106478E-1 ); |
| 195 | _PS_CONST( atancof_p3, 3.33329491539E-1 ); |
| 196 | |
| 197 | v4sf atan_ps( v4sf x ) |
| 198 | { |
| 199 | v4sf sign_bit, y; |
| 200 | |
| 201 | sign_bit = x; |
| 202 | /* take the absolute value */ |
| 203 | x = _mm_and_ps( x, *(v4sf*)_ps_inv_sign_mask ); |
| 204 | /* extract the sign bit (upper one) */ |
| 205 | sign_bit = _mm_and_ps( sign_bit, *(v4sf*)_ps_sign_mask ); |
| 206 | |
| 207 | /* range reduction, init x and y depending on range */ |
| 208 | #ifdef USE_SSE2 |
| 209 | /* x > 2.414213562373095 */ |
| 210 | v4sf cmp0 = _mm_cmpgt_ps( x, *(v4sf*)_ps_atanrange_hi ); |
| 211 | /* x > 0.4142135623730950 */ |
| 212 | v4sf cmp1 = _mm_cmpgt_ps( x, *(v4sf*)_ps_atanrange_lo ); |
| 213 | |
| 214 | /* x > 0.4142135623730950 && !( x > 2.414213562373095 ) */ |
| 215 | v4sf cmp2 = _mm_andnot_ps( cmp0, cmp1 ); |
| 216 | |
| 217 | /* -( 1.0/x ) */ |
| 218 | v4sf y0 = _mm_and_ps( cmp0, *(v4sf*)_ps_cephes_PIO2F ); |
| 219 | v4sf x0 = _mm_div_ps( *(v4sf*)_ps_1, x ); |
| 220 | x0 = _mm_xor_ps( x0, *(v4sf*)_ps_sign_mask ); |
| 221 | |
| 222 | v4sf y1 = _mm_and_ps( cmp2, *(v4sf*)_ps_cephes_PIO4F ); |
| 223 | /* (x-1.0)/(x+1.0) */ |
| 224 | v4sf x1_o = _mm_sub_ps( x, *(v4sf*)_ps_1 ); |
| 225 | v4sf x1_u = _mm_add_ps( x, *(v4sf*)_ps_1 ); |
| 226 | v4sf x1 = _mm_div_ps( x1_o, x1_u ); |
| 227 | |
| 228 | v4sf x2 = _mm_and_ps( cmp2, x1 ); |
| 229 | x0 = _mm_and_ps( cmp0, x0 ); |
| 230 | x2 = _mm_or_ps( x2, x0 ); |
| 231 | cmp1 = _mm_or_ps( cmp0, cmp2 ); |
| 232 | x2 = _mm_and_ps( cmp1, x2 ); |
| 233 | x = _mm_andnot_ps( cmp1, x ); |
| 234 | x = _mm_or_ps( x2, x ); |
| 235 | |
| 236 | y = _mm_or_ps( y0, y1 ); |
| 237 | #else |
| 238 | #error sse1 & mmx version not implemented |
| 239 | #endif |
| 240 | |
| 241 | v4sf zz = _mm_mul_ps( x, x ); |
| 242 | v4sf acc = *(v4sf*)_ps_atancof_p0; |
| 243 | acc = _mm_mul_ps( acc, zz ); |
| 244 | acc = _mm_sub_ps( acc, *(v4sf*)_ps_atancof_p1 ); |
| 245 | acc = _mm_mul_ps( acc, zz ); |
| 246 | acc = _mm_add_ps( acc, *(v4sf*)_ps_atancof_p2 ); |
| 247 | acc = _mm_mul_ps( acc, zz ); |
| 248 | acc = _mm_sub_ps( acc, *(v4sf*)_ps_atancof_p3 ); |
| 249 | acc = _mm_mul_ps( acc, zz ); |
| 250 | acc = _mm_mul_ps( acc, x ); |
| 251 | acc = _mm_add_ps( acc, x ); |
| 252 | y = _mm_add_ps( y, acc ); |
| 253 | |
| 254 | /* update the sign */ |
| 255 | y = _mm_xor_ps( y, sign_bit ); |
| 256 | |
| 257 | return y; |
| 258 | } |
| 259 | |
| 260 | v4sf atan2_ps( v4sf y, v4sf x ) |
| 261 | { |
| 262 | v4sf x_eq_0 = _mm_cmpeq_ps( x, *(v4sf*)_ps_0 ); |
| 263 | v4sf x_gt_0 = _mm_cmpgt_ps( x, *(v4sf*)_ps_0 ); |
| 264 | v4sf x_le_0 = _mm_cmple_ps( x, *(v4sf*)_ps_0 ); |
| 265 | v4sf y_eq_0 = _mm_cmpeq_ps( y, *(v4sf*)_ps_0 ); |
| 266 | v4sf x_lt_0 = _mm_cmplt_ps( x, *(v4sf*)_ps_0 ); |
| 267 | v4sf y_lt_0 = _mm_cmplt_ps( y, *(v4sf*)_ps_0 ); |
| 268 | |
| 269 | v4sf zero_mask = _mm_and_ps( x_eq_0, y_eq_0 ); |
| 270 | v4sf zero_mask_other_case = _mm_and_ps( y_eq_0, x_gt_0 ); |
| 271 | zero_mask = _mm_or_ps( zero_mask, zero_mask_other_case ); |
| 272 | |
| 273 | v4sf pio2_mask = _mm_andnot_ps( y_eq_0, x_eq_0 ); |
| 274 | v4sf pio2_mask_sign = _mm_and_ps( y_lt_0, *(v4sf*)_ps_sign_mask ); |
| 275 | v4sf pio2_result = *(v4sf*)_ps_cephes_PIO2F; |
| 276 | pio2_result = _mm_xor_ps( pio2_result, pio2_mask_sign ); |
| 277 | pio2_result = _mm_and_ps( pio2_mask, pio2_result ); |
| 278 | |
| 279 | v4sf pi_mask = _mm_and_ps( y_eq_0, x_le_0 ); |
| 280 | v4sf pi = *(v4sf*)_ps_cephes_PIF; |
| 281 | v4sf pi_result = _mm_and_ps( pi_mask, pi ); |
| 282 | |
| 283 | v4sf swap_sign_mask_offset = _mm_and_ps( x_lt_0, y_lt_0 ); |
| 284 | swap_sign_mask_offset = _mm_and_ps( swap_sign_mask_offset, *(v4sf*)_ps_sign_mask ); |
| 285 | |
| 286 | v4sf offset0 = _mm_setzero_ps(); |
| 287 | v4sf offset1 = *(v4sf*)_ps_cephes_PIF; |
| 288 | offset1 = _mm_xor_ps( offset1, swap_sign_mask_offset ); |
| 289 | |
| 290 | v4sf offset = _mm_andnot_ps( x_lt_0, offset0 ); |
| 291 | offset = _mm_and_ps( x_lt_0, offset1 ); |
| 292 | |
| 293 | v4sf arg = _mm_div_ps( y, x ); |
| 294 | v4sf atan_result = atan_ps( arg ); |
| 295 | atan_result = _mm_add_ps( atan_result, offset ); |
| 296 | |
| 297 | /* select between zero_result, pio2_result and atan_result */ |
| 298 | |
| 299 | v4sf result = _mm_andnot_ps( zero_mask, pio2_result ); |
| 300 | atan_result = _mm_andnot_ps( pio2_mask, atan_result ); |
| 301 | atan_result = _mm_andnot_ps( pio2_mask, atan_result); |
| 302 | result = _mm_or_ps( result, atan_result ); |
| 303 | result = _mm_or_ps( result, pi_result ); |
| 304 | |
| 305 | return result; |
| 306 | } |
| 307 | |
| 308 | /* for convenience of calling simd sqrt */ |
| 309 | float sqrt_ps( float x ) |
| 310 | { |
| 311 | v4sf sse_value = _mm_set_ps1( x ); |
| 312 | sse_value = _mm_sqrt_ps( sse_value ); |
| 313 | return _mm_cvtss_f32( sse_value ); |
| 314 | } |
| 315 | float rsqrt_ps( float x ) |
| 316 | { |
| 317 | v4sf sse_value = _mm_set_ps1( x ); |
| 318 | sse_value = _mm_rsqrt_ps( sse_value ); |
| 319 | return _mm_cvtss_f32( sse_value ); |
| 320 | } |
| 321 | |
| 322 | /* atan2 implementation using atan, used as a reference to implement atan2_ps */ |
| 323 | float atan2_ref( float y, float x ) |
| 324 | { |
| 325 | if( x == 0.0f ) { |
| 326 | if( y == 0.0f ) { |
| 327 | return 0.0f; |
| 328 | } |
| 329 | float result = _ps_cephes_PIO2F[0]; |
| 330 | if( y < 0.0f ) { |
| 331 | result = -result; |
| 332 | } |
| 333 | return result; |
| 334 | } |
| 335 | |
| 336 | if( y == 0.0f ) { |
| 337 | if( x > 0.0f ) { |
| 338 | return 0.0f; |
| 339 | } |
| 340 | return PIF; |
| 341 | } |
| 342 | |
| 343 | float offset = 0; |
| 344 | if( x < 0.0f ) { |
| 345 | offset = PIF; |
| 346 | if( y < 0.0f ) { |
| 347 | offset = -offset; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | v4sf val = _mm_set_ps1( y / x ); |
| 352 | val = atan_ps( val ); |
| 353 | return offset + _mm_cvtss_f32( val ); |
| 354 | } |
| 355 | |
| 356 | #ifdef _MSC_VER |
| 357 | #pragma warning( pop ) |
| 358 | #endif |
| 359 | |
| 360 | #endif |