@@ -41,38 +41,26 @@ static inline int
41
41
pg_leftmost_one_pos32 (uint32 word )
42
42
{
43
43
#ifdef HAVE__BUILTIN_CLZ
44
- int bitscan_result ;
44
+ Assert (word != 0 );
45
+
46
+ return 31 - __builtin_clz (word );
45
47
#elif defined(_MSC_VER )
46
- unsigned long bitscan_result ;
48
+ unsigned long result ;
47
49
bool non_zero ;
48
- #endif
49
50
50
- #if !defined(HAVE_BITSCAN_REVERSE ) || defined(USE_ASSERT_CHECKING )
51
- int result ;
51
+ non_zero = _BitScanReverse (& result , word );
52
+ Assert (non_zero );
53
+ return (int ) result ;
54
+ #else
52
55
int shift = 32 - 8 ;
53
56
54
57
Assert (word != 0 );
55
58
56
59
while ((word >> shift ) == 0 )
57
60
shift -= 8 ;
58
61
59
- result = shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
60
- #endif
61
-
62
- #ifdef HAVE_BITSCAN_REVERSE
63
-
64
- #if defined(HAVE__BUILTIN_CLZ )
65
- bitscan_result = 31 - __builtin_clz (word );
66
- #elif defined(_MSC_VER )
67
- non_zero = _BitScanReverse (& bitscan_result , word );
68
- Assert (non_zero );
69
- #endif
70
- Assert (bitscan_result == result );
71
- return bitscan_result ;
72
-
73
- #else
74
- return result ;
75
- #endif /* HAVE_BITSCAN_REVERSE */
62
+ return shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
63
+ #endif /* HAVE__BUILTIN_CLZ */
76
64
}
77
65
78
66
/*
@@ -83,45 +71,33 @@ static inline int
83
71
pg_leftmost_one_pos64 (uint64 word )
84
72
{
85
73
#ifdef HAVE__BUILTIN_CLZ
86
- int bitscan_result ;
87
- #elif defined(_MSC_VER )
88
- unsigned long bitscan_result ;
89
- bool non_zero ;
90
- #endif
91
-
92
- #if !defined(HAVE_BITSCAN_REVERSE ) || defined(USE_ASSERT_CHECKING )
93
- int result ;
94
- int shift = 64 - 8 ;
95
-
96
74
Assert (word != 0 );
97
75
98
- while ((word >> shift ) == 0 )
99
- shift -= 8 ;
100
-
101
- result = shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
102
- #endif
103
-
104
- #ifdef HAVE_BITSCAN_REVERSE
105
-
106
- #if defined(HAVE__BUILTIN_CLZ )
107
76
#if defined(HAVE_LONG_INT_64 )
108
- bitscan_result = 63 - __builtin_clzl (word );
77
+ return 63 - __builtin_clzl (word );
109
78
#elif defined(HAVE_LONG_LONG_INT_64 )
110
- bitscan_result = 63 - __builtin_clzll (word );
79
+ return 63 - __builtin_clzll (word );
111
80
#else
112
81
#error must have a working 64-bit integer datatype
113
82
#endif /* HAVE_LONG_INT_64 */
114
83
115
84
#elif defined(_MSC_VER )
116
- non_zero = _BitScanReverse64 (& bitscan_result , word );
117
- Assert (non_zero );
118
- #endif /* HAVE__BUILTIN_CLZ */
119
- Assert (bitscan_result == result );
120
- return bitscan_result ;
85
+ unsigned long result ;
86
+ bool non_zero ;
121
87
88
+ non_zero = _BitScanReverse64 (& result , word );
89
+ Assert (non_zero );
90
+ return (int ) result ;
122
91
#else
123
- return result ;
124
- #endif /* HAVE_BITSCAN_REVERSE */
92
+ int shift = 64 - 8 ;
93
+
94
+ Assert (word != 0 );
95
+
96
+ while ((word >> shift ) == 0 )
97
+ shift -= 8 ;
98
+
99
+ return shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
100
+ #endif /* HAVE__BUILTIN_CLZ */
125
101
}
126
102
127
103
/*
@@ -133,15 +109,17 @@ static inline int
133
109
pg_rightmost_one_pos32 (uint32 word )
134
110
{
135
111
#ifdef HAVE__BUILTIN_CTZ
136
- const uint32 orig_word = word ;
137
- int bitscan_result ;
112
+ Assert (word != 0 );
113
+
114
+ return __builtin_ctz (word );
138
115
#elif defined(_MSC_VER )
139
- const unsigned long orig_word = word ;
140
- unsigned long bitscan_result ;
116
+ unsigned long result ;
141
117
bool non_zero ;
142
- #endif
143
118
144
- #if !defined(HAVE_BITSCAN_FORWARD ) || defined(USE_ASSERT_CHECKING )
119
+ non_zero = _BitScanForward (& result , word );
120
+ Assert (non_zero );
121
+ return (int ) result ;
122
+ #else
145
123
int result = 0 ;
146
124
147
125
Assert (word != 0 );
@@ -152,22 +130,8 @@ pg_rightmost_one_pos32(uint32 word)
152
130
result += 8 ;
153
131
}
154
132
result += pg_rightmost_one_pos [word & 255 ];
155
- #endif
156
-
157
- #ifdef HAVE_BITSCAN_FORWARD
158
-
159
- #if defined(HAVE__BUILTIN_CTZ )
160
- bitscan_result = __builtin_ctz (orig_word );
161
- #elif defined(_MSC_VER )
162
- non_zero = _BitScanForward (& bitscan_result , orig_word );
163
- Assert (non_zero );
164
- #endif
165
- Assert (bitscan_result == result );
166
- return bitscan_result ;
167
-
168
- #else
169
133
return result ;
170
- #endif /* HAVE_BITSCAN_FORWARD */
134
+ #endif /* HAVE__BUILTIN_CTZ */
171
135
}
172
136
173
137
/*
@@ -178,15 +142,24 @@ static inline int
178
142
pg_rightmost_one_pos64 (uint64 word )
179
143
{
180
144
#ifdef HAVE__BUILTIN_CTZ
181
- const uint64 orig_word = word ;
182
- int bitscan_result ;
145
+ Assert (word != 0 );
146
+
147
+ #if defined(HAVE_LONG_INT_64 )
148
+ return __builtin_ctzl (word );
149
+ #elif defined(HAVE_LONG_LONG_INT_64 )
150
+ return __builtin_ctzll (word );
151
+ #else
152
+ #error must have a working 64-bit integer datatype
153
+ #endif /* HAVE_LONG_INT_64 */
154
+
183
155
#elif defined(_MSC_VER )
184
- const unsigned __int64 orig_word = word ;
185
- unsigned long bitscan_result ;
156
+ unsigned long result ;
186
157
bool non_zero ;
187
- #endif
188
158
189
- #if !defined(HAVE_BITSCAN_FORWARD ) || defined(USE_ASSERT_CHECKING )
159
+ non_zero = _BitScanForward64 (& result , word );
160
+ Assert (non_zero );
161
+ return (int ) result ;
162
+ #else
190
163
int result = 0 ;
191
164
192
165
Assert (word != 0 );
@@ -197,29 +170,8 @@ pg_rightmost_one_pos64(uint64 word)
197
170
result += 8 ;
198
171
}
199
172
result += pg_rightmost_one_pos [word & 255 ];
200
- #endif
201
-
202
- #ifdef HAVE_BITSCAN_FORWARD
203
-
204
- #if defined(HAVE__BUILTIN_CTZ )
205
- #if defined(HAVE_LONG_INT_64 )
206
- bitscan_result = __builtin_ctzl (orig_word );
207
- #elif defined(HAVE_LONG_LONG_INT_64 )
208
- bitscan_result = __builtin_ctzll (orig_word );
209
- #else
210
- #error must have a working 64-bit integer datatype
211
- #endif /* HAVE_LONG_INT_64 */
212
-
213
- #elif defined(_MSC_VER )
214
- non_zero = _BitScanForward64 (& bitscan_result , orig_word );
215
- Assert (non_zero );
216
- #endif /* HAVE__BUILTIN_CTZ */
217
- Assert (bitscan_result == result );
218
- return bitscan_result ;
219
-
220
- #else
221
173
return result ;
222
- #endif /* HAVE_BITSCAN_FORWARD */
174
+ #endif /* HAVE__BUILTIN_CTZ */
223
175
}
224
176
225
177
/*
0 commit comments