Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
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
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
40
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
47
49#define RB_FIX2INT RB_FIX2INT
50#define RB_NUM2UINT RB_NUM2UINT
51#define RB_FIX2UINT RB_FIX2UINT
56long rb_fix2int(VALUE);
57unsigned long rb_num2uint(VALUE);
58unsigned long rb_fix2uint(VALUE);
60
62static inline int
63RB_FIX2INT(VALUE x)
64{
65 /* "FIX2INT raises a TypeError if passed nil", says rubyspec. Not sure if
66 * that is a desired behaviour but just preserve backwards compatilibily.
67 */
68#if 0
69 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
70#endif
71 long ret;
72
73 if /* constexpr */ (sizeof(int) < sizeof(long)) {
74 ret = rb_fix2int(x);
75 }
76 else {
77 ret = RB_FIX2LONG(x);
78 }
79
80 return RBIMPL_CAST((int)ret);
81}
82
83static inline int
84rb_num2int_inline(VALUE x)
85{
86 long ret;
87
88 if /* constexpr */ (sizeof(int) == sizeof(long)) {
89 ret = RB_NUM2LONG(x);
90 }
91 else if (RB_FIXNUM_P(x)) {
92 ret = rb_fix2int(x);
93 }
94 else {
95 ret = rb_num2int(x);
96 }
97
98 return RBIMPL_CAST((int)ret);
99}
100
102static inline unsigned int
103RB_NUM2UINT(VALUE x)
104{
105 unsigned long ret;
106
107 if /* constexpr */ (sizeof(int) < sizeof(long)) {
108 ret = rb_num2uint(x);
109 }
110 else {
111 ret = RB_NUM2ULONG(x);
112 }
113
114 return RBIMPL_CAST((unsigned int)ret);
115}
116
118static inline unsigned int
119RB_FIX2UINT(VALUE x)
120{
121#if 0 /* Ditto for RB_FIX2INT. */
122 RBIMPL_ASSERT_OR_ASSUME(RB_FIXNUM_P(x));
123#endif
124 unsigned long ret;
125
126 if /* constexpr */ (sizeof(int) < sizeof(long)) {
127 ret = rb_fix2uint(x);
128 }
129 else {
130 ret = RB_FIX2ULONG(x);
131 }
132
133 return RBIMPL_CAST((unsigned int)ret);
134}
135
136RBIMPL_WARNING_PUSH()
137#if RBIMPL_COMPILER_IS(GCC)
138RBIMPL_WARNING_IGNORED(-Wtype-limits) /* We can ignore them here. */
139#elif RBIMPL_HAS_WARNING("-Wtautological-constant-out-of-range-compare")
140RBIMPL_WARNING_IGNORED(-Wtautological-constant-out-of-range-compare)
141#endif
142
143static inline VALUE
144rb_int2num_inline(int v)
145{
146 if (RB_FIXABLE(v))
147 return RB_INT2FIX(v);
148 else
149 return rb_int2big(v);
150}
151
152static inline VALUE
153rb_uint2num_inline(unsigned int v)
154{
155 if (RB_POSFIXABLE(v))
156 return RB_LONG2FIX(v);
157 else
158 return rb_uint2big(v);
159}
160
161RBIMPL_WARNING_POP()
162
163#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(expr)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition: assert.h:229
VALUE rb_int2big(intptr_t n)
Definition: bignum.c:3186
VALUE rb_uint2big(uintptr_t n)
Definition: bignum.c:3164
Defines RBIMPL_COMPILER_IS.
Defines RBIMPL_ATTR_CONST.
RBIMPL_ATTR_CONSTEXPR.
#define range(low, item, hi)
Definition: date_strftime.c:21
Tewaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
string_t out
Definition: enough.c:230
Handling of integers formerly known as Fixnums.
#define RB_FIXABLE(_)
Definition: fixnum.h:40
#define RB_POSFIXABLE(_)
Definition: fixnum.h:38
Thin wrapper to ruby/config.h.
long rb_num2int(VALUE)
Definition: numeric.c:3042
unsigned long rb_fix2uint(VALUE)
Definition: numeric.c:3060
unsigned long rb_num2uint(VALUE)
Definition: numeric.c:3054
long rb_fix2int(VALUE)
Definition: numeric.c:3048
Arithmetic conversion between C's intptr_t and Ruby's.
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
Arithmetic conversion between C's long and Ruby's.
#define RB_FIX2ULONG
Definition: long.h:54
#define RB_NUM2ULONG
Definition: long.h:58
#define RB_FIX2LONG
Definition: long.h:53
#define RB_LONG2FIX
Definition: long.h:55
#define RB_NUM2LONG
Definition: long.h:57
Defines enum ruby_special_consts.
Defines VALUE and ID.
unsigned long VALUE
Definition: value.h:38
Defines RBIMPL_WARNING_PUSH.