1 | /*
|
---|
2 | ** mruby/numeric.h - Numeric, Integer, Float, Fixnum class
|
---|
3 | **
|
---|
4 | ** See Copyright Notice in mruby.h
|
---|
5 | */
|
---|
6 |
|
---|
7 | #ifndef MRUBY_NUMERIC_H
|
---|
8 | #define MRUBY_NUMERIC_H
|
---|
9 |
|
---|
10 | #include "mruby/common.h"
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Numeric class and it's sub-classes.
|
---|
14 | *
|
---|
15 | * Integer, Float and Fixnum
|
---|
16 | */
|
---|
17 | MRB_BEGIN_DECL
|
---|
18 |
|
---|
19 | #define POSFIXABLE(f) ((f) <= MRB_INT_MAX)
|
---|
20 | #define NEGFIXABLE(f) ((f) >= MRB_INT_MIN)
|
---|
21 | #define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f))
|
---|
22 |
|
---|
23 | MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
|
---|
24 | MRB_API mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, int base);
|
---|
25 | /* ArgumentError if format string doesn't match /%(\.[0-9]+)?[aAeEfFgG]/ */
|
---|
26 | MRB_API mrb_value mrb_float_to_str(mrb_state *mrb, mrb_value x, const char *fmt);
|
---|
27 | MRB_API mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x);
|
---|
28 |
|
---|
29 | mrb_value mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
30 | mrb_value mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
31 | mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
32 | mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
33 |
|
---|
34 | #define MRB_UINT_MAKE2(n) uint ## n ## _t
|
---|
35 | #define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n)
|
---|
36 | #define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT)
|
---|
37 |
|
---|
38 | #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
|
---|
39 |
|
---|
40 | /* Idea from Potion: https://github.com/perl11/potion (MIT) */
|
---|
41 | #if (defined(__clang__) && ((__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ >= 4))) \
|
---|
42 | || (defined(__GNUC__) && __GNUC__ >= 5)
|
---|
43 |
|
---|
44 | static inline mrb_bool
|
---|
45 | mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
|
---|
46 | {
|
---|
47 | mrb_bool of;
|
---|
48 |
|
---|
49 | #ifdef MRB_INT64
|
---|
50 | long long val;
|
---|
51 | of = __builtin_saddll_overflow(augend, addend, &val) ||
|
---|
52 | #else
|
---|
53 | int val;
|
---|
54 | of = __builtin_sadd_overflow(augend, addend, &val) ||
|
---|
55 | #endif
|
---|
56 | (val > MRB_INT_MAX) || (val < MRB_INT_MIN);
|
---|
57 |
|
---|
58 | *sum = (mrb_int) val;
|
---|
59 | return of;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static inline mrb_bool
|
---|
63 | mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
|
---|
64 | {
|
---|
65 | mrb_bool of;
|
---|
66 |
|
---|
67 | #ifdef MRB_INT64
|
---|
68 | long long val;
|
---|
69 | of = __builtin_ssubll_overflow(minuend, subtrahend, &val) ||
|
---|
70 | #else
|
---|
71 | int val;
|
---|
72 | of = __builtin_ssub_overflow(minuend, subtrahend, &val) ||
|
---|
73 | #endif
|
---|
74 | (val > MRB_INT_MAX) || (val < MRB_INT_MIN);
|
---|
75 |
|
---|
76 | *difference = (mrb_int) val;
|
---|
77 | return of;
|
---|
78 | }
|
---|
79 | #else
|
---|
80 |
|
---|
81 | static inline mrb_bool
|
---|
82 | mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
|
---|
83 | {
|
---|
84 | mrb_uint x = (mrb_uint)augend;
|
---|
85 | mrb_uint y = (mrb_uint)addend;
|
---|
86 | mrb_uint z = (mrb_uint)(x + y);
|
---|
87 | *sum = (mrb_int)z;
|
---|
88 | return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static inline mrb_bool
|
---|
92 | mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
|
---|
93 | {
|
---|
94 | mrb_uint x = (mrb_uint)minuend;
|
---|
95 | mrb_uint y = (mrb_uint)subtrahend;
|
---|
96 | mrb_uint z = (mrb_uint)(x - y);
|
---|
97 | *difference = (mrb_int)z;
|
---|
98 | return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
|
---|
99 | }
|
---|
100 |
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | #undef MRB_INT_OVERFLOW_MASK
|
---|
104 | #undef mrb_uint
|
---|
105 | #undef MRB_UINT_MAKE
|
---|
106 | #undef MRB_UINT_MAKE2
|
---|
107 |
|
---|
108 | MRB_END_DECL
|
---|
109 |
|
---|
110 | #endif /* MRUBY_NUMERIC_H */
|
---|