@@ -41,38 +41,26 @@ static inline int
4141pg_leftmost_one_pos32 (uint32 word )
4242{
4343#ifdef HAVE__BUILTIN_CLZ
44- int bitscan_result ;
44+ Assert (word != 0 );
45+
46+ return 31 - __builtin_clz (word );
4547#elif defined(_MSC_VER )
46- unsigned long bitscan_result ;
48+ unsigned long result ;
4749 bool non_zero ;
48- #endif
4950
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
5255 int shift = 32 - 8 ;
5356
5457 Assert (word != 0 );
5558
5659 while ((word >> shift ) == 0 )
5760 shift -= 8 ;
5861
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 */
7664}
7765
7866/*
@@ -83,45 +71,33 @@ static inline int
8371pg_leftmost_one_pos64 (uint64 word )
8472{
8573#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-
9674 Assert (word != 0 );
9775
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 )
10776#if defined(HAVE_LONG_INT_64 )
108- bitscan_result = 63 - __builtin_clzl (word );
77+ return 63 - __builtin_clzl (word );
10978#elif defined(HAVE_LONG_LONG_INT_64 )
110- bitscan_result = 63 - __builtin_clzll (word );
79+ return 63 - __builtin_clzll (word );
11180#else
11281#error must have a working 64-bit integer datatype
11382#endif /* HAVE_LONG_INT_64 */
11483
11584#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 ;
12187
88+ non_zero = _BitScanReverse64 (& result , word );
89+ Assert (non_zero );
90+ return (int ) result ;
12291#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 */
125101}
126102
127103/*
@@ -133,15 +109,17 @@ static inline int
133109pg_rightmost_one_pos32 (uint32 word )
134110{
135111#ifdef HAVE__BUILTIN_CTZ
136- const uint32 orig_word = word ;
137- int bitscan_result ;
112+ Assert (word != 0 );
113+
114+ return __builtin_ctz (word );
138115#elif defined(_MSC_VER )
139- const unsigned long orig_word = word ;
140- unsigned long bitscan_result ;
116+ unsigned long result ;
141117 bool non_zero ;
142- #endif
143118
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
145123 int result = 0 ;
146124
147125 Assert (word != 0 );
@@ -152,22 +130,8 @@ pg_rightmost_one_pos32(uint32 word)
152130 result += 8 ;
153131 }
154132 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
169133 return result ;
170- #endif /* HAVE_BITSCAN_FORWARD */
134+ #endif /* HAVE__BUILTIN_CTZ */
171135}
172136
173137/*
@@ -178,15 +142,24 @@ static inline int
178142pg_rightmost_one_pos64 (uint64 word )
179143{
180144#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+
183155#elif defined(_MSC_VER )
184- const unsigned __int64 orig_word = word ;
185- unsigned long bitscan_result ;
156+ unsigned long result ;
186157 bool non_zero ;
187- #endif
188158
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
190163 int result = 0 ;
191164
192165 Assert (word != 0 );
@@ -197,29 +170,8 @@ pg_rightmost_one_pos64(uint64 word)
197170 result += 8 ;
198171 }
199172 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
221173 return result ;
222- #endif /* HAVE_BITSCAN_FORWARD */
174+ #endif /* HAVE__BUILTIN_CTZ */
223175}
224176
225177/*
0 commit comments