Ruby: internal/fixnum.h Source File

Ruby 3.4.9p82 (2026-03-11 revision 76cca827ab52ab1d346a728f068d5b8da3e2952b)

fixnum.h
1#ifndef INTERNAL_FIXNUM_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_FIXNUM_H
11#include "ruby/internal/config.h" /* for HAVE_LONG_LONG */
12#include <limits.h> /* for CHAR_BIT */
13#include "internal/bits.h" /* for MUL_OVERFLOW_FIXNUM_P */
14#include "internal/compilers.h" /* for __has_builtin */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/intern.h" /* for rb_big_mul */
17#include "ruby/ruby.h" /* for RB_FIXABLE */
18
19#if HAVE_LONG_LONG && SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
20# define DLONG LONG_LONG
21# define DL2NUM(x) LL2NUM(x)
22#elif defined(HAVE_INT128_T) && !(defined(__OpenBSD__) && defined(__mips64__))
23# define DLONG int128_t
24# define DL2NUM(x) (RB_FIXABLE(x) ? LONG2FIX(x) : rb_int128t2big(x))
25 VALUE rb_int128t2big(int128_t n); /* in bignum.c */
26#endif
27
28static inline long rb_overflowed_fix_to_int(long x);
29static inline VALUE rb_fix_plus_fix(VALUE x, VALUE y);
30static inline VALUE rb_fix_minus_fix(VALUE x, VALUE y);
31static inline VALUE rb_fix_mul_fix(VALUE x, VALUE y);
32static inline void rb_fix_divmod_fix(VALUE x, VALUE y, VALUE *divp, VALUE *modp);
33static inline VALUE rb_fix_div_fix(VALUE x, VALUE y);
34static inline VALUE rb_fix_mod_fix(VALUE x, VALUE y);
35static inline bool FIXNUM_POSITIVE_P(VALUE num);
36static inline bool FIXNUM_NEGATIVE_P(VALUE num);
37static inline bool FIXNUM_ZERO_P(VALUE num);
38
39static inline long
40rb_overflowed_fix_to_int(long x)
41{
42 return (long)((unsigned long)(x >> 1) ^ (1LU << (SIZEOF_LONG * CHAR_BIT - 1)));
43}
44
45static inline VALUE
46rb_fix_plus_fix(VALUE x, VALUE y)
47{
48#if !__has_builtin(__builtin_add_overflow)
49 long lz = FIX2LONG(x) + FIX2LONG(y);
50 return LONG2NUM(lz);
51#else
52 long lz;
53 /* NOTE
54 * (1) `LONG2FIX(FIX2LONG(x)+FIX2LONG(y))`
55 + = `((lx*2+1)/2 + (ly*2+1)/2)*2+1`
56 + = `lx*2 + ly*2 + 1`
57 + = `(lx*2+1) + (ly*2+1) - 1`
58 + = `x + y - 1`
59 * (2) Fixnum's LSB is always 1.
60 * It means you can always run `x - 1` without overflow.
61 * (3) Of course `z = x + (y-1)` may overflow.
62 * At that time true value is
63 * * positive: 0b0 1xxx...1, and z = 0b1xxx...1
64 * * negative: 0b1 0xxx...1, and z = 0b0xxx...1
65 * To convert this true value to long,
66 * (a) Use arithmetic shift
67 * * positive: 0b11xxx...
68 * * negative: 0b00xxx...
69 * (b) invert MSB
70 * * positive: 0b01xxx...
71 * * negative: 0b10xxx...
72 */
73 if (__builtin_add_overflow((long)x, (long)y-1, &lz)) {
74 return rb_int2big(rb_overflowed_fix_to_int(lz));
75 }
76 else {
77 return (VALUE)lz;
78 }
79#endif
80}
81
82static inline VALUE
83rb_fix_minus_fix(VALUE x, VALUE y)
84{
85#if !__has_builtin(__builtin_sub_overflow)
86 long lz = FIX2LONG(x) - FIX2LONG(y);
87 return LONG2NUM(lz);
88#else
89 long lz;
90 if (__builtin_sub_overflow((long)x, (long)y-1, &lz)) {
91 return rb_int2big(rb_overflowed_fix_to_int(lz));
92 }
93 else {
94 return (VALUE)lz;
95 }
96#endif
97}
98
99/* arguments must be Fixnum */
100static inline VALUE
101rb_fix_mul_fix(VALUE x, VALUE y)
102{
103 long lx = FIX2LONG(x);
104 long ly = FIX2LONG(y);
105#ifdef DLONG
106 return DL2NUM((DLONG)lx * (DLONG)ly);
107#else
108 if (MUL_OVERFLOW_FIXNUM_P(lx, ly)) {
109 return rb_big_mul(rb_int2big(lx), rb_int2big(ly));
110 }
111 else {
112 return LONG2FIX(lx * ly);
113 }
114#endif
115}
116
117/*
118 * This behaves different from C99 for negative arguments.
119 * Note that div may overflow fixnum.
120 */
121static inline void
122rb_fix_divmod_fix(VALUE a, VALUE b, VALUE *divp, VALUE *modp)
123{
124 /* assume / and % comply C99.
125 * ldiv(3) won't be inlined by GCC and clang.
126 * I expect / and % are compiled as single idiv.
127 */
128 long x = FIX2LONG(a);
129 long y = FIX2LONG(b);
130 long div, mod;
131 if (x == FIXNUM_MIN && y == -1) {
132 if (divp) *divp = LONG2NUM(-FIXNUM_MIN);
133 if (modp) *modp = LONG2FIX(0);
134 return;
135 }
136 div = x / y;
137 mod = x % y;
138 if (y > 0 ? mod < 0 : mod > 0) {
139 mod += y;
140 div -= 1;
141 }
142 if (divp) *divp = LONG2FIX(div);
143 if (modp) *modp = LONG2FIX(mod);
144}
145
146/* div() for Ruby
147 * This behaves different from C99 for negative arguments.
148 */
149static inline VALUE
150rb_fix_div_fix(VALUE x, VALUE y)
151{
152 VALUE div;
153 rb_fix_divmod_fix(x, y, &div, NULL);
154 return div;
155}
156
157/* mod() for Ruby
158 * This behaves different from C99 for negative arguments.
159 */
160static inline VALUE
161rb_fix_mod_fix(VALUE x, VALUE y)
162{
163 VALUE mod;
164 rb_fix_divmod_fix(x, y, NULL, &mod);
165 return mod;
166}
167
168static inline bool
169FIXNUM_POSITIVE_P(VALUE num)
170{
171 return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0);
172}
173
174static inline bool
175FIXNUM_NEGATIVE_P(VALUE num)
176{
177 return (SIGNED_VALUE)num < 0;
178}
179
180static inline bool
181FIXNUM_ZERO_P(VALUE num)
182{
183 return num == INT2FIX(0);
184}
185#endif /* INTERNAL_FIXNUM_H */
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
LONG2FIX
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
LONG2NUM
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
FIXNUM_MIN
#define FIXNUM_MIN
Old name of RUBY_FIXNUM_MIN.
Definition fixnum.h:27
FIX2LONG
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
intern.h
ruby.h
stdbool.h
C99 shim for <stdbool.h>.
U
Definition dtoa.c:305
SIGNED_VALUE
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40