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