summaryrefslogtreecommitdiff
path: root/lib/integer_length.c
blob: f1fb48b989336b18f2fb34b5c9f634f76e92c031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* integer_length - find most significant bit in an 'unsigned int'.
   Copyright (C) 2011-2022 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2011.  */

#include <config.h>

/* Specification.  */
#include "integer_length.h"

#include <limits.h>

#include "float+.h"

#if defined _MSC_VER && !(__clang_major__ >= 4)
# include <intrin.h>
#endif

#define NBITS (sizeof (unsigned int) * CHAR_BIT)

int
integer_length (unsigned int x)
{
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__clang_major__ >= 4)
  if (x == 0)
    return 0;
  else
    return NBITS - __builtin_clz (x);
#elif defined _MSC_VER
  /* _BitScanReverse
     <https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64> */
  unsigned long bit;
  if (_BitScanReverse (&bit, x))
    return bit + 1;
  else
    return 0;
#else
# if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
  if (NBITS <= DBL_MANT_BIT)
    {
      /* Use 'double' operations.
         Assumes an IEEE 754 'double' implementation.  */
#  define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
#  define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
#  define NWORDS \
    ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
      typedef union { double value; unsigned int word[NWORDS]; }
              memory_double;

      if (x == 0)
        return 0;
      else
        {
          memory_double m;
          unsigned int exponent;

          if (1)
            {
              /* Use a single integer to floating-point conversion.  */
              m.value = x;
            }
          else
            {
              /* Use a single floating-point subtraction.  */
              /* 2^(DBL_MANT_DIG-1).  */
              static const double TWO_DBL_MANT_DIG =
                /* Assume DBL_MANT_DIG <= 5 * 31.
                   Use the identity
                   n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
                (double) (1U << ((DBL_MANT_DIG - 1) / 5))
                * (double) (1U << ((DBL_MANT_DIG - 1 + 1) / 5))
                * (double) (1U << ((DBL_MANT_DIG - 1 + 2) / 5))
                * (double) (1U << ((DBL_MANT_DIG - 1 + 3) / 5))
                * (double) (1U << ((DBL_MANT_DIG - 1 + 4) / 5));

              /* Construct 2^(DBL_MANT_DIG-1) + x by hand.  */
              m.word[DBL_EXPBIT0_WORD] =
                (DBL_MANT_DIG + DBL_EXP_BIAS) << DBL_EXPBIT0_BIT;
              m.word[1 - DBL_EXPBIT0_WORD] = x;

              /* Subtract 2^(DBL_MANT_DIG-1).  */
              m.value = m.value - TWO_DBL_MANT_DIG;
            }

          exponent =
            (m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT) & DBL_EXP_MASK;
          return exponent - DBL_EXP_BIAS;
        }
    }
  else
# endif
    if (NBITS == 32)
      {
        /* 6 comparisons.  */
        if (x != 0)
          {
            int result = 1;
            if (x >= 0x10000)
              {
                x = x >> 16;
                result += 16;
              }
            if (x >= 0x100)
              {
                x = x >> 8;
                result += 8;
              }
            if (x >= 0x10)
              {
                x = x >> 4;
                result += 4;
              }
            if (x >= 0x4)
              {
                x = x >> 2;
                result += 2;
              }
            if (x >= 0x2)
              {
                x = x >> 1;
                result += 1;
              }
            return result;
          }
        else
          return 0;
      }
    else
      {
        /* Naive loop.
           Works for any value of NBITS.  */
        int j;

        for (j = NBITS - 1; j >= 0; j--)
          if (x & (1U << j))
            return j + 1;
        return 0;
      }
#endif
}