3 #if defined(__GNUC__) && (__GNUC__ >= 4)
4 # define TEST_BUILTINS 1
6 # define TEST_BUILTINS 0
11 builtin_bit_nth_lsf1 (gulong mask
, gint nth_bit
)
15 if (G_LIKELY (nth_bit
< GLIB_SIZEOF_LONG
* 8 - 1))
16 mask
&= -(1UL<<(nth_bit
+1));
20 return __builtin_ffsl(mask
) - 1;
24 builtin_bit_nth_lsf2 (gulong mask
, gint nth_bit
)
28 if (G_LIKELY (nth_bit
< GLIB_SIZEOF_LONG
* 8 - 1))
29 mask
&= -(1UL<<(nth_bit
+1));
33 return mask
? __builtin_ctzl(mask
) : -1;
37 builtin_bit_nth_msf (gulong mask
, gint nth_bit
)
39 if (nth_bit
>= 0 && nth_bit
< GLIB_SIZEOF_LONG
* 8)
40 mask
&= (1UL<<nth_bit
)-1;
41 return mask
? GLIB_SIZEOF_LONG
* 8 - 1 - __builtin_clzl(mask
) : -1;
46 builtin_bit_storage (gulong number
)
48 return number
? GLIB_SIZEOF_LONG
* 8 - __builtin_clzl(number
) : 1;
54 naive_bit_nth_lsf (gulong mask
, gint nth_bit
)
56 if (G_UNLIKELY (nth_bit
< -1))
58 while (nth_bit
< ((GLIB_SIZEOF_LONG
* 8) - 1))
61 if (mask
& (1UL << nth_bit
))
68 naive_bit_nth_msf (gulong mask
, gint nth_bit
)
70 if (nth_bit
< 0 || G_UNLIKELY (nth_bit
> GLIB_SIZEOF_LONG
* 8))
71 nth_bit
= GLIB_SIZEOF_LONG
* 8;
75 if (mask
& (1UL << nth_bit
))
82 naive_bit_storage (gulong number
)
97 #define TEST(f1, f2, i) \
98 if (f1 (i) != f2 (i)) { \
99 g_error (G_STRINGIFY (f1) " (%lu) = %d; " \
100 G_STRINGIFY (f2) " (%lu) = %d; ", \
105 #define TEST2(f1, f2, i, n) \
106 if (f1 (i, n) != f2 (i, n)) { \
107 g_error (G_STRINGIFY (f1) " (%lu, %d) = %d; " \
108 G_STRINGIFY (f2) " (%lu, %d) = %d; ", \
120 /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */
121 for (i
= 0; (glong
)i
< 1500 ; i
= -(i
+((glong
)i
>=0))) {
124 TEST (naive_bit_storage
, builtin_bit_storage
, i
);
126 TEST (naive_bit_storage
, g_bit_storage
, i
);
128 for (nth_bit
= -3; nth_bit
<= 2 + GLIB_SIZEOF_LONG
* 8; nth_bit
++) {
131 TEST2 (naive_bit_nth_lsf
, builtin_bit_nth_lsf1
, i
, nth_bit
);
132 TEST2 (naive_bit_nth_lsf
, builtin_bit_nth_lsf2
, i
, nth_bit
);
134 TEST2 (naive_bit_nth_lsf
, g_bit_nth_lsf
, i
, nth_bit
);
137 TEST2 (naive_bit_nth_msf
, builtin_bit_nth_msf
, i
, nth_bit
);
139 TEST2 (naive_bit_nth_msf
, g_bit_nth_msf
, i
, nth_bit
);