Ruby 4.1.0dev (2026-10-30 revision 7704181e64db25a20de9d5e89ff9661af4fd1820)
long.h
Go to the documentation of this file.
1#ifndef RBIMPL_ARITHMETIC_LONG_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_ARITHMETIC_LONG_H
31#include "ruby/internal/config.h"
32#include "ruby/internal/arithmetic/fixnum.h" /* FIXABLE */
33#include "ruby/internal/arithmetic/intptr_t.h" /* rb_int2big etc.*/
40#include "ruby/internal/cast.h"
42#include "ruby/internal/special_consts.h" /* FIXNUM_FLAG */
43#include "ruby/internal/value.h"
44#include "ruby/assert.h"
45
46#define FIX2LONG RB_FIX2LONG
47#define FIX2ULONG RB_FIX2ULONG
48#define INT2FIX RB_INT2FIX
49#define LONG2FIX RB_INT2FIX
50#define LONG2NUM RB_LONG2NUM
51#define NUM2LONG RB_NUM2LONG
52#define NUM2ULONG RB_NUM2ULONG
53#define RB_FIX2LONG rb_fix2long
54#define RB_FIX2ULONG rb_fix2ulong
55#define RB_LONG2FIX RB_INT2FIX
56#define RB_LONG2NUM rb_long2num_inline
57#define RB_NUM2LONG rb_num2long_inline
58#define RB_NUM2ULONG rb_num2ulong_inline
59#define RB_ULONG2NUM rb_ulong2num_inline
60#define ULONG2NUM RB_ULONG2NUM
61#define rb_fix_new RB_INT2FIX
62#define rb_long2int rb_long2int_inline
65#define RB_INT2FIX RB_INT2FIX
69
79
89
98long rb_num2long(VALUE num);
99
108unsigned long rb_num2ulong(VALUE num);
110
111/* defined in len_t.h */
115static inline VALUE RB_INT2FIX(long i);
116static inline long rb_fix2long(VALUE x);
117static inline unsigned long rb_fix2ulong(VALUE x);
118
126static inline int
128{
129 int i = RBIMPL_CAST((int)n);
130
131 if /* constexpr */ (sizeof(long) <= sizeof(int)) {
132 RBIMPL_ASSUME(i == n);
133 }
134
135 if (i != n)
136 rb_out_of_int(n);
137
138 return i;
139}
140
149static inline long
151{
152 if (RB_FIXNUM_P(x))
153 return RB_FIX2LONG(x);
154 else
155 return rb_num2long(x);
156}
157
175static inline unsigned long
177{
178 if (RB_FIXNUM_P(x))
179 return RB_FIX2ULONG(x);
180 else
181 return rb_num2ulong(x);
182}
183
190static inline VALUE
192{
193 if (RB_FIXABLE(v))
194 return RB_LONG2FIX(v);
195 else
196 return rb_int2big(v);
197}
198
205static inline VALUE
206rb_ulong2num_inline(unsigned long v)
207{
208 if (RB_POSFIXABLE(v))
209 return RB_LONG2FIX(RBIMPL_CAST((long)v));
210 else
211 return rb_uint2big(v);
212}
213
222#if RBIMPL_HAS_ATTR_CONSTEXPR_CXX14
223# /* C++ can write constexpr as enum values. */
224
225#elif ! defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P)
226# undef INT2FIX
227# define INT2FIX(i) (RBIMPL_CAST((VALUE)(i)) << 1 | RUBY_FIXNUM_FLAG)
228
229#else
230# undef INT2FIX
231# define INT2FIX(i) \
232 __builtin_choose_expr( \
233 __builtin_constant_p(i), \
234 RBIMPL_CAST((VALUE)(i)) << 1 | RUBY_FIXNUM_FLAG, \
235 RB_INT2FIX(i))
236#endif
239#endif /* RBIMPL_ARITHMETIC_LONG_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition artificial.h:43
Defines RBIMPL_ATTR_COLD.
#define RBIMPL_ATTR_COLD()
Wraps (or simulates) __attribute__((cold))
Definition cold.h:34
Defines RBIMPL_ATTR_CONST.
#define RBIMPL_ATTR_CONST_UNLESS_DEBUG()
Enables RBIMPL_ATTR_CONST if and only if.
Definition const.h:41
RBIMPL_ATTR_CONSTEXPR.
#define RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(_)
Enables RBIMPL_ATTR_CONSTEXPR if and only if.
Definition constexpr.h:79
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
Defines RBIMPL_ASSUME / RBIMPL_UNREACHABLE.
#define RBIMPL_ASSUME(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:76
Arithmetic conversion between C's intptr_t and Ruby's.
static long rb_num2long_inline(VALUE x)
Converts an instance of rb_cNumeric into C's long.
Definition long.h:150
#define RB_FIX2ULONG
Just another name of rb_fix2ulong.
Definition long.h:54
static unsigned long rb_num2ulong_inline(VALUE x)
Converts an instance of rb_cNumeric into C's unsigned long.
Definition long.h:176
static int rb_long2int_inline(long n)
Checks if int can hold the given integer.
Definition long.h:127
static VALUE rb_ulong2num_inline(unsigned long v)
Converts a C's unsigned long into an instance of rb_cInteger.
Definition long.h:206
#define RB_FIX2LONG
Just another name of rb_fix2long.
Definition long.h:53
void rb_out_of_int(SIGNED_VALUE num)
This is an utility function to raise an rb_eRangeError.
Definition numeric.c:3191
void rb_out_of_long(SIGNED_VALUE num)
This is an utility function to raise an rb_eRangeError.
Definition numeric.c:3198
long rb_num2long(VALUE num)
Converts an instance of rb_cNumeric into C's long.
Definition numeric.c:3116
#define RB_LONG2FIX
Just another name of RB_INT2FIX.
Definition long.h:55
unsigned long rb_num2ulong(VALUE num)
Converts an instance of rb_cNumeric into C's unsigned long.
Definition numeric.c:3185
static VALUE rb_long2num_inline(long v)
Converts a C's long into an instance of rb_cInteger.
Definition long.h:191
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
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.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40