|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <glib.h> |
|
19 #ifdef __SYMBIAN32__ |
|
20 #include "mrt2_glib2_test.h" |
|
21 #endif /*__SYMBIAN32__*/ |
|
22 |
|
23 #if defined(__GNUC__) && (__GNUC__ >= 4) |
|
24 # define TEST_BUILTINS 1 |
|
25 #else |
|
26 # define TEST_BUILTINS 0 |
|
27 #endif |
|
28 |
|
29 #if TEST_BUILTINS |
|
30 static gint |
|
31 builtin_bit_nth_lsf1 (gulong mask, gint nth_bit) |
|
32 { |
|
33 if (nth_bit >= 0) |
|
34 { |
|
35 if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1)) |
|
36 mask &= -(1UL<<(nth_bit+1)); |
|
37 else |
|
38 mask = 0; |
|
39 } |
|
40 return __builtin_ffsl(mask) - 1; |
|
41 } |
|
42 |
|
43 static gint |
|
44 builtin_bit_nth_lsf2 (gulong mask, gint nth_bit) |
|
45 { |
|
46 if (nth_bit >= 0) |
|
47 { |
|
48 if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1)) |
|
49 mask &= -(1UL<<(nth_bit+1)); |
|
50 else |
|
51 mask = 0; |
|
52 } |
|
53 return mask ? __builtin_ctzl(mask) : -1; |
|
54 } |
|
55 |
|
56 static gint |
|
57 builtin_bit_nth_msf (gulong mask, gint nth_bit) |
|
58 { |
|
59 if (nth_bit >= 0 && nth_bit < GLIB_SIZEOF_LONG * 8) |
|
60 mask &= (1UL<<nth_bit)-1; |
|
61 return mask ? GLIB_SIZEOF_LONG * 8 - 1 - __builtin_clzl(mask) : -1; |
|
62 } |
|
63 |
|
64 |
|
65 static guint |
|
66 builtin_bit_storage (gulong number) |
|
67 { |
|
68 return number ? GLIB_SIZEOF_LONG * 8 - __builtin_clzl(number) : 1; |
|
69 } |
|
70 #endif |
|
71 |
|
72 |
|
73 static gint |
|
74 naive_bit_nth_lsf (gulong mask, gint nth_bit) |
|
75 { |
|
76 if (G_UNLIKELY (nth_bit < -1)) |
|
77 nth_bit = -1; |
|
78 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1)) |
|
79 { |
|
80 nth_bit++; |
|
81 if (mask & (1UL << nth_bit)) |
|
82 return nth_bit; |
|
83 } |
|
84 return -1; |
|
85 } |
|
86 |
|
87 static gint |
|
88 naive_bit_nth_msf (gulong mask, gint nth_bit) |
|
89 { |
|
90 if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8)) |
|
91 nth_bit = GLIB_SIZEOF_LONG * 8; |
|
92 while (nth_bit > 0) |
|
93 { |
|
94 nth_bit--; |
|
95 if (mask & (1UL << nth_bit)) |
|
96 return nth_bit; |
|
97 } |
|
98 return -1; |
|
99 } |
|
100 |
|
101 static guint |
|
102 naive_bit_storage (gulong number) |
|
103 { |
|
104 register guint n_bits = 0; |
|
105 |
|
106 do |
|
107 { |
|
108 n_bits++; |
|
109 number >>= 1; |
|
110 } |
|
111 while (number); |
|
112 return n_bits; |
|
113 } |
|
114 |
|
115 |
|
116 |
|
117 #define TEST(f1, f2, i) \ |
|
118 if (f1 (i) != f2 (i)) { \ |
|
119 g_error (G_STRINGIFY (f1) " (%lu) = %d; " \ |
|
120 G_STRINGIFY (f2) " (%lu) = %d; ", \ |
|
121 i, f1 (i), \ |
|
122 i, f2 (i)); \ |
|
123 return 1; \ |
|
124 } |
|
125 #define TEST2(f1, f2, i, n) \ |
|
126 if (f1 (i, n) != f2 (i, n)) { \ |
|
127 g_error (G_STRINGIFY (f1) " (%lu, %d) = %d; " \ |
|
128 G_STRINGIFY (f2) " (%lu, %d) = %d; ", \ |
|
129 i, n, f1 (i, n), \ |
|
130 i, n, f2 (i, n)); \ |
|
131 return 1; \ |
|
132 } |
|
133 |
|
134 int |
|
135 main (void) |
|
136 { |
|
137 gulong i; |
|
138 gint nth_bit; |
|
139 #ifdef __SYMBIAN32__ |
|
140 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
141 g_set_print_handler(mrtPrintHandler); |
|
142 #endif /*__SYMBIAN32__*/ |
|
143 |
|
144 |
|
145 /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */ |
|
146 for (i = 0; (glong)i < 1500 ; i = -(i+((glong)i>=0))) { |
|
147 |
|
148 #if TEST_BUILTINS |
|
149 TEST (naive_bit_storage, builtin_bit_storage, i); |
|
150 #endif |
|
151 TEST (naive_bit_storage, g_bit_storage, i); |
|
152 |
|
153 for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++) { |
|
154 |
|
155 #if TEST_BUILTINS |
|
156 TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf1, i, nth_bit); |
|
157 TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf2, i, nth_bit); |
|
158 #endif |
|
159 TEST2 (naive_bit_nth_lsf, g_bit_nth_lsf, i, nth_bit); |
|
160 |
|
161 #if TEST_BUILTINS |
|
162 TEST2 (naive_bit_nth_msf, builtin_bit_nth_msf, i, nth_bit); |
|
163 #endif |
|
164 TEST2 (naive_bit_nth_msf, g_bit_nth_msf, i, nth_bit); |
|
165 |
|
166 } |
|
167 } |
|
168 #ifdef __SYMBIAN32__ |
|
169 testResultXml("bit-test"); |
|
170 #endif /* EMULATOR */ |
|
171 |
|
172 return 0; |
|
173 } |