1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/bit-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,159 @@
1.4 +/*
1.5 +* Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
1.6 +*/
1.7 +#include <glib.h>
1.8 +#ifdef __SYMBIAN32__
1.9 +#include "mrt2_glib2_test.h"
1.10 +#endif /*__SYMBIAN32__*/
1.11 +
1.12 +#if defined(__GNUC__) && (__GNUC__ >= 4)
1.13 +# define TEST_BUILTINS 1
1.14 +#else
1.15 +# define TEST_BUILTINS 0
1.16 +#endif
1.17 +
1.18 +#if TEST_BUILTINS
1.19 +static gint
1.20 +builtin_bit_nth_lsf1 (gulong mask, gint nth_bit)
1.21 +{
1.22 + if (nth_bit >= 0)
1.23 + {
1.24 + if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
1.25 + mask &= -(1UL<<(nth_bit+1));
1.26 + else
1.27 + mask = 0;
1.28 + }
1.29 + return __builtin_ffsl(mask) - 1;
1.30 +}
1.31 +
1.32 +static gint
1.33 +builtin_bit_nth_lsf2 (gulong mask, gint nth_bit)
1.34 +{
1.35 + if (nth_bit >= 0)
1.36 + {
1.37 + if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
1.38 + mask &= -(1UL<<(nth_bit+1));
1.39 + else
1.40 + mask = 0;
1.41 + }
1.42 + return mask ? __builtin_ctzl(mask) : -1;
1.43 +}
1.44 +
1.45 +static gint
1.46 +builtin_bit_nth_msf (gulong mask, gint nth_bit)
1.47 +{
1.48 + if (nth_bit >= 0 && nth_bit < GLIB_SIZEOF_LONG * 8)
1.49 + mask &= (1UL<<nth_bit)-1;
1.50 + return mask ? GLIB_SIZEOF_LONG * 8 - 1 - __builtin_clzl(mask) : -1;
1.51 +}
1.52 +
1.53 +
1.54 +static guint
1.55 +builtin_bit_storage (gulong number)
1.56 +{
1.57 + return number ? GLIB_SIZEOF_LONG * 8 - __builtin_clzl(number) : 1;
1.58 +}
1.59 +#endif
1.60 +
1.61 +
1.62 +static gint
1.63 +naive_bit_nth_lsf (gulong mask, gint nth_bit)
1.64 +{
1.65 + if (G_UNLIKELY (nth_bit < -1))
1.66 + nth_bit = -1;
1.67 + while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
1.68 + {
1.69 + nth_bit++;
1.70 + if (mask & (1UL << nth_bit))
1.71 + return nth_bit;
1.72 + }
1.73 + return -1;
1.74 +}
1.75 +
1.76 +static gint
1.77 +naive_bit_nth_msf (gulong mask, gint nth_bit)
1.78 +{
1.79 + if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
1.80 + nth_bit = GLIB_SIZEOF_LONG * 8;
1.81 + while (nth_bit > 0)
1.82 + {
1.83 + nth_bit--;
1.84 + if (mask & (1UL << nth_bit))
1.85 + return nth_bit;
1.86 + }
1.87 + return -1;
1.88 +}
1.89 +
1.90 +static guint
1.91 +naive_bit_storage (gulong number)
1.92 +{
1.93 + register guint n_bits = 0;
1.94 +
1.95 + do
1.96 + {
1.97 + n_bits++;
1.98 + number >>= 1;
1.99 + }
1.100 + while (number);
1.101 + return n_bits;
1.102 +}
1.103 +
1.104 +
1.105 +
1.106 +#define TEST(f1, f2, i) \
1.107 + if (f1 (i) != f2 (i)) { \
1.108 + g_error (G_STRINGIFY (f1) " (%lu) = %d; " \
1.109 + G_STRINGIFY (f2) " (%lu) = %d; ", \
1.110 + i, f1 (i), \
1.111 + i, f2 (i)); \
1.112 + return 1; \
1.113 + }
1.114 +#define TEST2(f1, f2, i, n) \
1.115 + if (f1 (i, n) != f2 (i, n)) { \
1.116 + g_error (G_STRINGIFY (f1) " (%lu, %d) = %d; " \
1.117 + G_STRINGIFY (f2) " (%lu, %d) = %d; ", \
1.118 + i, n, f1 (i, n), \
1.119 + i, n, f2 (i, n)); \
1.120 + return 1; \
1.121 + }
1.122 +
1.123 +int
1.124 +main (void)
1.125 +{
1.126 + gulong i;
1.127 + gint nth_bit;
1.128 + #ifdef __SYMBIAN32__
1.129 + 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);
1.130 + g_set_print_handler(mrtPrintHandler);
1.131 + #endif /*__SYMBIAN32__*/
1.132 +
1.133 +
1.134 + /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */
1.135 + for (i = 0; (glong)i < 1500 ; i = -(i+((glong)i>=0))) {
1.136 +
1.137 +#if TEST_BUILTINS
1.138 + TEST (naive_bit_storage, builtin_bit_storage, i);
1.139 +#endif
1.140 + TEST (naive_bit_storage, g_bit_storage, i);
1.141 +
1.142 + for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++) {
1.143 +
1.144 +#if TEST_BUILTINS
1.145 + TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf1, i, nth_bit);
1.146 + TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf2, i, nth_bit);
1.147 +#endif
1.148 + TEST2 (naive_bit_nth_lsf, g_bit_nth_lsf, i, nth_bit);
1.149 +
1.150 +#if TEST_BUILTINS
1.151 + TEST2 (naive_bit_nth_msf, builtin_bit_nth_msf, i, nth_bit);
1.152 +#endif
1.153 + TEST2 (naive_bit_nth_msf, g_bit_nth_msf, i, nth_bit);
1.154 +
1.155 + }
1.156 + }
1.157 + #ifdef __SYMBIAN32__
1.158 + testResultXml("bit-test");
1.159 + #endif /* EMULATOR */
1.160 +
1.161 + return 0;
1.162 +}