Disabled TRAP cache of CodeQL again
[ruby.git] / internal / fixnum.h
blobb78e31460eee1a3efd923bf231192160971f315d
1 #ifndef INTERNAL_FIXNUM_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_FIXNUM_H
3 /**
4 * @author Ruby developers <ruby-core@ruby-lang.org>
5 * @copyright This file is a part of the programming language Ruby.
6 * Permission is hereby granted, to either redistribute and/or
7 * modify this file, provided that the conditions mentioned in the
8 * file COPYING are met. Consult the file for details.
9 * @brief Internal header for Fixnums.
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 */
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
28 static inline long rb_overflowed_fix_to_int(long x);
29 static inline VALUE rb_fix_plus_fix(VALUE x, VALUE y);
30 static inline VALUE rb_fix_minus_fix(VALUE x, VALUE y);
31 static inline VALUE rb_fix_mul_fix(VALUE x, VALUE y);
32 static inline void rb_fix_divmod_fix(VALUE x, VALUE y, VALUE *divp, VALUE *modp);
33 static inline VALUE rb_fix_div_fix(VALUE x, VALUE y);
34 static inline VALUE rb_fix_mod_fix(VALUE x, VALUE y);
35 static inline bool FIXNUM_POSITIVE_P(VALUE num);
36 static inline bool FIXNUM_NEGATIVE_P(VALUE num);
37 static inline bool FIXNUM_ZERO_P(VALUE num);
39 static inline long
40 rb_overflowed_fix_to_int(long x)
42 return (long)((unsigned long)(x >> 1) ^ (1LU << (SIZEOF_LONG * CHAR_BIT - 1)));
45 static inline VALUE
46 rb_fix_plus_fix(VALUE x, VALUE y)
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...
73 if (__builtin_add_overflow((long)x, (long)y-1, &lz)) {
74 return rb_int2big(rb_overflowed_fix_to_int(lz));
76 else {
77 return (VALUE)lz;
79 #endif
82 static inline VALUE
83 rb_fix_minus_fix(VALUE x, VALUE y)
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));
93 else {
94 return (VALUE)lz;
96 #endif
99 /* arguments must be Fixnum */
100 static inline VALUE
101 rb_fix_mul_fix(VALUE x, VALUE y)
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));
111 else {
112 return LONG2FIX(lx * ly);
114 #endif
118 * This behaves different from C99 for negative arguments.
119 * Note that div may overflow fixnum.
121 static inline void
122 rb_fix_divmod_fix(VALUE a, VALUE b, VALUE *divp, VALUE *modp)
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.
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;
136 div = x / y;
137 mod = x % y;
138 if (y > 0 ? mod < 0 : mod > 0) {
139 mod += y;
140 div -= 1;
142 if (divp) *divp = LONG2FIX(div);
143 if (modp) *modp = LONG2FIX(mod);
146 /* div() for Ruby
147 * This behaves different from C99 for negative arguments.
149 static inline VALUE
150 rb_fix_div_fix(VALUE x, VALUE y)
152 VALUE div;
153 rb_fix_divmod_fix(x, y, &div, NULL);
154 return div;
157 /* mod() for Ruby
158 * This behaves different from C99 for negative arguments.
160 static inline VALUE
161 rb_fix_mod_fix(VALUE x, VALUE y)
163 VALUE mod;
164 rb_fix_divmod_fix(x, y, NULL, &mod);
165 return mod;
168 static inline bool
169 FIXNUM_POSITIVE_P(VALUE num)
171 return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0);
174 static inline bool
175 FIXNUM_NEGATIVE_P(VALUE num)
177 return (SIGNED_VALUE)num < 0;
180 static inline bool
181 FIXNUM_ZERO_P(VALUE num)
183 return num == INT2FIX(0);
185 #endif /* INTERNAL_FIXNUM_H */