mruby 3.3.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
numeric.h
Go to the documentation of this file.
1
7#ifndef MRUBY_NUMERIC_H
8#define MRUBY_NUMERIC_H
9
10#include "common.h"
11
18
19#define TYPED_POSFIXABLE(f,t) ((f) <= (t)MRB_FIXNUM_MAX)
20#define TYPED_NEGFIXABLE(f,t) ((f) >= (t)MRB_FIXNUM_MIN)
21#define TYPED_FIXABLE(f,t) (TYPED_POSFIXABLE(f,t) && TYPED_NEGFIXABLE(f,t))
22#define POSFIXABLE(f) TYPED_POSFIXABLE(f,mrb_int)
23#define NEGFIXABLE(f) TYPED_NEGFIXABLE(f,mrb_int)
24#define FIXABLE(f) TYPED_FIXABLE(f,mrb_int)
25#ifndef MRB_NO_FLOAT
26#ifdef MRB_INT64
27#define FIXABLE_FLOAT(f) ((f)>=-9223372036854775808.0 && (f)<9223372036854775808.0)
28#else
29#define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,mrb_float)
30#endif
31#endif
32
33/* utility functions */
34MRB_API mrb_value mrb_num_add(mrb_state *mrb, mrb_value x, mrb_value y);
35MRB_API mrb_value mrb_num_sub(mrb_state *mrb, mrb_value x, mrb_value y);
36MRB_API mrb_value mrb_num_mul(mrb_state *mrb, mrb_value x, mrb_value y);
37/* obsolete old names */
38#define mrb_num_plus(mrb, x, y) mrb_num_add(mrb, x, y)
39#define mrb_num_minus(mrb, x, y) mrb_num_sub(mrb, x, y)
40
41MRB_API mrb_value mrb_integer_to_str(mrb_state *mrb, mrb_value x, mrb_int base);
42MRB_API char *mrb_int_to_cstr(char *buf, size_t len, mrb_int n, mrb_int base);
43
44/* obsolete function(s); will be removed */
45#define mrb_fixnum_to_str(mrb, x, base) mrb_integer_to_str(mrb, x, base)
46
47#ifndef __has_builtin
48 #define __has_builtin(x) 0
49#endif
50
51#if (defined(__GNUC__) && __GNUC__ >= 5) || \
52 (__has_builtin(__builtin_add_overflow) && \
53 __has_builtin(__builtin_sub_overflow) && \
54 __has_builtin(__builtin_mul_overflow))
55# define MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
56#endif
57
58/*
59// Clang 3.8 and 3.9 have problem compiling mruby in 32-bit mode, when MRB_INT64 is set
60// because of missing __mulodi4 and similar functions in its runtime. We need to use custom
61// implementation for them.
62*/
63#ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
64#if defined(__clang__) && (__clang_major__ == 3) && (__clang_minor__ >= 8) && \
65 defined(MRB_32BIT) && defined(MRB_INT64)
66#undef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
67#endif
68#endif
69
70#ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
71
72static inline mrb_bool
73mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
74{
75 return __builtin_add_overflow(augend, addend, sum);
76}
77
78static inline mrb_bool
79mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
80{
81 return __builtin_sub_overflow(minuend, subtrahend, difference);
82}
83
84static inline mrb_bool
85mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
86{
87 return __builtin_mul_overflow(multiplier, multiplicand, product);
88}
89
90#else
91
92#define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1))
93
94static inline mrb_bool
95mrb_int_add_overflow(mrb_int a, mrb_int b, mrb_int *c)
96{
97 mrb_uint x = (mrb_uint)a;
98 mrb_uint y = (mrb_uint)b;
99 mrb_uint z = (mrb_uint)(x + y);
100 *c = (mrb_int)z;
101 return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
102}
103
104static inline mrb_bool
105mrb_int_sub_overflow(mrb_int a, mrb_int b, mrb_int *c)
106{
107 mrb_uint x = (mrb_uint)a;
108 mrb_uint y = (mrb_uint)b;
109 mrb_uint z = (mrb_uint)(x - y);
110 *c = (mrb_int)z;
111 return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
112}
113
114static inline mrb_bool
115mrb_int_mul_overflow(mrb_int a, mrb_int b, mrb_int *c)
116{
117#ifdef MRB_INT32
118 int64_t n = (int64_t)a * b;
119 *c = (mrb_int)n;
120 return n > MRB_INT_MAX || n < MRB_INT_MIN;
121#else /* MRB_INT64 */
122 if (a > 0 && b > 0 && a > MRB_INT_MAX / b) return TRUE;
123 if (a < 0 && b > 0 && a < MRB_INT_MIN / b) return TRUE;
124 if (a > 0 && b < 0 && b < MRB_INT_MIN / a) return TRUE;
125 if (a < 0 && b < 0 && (a <= MRB_INT_MIN || b <= MRB_INT_MIN || -a > MRB_INT_MAX / -b))
126 return TRUE;
127 *c = a * b;
128 return FALSE;
129#endif
130}
131
132#undef MRB_INT_OVERFLOW_MASK
133
134#endif
135
136#ifndef MRB_NO_FLOAT
137
138# define MRB_FLT_RADIX FLT_RADIX
139
140# ifdef MRB_USE_FLOAT32
141# define MRB_FLT_MANT_DIG FLT_MANT_DIG
142# define MRB_FLT_EPSILON FLT_EPSILON
143# define MRB_FLT_DIG FLT_DIG
144# define MRB_FLT_MIN_EXP FLT_MIN_EXP
145# define MRB_FLT_MIN FLT_MIN
146# define MRB_FLT_MIN_10_EXP FLT_MIN_10_EXP
147# define MRB_FLT_MAX_EXP FLT_MAX_EXP
148# define MRB_FLT_MAX FLT_MAX
149# define MRB_FLT_MAX_10_EXP FLT_MAX_10_EXP
150
151# else /* not MRB_USE_FLOAT32 */
152# define MRB_FLT_MANT_DIG DBL_MANT_DIG
153# define MRB_FLT_EPSILON DBL_EPSILON
154# define MRB_FLT_DIG DBL_DIG
155# define MRB_FLT_MIN_EXP DBL_MIN_EXP
156# define MRB_FLT_MIN DBL_MIN
157# define MRB_FLT_MIN_10_EXP DBL_MIN_10_EXP
158# define MRB_FLT_MAX_EXP DBL_MAX_EXP
159# define MRB_FLT_MAX DBL_MAX
160# define MRB_FLT_MAX_10_EXP DBL_MAX_10_EXP
161# endif /* MRB_USE_FLOAT32 */
162
163MRB_API mrb_value mrb_float_to_integer(mrb_state *mrb, mrb_value val);
164
165/* internal functions */
166mrb_float mrb_div_float(mrb_float x, mrb_float y);
167mrb_value mrb_float_to_str(mrb_state *mrb, mrb_value x, const char *fmt);
168int mrb_format_float(mrb_float f, char *buf, size_t buf_size, char fmt, int prec, char sign);
169
170#endif /* MRB_NO_FLOAT */
171
173
174#endif /* MRUBY_NUMERIC_H */
mruby Boolean.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:79
Definition mruby.h:256
Definition boxing_nan.h:40