SuperTuxKart 1.5 upstream source (from official release tarball)
This commit is contained in:
Executable
+568
@@ -0,0 +1,568 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2008-2016 Alexis Naveros.
|
||||
*
|
||||
*
|
||||
* The SIMD trigonometry functions are Copyright (C) 2007 Julien Pommier
|
||||
* See copyright notice for simd4f_sin_ps(), simd4f_cos_ps(), simd4f_sincos_ps()
|
||||
*
|
||||
*
|
||||
* Some functions are Copyright (C) 2008 José Fonseca
|
||||
* See copyright notice for simd4f_exp2_ps(), simd4f_log2_ps(), simd4f_pow_ps()
|
||||
*
|
||||
*
|
||||
* Portions developed under contract to the SURVICE Engineering Company.
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
#include "cpusimd.h"
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE_SUPPORT
|
||||
|
||||
const uint32_t CPU_ALIGN16 simd4fSignMask[4] = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
|
||||
const uint32_t CPU_ALIGN16 simd4fSignMaskInv[4] = { 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff };
|
||||
const float CPU_ALIGN16 simd4fHalf[4] = { 0.5, 0.5, 0.5, 0.5 };
|
||||
const float CPU_ALIGN16 simd4fOne[4] = { 1.0, 1.0, 1.0, 1.0 };
|
||||
const float CPU_ALIGN16 simd4fTwo[4] = { 2.0, 2.0, 2.0, 2.0 };
|
||||
const float CPU_ALIGN16 simd4fThree[4] = { 3.0, 3.0, 3.0, 3.0 };
|
||||
const uint32_t CPU_ALIGN16 simd4uOne[4] = { 1, 1, 1, 1 };
|
||||
const uint32_t CPU_ALIGN16 simd4uOneInv[4] = { ~1, ~1, ~1, ~1 };
|
||||
const uint32_t CPU_ALIGN16 simd4uTwo[4] = { 2, 2, 2, 2 };
|
||||
const uint32_t CPU_ALIGN16 simd4uFour[4] = { 4, 4, 4, 4 };
|
||||
const float CPU_ALIGN16 simd4fQuarter[4] = { 0.25, 0.25, 0.25, 0.25 };
|
||||
// Avoid double to float conversion warnings when using M_PI --> use our own PI
|
||||
#define PI 3.14159265358979323846f
|
||||
const float CPU_ALIGN16 simd4fPi[4] = { PI, PI, PI, PI };
|
||||
const float CPU_ALIGN16 simd4fZeroOneTwoThree[4] = { 0.0, 1.0, 2.0, 3.0 };
|
||||
const uint32_t CPU_ALIGN16 simd4fAlphaMask[4] = { 0x00000000, 0x00000000, 0x00000000, 0xffffffff };
|
||||
const float CPU_ALIGN16 simd4f255[4] = { 255.0f, 255.0f, 255.0f, 255.0f };
|
||||
const float CPU_ALIGN16 simd4f255Inv[4] = { 1.0f/255.0f, 1.0f/255.0f, 1.0f/255.0f, 1.0f/255.0f };
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
|
||||
/* Copyright (C) 2007 Julien Pommier
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
(this is the zlib license)
|
||||
*/
|
||||
|
||||
static const float CPU_ALIGN16 simd4f_cephes_FOPI[4] = { 1.27323954473516f, 1.27323954473516f, 1.27323954473516f, 1.27323954473516f };
|
||||
static const float CPU_ALIGN16 simd4f_minus_cephes_DP1[4] = { -0.78515625, -0.78515625, -0.78515625, -0.78515625 };
|
||||
static const float CPU_ALIGN16 simd4f_minus_cephes_DP2[4] = { -2.4187564849853515625e-4, -2.4187564849853515625e-4, -2.4187564849853515625e-4, -2.4187564849853515625e-4 };
|
||||
static const float CPU_ALIGN16 simd4f_minus_cephes_DP3[4] = { -3.77489497744594108e-8f, -3.77489497744594108e-8f, -3.77489497744594108e-8f, -3.77489497744594108e-8f };
|
||||
static const float CPU_ALIGN16 simd4f_sincof_p0[4] = { -1.9515295891E-4f, -1.9515295891E-4f, -1.9515295891E-4f, -1.9515295891E-4f };
|
||||
static const float CPU_ALIGN16 simd4f_sincof_p1[4] = { 8.3321608736E-3f, 8.3321608736E-3f, 8.3321608736E-3f, 8.3321608736E-3f };
|
||||
static const float CPU_ALIGN16 simd4f_sincof_p2[4] = { -1.6666654611E-1f, -1.6666654611E-1f, -1.6666654611E-1f, -1.6666654611E-1f };
|
||||
static const float CPU_ALIGN16 simd4f_coscof_p0[4] = { 2.443315711809948E-005f, 2.443315711809948E-005f, 2.443315711809948E-005f, 2.443315711809948E-005f };
|
||||
static const float CPU_ALIGN16 simd4f_coscof_p1[4] = { -1.388731625493765E-003f, -1.388731625493765E-003f, -1.388731625493765E-003f, -1.388731625493765E-003f };
|
||||
static const float CPU_ALIGN16 simd4f_coscof_p2[4] = { 4.166664568298827E-002f, 4.166664568298827E-002f, 4.166664568298827E-002f, 4.166664568298827E-002f };
|
||||
|
||||
__m128 simd4f_sin_ps( __m128 x )
|
||||
{
|
||||
__m128 xmm1, xmm2, xmm3, sign_bit, y;
|
||||
__m128i emm0, emm2;
|
||||
|
||||
xmm2 = _mm_setzero_ps();
|
||||
|
||||
sign_bit = x;
|
||||
/* take the absolute value */
|
||||
x = _mm_and_ps( x, *(__m128 *)simd4fSignMaskInv );
|
||||
/* extract the sign bit (upper one) */
|
||||
sign_bit = _mm_and_ps(sign_bit, *(__m128 *)simd4fSignMask);
|
||||
|
||||
/* scale by 4/Pi */
|
||||
y = _mm_mul_ps(x, *(__m128 *)simd4f_cephes_FOPI);
|
||||
|
||||
/* store the integer part of y in mm0 */
|
||||
emm2 = _mm_cvttps_epi32(y);
|
||||
/* j=(j+1) & (~1) (see the cephes sources) */
|
||||
emm2 = _mm_add_epi32(emm2, *(__m128i*)simd4uOne);
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uOneInv);
|
||||
y = _mm_cvtepi32_ps(emm2);
|
||||
|
||||
/* get the swap sign flag */
|
||||
emm0 = _mm_and_si128(emm2, *(__m128i*)simd4uFour);
|
||||
emm0 = _mm_slli_epi32(emm0, 29);
|
||||
/* get the polynom selection mask
|
||||
there is one polynom for 0 <= x <= Pi/4
|
||||
and another one for Pi/4<x<=Pi/2
|
||||
Both branches will be computed.
|
||||
*/
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uTwo);
|
||||
emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
|
||||
|
||||
__m128 swap_sign_bit = _mm_castsi128_ps(emm0);
|
||||
__m128 poly_mask = _mm_castsi128_ps(emm2);
|
||||
sign_bit = _mm_xor_ps(sign_bit, swap_sign_bit);
|
||||
|
||||
/* The magic pass: "Extended precision modular arithmetic"
|
||||
x = ((x - y * DP1) - y * DP2) - y * DP3; */
|
||||
xmm1 = *(__m128 *)simd4f_minus_cephes_DP1;
|
||||
xmm2 = *(__m128 *)simd4f_minus_cephes_DP2;
|
||||
xmm3 = *(__m128 *)simd4f_minus_cephes_DP3;
|
||||
xmm1 = _mm_mul_ps(y, xmm1);
|
||||
xmm2 = _mm_mul_ps(y, xmm2);
|
||||
xmm3 = _mm_mul_ps(y, xmm3);
|
||||
x = _mm_add_ps(x, xmm1);
|
||||
x = _mm_add_ps(x, xmm2);
|
||||
x = _mm_add_ps(x, xmm3);
|
||||
|
||||
/* Evaluate the first polynom (0 <= x <= Pi/4) */
|
||||
y = *(__m128 *)simd4f_coscof_p0;
|
||||
__m128 z = _mm_mul_ps(x,x);
|
||||
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128 *)simd4f_coscof_p1);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128 *)simd4f_coscof_p2);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_mul_ps(y, z);
|
||||
__m128 tmp = _mm_mul_ps(z, *(__m128 *)simd4fHalf);
|
||||
y = _mm_sub_ps(y, tmp);
|
||||
y = _mm_add_ps(y, *(__m128 *)simd4fOne);
|
||||
|
||||
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
|
||||
|
||||
__m128 y2 = *(__m128 *)simd4f_sincof_p0;
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128 *)simd4f_sincof_p1);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128 *)simd4f_sincof_p2);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_mul_ps(y2, x);
|
||||
y2 = _mm_add_ps(y2, x);
|
||||
|
||||
/* select the correct result from the two polynoms */
|
||||
xmm3 = poly_mask;
|
||||
y2 = _mm_and_ps(xmm3, y2);
|
||||
y = _mm_andnot_ps(xmm3, y);
|
||||
y = _mm_add_ps(y,y2);
|
||||
/* update the sign */
|
||||
y = _mm_xor_ps(y, sign_bit);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* almost the same as sin_ps */
|
||||
__m128 simd4f_cos_ps( __m128 x )
|
||||
{
|
||||
__m128 xmm1, xmm2, xmm3, y;
|
||||
__m128i emm0, emm2;
|
||||
|
||||
xmm2 = _mm_setzero_ps();
|
||||
|
||||
/* take the absolute value */
|
||||
x = _mm_and_ps(x, *(__m128*)simd4fSignMaskInv);
|
||||
|
||||
/* scale by 4/Pi */
|
||||
y = _mm_mul_ps(x, *(__m128*)simd4f_cephes_FOPI);
|
||||
|
||||
/* store the integer part of y in mm0 */
|
||||
emm2 = _mm_cvttps_epi32(y);
|
||||
/* j=(j+1) & (~1) (see the cephes sources) */
|
||||
emm2 = _mm_add_epi32(emm2, *(__m128i*)simd4uOne);
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uOneInv);
|
||||
y = _mm_cvtepi32_ps(emm2);
|
||||
|
||||
emm2 = _mm_sub_epi32(emm2, *(__m128i*)simd4uTwo);
|
||||
|
||||
/* get the swap sign flag */
|
||||
emm0 = _mm_andnot_si128(emm2, *(__m128i*)simd4uFour);
|
||||
emm0 = _mm_slli_epi32(emm0, 29);
|
||||
/* get the polynom selection mask */
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uTwo);
|
||||
emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
|
||||
|
||||
__m128 sign_bit = _mm_castsi128_ps(emm0);
|
||||
__m128 poly_mask = _mm_castsi128_ps(emm2);
|
||||
/* The magic pass: "Extended precision modular arithmetic"
|
||||
x = ((x - y * DP1) - y * DP2) - y * DP3; */
|
||||
xmm1 = *(__m128*)simd4f_minus_cephes_DP1;
|
||||
xmm2 = *(__m128*)simd4f_minus_cephes_DP2;
|
||||
xmm3 = *(__m128*)simd4f_minus_cephes_DP3;
|
||||
xmm1 = _mm_mul_ps(y, xmm1);
|
||||
xmm2 = _mm_mul_ps(y, xmm2);
|
||||
xmm3 = _mm_mul_ps(y, xmm3);
|
||||
x = _mm_add_ps(x, xmm1);
|
||||
x = _mm_add_ps(x, xmm2);
|
||||
x = _mm_add_ps(x, xmm3);
|
||||
|
||||
/* Evaluate the first polynom (0 <= x <= Pi/4) */
|
||||
y = *(__m128*)simd4f_coscof_p0;
|
||||
__m128 z = _mm_mul_ps(x,x);
|
||||
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4f_coscof_p1);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4f_coscof_p2);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_mul_ps(y, z);
|
||||
__m128 tmp = _mm_mul_ps(z, *(__m128*)simd4fHalf);
|
||||
y = _mm_sub_ps(y, tmp);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4fOne);
|
||||
|
||||
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
|
||||
|
||||
__m128 y2 = *(__m128*)simd4f_sincof_p0;
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128*)simd4f_sincof_p1);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128*)simd4f_sincof_p2);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_mul_ps(y2, x);
|
||||
y2 = _mm_add_ps(y2, x);
|
||||
|
||||
/* select the correct result from the two polynoms */
|
||||
xmm3 = poly_mask;
|
||||
y2 = _mm_and_ps(xmm3, y2); //, xmm3);
|
||||
y = _mm_andnot_ps(xmm3, y);
|
||||
y = _mm_add_ps(y,y2);
|
||||
/* update the sign */
|
||||
y = _mm_xor_ps(y, sign_bit);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
/* since sin_ps and cos_ps are almost identical, sincos_ps could replace both of them..
|
||||
it is almost as fast, and gives you a free cosine with your sine */
|
||||
void simd4f_sincos_ps( __m128 x, __m128 *s, __m128 *c )
|
||||
{
|
||||
__m128 xmm1, xmm2, xmm3, sign_bit_sin, y;
|
||||
__m128i emm0, emm2, emm4;
|
||||
|
||||
xmm3 = _mm_setzero_ps();
|
||||
|
||||
sign_bit_sin = x;
|
||||
/* take the absolute value */
|
||||
x = _mm_and_ps(x, *(__m128*)simd4fSignMaskInv);
|
||||
/* extract the sign bit (upper one) */
|
||||
sign_bit_sin = _mm_and_ps(sign_bit_sin, *(__m128*)simd4fSignMask);
|
||||
|
||||
/* scale by 4/Pi */
|
||||
y = _mm_mul_ps(x, *(__m128*)simd4f_cephes_FOPI);
|
||||
|
||||
/* store the integer part of y in emm2 */
|
||||
emm2 = _mm_cvttps_epi32(y);
|
||||
|
||||
/* j=(j+1) & (~1) (see the cephes sources) */
|
||||
emm2 = _mm_add_epi32(emm2, *(__m128i*)simd4uOne);
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uOneInv);
|
||||
y = _mm_cvtepi32_ps(emm2);
|
||||
|
||||
emm4 = emm2;
|
||||
|
||||
/* get the swap sign flag for the sine */
|
||||
emm0 = _mm_and_si128(emm2, *(__m128i*)simd4uFour);
|
||||
emm0 = _mm_slli_epi32(emm0, 29);
|
||||
__m128 swap_sign_bit_sin = _mm_castsi128_ps(emm0);
|
||||
|
||||
/* get the polynom selection mask for the sine*/
|
||||
emm2 = _mm_and_si128(emm2, *(__m128i*)simd4uTwo);
|
||||
emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
|
||||
__m128 poly_mask = _mm_castsi128_ps(emm2);
|
||||
|
||||
/* The magic pass: "Extended precision modular arithmetic"
|
||||
x = ((x - y * DP1) - y * DP2) - y * DP3; */
|
||||
xmm1 = *(__m128*)simd4f_minus_cephes_DP1;
|
||||
xmm2 = *(__m128*)simd4f_minus_cephes_DP2;
|
||||
xmm3 = *(__m128*)simd4f_minus_cephes_DP3;
|
||||
xmm1 = _mm_mul_ps(y, xmm1);
|
||||
xmm2 = _mm_mul_ps(y, xmm2);
|
||||
xmm3 = _mm_mul_ps(y, xmm3);
|
||||
x = _mm_add_ps(x, xmm1);
|
||||
x = _mm_add_ps(x, xmm2);
|
||||
x = _mm_add_ps(x, xmm3);
|
||||
|
||||
emm4 = _mm_sub_epi32(emm4, *(__m128i*)simd4uTwo);
|
||||
emm4 = _mm_andnot_si128(emm4, *(__m128i*)simd4uFour);
|
||||
emm4 = _mm_slli_epi32(emm4, 29);
|
||||
__m128 sign_bit_cos = _mm_castsi128_ps(emm4);
|
||||
|
||||
sign_bit_sin = _mm_xor_ps(sign_bit_sin, swap_sign_bit_sin);
|
||||
|
||||
/* Evaluate the first polynom (0 <= x <= Pi/4) */
|
||||
__m128 z = _mm_mul_ps(x,x);
|
||||
y = *(__m128*)simd4f_coscof_p0;
|
||||
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4f_coscof_p1);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4f_coscof_p2);
|
||||
y = _mm_mul_ps(y, z);
|
||||
y = _mm_mul_ps(y, z);
|
||||
__m128 tmp = _mm_mul_ps(z, *(__m128*)simd4fHalf);
|
||||
y = _mm_sub_ps(y, tmp);
|
||||
y = _mm_add_ps(y, *(__m128*)simd4fOne);
|
||||
|
||||
/* Evaluate the second polynom (Pi/4 <= x <= 0) */
|
||||
|
||||
__m128 y2 = *(__m128*)simd4f_sincof_p0;
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128*)simd4f_sincof_p1);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_add_ps(y2, *(__m128*)simd4f_sincof_p2);
|
||||
y2 = _mm_mul_ps(y2, z);
|
||||
y2 = _mm_mul_ps(y2, x);
|
||||
y2 = _mm_add_ps(y2, x);
|
||||
|
||||
/* select the correct result from the two polynoms */
|
||||
xmm3 = poly_mask;
|
||||
__m128 ysin2 = _mm_and_ps(xmm3, y2);
|
||||
__m128 ysin1 = _mm_andnot_ps(xmm3, y);
|
||||
y2 = _mm_sub_ps(y2,ysin2);
|
||||
y = _mm_sub_ps(y, ysin1);
|
||||
|
||||
xmm1 = _mm_add_ps(ysin1,ysin2);
|
||||
xmm2 = _mm_add_ps(y,y2);
|
||||
|
||||
/* update the sign */
|
||||
*s = _mm_xor_ps(xmm1, sign_bit_sin);
|
||||
*c = _mm_xor_ps(xmm2, sign_bit_cos);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
|
||||
/* Copyright (C) 2008 José Fonseca
|
||||
http://jrfonseca.blogspot.ca/2008/09/fast-sse2-pow-tables-or-polynomials.html
|
||||
MIT license
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#define POLY0(x,c0) _mm_set1_ps(c0)
|
||||
#define POLY1(x,c0,c1) _mm_add_ps(_mm_mul_ps(POLY0(x, c1), x), _mm_set1_ps(c0))
|
||||
#define POLY2(x,c0,c1,c2) _mm_add_ps(_mm_mul_ps(POLY1(x, c1, c2), x), _mm_set1_ps(c0))
|
||||
#define POLY3(x,c0,c1,c2,c3) _mm_add_ps(_mm_mul_ps(POLY2(x, c1, c2, c3), x), _mm_set1_ps(c0))
|
||||
#define POLY4(x,c0,c1,c2,c3,c4) _mm_add_ps(_mm_mul_ps(POLY3(x, c1, c2, c3, c4), x), _mm_set1_ps(c0))
|
||||
#define POLY5(x,c0,c1,c2,c3,c4,c5) _mm_add_ps(_mm_mul_ps(POLY4(x, c1, c2, c3, c4, c5), x), _mm_set1_ps(c0))
|
||||
|
||||
#define EXP_POLY_DEGREE 3
|
||||
#define LOG_POLY_DEGREE 5
|
||||
|
||||
__m128 simd4f_exp2_ps( __m128 x )
|
||||
{
|
||||
__m128i ipart;
|
||||
__m128 fpart, expipart, expfpart;
|
||||
|
||||
x = _mm_min_ps( x, _mm_set1_ps( 129.00000f ) );
|
||||
x = _mm_max_ps( x, _mm_set1_ps( -126.99999f ) );
|
||||
/* ipart = int(x - 0.5) */
|
||||
ipart = _mm_cvtps_epi32( _mm_sub_ps( x, _mm_set1_ps( 0.5f ) ) );
|
||||
/* fpart = x - ipart */
|
||||
fpart = _mm_sub_ps( x, _mm_cvtepi32_ps( ipart ) );
|
||||
/* expipart = (float) (1 << ipart) */
|
||||
expipart = _mm_castsi128_ps( _mm_slli_epi32( _mm_add_epi32( ipart, _mm_set1_epi32( 127 ) ), 23 ) );
|
||||
/* minimax polynomial fit of 2**x, in range [-0.5, 0.5[ */
|
||||
#if EXP_POLY_DEGREE == 5
|
||||
expfpart = POLY5( fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, 5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f );
|
||||
#elif EXP_POLY_DEGREE == 4
|
||||
expfpart = POLY4( fpart, 1.0000026f, 6.9300383e-1f, 2.4144275e-1f, 5.2011464e-2f, 1.3534167e-2f );
|
||||
#elif EXP_POLY_DEGREE == 3
|
||||
expfpart = POLY3( fpart, 9.9992520e-1f, 6.9583356e-1f, 2.2606716e-1f, 7.8024521e-2f );
|
||||
#elif EXP_POLY_DEGREE == 2
|
||||
expfpart = POLY2( fpart, 1.0017247f, 6.5763628e-1f, 3.3718944e-1f );
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
return _mm_mul_ps(expipart, expfpart);
|
||||
}
|
||||
|
||||
__m128 simd4f_log2_ps( __m128 x )
|
||||
{
|
||||
__m128i expmask, mantmask, i;
|
||||
__m128 one, vexp, mant, logmant;
|
||||
|
||||
expmask = _mm_set1_epi32( 0x7f800000 );
|
||||
mantmask = _mm_set1_epi32( 0x007fffff );
|
||||
one = _mm_set1_ps( 1.0f );
|
||||
i = _mm_castps_si128( x );
|
||||
/* exp = (float) exponent(x) */
|
||||
vexp = _mm_cvtepi32_ps( _mm_sub_epi32( _mm_srli_epi32( _mm_and_si128( i, expmask ), 23 ), _mm_set1_epi32( 127 ) ) );
|
||||
/* mant = (float) mantissa(x) */
|
||||
mant = _mm_or_ps( _mm_castsi128_ps( _mm_and_si128( i, mantmask ) ), one );
|
||||
/* Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[
|
||||
* These coefficients can be generate with
|
||||
* http://www.boost.org/doc/libs/1_36_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals2/minimax.html
|
||||
*/
|
||||
#if LOG_POLY_DEGREE == 6
|
||||
logmant = POLY5( mant, 3.11578814719469302614f, -3.32419399085241980044f, 2.59883907202499966007f, -1.23152682416275988241f, 0.318212422185251071475f, -0.0344359067839062357313f );
|
||||
#elif LOG_POLY_DEGREE == 5
|
||||
logmant = POLY4( mant, 2.8882704548164776201f, -2.52074962577807006663f, 1.48116647521213171641f, -0.465725644288844778798f, 0.0596515482674574969533f );
|
||||
#elif LOG_POLY_DEGREE == 4
|
||||
logmant = POLY3( mant, 2.61761038894603480148f, -1.75647175389045657003f, 0.688243882994381274313f, -0.107254423828329604454f );
|
||||
#elif LOG_POLY_DEGREE == 3
|
||||
logmant = POLY2( mant, 2.28330284476918490682f, -1.04913055217340124191f, 0.204446009836232697516f );
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
/* This effectively increases the polynomial degree by one, but ensures that log2(1) == 0*/
|
||||
logmant = _mm_mul_ps( logmant, _mm_sub_ps(mant, one ) );
|
||||
return _mm_add_ps( logmant, vexp );
|
||||
}
|
||||
|
||||
|
||||
__m128 simd4f_pow_ps( __m128 x, __m128 y )
|
||||
{
|
||||
return simd4f_exp2_ps( _mm_mul_ps( simd4f_log2_ps( x ), y ) );
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
|
||||
/*
|
||||
By Potatoswatter
|
||||
http://stackoverflow.com/questions/6475373/optimizations-for-pow-with-const-non-integer-exponent
|
||||
*/
|
||||
|
||||
#ifndef CC_ALWAYSINLINE
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER)
|
||||
#define CC_ALWAYSINLINE __attribute__((always_inline))
|
||||
#else
|
||||
#define CC_ALWAYSINLINE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static inline CC_ALWAYSINLINE __m128 simd4f_fastpow_ps( __m128 arg, uint32_t expnum, uint32_t expden, uint32_t coeffnum, uint32_t coeffden )
|
||||
{
|
||||
__m128 ret = arg;
|
||||
float corrfactor, powfactor;
|
||||
/* Apply a constant pre-correction factor. */
|
||||
corrfactor = (float)(exp2( 127.0 * expden / expnum - 127.0 ) * pow( 1.0 * coeffnum / coeffden, 1.0 * expden / expnum ));
|
||||
powfactor = 1.0f * expnum / expden;
|
||||
ret = _mm_mul_ps( ret, _mm_set1_ps( corrfactor ) );
|
||||
/* Reinterpret arg as integer to obtain logarithm. */
|
||||
ret = _mm_cvtepi32_ps( _mm_castps_si128( ret ) );
|
||||
/* Multiply logarithm by power. */
|
||||
ret = _mm_mul_ps( ret, _mm_set1_ps( powfactor ) );
|
||||
/* Convert back to "integer" to exponentiate. */
|
||||
ret = _mm_castsi128_ps( _mm_cvtps_epi32( ret ) );
|
||||
return ret;
|
||||
}
|
||||
|
||||
__m128 simd4f_pow12d5_ps( __m128 arg )
|
||||
{
|
||||
/* Lower exponents provide lower initial error, but too low causes overflow. */
|
||||
__m128 xf = simd4f_fastpow_ps( arg, 4, 5, (int)( 1.38316186f * (float)1e9 ), (int)1e9 );
|
||||
/* Imprecise 4-cycle sqrt is still far better than fastpow, good enough. */
|
||||
__m128 xfm4 = _mm_rsqrt_ps( xf );
|
||||
__m128 xf4 = _mm_mul_ps( xf, xfm4 );
|
||||
/* Precisely calculate x^2 and x^3 */
|
||||
__m128 x2 = _mm_mul_ps( arg, arg );
|
||||
__m128 x3 = _mm_mul_ps( x2, arg );
|
||||
/* Overestimate of x^2 * x^0.4 */
|
||||
x2 = _mm_mul_ps( x2, xf4 );
|
||||
/* Get x^-0.2 from x^0.4, and square it for x^-0.4. Combine into x^-0.6. */
|
||||
__m128 xfm2 = _mm_rsqrt_ps( xf4 );
|
||||
x3 = _mm_mul_ps( x3, xfm4 );
|
||||
x3 = _mm_mul_ps( x3, xfm2 );
|
||||
return _mm_mul_ps( _mm_add_ps( x2, x3 ), _mm_set1_ps( 1.0f/1.960131704207789f * 0.9999f ) );
|
||||
}
|
||||
|
||||
__m128 simd4f_pow5d12_ps( __m128 arg )
|
||||
{
|
||||
/* 5/12 is too small, so compute the 4th root of 20/12 instead. */
|
||||
/* 20/12 = 5/3 = 1 + 2/3 = 2 - 1/3. 2/3 is a suitable argument for fastpow. */
|
||||
/* weighting coefficient: a^-1/2 = 2 a; a = 2^-2/3 */
|
||||
__m128 xf = simd4f_fastpow_ps( arg, 2, 3, (int)( 0.629960524947437f * (float)1e9 ), (int)1e9 );
|
||||
__m128 xover = _mm_mul_ps( arg, xf );
|
||||
__m128 xfm1 = _mm_rsqrt_ps( xf );
|
||||
__m128 x2 = _mm_mul_ps( arg, arg );
|
||||
__m128 xunder = _mm_mul_ps( x2, xfm1 );
|
||||
/* sqrt2 * over + 2 * sqrt2 * under */
|
||||
__m128 xavg = _mm_mul_ps( _mm_set1_ps( 1.0f/( 3.0f * 0.629960524947437f ) * 0.999852f ), _mm_add_ps( xover, xunder ) );
|
||||
xavg = _mm_mul_ps( xavg, _mm_rsqrt_ps( xavg ) );
|
||||
xavg = _mm_mul_ps( xavg, _mm_rsqrt_ps( xavg ) );
|
||||
return xavg;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2008-2016 Alexis Naveros.
|
||||
*
|
||||
* The SIMD trigonometry functions are Copyright (C) 2007 Julien Pommier
|
||||
* See copyright notice for simd4f_sin_ps(), simd4f_cos_ps(), simd4f_sincos_ps()
|
||||
*
|
||||
* Portions developed under contract to the SURVICE Engineering Company.
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CPUSIMD_H
|
||||
#define CPUSIMD_H
|
||||
|
||||
|
||||
////
|
||||
#include <simd_wrapper.h>
|
||||
|
||||
#if __SSE4A__ || CPU_ENABLE_SSE4A
|
||||
#include <ammintrin.h>
|
||||
#define CPU_SSE4A_SUPPORT (1)
|
||||
#endif
|
||||
#if __AVX__ || CPU_ENABLE_AVX
|
||||
#include <immintrin.h>
|
||||
#define CPU_AVX_SUPPORT (1)
|
||||
#endif
|
||||
#if __AVX2__ || CPU_ENABLE_AVX2
|
||||
#include <immintrin.h>
|
||||
#define CPU_AVX2_SUPPORT (1)
|
||||
#endif
|
||||
#if __XOP__ || CPU_ENABLE_XOP
|
||||
#include <immintrin.h>
|
||||
#define CPU_XOP_SUPPORT (1)
|
||||
#endif
|
||||
#if __FMA3__ || CPU_ENABLE_FMA3
|
||||
#include <immintrin.h>
|
||||
#define CPU_FMA3_SUPPORT (1)
|
||||
#endif
|
||||
#if __FMA4__ || CPU_ENABLE_FMA4
|
||||
#include <immintrin.h>
|
||||
#define CPU_FMA4_SUPPORT (1)
|
||||
#endif
|
||||
#if __RDRND__ || CPU_ENABLE_RDRND
|
||||
#include <immintrin.h>
|
||||
#define CPU_RDRND_SUPPORT (1)
|
||||
#endif
|
||||
#if __POPCNT__ || CPU_ENABLE_POPCNT
|
||||
#include <popcntintrin.h>
|
||||
#define CPU_POPCNT_SUPPORT (1)
|
||||
#endif
|
||||
#if __LZCNT__ || CPU_ENABLE_LZCNT
|
||||
#include <x86intrin.h>
|
||||
#define CPU_LZCNT_SUPPORT (1)
|
||||
#endif
|
||||
#if __F16C__ || CPU_ENABLE_F16C
|
||||
#include <x86intrin.h>
|
||||
#define CPU_F16C_SUPPORT (1)
|
||||
#endif
|
||||
#if __BMI__ || CPU_ENABLE_BMI
|
||||
#include <x86intrin.h>
|
||||
#define CPU_BMI_SUPPORT (1)
|
||||
#endif
|
||||
#if __BMI2__ || CPU_ENABLE_BMI2
|
||||
#include <x86intrin.h>
|
||||
#define CPU_BMI2_SUPPORT (1)
|
||||
#endif
|
||||
#if __TBM__ || CPU_ENABLE_TBM
|
||||
#include <x86intrin.h>
|
||||
#define CPU_TBM_SUPPORT (1)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER)
|
||||
#define CPU_ALIGN16 __attribute__((aligned(16)))
|
||||
#define CPU_ALIGN32 __attribute__((aligned(32)))
|
||||
#define CPU_ALIGN64 __attribute__((aligned(64)))
|
||||
#elif defined(_MSC_VER)
|
||||
#define CPU_ALIGN16 __declspec(align(16))
|
||||
#define CPU_ALIGN64 __declspec(align(64))
|
||||
#else
|
||||
#define CPU_ALIGN16
|
||||
#define CPU_ALIGN32
|
||||
#define CPU_ALIGN64
|
||||
#warning "SSE/AVX Disabled: Unsupported Compiler."
|
||||
#undef CPU_SSE_SUPPORT
|
||||
#undef CPU_SSE2_SUPPORT
|
||||
#undef CPU_SSE3_SUPPORT
|
||||
#undef CPU_SSSE3_SUPPORT
|
||||
#undef CPU_SSE4_1_SUPPORT
|
||||
#undef CPU_SSE4_2_SUPPORT
|
||||
#undef CPU_AVX_SUPPORT
|
||||
#undef CPU_AVX2_SUPPORT
|
||||
#undef CPU_XOP_SUPPORT
|
||||
#undef CPU_FMA3_SUPPORT
|
||||
#undef CPU_FMA4_SUPPORT
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE_SUPPORT
|
||||
#define CPU_APPROX_DIV_FLOAT(z,w) _mm_cvtss_f32(_mm_mul_ss(_mm_set_ss(z),_mm_rcp_ss(_mm_set_ss(w))))
|
||||
#define CPU_APPROX_SQRT_FLOAT(z) _mm_cvtss_f32(_mm_mul_ss(_mm_set_ss(z),_mm_rsqrt_ss(_mm_set_ss(z))))
|
||||
#define CPU_APPROX_RSQRT_FLOAT(z) _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(z)))
|
||||
#define CPU_APPROX_DIVSQRT_FLOAT(z,w) _mm_cvtss_f32(_mm_mul_ss(_mm_set_ss(z),_mm_rsqrt_ss(_mm_set_ss(w))))
|
||||
#else
|
||||
#define CPU_APPROX_DIV_FLOAT(z,w) ((z)/(w))
|
||||
#define CPU_APPROX_SQRT_FLOAT(z) (sqrtf(z))
|
||||
#define CPU_APPROX_RSQRT_FLOAT(z) (1.0/sqrtf(z))
|
||||
#define CPU_APPROX_DIVSQRT_FLOAT(z,w) ((z)/sqrtf(w))
|
||||
#endif
|
||||
|
||||
|
||||
#if CPU_SSE3_SUPPORT
|
||||
#define CPU_HADD_PS(vx,vy) _mm_hadd_ps(vx,vy)
|
||||
#define CPU_HADD_PD(vx,vy) _mm_hadd_pd(vx,vy)
|
||||
#elif CPU_SSE_SUPPORT
|
||||
static inline __m128 CPU_HADD_PS( __m128 vx, __m128 vy )
|
||||
{
|
||||
__m128 vh, vl;
|
||||
vh = _mm_shuffle_ps( vx, vy, _MM_SHUFFLE(3,1,3,1) );
|
||||
vl = _mm_shuffle_ps( vx, vy, _MM_SHUFFLE(2,0,2,0) );
|
||||
return _mm_add_ps( vh, vl );
|
||||
}
|
||||
#define CPU_HADD_PD(vx,vy) _mm_add_sd(vx,_mm_unpackhi_pd(vy,vy))
|
||||
#endif
|
||||
|
||||
|
||||
#if CPU_SSE4_1_SUPPORT
|
||||
#define CPU_CVT_U8_TO_I32(x,vzero) _mm_cvtepu8_epi32(x)
|
||||
#define CPU_CVT_S8_TO_I32(x,vzero) _mm_cvtepi8_epi32(x)
|
||||
#elif CPU_SSE2_SUPPORT
|
||||
#define CPU_CVT_U8_TO_I32(x,vzero) _mm_unpacklo_epi16(_mm_unpacklo_epi8((x),(vzero)),(vzero))
|
||||
static inline __m128i CPU_CVT_S8_TO_I32( __m128i vx, __m128i vzero )
|
||||
{
|
||||
__m128i vsign;
|
||||
vsign = _mm_cmpgt_epi8( vzero, vx );
|
||||
return _mm_unpacklo_epi16( _mm_unpacklo_epi8( vx, vsign ), _mm_unpacklo_epi8( vsign, vsign ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if CPU_SSE4_1_SUPPORT
|
||||
#define CPU_BLENDV_PS(x,y,mask) _mm_blendv_ps(x,y,mask)
|
||||
#define CPU_BLENDV_PD(x,y,mask) _mm_blendv_pd(x,y,mask)
|
||||
#elif CPU_SSE2_SUPPORT
|
||||
#define CPU_BLENDV_PS(x,y,mask) _mm_or_ps(_mm_andnot_ps(mask,x),_mm_and_ps(y,mask))
|
||||
#define CPU_BLENDV_PD(x,y,mask) _mm_or_pd(_mm_andnot_pd(mask,x),_mm_and_pd(y,mask))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
CPU_FMADD = ((f0*f1)+t0)
|
||||
CPU_FMSUB = ((f0*f1)-t0)
|
||||
*/
|
||||
#if CPU_FMA3_SUPPORT
|
||||
#define CPU_FMADD_SS(f0,f1,t0) _mm_fmadd_ss(f0,f1,t0)
|
||||
#define CPU_FMADD_PS(f0,f1,t0) _mm_fmadd_ps(f0,f1,t0)
|
||||
#define CPU_FMADD_SD(f0,f1,t0) _mm_fmadd_sd(f0,f1,t0)
|
||||
#define CPU_FMADD_PD(f0,f1,t0) _mm_fmadd_pd(f0,f1,t0)
|
||||
#define CPU_FMSUB_SS(f0,f1,t0) _mm_fmsub_ss(f0,f1,t0)
|
||||
#define CPU_FMSUB_PS(f0,f1,t0) _mm_fmsub_ps(f0,f1,t0)
|
||||
#define CPU_FMSUB_SD(f0,f1,t0) _mm_fmsub_sd(f0,f1,t0)
|
||||
#define CPU_FMSUB_PD(f0,f1,t0) _mm_fmsub_pd(f0,f1,t0)
|
||||
#define CPU_FMADD256_SS(f0,f1,t0) _mm256_fmadd_ss(f0,f1,t0)
|
||||
#define CPU_FMADD256_PS(f0,f1,t0) _mm256_fmadd_ps(f0,f1,t0)
|
||||
#define CPU_FMADD256_SD(f0,f1,t0) _mm256_fmadd_sd(f0,f1,t0)
|
||||
#define CPU_FMADD256_PD(f0,f1,t0) _mm256_fmadd_pd(f0,f1,t0)
|
||||
#define CPU_FMSUB256_SS(f0,f1,t0) _mm256_fmsub_ss(f0,f1,t0)
|
||||
#define CPU_FMSUB256_PS(f0,f1,t0) _mm256_fmsub_ps(f0,f1,t0)
|
||||
#define CPU_FMSUB256_SD(f0,f1,t0) _mm256_fmsub_sd(f0,f1,t0)
|
||||
#define CPU_FMSUB256_PD(f0,f1,t0) _mm256_fmsub_pd(f0,f1,t0)
|
||||
#elif CPU_FMA4_SUPPORT
|
||||
#define CPU_FMADD_SS(f0,f1,t0) _mm_macc_ss(f0,f1,t0)
|
||||
#define CPU_FMADD_PS(f0,f1,t0) _mm_macc_ps(f0,f1,t0)
|
||||
#define CPU_FMADD_SD(f0,f1,t0) _mm_macc_sd(f0,f1,t0)
|
||||
#define CPU_FMADD_PD(f0,f1,t0) _mm_macc_pd(f0,f1,t0)
|
||||
#define CPU_FMSUB_SS(f0,f1,t0) _mm_msub_ss(f0,f1,t0)
|
||||
#define CPU_FMSUB_PS(f0,f1,t0) _mm_msub_ps(f0,f1,t0)
|
||||
#define CPU_FMSUB_SD(f0,f1,t0) _mm_msub_sd(f0,f1,t0)
|
||||
#define CPU_FMSUB_PD(f0,f1,t0) _mm_msub_pd(f0,f1,t0)
|
||||
#define CPU_FMADD256_SS(f0,f1,t0) _mm256_macc_ss(f0,f1,t0)
|
||||
#define CPU_FMADD256_PS(f0,f1,t0) _mm256_macc_ps(f0,f1,t0)
|
||||
#define CPU_FMADD256_SD(f0,f1,t0) _mm256_macc_sd(f0,f1,t0)
|
||||
#define CPU_FMADD256_PD(f0,f1,t0) _mm256_macc_pd(f0,f1,t0)
|
||||
#define CPU_FMSUB256_SS(f0,f1,t0) _mm256_msub_ss(f0,f1,t0)
|
||||
#define CPU_FMSUB256_PS(f0,f1,t0) _mm256_msub_ps(f0,f1,t0)
|
||||
#define CPU_FMSUB256_SD(f0,f1,t0) _mm256_msub_sd(f0,f1,t0)
|
||||
#define CPU_FMSUB256_PD(f0,f1,t0) _mm256_msub_pd(f0,f1,t0)
|
||||
#else
|
||||
#define CPU_FMADD_SS(f0,f1,t0) _mm_add_ss(_mm_mul_ss(f0,f1),t0)
|
||||
#define CPU_FMADD_PS(f0,f1,t0) _mm_add_ps(_mm_mul_ps(f0,f1),t0)
|
||||
#define CPU_FMADD_SD(f0,f1,t0) _mm_add_sd(_mm_mul_sd(f0,f1),t0)
|
||||
#define CPU_FMADD_PD(f0,f1,t0) _mm_add_pd(_mm_mul_pd(f0,f1),t0)
|
||||
#define CPU_FMSUB_SS(f0,f1,t0) _mm_sub_ss(_mm_mul_ss(f0,f1),t0)
|
||||
#define CPU_FMSUB_PS(f0,f1,t0) _mm_sub_ps(_mm_mul_ps(f0,f1),t0)
|
||||
#define CPU_FMSUB_SD(f0,f1,t0) _mm_sub_sd(_mm_mul_sd(f0,f1),t0)
|
||||
#define CPU_FMSUB_PD(f0,f1,t0) _mm_sub_pd(_mm_mul_pd(f0,f1),t0)
|
||||
#define CPU_FMADD256_SS(f0,f1,t0) _mm256_add_ss(_mm256_mul_ss(f0,f1),t0)
|
||||
#define CPU_FMADD256_PS(f0,f1,t0) _mm256_add_ps(_mm256_mul_ps(f0,f1),t0)
|
||||
#define CPU_FMADD256_SD(f0,f1,t0) _mm256_add_sd(_mm256_mul_sd(f0,f1),t0)
|
||||
#define CPU_FMADD256_PD(f0,f1,t0) _mm256_add_pd(_mm256_mul_pd(f0,f1),t0)
|
||||
#define CPU_FMSUB256_SS(f0,f1,t0) _mm256_sub_ss(_mm256_mul_ss(f0,f1),t0)
|
||||
#define CPU_FMSUB256_PS(f0,f1,t0) _mm256_sub_ps(_mm256_mul_ps(f0,f1),t0)
|
||||
#define CPU_FMSUB256_SD(f0,f1,t0) _mm256_sub_sd(_mm256_mul_sd(f0,f1),t0)
|
||||
#define CPU_FMSUB256_PD(f0,f1,t0) _mm256_sub_pd(_mm256_mul_pd(f0,f1),t0)
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE_SUPPORT
|
||||
|
||||
extern const uint32_t simd4fSignMask[4];
|
||||
extern const uint32_t simd4fSignMaskInv[4];
|
||||
extern const float simd4fHalf[4];
|
||||
extern const float simd4fOne[4];
|
||||
extern const float simd4fTwo[4];
|
||||
extern const float simd4fThree[4];
|
||||
extern const uint32_t simd4uOne[4];
|
||||
extern const uint32_t simd4uOneInv[4];
|
||||
extern const uint32_t simd4uTwo[4];
|
||||
extern const uint32_t simd4uFour[4];
|
||||
extern const float simd4fQuarter[4];
|
||||
extern const float simd4fPi[4];
|
||||
extern const float simd4fZeroOneTwoThree[4];
|
||||
extern const uint32_t simd4fAlphaMask[4];
|
||||
extern const float simd4f255[4];
|
||||
extern const float simd4f255Inv[4];
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
/* Input range between -8192 and 8192 */
|
||||
__m128 simd4f_sin_ps( __m128 x );
|
||||
__m128 simd4f_cos_ps( __m128 x );
|
||||
void simd4f_sincos_ps( __m128 x, __m128 *s, __m128 *c );
|
||||
|
||||
#endif
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
__m128 simd4f_exp2_ps( __m128 x );
|
||||
__m128 simd4f_log2_ps( __m128 x );
|
||||
__m128 simd4f_pow_ps( __m128 x, __m128 y );
|
||||
|
||||
#endif
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
__m128 simd4f_pow12d5_ps( __m128 arg );
|
||||
__m128 simd4f_pow5d12_ps( __m128 arg );
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE2_SUPPORT
|
||||
|
||||
#ifndef CC_ALWAYSINLINE
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER)
|
||||
#define CC_ALWAYSINLINE __attribute__((always_inline))
|
||||
#else
|
||||
#define CC_ALWAYSINLINE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static inline CC_ALWAYSINLINE __m128 simd4f_pow12d5_inline_ps( __m128 vx )
|
||||
{
|
||||
__m128 vpow, vpwsqrtinv, vpwsqrt, vx2;
|
||||
vx2 = _mm_mul_ps( vx, vx );
|
||||
vpow = _mm_castsi128_ps( _mm_cvtps_epi32( _mm_mul_ps( _mm_cvtepi32_ps( _mm_castps_si128( _mm_mul_ps( vx, _mm_set1_ps( 5417434112.0f ) ) ) ), _mm_set1_ps( 0.8f ) ) ) );
|
||||
vpwsqrtinv = _mm_rsqrt_ps( vpow );
|
||||
vpwsqrt = _mm_mul_ps( vpow, vpwsqrtinv );
|
||||
return _mm_mul_ps( _mm_add_ps( _mm_mul_ps( vx2, vpwsqrt ), _mm_mul_ps( _mm_mul_ps( _mm_mul_ps( vx2, vx ), vpwsqrtinv ), _mm_rsqrt_ps( vpwsqrt ) ) ), _mm_set1_ps( 0.51011878327f ) );
|
||||
}
|
||||
|
||||
static inline CC_ALWAYSINLINE __m128 simd4f_pow5d12_inline_ps( __m128 vx )
|
||||
{
|
||||
__m128 vpow;
|
||||
vpow = _mm_castsi128_ps( _mm_cvtps_epi32( _mm_mul_ps( _mm_cvtepi32_ps( _mm_castps_si128( _mm_mul_ps( vx, _mm_set1_ps( 6521909350804488192.0f ) ) ) ), _mm_set1_ps( 0.666666666666f ) ) ) );
|
||||
vx = _mm_mul_ps( _mm_add_ps( _mm_mul_ps( vx, vpow ), _mm_mul_ps( _mm_mul_ps( vx, vx ), _mm_rsqrt_ps( vpow ) ) ), _mm_set1_ps( 0.5290553722f ) );
|
||||
#if 0
|
||||
vx = _mm_mul_ps( vx, _mm_rsqrt_ps( vx ) );
|
||||
vx = _mm_mul_ps( vx, _mm_rsqrt_ps( vx ) );
|
||||
#else
|
||||
vx = _mm_sqrt_ps( vx );
|
||||
vx = _mm_sqrt_ps( vx );
|
||||
#endif
|
||||
return vx;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#if CPU_SSE_SUPPORT
|
||||
|
||||
static inline void simdPrintDebugSSE4f( char *str, __m128 v )
|
||||
{
|
||||
float CPU_ALIGN16 store[4];
|
||||
_mm_store_ps( (float *)store, v );
|
||||
printf( "%s %f %f %f %f\n", str, (double)store[0], (double)store[1], (double)store[2], (double)store[3] );
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void simdPrintDebugSSE2d( char *str, __m128d v )
|
||||
{
|
||||
double CPU_ALIGN16 store[2];
|
||||
_mm_store_pd( (double *)store, v );
|
||||
printf( "%s %f %f\n", str, store[0], store[1] );
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void simdPrintDebugSSE16u8( char *str, __m128i v )
|
||||
{
|
||||
uint8_t CPU_ALIGN16 store[16];
|
||||
_mm_store_si128( (void *)store, v );
|
||||
printf( "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n", str, store[0], store[1], store[2], store[3], store[4], store[5], store[6], store[7], store[8], store[9], store[10], store[11], store[12], store[13], store[14], store[15] );
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void simdPrintDebugSSE8u16( char *str, __m128i v )
|
||||
{
|
||||
uint16_t CPU_ALIGN16 store[8];
|
||||
_mm_store_si128( (void *)store, v );
|
||||
printf( "%s %d %d %d %d %d %d %d %d\n", str, store[0], store[1], store[2], store[3], store[4], store[5], store[6], store[7] );
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void simdPrintDebugSSE4u32( char *str, __m128i v )
|
||||
{
|
||||
uint32_t CPU_ALIGN16 store[4];
|
||||
_mm_store_si128( (void *)store, v );
|
||||
printf( "%s %d %d %d %d\n", str, store[0], store[1], store[2], store[3] );
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void simdPrintDebugSSE2u64( char *str, __m128i v )
|
||||
{
|
||||
uint64_t CPU_ALIGN16 store[2];
|
||||
_mm_store_si128( (void *)store, v );
|
||||
printf( "%s %lld %lld\n", str, (long long)store[0], (long long)store[1] );
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2007-2017 Alexis Naveros.
|
||||
* Portions developed under contract to the SURVICE Engineering Company.
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef IMG_H
|
||||
#define IMG_H
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int type;
|
||||
int bytesperpixel;
|
||||
int bytesperline;
|
||||
} imgFormat;
|
||||
|
||||
enum
|
||||
{
|
||||
IMG_FORMAT_TYPE_ANY,
|
||||
IMG_FORMAT_TYPE_RGB24,
|
||||
IMG_FORMAT_TYPE_BGR24,
|
||||
IMG_FORMAT_TYPE_RGBX32,
|
||||
IMG_FORMAT_TYPE_BGRX32,
|
||||
IMG_FORMAT_TYPE_RGBA32,
|
||||
IMG_FORMAT_TYPE_BGRA32,
|
||||
IMG_FORMAT_TYPE_GRAYSCALE,
|
||||
IMG_FORMAT_TYPE_GRAYALPHA
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
imgFormat format;
|
||||
void *data;
|
||||
} imgImage;
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
void imgCopyRect( imgImage *image, int dstx, int dsty, int srcx, int srcy, int sizex, int sizey );
|
||||
|
||||
void (*imgBlendGetFunction( imgImage *dstimage, imgImage *srcimage ))( imgImage *dstimage, int dstx, int dsty, imgImage *srcimage );
|
||||
int imgBlendImage( imgImage *dstimage, int dstx, int dsty, imgImage *srcimage );
|
||||
|
||||
void imgAllocCopy( imgImage *dst, imgImage *src );
|
||||
void imgAllocCopyExtendBorder( imgImage *dstimage, imgImage *srcimage, int extendsize );
|
||||
void imgAllocExtractChannel( imgImage *dst, imgImage *src, int channelindex );
|
||||
void imgAllocExtractChannelExtendBorder( imgImage *dstimage, imgImage *srcimage, int channelindex, int extendsize );
|
||||
void imgAllocCopyChannelToAlpha( imgImage *dstimage, imgImage *srcimage, int channelindex, unsigned char r, unsigned char g, unsigned char b );
|
||||
void imgAllocAdjustBrightnessContrast( imgImage *dstimage, imgImage *srcimage, float brightness, float contrast );
|
||||
|
||||
void imgFree( imgImage *image );
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,150 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2014-2017 Alexis Naveros.
|
||||
* Portions developed under contract to the SURVICE Engineering Company.
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
|
||||
#ifndef IMGRESIZE_H
|
||||
#define IMGRESIZE_H
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/* Specify filter type, from the IM_REDUCE_FILTER_* list */
|
||||
int filter;
|
||||
/* High quality, a little slow: hopcount=3; */
|
||||
/* Good quality, much faster: hopcount=2; */
|
||||
int hopcount;
|
||||
/* Strong preservation/amplification of details: alpha=2.0f; */
|
||||
/* Mild preservation/amplification of details: alpha=6.0f; */
|
||||
float alpha;
|
||||
/* NORMALMAP filters: factor to amyplify normals on X and Y before normalization */
|
||||
float amplifynormal;
|
||||
/* NORMALMAP_SUSTAIN filters: Preserve a factor of deviation "energy" as calculated by sqrtf(x*x+y*y) */
|
||||
float normalsustainfactor;
|
||||
} imReduceOptions;
|
||||
|
||||
static inline void imReduceSetOptions( imReduceOptions *options, int filter, int hopcount, float alpha, float amplifynormal, float normalsustainfactor )
|
||||
{
|
||||
options->filter = filter;
|
||||
options->hopcount = hopcount;
|
||||
options->alpha = alpha;
|
||||
options->amplifynormal = amplifynormal;
|
||||
options->normalsustainfactor = normalsustainfactor;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* Reduce the image's dimensions by an integer divisor ~ this is fairly fast */
|
||||
int imReduceImageKaiserDataDivisor( unsigned char *dstdata, unsigned char *srcdata, int width, int height, int bytesperpixel, int bytesperline, int sizedivisor, imReduceOptions *options );
|
||||
/* Same as imReduceImageKaiserDataDivisor(), but imgdst is allocated */
|
||||
int imReduceImageKaiserDivisor( imgImage *imgdst, imgImage *imgsrc, int sizedivisor, imReduceOptions *options );
|
||||
|
||||
|
||||
/* Reduce the image's dimensions to match the newwidth and newheight ~ this is a little slower */
|
||||
int imReduceImageKaiserData( unsigned char *dstdata, unsigned char *srcdata, int width, int height, int bytesperpixel, int bytesperline, int newwidth, int newheight, imReduceOptions *options );
|
||||
/* Same as imReduceImageKaiserData(), but imgdst is allocated */
|
||||
int imReduceImageKaiser( imgImage *imgdst, imgImage *imgsrc, int newwidth, int newheight, imReduceOptions *options );
|
||||
|
||||
|
||||
/* Resize by half with a dumb box filter ~ don't use that except for the smallest mipmaps */
|
||||
/* Filters with ALPHANORM and/or SUSTAIN keywords are processed as the regular base filter only */
|
||||
int imReduceImageHalfBoxData( unsigned char *dstdata, unsigned char *srcdata, int width, int height, int bytesperpixel, int bytesperline, imReduceOptions *options );
|
||||
int imReduceImageHalfBox( imgImage *imgdst, imgImage *imgsrc, imReduceOptions *options );
|
||||
|
||||
|
||||
/*
|
||||
Keywords for image reduction filters
|
||||
|
||||
LINEAR: Data is linear, note that this is *not* the format of typical diffuse textures
|
||||
SRGB: Color is in sRGB space, any alpha is presumed linear
|
||||
NORMALMAP: RGB represents a XYZ vector as (2.0*RGB)-1.0f, any alpha is presumed linear
|
||||
|
||||
ALPHANORM: Alpha normalization, the weight of pixels is proportional to their alpha values
|
||||
(do you have "black" fully transparent pixels? please use an ALPHANORM filter)
|
||||
SUSTAIN: The "energy" of the normal map is sustained, amplified to preserve the level of details
|
||||
Note that this filter is rather slow (set options->normalsustainfactor to 0.75 or so)
|
||||
*/
|
||||
|
||||
enum
|
||||
{
|
||||
/* Linear space */
|
||||
IM_REDUCE_FILTER_LINEAR,
|
||||
IM_REDUCE_FILTER_LINEAR_ALPHANORM,
|
||||
|
||||
/* sRGB space (probably what you want for diffuse textures) */
|
||||
IM_REDUCE_FILTER_SRGB,
|
||||
IM_REDUCE_FILTER_SRGB_ALPHANORM,
|
||||
|
||||
/* RGB represents a XYZ vector as (2.0*RGB)-1.0f, any alpha is presumed linear */
|
||||
IM_REDUCE_FILTER_NORMALMAP,
|
||||
IM_REDUCE_FILTER_NORMALMAP_ALPHANORM,
|
||||
IM_REDUCE_FILTER_NORMALMAP_SUSTAIN,
|
||||
IM_REDUCE_FILTER_NORMALMAP_SUSTAIN_ALPHANORM,
|
||||
|
||||
/* Custom specialized filters */
|
||||
IM_REDUCE_FILTER_WATERMAP,
|
||||
IM_REDUCE_FILTER_PLANTMAP,
|
||||
IM_REDUCE_FILTER_FOLLIAGE,
|
||||
IM_REDUCE_FILTER_SKY,
|
||||
IM_REDUCE_FILTER_FOG
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#define IM_MIPMAP_CASCADE_MAX (16)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int width;
|
||||
int height;
|
||||
int layercount;
|
||||
int bytesperpixel;
|
||||
int bytesperline;
|
||||
imReduceOptions *options;
|
||||
void *mipmap[IM_MIPMAP_CASCADE_MAX];
|
||||
} imMipmapCascade;
|
||||
|
||||
|
||||
int imBuildMipmapCascade( imMipmapCascade *cascade, void *imagedata, int width, int height, int layercount, int bytesperpixel, int bytesperline, imReduceOptions *options, int cascadeflags );
|
||||
|
||||
void imFreeMipmapCascade( imMipmapCascade *cascade );
|
||||
|
||||
/* For base texture, propagate RGB channels to neighbors if they are fully transparent (ignored if bytesperpixel != 4 ) */
|
||||
#define IM_CASCADE_FLAGS_COLOR_BORDER_BASE (0x1)
|
||||
/* For generated mipmaps, propagate RGB channels to neighbors if they are fully transparent (ignored if bytesperpixel != 4 ) */
|
||||
#define IM_CASCADE_FLAGS_COLOR_BORDER_MIPMAPS (0x2)
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
void imPropagateAlphaBorder( unsigned char *imagedata, int width, int height, int bytesperpixel, int bytesperline );
|
||||
|
||||
|
||||
////
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user