| 1 |
/* |
| 2 |
* This file is a part of Luminance HDR package |
| 3 |
* ---------------------------------------------------------------------- |
| 4 |
* Copyright (C) 2011 Davide Anastasia |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* ---------------------------------------------------------------------- |
| 20 |
*/ |
| 21 |
|
| 22 |
//! @brief SSE for high performance vector operations |
| 23 |
//! @author Davide Anastasia, <[email protected]> |
| 24 |
|
| 25 |
#include <Libpfs/utils/numeric.h> |
| 26 |
|
| 27 |
namespace pfs { |
| 28 |
namespace utils { |
| 29 |
|
| 30 |
#ifdef LUMINANCE_USE_SSE |
| 31 |
|
| 32 |
/* Implementation lifted from |
| 33 |
* http://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html |
| 34 |
*/ |
| 35 |
|
| 36 |
#define EXP_POLY_DEGREE 5 |
| 37 |
|
| 38 |
#define POLY0(x, c0) _mm_set1_ps(c0) |
| 39 |
#define POLY1(x, c0, c1) \ |
| 40 |
_mm_add_ps(_mm_mul_ps(POLY0(x, c1), x), _mm_set1_ps(c0)) |
| 41 |
#define POLY2(x, c0, c1, c2) \ |
| 42 |
_mm_add_ps(_mm_mul_ps(POLY1(x, c1, c2), x), _mm_set1_ps(c0)) |
| 43 |
#define POLY3(x, c0, c1, c2, c3) \ |
| 44 |
_mm_add_ps(_mm_mul_ps(POLY2(x, c1, c2, c3), x), _mm_set1_ps(c0)) |
| 45 |
#define POLY4(x, c0, c1, c2, c3, c4) \ |
| 46 |
_mm_add_ps(_mm_mul_ps(POLY3(x, c1, c2, c3, c4), x), _mm_set1_ps(c0)) |
| 47 |
#define POLY5(x, c0, c1, c2, c3, c4, c5) \ |
| 48 |
_mm_add_ps(_mm_mul_ps(POLY4(x, c1, c2, c3, c4, c5), x), _mm_set1_ps(c0)) |
| 49 |
|
| 50 |
v4sf _mm_exp2_ps(v4sf x) { |
| 51 |
__m128i ipart; |
| 52 |
v4sf fpart, expipart, expfpart; |
| 53 |
|
| 54 |
x = _mm_min_ps(x, _mm_set1_ps(129.00000f)); |
| 55 |
x = _mm_max_ps(x, _mm_set1_ps(-126.99999f)); |
| 56 |
|
| 57 |
/* ipart = int(x - 0.5) */ |
| 58 |
ipart = _mm_cvtps_epi32(_mm_sub_ps(x, _mm_set1_ps(0.5f))); |
| 59 |
|
| 60 |
/* fpart = x - ipart */ |
| 61 |
fpart = _mm_sub_ps(x, _mm_cvtepi32_ps(ipart)); |
| 62 |
|
| 63 |
/* expipart = (float) (1 << ipart) */ |
| 64 |
expipart = _mm_castsi128_ps( |
| 65 |
_mm_slli_epi32(_mm_add_epi32(ipart, _mm_set1_epi32(127)), 23)); |
| 66 |
|
| 67 |
/* minimax polynomial fit of 2**x, in range [-0.5, 0.5[ */ |
| 68 |
#if EXP_POLY_DEGREE == 5 |
| 69 |
expfpart = POLY5(fpart, 9.9999994e-1f, 6.9315308e-1f, 2.4015361e-1f, |
| 70 |
5.5826318e-2f, 8.9893397e-3f, 1.8775767e-3f); |
| 71 |
#elif EXP_POLY_DEGREE == 4 |
| 72 |
expfpart = POLY4(fpart, 1.0000026f, 6.9300383e-1f, 2.4144275e-1f, |
| 73 |
5.2011464e-2f, 1.3534167e-2f); |
| 74 |
#elif EXP_POLY_DEGREE == 3 |
| 75 |
expfpart = POLY3(fpart, 9.9992520e-1f, 6.9583356e-1f, 2.2606716e-1f, |
| 76 |
7.8024521e-2f); |
| 77 |
#elif EXP_POLY_DEGREE == 2 |
| 78 |
expfpart = POLY2(fpart, 1.0017247f, 6.5763628e-1f, 3.3718944e-1f); |
| 79 |
#else |
| 80 |
#error |
| 81 |
#endif |
| 82 |
|
| 83 |
return _mm_mul_ps(expipart, expfpart); |
| 84 |
} |
| 85 |
|
| 86 |
#define LOG_POLY_DEGREE 5 |
| 87 |
|
| 88 |
v4sf _mm_log2_ps(v4sf x) { |
| 89 |
__m128i exp = _mm_set1_epi32(0x7F800000); |
| 90 |
__m128i mant = _mm_set1_epi32(0x007FFFFF); |
| 91 |
|
| 92 |
v4sf one = _mm_set1_ps(1.0f); |
| 93 |
|
| 94 |
__m128i i = _mm_castps_si128(x); |
| 95 |
|
| 96 |
v4sf e = _mm_cvtepi32_ps(_mm_sub_epi32( |
| 97 |
_mm_srli_epi32(_mm_and_si128(i, exp), 23), _mm_set1_epi32(127))); |
| 98 |
|
| 99 |
v4sf m = _mm_or_ps(_mm_castsi128_ps(_mm_and_si128(i, mant)), one); |
| 100 |
|
| 101 |
v4sf p; |
| 102 |
|
| 103 |
/* Minimax polynomial fit of log2(x)/(x - 1), for x in range [1, 2[ */ |
| 104 |
#if LOG_POLY_DEGREE == 6 |
| 105 |
p = POLY5(m, 3.1157899f, -3.3241990f, 2.5988452f, -1.2315303f, |
| 106 |
3.1821337e-1f, -3.4436006e-2f); |
| 107 |
#elif LOG_POLY_DEGREE == 5 |
| 108 |
p = POLY4(m, 2.8882704548164776201f, -2.52074962577807006663f, |
| 109 |
1.48116647521213171641f, -0.465725644288844778798f, |
| 110 |
0.0596515482674574969533f); |
| 111 |
#elif LOG_POLY_DEGREE == 4 |
| 112 |
p = POLY3(m, 2.61761038894603480148f, -1.75647175389045657003f, |
| 113 |
0.688243882994381274313f, -0.107254423828329604454f); |
| 114 |
#elif LOG_POLY_DEGREE == 3 |
| 115 |
p = POLY2(m, 2.28330284476918490682f, -1.04913055217340124191f, |
| 116 |
0.204446009836232697516f); |
| 117 |
#else |
| 118 |
#error |
| 119 |
#endif |
| 120 |
|
| 121 |
/* This effectively increases the polynomial degree by one, but ensures that |
| 122 |
* log2(1) == 0*/ |
| 123 |
p = _mm_mul_ps(p, _mm_sub_ps(m, one)); |
| 124 |
|
| 125 |
return _mm_add_ps(p, e); |
| 126 |
} |
| 127 |
|
| 128 |
v4sf _mm_pow_ps(v4sf x, v4sf y) { return _mm_exp2_ps(_mm_log2_ps(x) * y); } |
| 129 |
|
| 130 |
#endif // LUMINANCE_USE_SSE |
| 131 |
|
| 132 |
} // utils |
| 133 |
} // pfs |