summaryrefslogtreecommitdiff
path: root/internal/bits.h
blob: 2530bd89bc3bb513a0e8054db98804cb81589074 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#ifndef INTERNAL_BITS_H /* -*- C -*- */
#define INTERNAL_BITS_H
/**
 * @file
 * @brief      Internal header for bitwise integer algorithms.
 * @author     \@shyouhei
 * @copyright  This  file  is   a  part  of  the   programming  language  Ruby.
 *             Permission  is hereby  granted,  to  either redistribute  and/or
 *             modify this file, provided that  the conditions mentioned in the
 *             file COPYING are met.  Consult the file for details.
 * @see        Henry S. Warren Jr., "Hacker's Delight" (2nd ed.), 2013.
 * @see        SEI CERT C Coding Standard  INT32-C.  "Ensure that operations on
 *             signed integers do not result in overflow"
 * @see        https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
 * @see        https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateleft
 * @see        https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateright
 * @see        https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort
 * @see        https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64
 * @see        https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64
 * @see        https://docs.microsoft.com/en-us/cpp/intrinsics/lzcnt16-lzcnt-lzcnt64
 * @see        https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64
 * @see        https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_lzcnt_u32
 * @see        https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_tzcnt_u32
 */
#include "ruby/config.h"
#include <limits.h>             /* for CHAR_BITS */
#include <stdint.h>             /* for uintptr_t */

#ifdef _MSC_VER
# include <stdlib.h>            /* for _byteswap_uint64 */
#endif

#if defined(__x86_64__) && defined(__LZCNT__) && ! defined(MJIT_HEADER)
# /* Rule out MJIT_HEADER, which does not interface well with <immintrin.h> */
# include <immintrin.h>         /* for _lzcnt_u64 */
#endif

#if defined(_MSC_VER) && defined(_WIN64)
# include <intrin.h>            /* for the following intrinsics */
# pragma intrinsic(_BitScanForward)
# pragma intrinsic(_BitScanForward64)
# pragma intrinsic(_BitScanReverse)
# pragma intrinsic(_BitScanReverse64)
#endif

#include "ruby/ruby.h"              /* for VALUE */
#include "internal/compilers.h"     /* for __has_builtin */
#include "internal/static_assert.h" /* for STATIC_ASSERT */

/* The most significant bit of the lower part of half-long integer.
 * If sizeof(long) == 4, this is 0x8000.
 * If sizeof(long) == 8, this is 0x80000000.
 */
#define HALF_LONG_MSB ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))

#define SIGNED_INTEGER_TYPE_P(T) (0 > ((T)0)-1)

#define SIGNED_INTEGER_MIN(T)                           \
    ((sizeof(T) == sizeof(int8_t))  ? ((T)INT8_MIN)  :  \
    ((sizeof(T) == sizeof(int16_t)) ? ((T)INT16_MIN) :  \
    ((sizeof(T) == sizeof(int32_t)) ? ((T)INT32_MIN) :  \
    ((sizeof(T) == sizeof(int64_t)) ? ((T)INT64_MIN) :  \
     0))))

#define SIGNED_INTEGER_MAX(T) ((T)(SIGNED_INTEGER_MIN(T) ^ ((T)~(T)0)))

#define UNSIGNED_INTEGER_MAX(T) ((T)~(T)0)

#if __has_builtin(__builtin_mul_overflow_p)
# define MUL_OVERFLOW_P(a, b) \
    __builtin_mul_overflow_p((a), (b), (__typeof__(a * b))0)
#elif __has_builtin(__builtin_mul_overflow)
# define MUL_OVERFLOW_P(a, b) \
    __extension__ ({ __typeof__(a) c; __builtin_mul_overflow((a), (b), &c); })
#endif

#define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
    (a) == 0 ? 0 : \
    (a) == -1 ? (b) < -(max) : \
    (a) > 0 ? \
      ((b) > 0 ? (max) / (a) < (b) : (min) / (a) > (b)) : \
      ((b) > 0 ? (min) / (a) < (b) : (max) / (a) > (b)))

#if __has_builtin(__builtin_mul_overflow_p)
/* __builtin_mul_overflow_p can take bitfield */
/* and GCC permits bitfields for integers other than int */
# define MUL_OVERFLOW_FIXNUM_P(a, b) \
    __extension__ ({ \
        struct { long fixnum : sizeof(long) * CHAR_BIT - 1; } c; \
        __builtin_mul_overflow_p((a), (b), c.fixnum); \
    })
#else
# define MUL_OVERFLOW_FIXNUM_P(a, b) \
    MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, FIXNUM_MIN, FIXNUM_MAX)
#endif

#ifdef MUL_OVERFLOW_P
# define MUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_P(a, b)
# define MUL_OVERFLOW_LONG_P(a, b)      MUL_OVERFLOW_P(a, b)
# define MUL_OVERFLOW_INT_P(a, b)       MUL_OVERFLOW_P(a, b)
#else
# define MUL_OVERFLOW_LONG_LONG_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LLONG_MIN, LLONG_MAX)
# define MUL_OVERFLOW_LONG_P(a, b)      MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, LONG_MIN, LONG_MAX)
# define MUL_OVERFLOW_INT_P(a, b)       MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, INT_MIN, INT_MAX)
#endif

#ifdef HAVE_UINT128_T
# define bit_length(x) \
    (unsigned int) \
    (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
     sizeof(x) <= sizeof(int64_t) ? 64 - nlz_int64((uint64_t)(x)) : \
                                   128 - nlz_int128((uint128_t)(x)))
#else
# define bit_length(x) \
    (unsigned int) \
    (sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
                                    64 - nlz_int64((uint64_t)(x)))
#endif

static inline uint16_t swap16(uint16_t);
static inline uint32_t swap32(uint32_t);
static inline uint64_t swap64(uint64_t);
static inline unsigned nlz_int(unsigned x);
static inline unsigned nlz_long(unsigned long x);
static inline unsigned nlz_long_long(unsigned long long x);
static inline unsigned nlz_intptr(uintptr_t x);
static inline unsigned nlz_int32(uint32_t x);
static inline unsigned nlz_int64(uint64_t x);
#ifdef HAVE_UINT128_T
static inline unsigned nlz_int128(uint128_t x);
#endif
static inline unsigned rb_popcount32(uint32_t x);
static inline unsigned rb_popcount64(uint64_t x);
static inline unsigned rb_popcount_intptr(uintptr_t x);
static inline int ntz_int32(uint32_t x);
static inline int ntz_int64(uint64_t x);
static inline int ntz_intptr(uintptr_t x);
static inline VALUE RUBY_BIT_ROTL(VALUE, int);
static inline VALUE RUBY_BIT_ROTR(VALUE, int);

static inline uint16_t
swap16(uint16_t x)
{
#if __has_builtin(__builtin_bswap16)
    return __builtin_bswap16(x);

#elif defined(_MSC_VER)
    return _byteswap_ushort(x);

#else
    return (x << 8) | (x >> 8);

#endif
}

static inline uint32_t
swap32(uint32_t x)
{
#if __has_builtin(__builtin_bswap32)
    return __builtin_bswap32(x);

#elif defined(_MSC_VER)
    return _byteswap_ulong(x);

#else
    x = ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16);
    x = ((x & 0x00FF00FF) <<  8) | ((x & 0xFF00FF00) >>  8);
    return x;

#endif
}

static inline uint64_t
swap64(uint64_t x)
{
#if __has_builtin(__builtin_bswap64)
    return __builtin_bswap64(x);

#elif defined(_MSC_VER)
    return _byteswap_uint64(x);

#else
    x = ((x & 0x00000000FFFFFFFFULL) << 32) | ((x & 0xFFFFFFFF00000000ULL) >> 32);
    x = ((x & 0x0000FFFF0000FFFFULL) << 16) | ((x & 0xFFFF0000FFFF0000ULL) >> 16);
    x = ((x & 0x00FF00FF00FF00FFULL) <<  8) | ((x & 0xFF00FF00FF00FF00ULL) >>  8);
    return x;

#endif
}

static inline unsigned int
nlz_int32(uint32_t x)
{
#if defined(_MSC_VER) && defined(_WIN64) && defined(__AVX2__)
    /* Note: It seems there is no such tihng like __LZCNT__ predefined in MSVC.
     * AMD  CPUs have  had this  instruction for  decades (since  K10) but  for
     * Intel, Haswell is  the oldest one.  We need to  use __AVX2__ for maximum
     * safety. */
    return (unsigned int)__lzcnt(x);

#elif defined(__x86_64__) && defined(__LZCNT__) && ! defined(MJIT_HEADER)
    return (unsigned int)_lzcnt_u32(x);

#elif defined(_MSC_VER) && defined(_Win64) /* &&! defined(__AVX2__) */
    unsigned long r;
    return _BitScanReverse(&r, x) ? (int)r : 32;

#elif __has_builtin(__builtin_clz)
    STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32);
    return x ? (unsigned int)__builtin_clz(x) : 32;

#else
    uint32_t y;
    unsigned n = 32;
    y = x >> 16; if (y) {n -= 16; x = y;}
    y = x >>  8; if (y) {n -=  8; x = y;}
    y = x >>  4; if (y) {n -=  4; x = y;}
    y = x >>  2; if (y) {n -=  2; x = y;}
    y = x >>  1; if (y) {return n - 2;}
    return (unsigned int)(n - x);
#endif
}

static inline unsigned int
nlz_int64(uint64_t x)
{
#if defined(_MSC_VER) && defined(_WIN64) && defined(__AVX2__)
    return (unsigned int)__lzcnt64(x);

#elif defined(__x86_64__) && defined(__LZCNT__) && ! defined(MJIT_HEADER)
    return (unsigned int)_lzcnt_u64(x);

#elif defined(_MSC_VER) && defined(_Win64) /* &&! defined(__AVX2__) */
    unsigned long r;
    return _BitScanReverse64(&r, x) ? (unsigned int)r : 64;

#elif __has_builtin(__builtin_clzl)
    if (x == 0) {
        return 64;
    }
    else if (sizeof(long) * CHAR_BIT == 64) {
        return (unsigned int)__builtin_clzl((unsigned long)x);
    }
    else if (sizeof(long long) * CHAR_BIT == 64) {
        return (unsigned int)__builtin_clzll((unsigned long long)x);
    }
    else {
        /* :FIXME: Is there a way to make this branch a compile-time error? */
        __builtin_unreachable();
    }

#else
    uint64_t y;
    unsigned int n = 64;
    y = x >> 32; if (y) {n -= 32; x = y;}
    y = x >> 16; if (y) {n -= 16; x = y;}
    y = x >>  8; if (y) {n -=  8; x = y;}
    y = x >>  4; if (y) {n -=  4; x = y;}
    y = x >>  2; if (y) {n -=  2; x = y;}
    y = x >>  1; if (y) {return n - 2;}
    return (unsigned int)(n - x);

#endif
}

#ifdef HAVE_UINT128_T
static inline unsigned int
nlz_int128(uint128_t x)
{
    uint64_t y = (uint64_t)(x >> 64);

    if (x == 0) {
        return 128;
    }
    else if (y == 0) {
        return (unsigned int)nlz_int64(y) + 64;
    }
    else {
        return (unsigned int)nlz_int64(y);
    }
}
#endif

static inline unsigned int
nlz_int(unsigned int x)
{
    if (sizeof(unsigned int) * CHAR_BIT == 32) {
        return nlz_int32((uint32_t)x);
    }
    else if (sizeof(unsigned int) * CHAR_BIT == 64) {
        return nlz_int64((uint64_t)x);
    }
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline unsigned int
nlz_long(unsigned long x)
{
    if (sizeof(unsigned long) * CHAR_BIT == 32) {
        return nlz_int32((uint32_t)x);
    }
    else if (sizeof(unsigned long) * CHAR_BIT == 64) {
        return nlz_int64((uint64_t)x);
    }
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline unsigned int
nlz_long_long(unsigned long long x)
{
    if (sizeof(unsigned long long) * CHAR_BIT == 64) {
        return nlz_int64((uint64_t)x);
    }
#ifdef HAVE_UINT128_T
    else if (sizeof(unsigned long long) * CHAR_BIT == 128) {
        return nlz_int128((uint128_t)x);
    }
#endif
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline unsigned int
nlz_intptr(uintptr_t x)
{
    if (sizeof(uintptr_t) == sizeof(unsigned int)) {
        return nlz_int((unsigned int)x);
    }
    if (sizeof(uintptr_t) == sizeof(unsigned long)) {
        return nlz_long((unsigned long)x);
    }
    if (sizeof(uintptr_t) == sizeof(unsigned long long)) {
        return nlz_long_long((unsigned long long)x);
    }
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline unsigned int
rb_popcount32(uint32_t x)
{
#if defined(_MSC_VER) && defined(_WIN64) && defined(__AVX__)
    /* Note: CPUs since Nehalem and Barcelona  have had this instruction so SSE
     * 4.2 should suffice, but it seems there is no such thing like __SSE_4_2__
     * predefined macro in MSVC.  They do have __AVX__ so use it instead. */
    return (unsigned int)__popcnt(x);

#elif __has_builtin(__builtin_popcount)
    STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT >= 32);
    return (unsigned int)__builtin_popcount(x);

#else
    x = (x & 0x55555555) + (x >> 1 & 0x55555555);
    x = (x & 0x33333333) + (x >> 2 & 0x33333333);
    x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
    x = (x & 0x001f001f) + (x >> 8 & 0x001f001f);
    x = (x & 0x0000003f) + (x >>16 & 0x0000003f);
    return (unsigned int)x;

#endif
}

static inline unsigned int
rb_popcount64(uint64_t x)
{
#if defined(_MSC_VER) && defined(_WIN64) && defined(__AVX__)
    return (unsigned int)__popcnt64(x);

#elif __has_builtin(__builtin_popcount)
    if (sizeof(long) * CHAR_BIT == 64) {
        return (unsigned int)__builtin_popcountl((unsigned long)x);
    }
    else if (sizeof(long long) * CHAR_BIT == 64) {
        return (unsigned int)__builtin_popcountll((unsigned long long)x);
    }
    else {
        /* :FIXME: Is there a way to make this branch a compile-time error? */
        __builtin_unreachable();
    }

#else
    x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
    x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
    x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707);
    x = (x & 0x001f001f001f001f) + (x >> 8 & 0x001f001f001f001f);
    x = (x & 0x0000003f0000003f) + (x >>16 & 0x0000003f0000003f);
    x = (x & 0x000000000000007f) + (x >>32 & 0x000000000000007f);
    return (unsigned int)x;

#endif
}

static inline unsigned int
rb_popcount_intptr(uintptr_t x)
{
    if (sizeof(uintptr_t) * CHAR_BIT == 64) {
        return rb_popcount64((uint64_t)x);
    }
    else if (sizeof(uintptr_t) * CHAR_BIT == 32) {
        return rb_popcount32((uint32_t)x);
    }
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline int
ntz_int32(uint32_t x)
{
#if defined(__x86_64__) && defined(__BMI__) && ! defined(MJIT_HEADER)
    return (unsigned)_tzcnt_u32(x);

#elif defined(_MSC_VER) && defined(_WIN64)
    /* :FIXME: Is there any way to issue TZCNT instead of BSF, apart from using
     *         assembly?  Because issueing LZCNT seems possible (see nlz.h). */
    unsigned long r;
    return _BitScanForward(&r, x) ? (int)r : 32;

#elif __has_builtin(__builtin_ctz)
    STATIC_ASSERT(sizeof_int, sizeof(int) * CHAR_BIT == 32);
    return x ? (unsigned)__builtin_ctz(x) : 32;

#else
    return rb_popcount32((~x) & (x-1));

#endif
}

static inline int
ntz_int64(uint64_t x)
{
#if defined(__x86_64__) && defined(__BMI__) && ! defined(MJIT_HEADER)
    return (unsigned)_tzcnt_u64(x);

#elif defined(_MSC_VER) && defined(_WIN64)
    unsigned long r;
    return _BitScanForward64(&r, x) ? (int)r : 64;

#elif __has_builtin(__builtin_ctzl)
    if (x == 0) {
        return 64;
    }
    else if (sizeof(long) * CHAR_BIT == 64) {
        return (unsigned)__builtin_ctzl((unsigned long)x);
    }
    else if (sizeof(long long) * CHAR_BIT == 64) {
        return (unsigned)__builtin_ctzll((unsigned long long)x);
    }
    else {
        /* :FIXME: Is there a way to make this branch a compile-time error? */
        __builtin_unreachable();
    }

#else
    return rb_popcount64((~x) & (x-1));

#endif
}

static inline int
ntz_intptr(uintptr_t x)
{
    if (sizeof(uintptr_t) * CHAR_BIT == 64) {
        return ntz_int64((uint64_t)x);
    }
    else if (sizeof(uintptr_t) * CHAR_BIT == 32) {
        return ntz_int32((uint32_t)x);
    }
    else {
        UNREACHABLE_RETURN(~0);
    }
}

static inline VALUE
RUBY_BIT_ROTL(VALUE v, int n)
{
#if __has_builtin(__builtin_rotateleft32) && (SIZEOF_VALUE * CHAR_BIT == 32)
    return __builtin_rotateleft32(v, n);

#elif __has_builtin(__builtin_rotateleft64) && (SIZEOF_VALUE * CHAR_BIT == 64)
    return __builtin_rotateleft64(v, n);

#else
    const int m = sizeof(VALUE) * CHAR_BIT;
    return (v << n) | (v >> (m - n));
#endif
}

static inline VALUE
RUBY_BIT_ROTR(VALUE v, int n)
{
#if __has_builtin(__builtin_rotateright32) && (SIZEOF_VALUE * CHAR_BIT == 32)
    return __builtin_rotateright32(v, n);

#elif __has_builtin(__builtin_rotateright64) && (SIZEOF_VALUE * CHAR_BIT == 64)
    return __builtin_rotateright64(v, n);

#else
    const int m = sizeof(VALUE) * CHAR_BIT;
    return (v << (m - n)) | (v >> n);
#endif
}

#endif /* INTERNAL_BITS_H */