Blender  V2.93
math_bits_inline.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #ifndef __MATH_BITS_INLINE_C__
22 #define __MATH_BITS_INLINE_C__
23 
24 #ifdef _MSC_VER
25 # include <intrin.h>
26 #endif
27 
28 #include "BLI_math_bits.h"
29 
30 MINLINE unsigned int bitscan_forward_uint(unsigned int a)
31 {
32  BLI_assert(a != 0);
33 #ifdef _MSC_VER
34  unsigned long ctz;
35  _BitScanForward(&ctz, a);
36  return ctz;
37 #else
38  return (unsigned int)__builtin_ctz(a);
39 #endif
40 }
41 
42 MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
43 {
44  BLI_assert(a != 0);
45 #ifdef _MSC_VER
46  unsigned long ctz;
47  _BitScanForward64(&ctz, a);
48  return ctz;
49 #else
50  return (unsigned int)__builtin_ctzll(a);
51 #endif
52 }
53 
55 {
56  return (int)bitscan_forward_uint((unsigned int)a);
57 }
58 
59 MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a)
60 {
61  unsigned int i = bitscan_forward_uint(*a);
62  *a &= (*a) - 1;
63  return i;
64 }
65 
67 {
68  return (int)bitscan_forward_clear_uint((unsigned int *)a);
69 }
70 
71 MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
72 {
73  BLI_assert(a != 0);
74 #ifdef _MSC_VER
75  unsigned long clz;
76  _BitScanReverse(&clz, a);
77  return 31 - clz;
78 #else
79  return (unsigned int)__builtin_clz(a);
80 #endif
81 }
82 
83 MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
84 {
85  BLI_assert(a != 0);
86 #ifdef _MSC_VER
87  unsigned long clz;
88  _BitScanReverse64(&clz, a);
89  return 31 - clz;
90 #else
91  return (unsigned int)__builtin_clzll(a);
92 #endif
93 }
94 
96 {
97  return (int)bitscan_reverse_uint((unsigned int)a);
98 }
99 
100 MINLINE unsigned int bitscan_reverse_clear_uint(unsigned int *a)
101 {
102  unsigned int i = bitscan_reverse_uint(*a);
103  *a &= ~(0x80000000 >> i);
104  return i;
105 }
106 
108 {
109  return (int)bitscan_reverse_clear_uint((unsigned int *)a);
110 }
111 
112 MINLINE unsigned int highest_order_bit_uint(unsigned int n)
113 {
114  if (n == 0) {
115  return 0;
116  }
117  return 1 << (sizeof(unsigned int) * 8 - bitscan_reverse_uint(n));
118 }
119 
120 MINLINE unsigned short highest_order_bit_s(unsigned short n)
121 {
122  n |= (unsigned short)(n >> 1);
123  n |= (unsigned short)(n >> 2);
124  n |= (unsigned short)(n >> 4);
125  n |= (unsigned short)(n >> 8);
126  return (unsigned short)(n - (n >> 1));
127 }
128 
129 #ifndef __GNUC__
130 MINLINE int count_bits_i(unsigned int i)
131 {
132  /* variable-precision SWAR algorithm. */
133  i = i - ((i >> 1) & 0x55555555);
134  i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
135  return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
136 }
137 #endif
138 
139 MINLINE int float_as_int(float f)
140 {
141  union {
142  int i;
143  float f;
144  } u;
145  u.f = f;
146  return u.i;
147 }
148 
149 MINLINE unsigned int float_as_uint(float f)
150 {
151  union {
152  unsigned int i;
153  float f;
154  } u;
155  u.f = f;
156  return u.i;
157 }
158 
159 MINLINE float int_as_float(int i)
160 {
161  union {
162  int i;
163  float f;
164  } u;
165  u.i = i;
166  return u.f;
167 }
168 
169 MINLINE float uint_as_float(unsigned int i)
170 {
171  union {
172  unsigned int i;
173  float f;
174  } u;
175  u.i = i;
176  return u.f;
177 }
178 
179 MINLINE float xor_fl(float x, int y)
180 {
181  return int_as_float(float_as_int(x) ^ y);
182 }
183 
184 #endif /* __MATH_BITS_INLINE_C__ */
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define MINLINE
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
MINLINE int float_as_int(float f)
MINLINE unsigned int bitscan_forward_uint(unsigned int a)
MINLINE int bitscan_forward_clear_i(int *a)
MINLINE float xor_fl(float x, int y)
MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a)
MINLINE float int_as_float(int i)
MINLINE int count_bits_i(unsigned int i)
MINLINE unsigned int float_as_uint(float f)
MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
MINLINE int bitscan_reverse_clear_i(int *a)
MINLINE unsigned short highest_order_bit_s(unsigned short n)
MINLINE float uint_as_float(unsigned int i)
MINLINE int bitscan_forward_i(int a)
MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
MINLINE unsigned int bitscan_reverse_clear_uint(unsigned int *a)
MINLINE unsigned int highest_order_bit_uint(unsigned int n)
MINLINE int bitscan_reverse_i(int a)
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
static unsigned a[3]
Definition: RandGen.cpp:92