glib/tests/bit-test.c
branchRCL_3
changeset 56 acd3cd4aaceb
equal deleted inserted replaced
54:4332f0f7be53 56:acd3cd4aaceb
       
     1 /*
       
     2 * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     3 */
       
     4 #include <glib.h>
       
     5 #ifdef __SYMBIAN32__
       
     6 #include "mrt2_glib2_test.h"
       
     7 #endif /*__SYMBIAN32__*/
       
     8 
       
     9 #if defined(__GNUC__) && (__GNUC__ >= 4)
       
    10 # define TEST_BUILTINS 1
       
    11 #else
       
    12 # define TEST_BUILTINS 0
       
    13 #endif
       
    14 
       
    15 #if TEST_BUILTINS
       
    16 static gint
       
    17 builtin_bit_nth_lsf1 (gulong mask, gint nth_bit)
       
    18 {
       
    19   if (nth_bit >= 0)
       
    20     {
       
    21       if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
       
    22 	mask &= -(1UL<<(nth_bit+1));
       
    23       else
       
    24 	mask = 0;
       
    25     }
       
    26   return __builtin_ffsl(mask) - 1;
       
    27 }
       
    28 
       
    29 static gint
       
    30 builtin_bit_nth_lsf2 (gulong mask, gint nth_bit)
       
    31 {
       
    32   if (nth_bit >= 0)
       
    33     {
       
    34       if (G_LIKELY (nth_bit < GLIB_SIZEOF_LONG * 8 - 1))
       
    35 	mask &= -(1UL<<(nth_bit+1));
       
    36       else
       
    37 	mask = 0;
       
    38     }
       
    39   return mask ? __builtin_ctzl(mask) : -1;
       
    40 }
       
    41 
       
    42 static gint
       
    43 builtin_bit_nth_msf (gulong mask, gint nth_bit)
       
    44 {
       
    45   if (nth_bit >= 0 && nth_bit < GLIB_SIZEOF_LONG * 8)
       
    46     mask &= (1UL<<nth_bit)-1;
       
    47   return mask ? GLIB_SIZEOF_LONG * 8 - 1 - __builtin_clzl(mask) : -1;
       
    48 }
       
    49 
       
    50 
       
    51 static guint
       
    52 builtin_bit_storage (gulong number)
       
    53 {
       
    54   return number ? GLIB_SIZEOF_LONG * 8 - __builtin_clzl(number) : 1;
       
    55 }
       
    56 #endif
       
    57 
       
    58 
       
    59 static gint
       
    60 naive_bit_nth_lsf (gulong mask, gint   nth_bit)
       
    61 {
       
    62   if (G_UNLIKELY (nth_bit < -1))
       
    63     nth_bit = -1;
       
    64   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
       
    65     {
       
    66       nth_bit++;
       
    67       if (mask & (1UL << nth_bit))
       
    68 	return nth_bit;
       
    69     }
       
    70   return -1;
       
    71 }
       
    72 
       
    73 static gint
       
    74 naive_bit_nth_msf (gulong mask, gint   nth_bit)
       
    75 {
       
    76   if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
       
    77     nth_bit = GLIB_SIZEOF_LONG * 8;
       
    78   while (nth_bit > 0)
       
    79     {
       
    80       nth_bit--;
       
    81       if (mask & (1UL << nth_bit))
       
    82 	return nth_bit;
       
    83     }
       
    84   return -1;
       
    85 }
       
    86 
       
    87 static guint
       
    88 naive_bit_storage (gulong number)
       
    89 {
       
    90   register guint n_bits = 0;
       
    91   
       
    92   do
       
    93     {
       
    94       n_bits++;
       
    95       number >>= 1;
       
    96     }
       
    97   while (number);
       
    98   return n_bits;
       
    99 }
       
   100 
       
   101 
       
   102 
       
   103 #define TEST(f1, f2, i) \
       
   104 	if (f1 (i) != f2 (i)) { \
       
   105 		g_error (G_STRINGIFY (f1) " (%lu) = %d; " \
       
   106 			 G_STRINGIFY (f2) " (%lu) = %d; ", \
       
   107 			 i, f1 (i), \
       
   108 			 i, f2 (i)); \
       
   109 		return 1; \
       
   110 	}
       
   111 #define TEST2(f1, f2, i, n) \
       
   112 	if (f1 (i, n) != f2 (i, n)) { \
       
   113 		g_error (G_STRINGIFY (f1) " (%lu, %d) = %d; " \
       
   114 			 G_STRINGIFY (f2) " (%lu, %d) = %d; ", \
       
   115 			 i, n, f1 (i, n), \
       
   116 			 i, n, f2 (i, n)); \
       
   117 		return 1; \
       
   118 	}
       
   119 
       
   120 int
       
   121 main (void)
       
   122 {
       
   123   gulong i;
       
   124   gint nth_bit;
       
   125   #ifdef __SYMBIAN32__
       
   126   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);
       
   127   g_set_print_handler(mrtPrintHandler);
       
   128   #endif /*__SYMBIAN32__*/
       
   129 
       
   130 
       
   131   /* we loop like this: 0, -1, 1, -2, 2, -3, 3, ... */
       
   132   for (i = 0; (glong)i < 1500 ; i = -(i+((glong)i>=0))) {
       
   133 
       
   134 #if TEST_BUILTINS
       
   135     TEST (naive_bit_storage, builtin_bit_storage, i);
       
   136 #endif
       
   137     TEST (naive_bit_storage, g_bit_storage, i);
       
   138 
       
   139     for (nth_bit = -3; nth_bit <= 2 + GLIB_SIZEOF_LONG * 8; nth_bit++) {
       
   140 
       
   141 #if TEST_BUILTINS
       
   142       TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf1, i, nth_bit);
       
   143       TEST2 (naive_bit_nth_lsf, builtin_bit_nth_lsf2, i, nth_bit);
       
   144 #endif
       
   145       TEST2 (naive_bit_nth_lsf, g_bit_nth_lsf, i, nth_bit);
       
   146 
       
   147 #if TEST_BUILTINS
       
   148       TEST2 (naive_bit_nth_msf, builtin_bit_nth_msf, i, nth_bit);
       
   149 #endif
       
   150       TEST2 (naive_bit_nth_msf, g_bit_nth_msf, i, nth_bit);
       
   151 
       
   152     }
       
   153   }
       
   154   #ifdef __SYMBIAN32__
       
   155   testResultXml("bit-test");
       
   156   #endif /* EMULATOR */
       
   157   
       
   158   return 0;
       
   159 }