Blender  V3.3
math_bits_inline.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #ifndef __MATH_BITS_INLINE_C__
8 #define __MATH_BITS_INLINE_C__
9 
10 #ifdef _MSC_VER
11 # include <intrin.h>
12 #endif
13 
14 #include "BLI_math_bits.h"
15 
16 MINLINE unsigned int bitscan_forward_uint(unsigned int a)
17 {
18  BLI_assert(a != 0);
19 #ifdef _MSC_VER
20  unsigned long ctz;
21  _BitScanForward(&ctz, a);
22  return ctz;
23 #else
24  return (unsigned int)__builtin_ctz(a);
25 #endif
26 }
27 
28 MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
29 {
30  BLI_assert(a != 0);
31 #ifdef _MSC_VER
32  unsigned long ctz;
33  _BitScanForward64(&ctz, a);
34  return ctz;
35 #else
36  return (unsigned int)__builtin_ctzll(a);
37 #endif
38 }
39 
41 {
42  return (int)bitscan_forward_uint((unsigned int)a);
43 }
44 
45 MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a)
46 {
47  unsigned int i = bitscan_forward_uint(*a);
48  *a &= (*a) - 1;
49  return i;
50 }
51 
53 {
54  return (int)bitscan_forward_clear_uint((unsigned int *)a);
55 }
56 
57 MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
58 {
59  BLI_assert(a != 0);
60 #ifdef _MSC_VER
61  unsigned long clz;
62  _BitScanReverse(&clz, a);
63  return 31 - clz;
64 #else
65  return (unsigned int)__builtin_clz(a);
66 #endif
67 }
68 
69 MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
70 {
71  BLI_assert(a != 0);
72 #ifdef _MSC_VER
73  unsigned long clz;
74  _BitScanReverse64(&clz, a);
75  return 31 - clz;
76 #else
77  return (unsigned int)__builtin_clzll(a);
78 #endif
79 }
80 
82 {
83  return (int)bitscan_reverse_uint((unsigned int)a);
84 }
85 
86 MINLINE unsigned int bitscan_reverse_clear_uint(unsigned int *a)
87 {
88  unsigned int i = bitscan_reverse_uint(*a);
89  *a &= ~(0x80000000 >> i);
90  return i;
91 }
92 
94 {
95  return (int)bitscan_reverse_clear_uint((unsigned int *)a);
96 }
97 
98 MINLINE unsigned int highest_order_bit_uint(unsigned int n)
99 {
100  if (n == 0) {
101  return 0;
102  }
103  return 1 << (sizeof(unsigned int) * 8 - bitscan_reverse_uint(n));
104 }
105 
106 MINLINE unsigned short highest_order_bit_s(unsigned short n)
107 {
108  n |= (unsigned short)(n >> 1);
109  n |= (unsigned short)(n >> 2);
110  n |= (unsigned short)(n >> 4);
111  n |= (unsigned short)(n >> 8);
112  return (unsigned short)(n - (n >> 1));
113 }
114 
115 #ifndef __GNUC__
116 MINLINE int count_bits_i(unsigned int i)
117 {
118  /* variable-precision SWAR algorithm. */
119  i = i - ((i >> 1) & 0x55555555);
120  i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
121  return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
122 }
123 #endif
124 
125 MINLINE int float_as_int(float f)
126 {
127  union {
128  int i;
129  float f;
130  } u;
131  u.f = f;
132  return u.i;
133 }
134 
135 MINLINE unsigned int float_as_uint(float f)
136 {
137  union {
138  unsigned int i;
139  float f;
140  } u;
141  u.f = f;
142  return u.i;
143 }
144 
145 MINLINE float int_as_float(int i)
146 {
147  union {
148  int i;
149  float f;
150  } u;
151  u.i = i;
152  return u.f;
153 }
154 
155 MINLINE float uint_as_float(unsigned int i)
156 {
157  union {
158  unsigned int i;
159  float f;
160  } u;
161  u.i = i;
162  return u.f;
163 }
164 
165 MINLINE float xor_fl(float x, int y)
166 {
167  return int_as_float(float_as_int(x) ^ y);
168 }
169 
170 #endif /* __MATH_BITS_INLINE_C__ */
#define BLI_assert(a)
Definition: BLI_assert.h:46
#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:78