Ruby 3.5.0dev (2025-02-20 revision 34098b669c0cbc024cd08e686891f1dfe0a10aaf)
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"
33#include "ruby/internal/value.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
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);
119
128static inline int
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
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
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
184static inline unsigned int
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
207static inline unsigned int
209{
210#if 0 /* Ditto for RB_FIX2INT. */
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
226#if RBIMPL_COMPILER_IS(GCC)
227RBIMPL_WARNING_IGNORED(-Wtype-limits) /* We can ignore them here. */
228#elif RBIMPL_HAS_WARNING("-Wtautological-constant-out-of-range-compare")
229RBIMPL_WARNING_IGNORED(-Wtautological-constant-out-of-range-compare)
230#endif
231
238static inline VALUE
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(RBIMPL_CAST((long)v));
258 else
259 return rb_uint2big(v);
260}
261
263
264#endif /* RBIMPL_ARITHMETIC_INT_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
Defines RBIMPL_COMPILER_IS.
Defines RBIMPL_ATTR_CONST.
RBIMPL_ATTR_CONSTEXPR.
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
Handling of integers formerly known as Fixnums.
#define RB_FIXABLE(_)
Checks if the passed value is in range of fixnum.
Definition fixnum.h:52
#define RB_POSFIXABLE(_)
Checks if the passed value is in range of fixnum, assuming it is a positive number.
Definition fixnum.h:43
static unsigned int RB_FIX2UINT(VALUE x)
Converts a Fixnum into C's int.
Definition int.h:208
unsigned long rb_num2uint(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long.
Definition numeric.c:3356
long rb_fix2int(VALUE num)
Identical to rb_num2int().
Definition numeric.c:3350
static int rb_num2int_inline(VALUE x)
Converts an instance of rb_cNumeric into C's int.
Definition int.h:158
long rb_num2int(VALUE num)
Converts an instance of rb_cNumeric into C's long.
Definition numeric.c:3344
static VALUE rb_uint2num_inline(unsigned int v)
Converts a C's unsigned int into an instance of rb_cInteger.
Definition int.h:254
static int RB_FIX2INT(VALUE x)
Converts a Fixnum into C's int.
Definition int.h:129
static unsigned int RB_NUM2UINT(VALUE x)
Converts an instance of rb_cNumeric into C's unsigned int.
Definition int.h:185
static VALUE rb_int2num_inline(int v)
Converts a C's int into an instance of rb_cInteger.
Definition int.h:239
unsigned long rb_fix2uint(VALUE num)
Identical to rb_num2uint().
Definition numeric.c:3362
Arithmetic conversion between C's intptr_t and Ruby's.
Arithmetic conversion between C's long and Ruby's.
static VALUE RB_INT2FIX(long i)
Converts a C's long into an instance of rb_cInteger.
Definition long.h:111
#define RB_FIX2ULONG
Just another name of rb_fix2ulong.
Definition long.h:54
#define RB_NUM2ULONG
Just another name of rb_num2ulong_inline.
Definition long.h:58
#define RB_FIX2LONG
Just another name of rb_fix2long.
Definition long.h:53
#define RB_LONG2FIX
Just another name of RB_INT2FIX.
Definition long.h:55
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
Definition long.h:57
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
Defines enum ruby_special_consts.
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Defines RBIMPL_WARNING_PUSH.
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.