Debian Codegraph @

include/ruby/internal/arithmetic/int.h

raw
1 #ifndef RBIMPL_ARITHMETIC_INT_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_ARITHMETIC_INT_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 `int` and Ruby's.
22 */
23 #include "ruby/internal/config.h"
24 #include "ruby/internal/arithmetic/fixnum.h"
25 #include "ruby/internal/arithmetic/intptr_t.h"
26 #include "ruby/internal/arithmetic/long.h"
27 #include "ruby/internal/attr/artificial.h"
28 #include "ruby/internal/attr/const.h"
29 #include "ruby/internal/attr/constexpr.h"
30 #include "ruby/internal/compiler_is.h"
31 #include "ruby/internal/dllexport.h"
32 #include "ruby/internal/special_consts.h"
33 #include "ruby/internal/value.h"
34 #include "ruby/internal/warning_push.h"
35 #include "ruby/assert.h"
36
37 #define RB_INT2NUM rb_int2num_inline /**< @alias{rb_int2num_inline} */
38 #define RB_NUM2INT rb_num2int_inline /**< @alias{rb_num2int_inline} */
39 #define RB_UINT2NUM rb_uint2num_inline /**< @alias{rb_uint2num_inline} */
40
41 #define FIX2INT RB_FIX2INT /**< @old{RB_FIX2INT} */
42 #define FIX2UINT RB_FIX2UINT /**< @old{RB_FIX2UINT} */
43 #define INT2NUM RB_INT2NUM /**< @old{RB_INT2NUM} */
44 #define NUM2INT RB_NUM2INT /**< @old{RB_NUM2INT} */
45 #define NUM2UINT RB_NUM2UINT /**< @old{RB_NUM2UINT} */
46 #define UINT2NUM RB_UINT2NUM /**< @old{RB_UINT2NUM} */
47
48 /** @cond INTERNAL_MACRO */
49 #define RB_FIX2INT RB_FIX2INT
50 #define RB_NUM2UINT RB_NUM2UINT
51 #define RB_FIX2UINT RB_FIX2UINT
52 /** @endcond */
53
54 RBIMPL_SYMBOL_EXPORT_BEGIN()
55
56 /**
57 * Converts an instance of ::rb_cNumeric into C's `long`.
58 *
59 * @param[in] num Something numeric.
60 * @exception rb_eTypeError `num` is not a numeric.
61 * @exception rb_eRangeError `num` is out of range of `int`.
62 * @return The passed value converted into C's `long`.
63 *
64 * @internal
65 *
66 * Yes, the API is really strange. It returns `long`, but raises when the
67 * value is out of `int`. This seems to be due to the fact that Matz favoured
68 * K&R before, and his machine at that moment was an ILP32 architecture.
69 */
70 long rb_num2int(VALUE num);
71
72 /**
73 * Identical to rb_num2int().
74 *
75 * @param[in] num Something numeric.
76 * @exception rb_eTypeError `num` is not a numeric.
77 * @exception rb_eRangeError `num` is out of range of `int`.
78 * @return The passed value converted into C's `long`.
79 *
80 * @internal
81 *
82 * This function seems to be a complete waste of disk space. @shyouhei has no
83 * idea why this is a different thing from rb_num2short().
84 */
85 long rb_fix2int(VALUE num);
86
87 /**
88 * Converts an instance of ::rb_cNumeric into C's `unsigned long`.
89 *
90 * @param[in] num Something numeric.
91 * @exception rb_eTypeError `num` is not a numeric.
92 * @exception rb_eRangeError `num` is out of range of `unsigned int`.
93 * @return The passed value converted into C's `unsigned long`.
94 *
95 * @internal
96 *
97 * Yes, the API is really strange. It returns `unsigned long`, but raises when
98 * the value is out of `unsigned int`. This seems to be due to the fact that
99 * Matz favoured K&R before, and his machine at that moment was an ILP32
100 * architecture.
101 */
102 unsigned long rb_num2uint(VALUE num);
103
104 /**
105 * Identical to rb_num2uint().
106 *
107 * @param[in] num Something numeric.
108 * @exception rb_eTypeError `num` is not a numeric.
109 * @exception rb_eRangeError `num` is out of range of `unsigned int`.
110 * @return The passed value converted into C's `unsigned long`.
111 *
112 * @internal
113 *
114 * This function seems to be a complete waste of disk space. @shyouhei has no
115 * idea why this is a different thing from rb_num2short().
116 */
117 unsigned long rb_fix2uint(VALUE num);
118 RBIMPL_SYMBOL_EXPORT_END()
119
120 RBIMPL_ATTR_ARTIFICIAL()
121 /**
122 * Converts a Fixnum into C's `int`.
123 *
124 * @param[in] x Some Fixnum.
125 * @pre Must not pass anything other than a Fixnum.
126 * @return The passed value converted into C's `int`.
127 */
128 static inline int
129 RB_FIX2INT(VALUE x)
130 {
131 /* "FIX2INT raises a TypeError if passed nil", says rubyspec. Not sure if
132 * that is a desired behaviour but just preserve backwards compatilibily.
133 */
134 #if 0
135 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
136 #endif
137 long ret;
138
139 if /* constexpr */ (sizeof(int) < sizeof(long)) {
140 ret = rb_fix2int(x);
141 }
142 else {
143 ret = RB_FIX2LONG(x);
144 }
145
146 return RBIMPL_CAST((int)ret);
147 }
148
149 /**
150 * Converts an instance of ::rb_cNumeric into C's `int`.
151 *
152 * @param[in] x Something numeric.
153 * @exception rb_eTypeError `x` is not a numeric.
154 * @exception rb_eRangeError `x` is out of range of `int`.
155 * @return The passed value converted into C's `int`.
156 */
157 static inline int
158 rb_num2int_inline(VALUE x)
159 {
160 long ret;
161
162 if /* constexpr */ (sizeof(int) == sizeof(long)) {
163 ret = RB_NUM2LONG(x);
164 }
165 else if (RB_FIXNUM_P(x)) {
166 ret = rb_fix2int(x);
167 }
168 else {
169 ret = rb_num2int(x);
170 }
171
172 return RBIMPL_CAST((int)ret);
173 }
174
175 /**
176 * Converts an instance of ::rb_cNumeric into C's `unsigned int`.
177 *
178 * @param[in] x Something numeric.
179 * @exception rb_eTypeError `x` is not a numeric.
180 * @exception rb_eRangeError `x` is out of range of `unsigned int`.
181 * @return The passed value converted into C's `unsigned int`.
182 */
183 RBIMPL_ATTR_ARTIFICIAL()
184 static inline unsigned int
185 RB_NUM2UINT(VALUE x)
186 {
187 unsigned long ret;
188
189 if /* constexpr */ (sizeof(int) < sizeof(long)) {
190 ret = rb_num2uint(x);
191 }
192 else {
193 ret = RB_NUM2ULONG(x);
194 }
195
196 return RBIMPL_CAST((unsigned int)ret);
197 }
198
199 RBIMPL_ATTR_ARTIFICIAL()
200 /**
201 * Converts a Fixnum into C's `int`.
202 *
203 * @param[in] x Some Fixnum.
204 * @pre Must not pass anything other than a Fixnum.
205 * @return The passed value converted into C's `int`.
206 */
207 static inline unsigned int
208 RB_FIX2UINT(VALUE x)
209 {
210 #if 0 /* Ditto for RB_FIX2INT. */
211 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
212 #endif
213 unsigned long ret;
214
215 if /* constexpr */ (sizeof(int) < sizeof(long)) {
216 ret = rb_fix2uint(x);
217 }
218 else {
219 ret = RB_FIX2ULONG(x);
220 }
221
222 return RBIMPL_CAST((unsigned int)ret);
223 }
224
225 RBIMPL_WARNING_PUSH()
226 #if RBIMPL_COMPILER_IS(GCC)
227 RBIMPL_WARNING_IGNORED(-Wtype-limits) /* We can ignore them here. */
228 #elif RBIMPL_HAS_WARNING("-Wtautological-constant-out-of-range-compare")
229 RBIMPL_WARNING_IGNORED(-Wtautological-constant-out-of-range-compare)
230 #endif
231
232 /**
233 * Converts a C's `int` into an instance of ::rb_cInteger.
234 *
235 * @param[in] v Arbitrary `int` value.
236 * @return An instance of ::rb_cInteger.
237 */
238 static inline VALUE
239 rb_int2num_inline(int v)
240 {
241 if (RB_FIXABLE(v))
242 return RB_INT2FIX(v);
243 else
244 return rb_int2big(v);
245 }
246
247 /**
248 * Converts a C's `unsigned int` into an instance of ::rb_cInteger.
249 *
250 * @param[in] v Arbitrary `unsigned int` value.
251 * @return An instance of ::rb_cInteger.
252 */
253 static inline VALUE
254 rb_uint2num_inline(unsigned int v)
255 {
256 if (RB_POSFIXABLE(v))
257 return RB_LONG2FIX(v);
258 else
259 return rb_uint2big(v);
260 }
261
262 RBIMPL_WARNING_POP()
263
264 #endif /* RBIMPL_ARITHMETIC_INT_H */