mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/numeric.h@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/numeric.h - Numeric, Integer, Float, Fixnum class |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_NUMERIC_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_NUMERIC_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 11 | extern "C" { |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | #define POSFIXABLE(f) ((f) <= MRB_INT_MAX) |
mzta | 0:158c61bb030f | 15 | #define NEGFIXABLE(f) ((f) >= MRB_INT_MIN) |
mzta | 0:158c61bb030f | 16 | #define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f)) |
mzta | 0:158c61bb030f | 17 | |
mzta | 0:158c61bb030f | 18 | MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val); |
mzta | 0:158c61bb030f | 19 | MRB_API mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, int base); |
mzta | 0:158c61bb030f | 20 | MRB_API mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x); |
mzta | 0:158c61bb030f | 21 | |
mzta | 0:158c61bb030f | 22 | mrb_value mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y); |
mzta | 0:158c61bb030f | 23 | mrb_value mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y); |
mzta | 0:158c61bb030f | 24 | mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y); |
mzta | 0:158c61bb030f | 25 | mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y); |
mzta | 0:158c61bb030f | 26 | |
mzta | 0:158c61bb030f | 27 | #define MRB_UINT_MAKE2(n) uint ## n ## _t |
mzta | 0:158c61bb030f | 28 | #define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n) |
mzta | 0:158c61bb030f | 29 | #define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT) |
mzta | 0:158c61bb030f | 30 | |
mzta | 0:158c61bb030f | 31 | #ifdef MRB_WORD_BOXING |
mzta | 0:158c61bb030f | 32 | # define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT)) |
mzta | 0:158c61bb030f | 33 | #else |
mzta | 0:158c61bb030f | 34 | # define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1)) |
mzta | 0:158c61bb030f | 35 | #endif |
mzta | 0:158c61bb030f | 36 | |
mzta | 0:158c61bb030f | 37 | static inline mrb_bool |
mzta | 0:158c61bb030f | 38 | mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum) |
mzta | 0:158c61bb030f | 39 | { |
mzta | 0:158c61bb030f | 40 | mrb_uint x = (mrb_uint)augend; |
mzta | 0:158c61bb030f | 41 | mrb_uint y = (mrb_uint)addend; |
mzta | 0:158c61bb030f | 42 | mrb_uint z = (mrb_uint)(x + y); |
mzta | 0:158c61bb030f | 43 | *sum = (mrb_int)z; |
mzta | 0:158c61bb030f | 44 | return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK); |
mzta | 0:158c61bb030f | 45 | } |
mzta | 0:158c61bb030f | 46 | |
mzta | 0:158c61bb030f | 47 | static inline mrb_bool |
mzta | 0:158c61bb030f | 48 | mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference) |
mzta | 0:158c61bb030f | 49 | { |
mzta | 0:158c61bb030f | 50 | mrb_uint x = (mrb_uint)minuend; |
mzta | 0:158c61bb030f | 51 | mrb_uint y = (mrb_uint)subtrahend; |
mzta | 0:158c61bb030f | 52 | mrb_uint z = (mrb_uint)(x - y); |
mzta | 0:158c61bb030f | 53 | *difference = (mrb_int)z; |
mzta | 0:158c61bb030f | 54 | return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK); |
mzta | 0:158c61bb030f | 55 | } |
mzta | 0:158c61bb030f | 56 | |
mzta | 0:158c61bb030f | 57 | #undef MRB_INT_OVERFLOW_MASK |
mzta | 0:158c61bb030f | 58 | #undef mrb_uint |
mzta | 0:158c61bb030f | 59 | #undef MRB_UINT_MAKE |
mzta | 0:158c61bb030f | 60 | #undef MRB_UINT_MAKE2 |
mzta | 0:158c61bb030f | 61 | |
mzta | 0:158c61bb030f | 62 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 63 | } /* extern "C" { */ |
mzta | 0:158c61bb030f | 64 | #endif |
mzta | 0:158c61bb030f | 65 | |
mzta | 0:158c61bb030f | 66 | #endif /* MRUBY_NUMERIC_H */ |
mzta | 0:158c61bb030f | 67 |