Debian Codegraph @

include/ruby/internal/arithmetic/long.h

raw
1 #ifndef RBIMPL_ARITHMETIC_LONG_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_ARITHMETIC_LONG_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Arithmetic conversion between C's `long` and Ruby's.
22 *
23 * ### Q&A ###
24 *
25 * - Q: Why are INT2FIX etc. here, not in `int.h`?
26 *
27 * - A: Because they are in fact handling `long`. It seems someone did not
28 * understand the difference of `int` and `long` when they designed those
29 * macros.
30 */
31 #include "ruby/internal/config.h"
32 #include "ruby/internal/arithmetic/fixnum.h" /* FIXABLE */
33 #include "ruby/internal/arithmetic/intptr_t.h" /* rb_int2big etc.*/
34 #include "ruby/internal/assume.h"
35 #include "ruby/internal/attr/artificial.h"
36 #include "ruby/internal/attr/cold.h"
37 #include "ruby/internal/attr/const.h"
38 #include "ruby/internal/attr/constexpr.h"
39 #include "ruby/internal/attr/noreturn.h"
40 #include "ruby/internal/cast.h"
41 #include "ruby/internal/dllexport.h"
42 #include "ruby/internal/special_consts.h" /* FIXNUM_FLAG */
43 #include "ruby/internal/value.h"
44 #include "ruby/assert.h"
45
46 #define FIX2LONG RB_FIX2LONG /**< @old{RB_FIX2LONG} */
47 #define FIX2ULONG RB_FIX2ULONG /**< @old{RB_FIX2ULONG} */
48 #define INT2FIX RB_INT2FIX /**< @old{RB_INT2FIX} */
49 #define LONG2FIX RB_INT2FIX /**< @old{RB_INT2FIX} */
50 #define LONG2NUM RB_LONG2NUM /**< @old{RB_LONG2NUM} */
51 #define NUM2LONG RB_NUM2LONG /**< @old{RB_NUM2LONG} */
52 #define NUM2ULONG RB_NUM2ULONG /**< @old{RB_NUM2ULONG} */
53 #define RB_FIX2LONG rb_fix2long /**< @alias{rb_fix2long} */
54 #define RB_FIX2ULONG rb_fix2ulong /**< @alias{rb_fix2ulong} */
55 #define RB_LONG2FIX RB_INT2FIX /**< @alias{RB_INT2FIX} */
56 #define RB_LONG2NUM rb_long2num_inline /**< @alias{rb_long2num_inline} */
57 #define RB_NUM2LONG rb_num2long_inline /**< @alias{rb_num2long_inline} */
58 #define RB_NUM2ULONG rb_num2ulong_inline /**< @alias{rb_num2ulong_inline} */
59 #define RB_ULONG2NUM rb_ulong2num_inline /**< @alias{rb_ulong2num_inline} */
60 #define ULONG2NUM RB_ULONG2NUM /**< @old{RB_ULONG2NUM} */
61 #define rb_fix_new RB_INT2FIX /**< @alias{RB_INT2FIX} */
62 #define rb_long2int rb_long2int_inline /**< @alias{rb_long2int_inline} */
63
64 /** @cond INTERNAL_MACRO */
65 #define RB_INT2FIX RB_INT2FIX
66 /** @endcond */
67
68 RBIMPL_SYMBOL_EXPORT_BEGIN()
69
70 RBIMPL_ATTR_NORETURN()
71 RBIMPL_ATTR_COLD()
72 /**
73 * This is an utility function to raise an ::rb_eRangeError.
74 *
75 * @param[in] num A signed value about to overflow.
76 * @exception rb_eRangeError `num` is out of range of `int`.
77 */
78 void rb_out_of_int(SIGNED_VALUE num);
79
80 /**
81 * Converts an instance of ::rb_cNumeric into C's `long`.
82 *
83 * @param[in] num Something numeric.
84 * @exception rb_eTypeError `num` is not a numeric.
85 * @exception rb_eRangeError `num` is out of range of `long`.
86 * @return The passed value converted into C's `long`.
87 */
88 long rb_num2long(VALUE num);
89
90 /**
91 * Converts an instance of ::rb_cNumeric into C's `unsigned long`.
92 *
93 * @param[in] num Something numeric.
94 * @exception rb_eTypeError `num` is not a numeric.
95 * @exception rb_eRangeError `num` is out of range of `unsigned long`.
96 * @return The passed value converted into C's `unsigned long`.
97 */
98 unsigned long rb_num2ulong(VALUE num);
99 RBIMPL_SYMBOL_EXPORT_END()
100
101 RBIMPL_ATTR_CONST_UNLESS_DEBUG()
102 RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14)
103 RBIMPL_ATTR_ARTIFICIAL()
104 /**
105 * Converts a C's `long` into an instance of ::rb_cInteger.
106 *
107 * @param[in] i Arbitrary `long` value.
108 * @return An instance of ::rb_cInteger.
109 */
110 static inline VALUE
111 RB_INT2FIX(long i)
112 {
113 RBIMPL_ASSERT_OR_ASSUME(RB_FIXABLE(i));
114
115 /* :NOTE: VALUE can be wider than long. As j being unsigned, 2j+1 is fully
116 * defined. Also it can be compiled into a single LEA instruction. */
117 const unsigned long j = i;
118 const unsigned long k = (j << 1) + RUBY_FIXNUM_FLAG;
119 const long l = k;
120 const SIGNED_VALUE m = l; /* Sign extend */
121 const VALUE n = m;
122
123 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(n));
124 return n;
125 }
126
127 /**
128 * Checks if `int` can hold the given integer.
129 *
130 * @param[in] n Arbitrary `long` value.
131 * @exception rb_eRangeError `n` is out of range of `int`.
132 * @return Identical value of type `int`
133 */
134 static inline int
135 rb_long2int_inline(long n)
136 {
137 int i = RBIMPL_CAST((int)n);
138
139 if /* constexpr */ (sizeof(long) <= sizeof(int)) {
140 RBIMPL_ASSUME(i == n);
141 }
142
143 if (i != n)
144 rb_out_of_int(n);
145
146 return i;
147 }
148
149 RBIMPL_ATTR_CONST_UNLESS_DEBUG()
150 RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14)
151 /**
152 * @private
153 *
154 * This is an implementation detail of rb_fix2long(). People don't use it
155 * directly.
156 *
157 * @param[in] x A Fixnum.
158 * @return Identical value of type `long`
159 * @pre Must not pass anything other than a Fixnum.
160 */
161 static inline long
162 rbimpl_fix2long_by_idiv(VALUE x)
163 {
164 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
165
166 /* :NOTE: VALUE can be wider than long. (x-1)/2 never overflows because
167 * RB_FIXNUM_P(x) holds. Also it has no portability issue like y>>1
168 * below. */
169 const SIGNED_VALUE y = x - RUBY_FIXNUM_FLAG;
170 const SIGNED_VALUE z = y / 2;
171 const long w = RBIMPL_CAST((long)z);
172
173 RBIMPL_ASSERT_OR_ASSUME(RB_FIXABLE(w));
174 return w;
175 }
176
177 RBIMPL_ATTR_CONST_UNLESS_DEBUG()
178 RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14)
179 /**
180 * @private
181 *
182 * This is an implementation detail of rb_fix2long(). People don't use it
183 * directly.
184 *
185 * @param[in] x A Fixnum.
186 * @return Identical value of type `long`
187 * @pre Must not pass anything other than a Fixnum.
188 */
189 static inline long
190 rbimpl_fix2long_by_shift(VALUE x)
191 {
192 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
193
194 /* :NOTE: VALUE can be wider than long. If right shift is arithmetic, this
195 * is noticeably faster than above. */
196 const SIGNED_VALUE y = x;
197 const SIGNED_VALUE z = y >> 1;
198 const long w = RBIMPL_CAST((long)z);
199
200 RBIMPL_ASSERT_OR_ASSUME(RB_FIXABLE(w));
201 return w;
202 }
203
204 RBIMPL_ATTR_CONST()
205 RBIMPL_ATTR_CONSTEXPR(CXX11)
206 /**
207 * @private
208 *
209 * This is an implementation detail of rb_fix2long(). People don't use it
210 * directly.
211 *
212 * @retval true This C compiler's right shift operator is arithmetic.
213 * @retval false This C compiler's right shift operator is logical.
214 */
215 static inline bool
216 rbimpl_right_shift_is_arithmetic_p(void)
217 {
218 return (-1 >> 1) == -1;
219 }
220
221 RBIMPL_ATTR_CONST_UNLESS_DEBUG()
222 RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14)
223 /**
224 * Converts a Fixnum into C's `long`.
225 *
226 * @param[in] x Some Fixnum.
227 * @pre Must not pass anything other than a Fixnum.
228 * @return The passed value converted into C's `long`.
229 */
230 static inline long
231 rb_fix2long(VALUE x)
232 {
233 if /* constexpr */ (rbimpl_right_shift_is_arithmetic_p()) {
234 return rbimpl_fix2long_by_shift(x);
235 }
236 else {
237 return rbimpl_fix2long_by_idiv(x);
238 }
239 }
240
241 RBIMPL_ATTR_CONST_UNLESS_DEBUG()
242 RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14)
243 /**
244 * Converts a Fixnum into C's `unsigned long`.
245 *
246 * @param[in] x Some Fixnum.
247 * @pre Must not pass anything other than a Fixnum.
248 * @return The passed value converted into C's `unsigned long`.
249 * @note Negative fixnums will be converted into large unsigned longs.
250 */
251 static inline unsigned long
252 rb_fix2ulong(VALUE x)
253 {
254 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
255 return rb_fix2long(x);
256 }
257
258 /**
259 * Converts an instance of ::rb_cNumeric into C's `long`.
260 *
261 * @param[in] x Something numeric.
262 * @exception rb_eTypeError `x` is not a numeric.
263 * @exception rb_eRangeError `x` is out of range of `long`.
264 * @return The passed value converted into C's `long`.
265 */
266 static inline long
267 rb_num2long_inline(VALUE x)
268 {
269 if (RB_FIXNUM_P(x))
270 return RB_FIX2LONG(x);
271 else
272 return rb_num2long(x);
273 }
274
275 /**
276 * Converts an instance of ::rb_cNumeric into C's `unsigned long`.
277 *
278 * @param[in] x Something numeric.
279 * @exception rb_eTypeError `x` is not a numeric.
280 * @exception rb_eRangeError `x` is out of range of `unsigned long`.
281 * @return The passed value converted into C's `unsigned long`.
282 *
283 * @internal
284 *
285 * This (negative fixnum would become a large unsigned long while negative
286 * bignum is an exception) has been THE behaviour of NUM2ULONG since the
287 * beginning. It is strange, but we can no longer change how it works at this
288 * moment. We have to get by with it.
289 *
290 * @see https://bugs.ruby-lang.org/issues/9089
291 */
292 static inline unsigned long
293 rb_num2ulong_inline(VALUE x)
294 {
295 if (RB_FIXNUM_P(x))
296 return RB_FIX2ULONG(x);
297 else
298 return rb_num2ulong(x);
299 }
300
301 /**
302 * Converts a C's `long` into an instance of ::rb_cInteger.
303 *
304 * @param[in] v Arbitrary `long` value.
305 * @return An instance of ::rb_cInteger.
306 */
307 static inline VALUE
308 rb_long2num_inline(long v)
309 {
310 if (RB_FIXABLE(v))
311 return RB_LONG2FIX(v);
312 else
313 return rb_int2big(v);
314 }
315
316 /**
317 * Converts a C's `unsigned long` into an instance of ::rb_cInteger.
318 *
319 * @param[in] v Arbitrary `unsigned long` value.
320 * @return An instance of ::rb_cInteger.
321 */
322 static inline VALUE
323 rb_ulong2num_inline(unsigned long v)
324 {
325 if (RB_POSFIXABLE(v))
326 return RB_LONG2FIX(v);
327 else
328 return rb_uint2big(v);
329 }
330
331 /**
332 * @cond INTERNAL_MACRO
333 *
334 * Following overload is necessary because sometimes INT2FIX is used as a enum
335 * value (e.g. `enum { FOO = INT2FIX(0) };`). THIS IS NG in theory because a
336 * VALUE does not fit into an enum (which must be a signed int). But we cannot
337 * break existing codes.
338 */
339 #if RBIMPL_HAS_ATTR_CONSTEXPR_CXX14
340 # /* C++ can write constexpr as enum values. */
341
342 #elif ! defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P)
343 # undef INT2FIX
344 # define INT2FIX(i) (RBIMPL_CAST((VALUE)(i)) << 1 | RUBY_FIXNUM_FLAG)
345
346 #else
347 # undef INT2FIX
348 # define INT2FIX(i) \
349 __builtin_choose_expr( \
350 __builtin_constant_p(i), \
351 RBIMPL_CAST((VALUE)(i)) << 1 | RUBY_FIXNUM_FLAG, \
352 RB_INT2FIX(i))
353 #endif
354 /** @endcond */
355
356 #endif /* RBIMPL_ARITHMETIC_LONG_H */