@@ -26,18 +26,27 @@ static inline int
26
26
pg_leftmost_one_pos32 (uint32 word )
27
27
{
28
28
#ifdef HAVE__BUILTIN_CLZ
29
- Assert (word != 0 );
29
+ int bitscan_result ;
30
+ #endif
30
31
31
- return 31 - __builtin_clz ( word );
32
- #else
32
+ #if !defined( HAVE__BUILTIN_CLZ ) || defined( USE_ASSERT_CHECKING )
33
+ int result ;
33
34
int shift = 32 - 8 ;
34
35
35
36
Assert (word != 0 );
36
37
37
38
while ((word >> shift ) == 0 )
38
39
shift -= 8 ;
39
40
40
- return shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
41
+ result = shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
42
+ #endif
43
+
44
+ #if defined(HAVE__BUILTIN_CLZ )
45
+ bitscan_result = 31 - __builtin_clz (word );
46
+ Assert (bitscan_result == result );
47
+ return bitscan_result ;
48
+ #else
49
+ return result ;
41
50
#endif /* HAVE__BUILTIN_CLZ */
42
51
}
43
52
@@ -49,24 +58,33 @@ static inline int
49
58
pg_leftmost_one_pos64 (uint64 word )
50
59
{
51
60
#ifdef HAVE__BUILTIN_CLZ
52
- Assert (word != 0 );
53
-
54
- #if defined(HAVE_LONG_INT_64 )
55
- return 63 - __builtin_clzl (word );
56
- #elif defined(HAVE_LONG_LONG_INT_64 )
57
- return 63 - __builtin_clzll (word );
58
- #else
59
- #error must have a working 64-bit integer datatype
61
+ int bitscan_result ;
60
62
#endif
61
- #else /* !HAVE__BUILTIN_CLZ */
63
+
64
+ #if !defined(HAVE__BUILTIN_CLZ ) || defined(USE_ASSERT_CHECKING )
65
+ int result ;
62
66
int shift = 64 - 8 ;
63
67
64
68
Assert (word != 0 );
65
69
66
70
while ((word >> shift ) == 0 )
67
71
shift -= 8 ;
68
72
69
- return shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
73
+ result = shift + pg_leftmost_one_pos [(word >> shift ) & 255 ];
74
+ #endif
75
+
76
+ #if defined(HAVE__BUILTIN_CLZ )
77
+ #if defined(HAVE_LONG_INT_64 )
78
+ bitscan_result = 63 - __builtin_clzl (word );
79
+ #elif defined(HAVE_LONG_LONG_INT_64 )
80
+ bitscan_result = 63 - __builtin_clzll (word );
81
+ #else
82
+ #error must have a working 64-bit integer datatype
83
+ #endif /* HAVE_LONG_INT_64 */
84
+ Assert (bitscan_result == result );
85
+ return bitscan_result ;
86
+ #else
87
+ return result ;
70
88
#endif /* HAVE__BUILTIN_CLZ */
71
89
}
72
90
@@ -79,10 +97,11 @@ static inline int
79
97
pg_rightmost_one_pos32 (uint32 word )
80
98
{
81
99
#ifdef HAVE__BUILTIN_CTZ
82
- Assert (word != 0 );
100
+ const uint32 orig_word = word ;
101
+ int bitscan_result ;
102
+ #endif
83
103
84
- return __builtin_ctz (word );
85
- #else
104
+ #if !defined(HAVE__BUILTIN_CTZ ) || defined(USE_ASSERT_CHECKING )
86
105
int result = 0 ;
87
106
88
107
Assert (word != 0 );
@@ -93,6 +112,13 @@ pg_rightmost_one_pos32(uint32 word)
93
112
result += 8 ;
94
113
}
95
114
result += pg_rightmost_one_pos [word & 255 ];
115
+ #endif
116
+
117
+ #if defined(HAVE__BUILTIN_CTZ )
118
+ bitscan_result = __builtin_ctz (orig_word );
119
+ Assert (bitscan_result == result );
120
+ return bitscan_result ;
121
+ #else
96
122
return result ;
97
123
#endif /* HAVE__BUILTIN_CTZ */
98
124
}
@@ -105,16 +131,11 @@ static inline int
105
131
pg_rightmost_one_pos64 (uint64 word )
106
132
{
107
133
#ifdef HAVE__BUILTIN_CTZ
108
- Assert (word != 0 );
109
-
110
- #if defined(HAVE_LONG_INT_64 )
111
- return __builtin_ctzl (word );
112
- #elif defined(HAVE_LONG_LONG_INT_64 )
113
- return __builtin_ctzll (word );
114
- #else
115
- #error must have a working 64-bit integer datatype
134
+ const uint64 orig_word = word ;
135
+ int bitscan_result ;
116
136
#endif
117
- #else /* !HAVE__BUILTIN_CTZ */
137
+
138
+ #if !defined(HAVE__BUILTIN_CTZ ) || defined(USE_ASSERT_CHECKING )
118
139
int result = 0 ;
119
140
120
141
Assert (word != 0 );
@@ -125,6 +146,19 @@ pg_rightmost_one_pos64(uint64 word)
125
146
result += 8 ;
126
147
}
127
148
result += pg_rightmost_one_pos [word & 255 ];
149
+ #endif
150
+
151
+ #if defined(HAVE__BUILTIN_CTZ )
152
+ #if defined(HAVE_LONG_INT_64 )
153
+ bitscan_result = __builtin_ctzl (orig_word );
154
+ #elif defined(HAVE_LONG_LONG_INT_64 )
155
+ bitscan_result = __builtin_ctzll (orig_word );
156
+ #else
157
+ #error must have a working 64-bit integer datatype
158
+ #endif /* HAVE_LONG_INT_64 */
159
+ Assert (bitscan_result == result );
160
+ return bitscan_result ;
161
+ #else
128
162
return result ;
129
163
#endif /* HAVE__BUILTIN_CTZ */
130
164
}
0 commit comments