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

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
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
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
54 RBIMPL_SYMBOL_EXPORT_BEGIN()
55 long rb_num2int(VALUE);
56long rb_fix2int(VALUE);
57unsigned long rb_num2uint(VALUE);
58unsigned long rb_fix2uint(VALUE);
59 RBIMPL_SYMBOL_EXPORT_END()
60
61 RBIMPL_ATTR_ARTIFICIAL()
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
101 RBIMPL_ATTR_ARTIFICIAL()
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
117 RBIMPL_ATTR_ARTIFICIAL()
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 */
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
rb_int2big
VALUE rb_int2big(intptr_t n)
Definition: bignum.c:3186
rb_uint2big
VALUE rb_uint2big(uintptr_t n)
Definition: bignum.c:3164
compiler_is.h
Defines RBIMPL_COMPILER_IS.
const.h
Defines RBIMPL_ATTR_CONST.
constexpr.h
RBIMPL_ATTR_CONSTEXPR.
range
#define range(low, item, hi)
Definition: date_strftime.c:21
dllexport.h
Tewaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
out
string_t out
Definition: enough.c:230
fixnum.h
Handling of integers formerly known as Fixnums.
RB_FIXABLE
#define RB_FIXABLE(_)
Definition: fixnum.h:40
RB_POSFIXABLE
#define RB_POSFIXABLE(_)
Definition: fixnum.h:38
config.h
Thin wrapper to ruby/config.h.
rb_num2int
long rb_num2int(VALUE)
Definition: numeric.c:3042
rb_fix2uint
unsigned long rb_fix2uint(VALUE)
Definition: numeric.c:3060
rb_num2uint
unsigned long rb_num2uint(VALUE)
Definition: numeric.c:3054
rb_fix2int
long rb_fix2int(VALUE)
Definition: numeric.c:3048
intptr_t.h
Arithmetic conversion between C's intptr_t and Ruby's.
long
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
int
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
long.h
Arithmetic conversion between C's long and Ruby's.
RB_FIX2ULONG
#define RB_FIX2ULONG
Definition: long.h:54
RB_NUM2ULONG
#define RB_NUM2ULONG
Definition: long.h:58
RB_FIX2LONG
#define RB_FIX2LONG
Definition: long.h:53
RB_LONG2FIX
#define RB_LONG2FIX
Definition: long.h:55
RB_NUM2LONG
#define RB_NUM2LONG
Definition: long.h:57
special_consts.h
Defines enum ruby_special_consts.
value.h
Defines VALUE and ID.
VALUE
unsigned long VALUE
Definition: value.h:38
warning_push.h
Defines RBIMPL_WARNING_PUSH.