Ruby: internal/bits.h File Reference

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

Macros
bits.h File Reference

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"`

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

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 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)))
int
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
uint64_t
unsigned long long uint64_t
Definition: sha2.h:102

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,
|  |  | b
| ) |  |

Value:
__extension__ ({ \
struct { long fixnum : sizeof(long) * CHAR_BIT - 1; } c = { 0 }; \
__builtin_mul_overflow_p((a), (b), c.fixnum); \
})
long
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
CHAR_BIT
#define CHAR_BIT
Definition: limits.h:44

Definition at line 113 of file bits.h.

## ◆ MUL_OVERFLOW_INT_P

#define MUL_OVERFLOW_INT_P | ( |  | a,
|  |  | b
| ) |  | 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,
|  |  | b
| ) |  | MUL_OVERFLOW_P(a, b)

Definition at line 124 of file bits.h.

## ◆ MUL_OVERFLOW_LONG_P

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

Definition at line 125 of file bits.h.

## ◆ MUL_OVERFLOW_P

#define MUL_OVERFLOW_P | ( |  | a,
|  |  | b
| ) |  | __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)))
max
int max
Definition: enough.c:225

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))))
T
unsigned char T
Definition: cls_uchar_va.c:10

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.