Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
Macros
bits.h File Reference

(b76ad15ed0da636161de0243c547ee1e6fc95681)

Internal header for bitwise integer algorithms. More...

#include "ruby/internal/config.h"
#include <limits.h>
#include <stdint.h>
#include "internal/compilers.h"
#include "ruby/ruby.h"
#include "internal/static_assert.h"
Include dependency graph for bits.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#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)
 
#define SIGNED_INTEGER_MAX(T)   ((T)(SIGNED_INTEGER_MIN(T) ^ ((T)~(T)0)))
 
#define UNSIGNED_INTEGER_MAX(T)   ((T)~(T)0)
 
#define MUL_OVERFLOW_P(a, b)   __builtin_mul_overflow_p((a), (b), (__typeof__(a * b))0)
 
#define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max)
 
#define MUL_OVERFLOW_FIXNUM_P(a, b)
 
#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)
 
#define bit_length(x)
 
#define swap16   ruby_swap16
 
#define swap32   ruby_swap32
 
#define swap64   ruby_swap64
 

Detailed Description

Internal header for bitwise integer algorithms.

Author
Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org
See also
Henry S. Warren Jr., "Hacker's Delight" (2nd ed.), 2013.
SEI CERT C Coding Standard INT32-C. "Ensure that operations on signed integers do not result in overflow"
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateleft
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-rotateright
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/rotl-rotl64-rotr-rotr64
https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64
https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanreverse-bitscanreverse64
https://docs.microsoft.com/en-us/cpp/intrinsics/lzcnt16-lzcnt-lzcnt64
https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_lzcnt_u32
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_tzcnt_u32
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_rotl64
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#text=_rotr64
https://stackoverflow.com/a/776523

Definition in file bits.h.

Macro Definition Documentation

◆ bit_length

#define bit_length (   x)
Value:
(unsigned int) \
(sizeof(x) <= sizeof(int32_t) ? 32 - nlz_int32((uint32_t)(x)) : \
64 - nlz_int64((uint64_t)(x)))

Definition at line 140 of file bits.h.

◆ HALF_LONG_MSB

#define HALF_LONG_MSB   ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2))

Definition at line 80 of file bits.h.

◆ MUL_OVERFLOW_FIXNUM_P

#define MUL_OVERFLOW_FIXNUM_P (   a,
 
)
Value:
__extension__ ({ \
struct { long fixnum : sizeof(long) * CHAR_BIT - 1; } c = { 0 }; \
__builtin_mul_overflow_p((a), (b), c.fixnum); \
})

Definition at line 113 of file bits.h.

◆ MUL_OVERFLOW_INT_P

#define MUL_OVERFLOW_INT_P (   a,
 
)    MUL_OVERFLOW_P(a, b)

Definition at line 126 of file bits.h.

◆ MUL_OVERFLOW_LONG_LONG_P

#define MUL_OVERFLOW_LONG_LONG_P (   a,
 
)    MUL_OVERFLOW_P(a, b)

Definition at line 124 of file bits.h.

◆ MUL_OVERFLOW_LONG_P

#define MUL_OVERFLOW_LONG_P (   a,
 
)    MUL_OVERFLOW_P(a, b)

Definition at line 125 of file bits.h.

◆ MUL_OVERFLOW_P

#define MUL_OVERFLOW_P (   a,
 
)    __builtin_mul_overflow_p((a), (b), (__typeof__(a * b))0)

Definition at line 96 of file bits.h.

◆ MUL_OVERFLOW_SIGNED_INTEGER_P

#define MUL_OVERFLOW_SIGNED_INTEGER_P (   a,
  b,
  min,
  max 
)
Value:
( \
(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)))

Definition at line 103 of file bits.h.

◆ SIGNED_INTEGER_MAX

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

Definition at line 91 of file bits.h.

◆ SIGNED_INTEGER_MIN

#define SIGNED_INTEGER_MIN (   T)
Value:
((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))))

Definition at line 84 of file bits.h.

◆ SIGNED_INTEGER_TYPE_P

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

Definition at line 82 of file bits.h.

◆ swap16

#define swap16   ruby_swap16

Definition at line 147 of file bits.h.

◆ swap32

#define swap32   ruby_swap32

Definition at line 151 of file bits.h.

◆ swap64

#define swap64   ruby_swap64

Definition at line 155 of file bits.h.

◆ UNSIGNED_INTEGER_MAX

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

Definition at line 93 of file bits.h.

CHAR_BIT
#define CHAR_BIT
Definition: limits.h:44
uint64_t
unsigned long long uint64_t
Definition: sha2.h:102
uint32_t
unsigned int uint32_t
Definition: sha2.h:101