1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29
30 #include "pipe/p_config.h"
31 #include "util/u_math.h"
32 #include "util/u_cpu_detect.h"
33
34 #if defined(PIPE_ARCH_SSE)
35 #include <xmmintrin.h>
36 /* This is defined in pmmintrin.h, but it can only be included when -msse3 is
37 * used, so just define it here to avoid further. */
38 #ifndef _MM_DENORMALS_ZERO_MASK
39 #define _MM_DENORMALS_ZERO_MASK 0x0040
40 #endif
41 #endif
42
43
44 /** 2^x, for x in [-1.0, 1.0) */
45 float pow2_table[POW2_TABLE_SIZE];
46
47
48 static void
init_pow2_table(void)49 init_pow2_table(void)
50 {
51 int i;
52 for (i = 0; i < POW2_TABLE_SIZE; i++)
53 pow2_table[i] = exp2f((i - POW2_TABLE_OFFSET) / POW2_TABLE_SCALE);
54 }
55
56
57 /** log2(x), for x in [1.0, 2.0) */
58 float log2_table[LOG2_TABLE_SIZE];
59
60
61 static void
init_log2_table(void)62 init_log2_table(void)
63 {
64 unsigned i;
65 for (i = 0; i < LOG2_TABLE_SIZE; i++)
66 log2_table[i] = (float) log2(1.0 + i * (1.0 / LOG2_TABLE_SCALE));
67 }
68
69
70 /**
71 * One time init for math utilities.
72 */
73 void
util_init_math(void)74 util_init_math(void)
75 {
76 static bool initialized = false;
77 if (!initialized) {
78 init_pow2_table();
79 init_log2_table();
80 initialized = true;
81 }
82 }
83
84 /**
85 * Fetches the contents of the fpstate (mxcsr on x86) register.
86 *
87 * On platforms without support for it just returns 0.
88 */
89 unsigned
util_fpstate_get(void)90 util_fpstate_get(void)
91 {
92 unsigned mxcsr = 0;
93
94 #if defined(PIPE_ARCH_SSE)
95 if (util_cpu_caps.has_sse) {
96 mxcsr = _mm_getcsr();
97 }
98 #endif
99
100 return mxcsr;
101 }
102
103 /**
104 * Make sure that the fp treats the denormalized floating
105 * point numbers as zero.
106 *
107 * This is the behavior required by D3D10. OpenGL doesn't care.
108 */
109 unsigned
util_fpstate_set_denorms_to_zero(unsigned current_mxcsr)110 util_fpstate_set_denorms_to_zero(unsigned current_mxcsr)
111 {
112 #if defined(PIPE_ARCH_SSE)
113 if (util_cpu_caps.has_sse) {
114 /* Enable flush to zero mode */
115 current_mxcsr |= _MM_FLUSH_ZERO_MASK;
116 if (util_cpu_caps.has_daz) {
117 /* Enable denormals are zero mode */
118 current_mxcsr |= _MM_DENORMALS_ZERO_MASK;
119 }
120 util_fpstate_set(current_mxcsr);
121 }
122 #endif
123 return current_mxcsr;
124 }
125
126 /**
127 * Set the state of the fpstate (mxcsr on x86) register.
128 *
129 * On platforms without support for it's a noop.
130 */
131 void
util_fpstate_set(unsigned mxcsr)132 util_fpstate_set(unsigned mxcsr)
133 {
134 #if defined(PIPE_ARCH_SSE)
135 if (util_cpu_caps.has_sse) {
136 _mm_setcsr(mxcsr);
137 }
138 #endif
139 }
140