1 #ifndef INTERNAL_FIXNUM_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_FIXNUM_H
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 */
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
);
40 rb_overflowed_fix_to_int(long x
)
42 return (long)((unsigned long)(x
>> 1) ^ (1LU << (SIZEOF_LONG
* CHAR_BIT
- 1)));
46 rb_fix_plus_fix(VALUE x
, VALUE y
)
48 #if !__has_builtin(__builtin_add_overflow)
49 long lz
= FIX2LONG(x
) + FIX2LONG(y
);
54 * (1) `LONG2FIX(FIX2LONG(x)+FIX2LONG(y))`
55 + = `((lx*2+1)/2 + (ly*2+1)/2)*2+1`
57 + = `(lx*2+1) + (ly*2+1) - 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...
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
));
83 rb_fix_minus_fix(VALUE x
, VALUE y
)
85 #if !__has_builtin(__builtin_sub_overflow)
86 long lz
= FIX2LONG(x
) - FIX2LONG(y
);
90 if (__builtin_sub_overflow((long)x
, (long)y
-1, &lz
)) {
91 return rb_int2big(rb_overflowed_fix_to_int(lz
));
99 /* arguments must be Fixnum */
101 rb_fix_mul_fix(VALUE x
, VALUE y
)
103 long lx
= FIX2LONG(x
);
104 long ly
= FIX2LONG(y
);
106 return DL2NUM((DLONG
)lx
* (DLONG
)ly
);
108 if (MUL_OVERFLOW_FIXNUM_P(lx
, ly
)) {
109 return rb_big_mul(rb_int2big(lx
), rb_int2big(ly
));
112 return LONG2FIX(lx
* ly
);
118 * This behaves different from C99 for negative arguments.
119 * Note that div may overflow fixnum.
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
);
131 if (x
== FIXNUM_MIN
&& y
== -1) {
132 if (divp
) *divp
= LONG2NUM(-FIXNUM_MIN
);
133 if (modp
) *modp
= LONG2FIX(0);
138 if (y
> 0 ? mod
< 0 : mod
> 0) {
142 if (divp
) *divp
= LONG2FIX(div
);
143 if (modp
) *modp
= LONG2FIX(mod
);
147 * This behaves different from C99 for negative arguments.
150 rb_fix_div_fix(VALUE x
, VALUE y
)
153 rb_fix_divmod_fix(x
, y
, &div
, NULL
);
158 * This behaves different from C99 for negative arguments.
161 rb_fix_mod_fix(VALUE x
, VALUE y
)
164 rb_fix_divmod_fix(x
, y
, NULL
, &mod
);
169 FIXNUM_POSITIVE_P(VALUE num
)
171 return (SIGNED_VALUE
)num
> (SIGNED_VALUE
)INT2FIX(0);
175 FIXNUM_NEGATIVE_P(VALUE num
)
177 return (SIGNED_VALUE
)num
< 0;
181 FIXNUM_ZERO_P(VALUE num
)
183 return num
== INT2FIX(0);
185 #endif /* INTERNAL_FIXNUM_H */