Ruby: include/ruby/internal/arithmetic/int.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

int.h
Go to the documentation of this file.
1#ifndef RBIMPL_ARITHMETIC_INT_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_ARITHMETIC_INT_H
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
38#define RB_NUM2INT rb_num2int_inline
39#define RB_UINT2NUM rb_uint2num_inline
41#define FIX2INT RB_FIX2INT
42#define FIX2UINT RB_FIX2UINT
43#define INT2NUM RB_INT2NUM
44#define NUM2INT RB_NUM2INT
45#define NUM2UINT RB_NUM2UINT
46#define UINT2NUM RB_UINT2NUM
49#define RB_FIX2INT RB_FIX2INT
50#define RB_NUM2UINT RB_NUM2UINT
51#define RB_FIX2UINT RB_FIX2UINT
54 RBIMPL_SYMBOL_EXPORT_BEGIN()
55
56
70long rb_num2int(VALUE num);
71
85long rb_fix2int(VALUE num);
86
102unsigned long rb_num2uint(VALUE num);
103
117unsigned long rb_fix2uint(VALUE num);
118 RBIMPL_SYMBOL_EXPORT_END()
119
120 RBIMPL_ATTR_ARTIFICIAL()
128static inline int
129RB_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
157static inline int
158rb_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
183 RBIMPL_ATTR_ARTIFICIAL()
184static inline unsigned int
185RB_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()
207static inline unsigned int
208RB_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
238static inline VALUE
239rb_int2num_inline(int v)
240{
241 if (RB_FIXABLE(v))
242 return RB_INT2FIX(v);
243 else
244 return rb_int2big(v);
245}
246
253static inline VALUE
254rb_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 */
artificial.h
Defines RBIMPL_ATTR_ARTIFICIAL.
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
assert.h
RBIMPL_ASSERT_OR_ASSUME
#define RBIMPL_ASSERT_OR_ASSUME(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:229
compiler_is.h
Defines RBIMPL_COMPILER_IS.
const.h
Defines RBIMPL_ATTR_CONST.
constexpr.h
RBIMPL_ATTR_CONSTEXPR.
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:106
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:97
fixnum.h
Handling of integers formerly known as Fixnums.
RB_FIXABLE
#define RB_FIXABLE(_)
Checks if the passed value is in range of fixnum.
Definition fixnum.h:52
RB_POSFIXABLE
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
Definition fixnum.h:43
RB_FIX2UINT
static unsigned int RB_FIX2UINT(VALUE x)
Converts a Fixnum into C's int.
Definition int.h:208
rb_num2uint
unsigned long rb_num2uint(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long.
Definition numeric.c:3296
rb_fix2int
long rb_fix2int(VALUE num)
Identical to rb_num2int().
Definition numeric.c:3290
rb_num2int_inline
static int rb_num2int_inline(VALUE x)
Converts an instance of rb_cNumeric into C's int.
Definition int.h:158
rb_num2int
long rb_num2int(VALUE num)
Converts an instance of rb_cNumeric into C's long.
Definition numeric.c:3284
rb_uint2num_inline
static VALUE rb_uint2num_inline(unsigned int v)
Converts a C's unsigned int into an instance of rb_cInteger.
Definition int.h:254
RB_FIX2INT
static int RB_FIX2INT(VALUE x)
Converts a Fixnum into C's int.
Definition int.h:129
RB_NUM2UINT
static unsigned int RB_NUM2UINT(VALUE x)
Converts an instance of rb_cNumeric into C's unsigned int.
Definition int.h:185
rb_int2num_inline
static VALUE rb_int2num_inline(int v)
Converts a C's int into an instance of rb_cInteger.
Definition int.h:239
rb_fix2uint
unsigned long rb_fix2uint(VALUE num)
Identical to rb_num2uint().
Definition numeric.c:3302
intptr_t.h
Arithmetic conversion between C's intptr_t and Ruby's.
long.h
Arithmetic conversion between C's long and Ruby's.
RB_INT2FIX
static VALUE RB_INT2FIX(long i)
Converts a C's long into an instance of rb_cInteger.
Definition long.h:111
RB_FIX2ULONG
#define RB_FIX2ULONG
Just another name of rb_fix2ulong.
Definition long.h:54
RB_NUM2ULONG
#define RB_NUM2ULONG
Just another name of rb_num2ulong_inline.
Definition long.h:58
RB_FIX2LONG
#define RB_FIX2LONG
Just another name of rb_fix2long.
Definition long.h:53
RB_LONG2FIX
#define RB_LONG2FIX
Just another name of RB_INT2FIX.
Definition long.h:55
RB_NUM2LONG
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
Definition long.h:57
special_consts.h
Defines enum ruby_special_consts.
RB_FIXNUM_P
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
Definition special_consts.h:247
value.h
Defines VALUE and ID.
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
warning_push.h
Defines RBIMPL_WARNING_PUSH.
RBIMPL_WARNING_IGNORED
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
Definition warning_push.h:80
RBIMPL_WARNING_PUSH
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
Definition warning_push.h:55
RBIMPL_WARNING_POP
#define RBIMPL_WARNING_POP()
Pops compiler warning state.
Definition warning_push.h:62