1 #include <limits.h>
2 #include <mruby.h>
3 #include <mruby/numeric.h>
4
5 /*
6 * call-seq:
7 * int.allbits?(mask) -> true or false
8 *
9 * Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
10 */
11 static mrb_value
mrb_int_allbits(mrb_state * mrb,mrb_value self)12 mrb_int_allbits(mrb_state *mrb, mrb_value self)
13 {
14 mrb_int n, m;
15
16 mrb_get_args(mrb, "i", &m);
17 n = mrb_int(mrb, self);
18 return mrb_bool_value((n & m) == m);
19 }
20
21 /*
22 * call-seq:
23 * int.anybits?(mask) -> true or false
24 *
25 * Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
26 */
27 static mrb_value
mrb_int_anybits(mrb_state * mrb,mrb_value self)28 mrb_int_anybits(mrb_state *mrb, mrb_value self)
29 {
30 mrb_int n, m;
31
32 mrb_get_args(mrb, "i", &m);
33 n = mrb_int(mrb, self);
34 return mrb_bool_value((n & m) != 0);
35 }
36
37 /*
38 * call-seq:
39 * int.nobits?(mask) -> true or false
40 *
41 * Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
42 */
43 static mrb_value
mrb_int_nobits(mrb_state * mrb,mrb_value self)44 mrb_int_nobits(mrb_state *mrb, mrb_value self)
45 {
46 mrb_int n, m;
47
48 mrb_get_args(mrb, "i", &m);
49 n = mrb_int(mrb, self);
50 return mrb_bool_value((n & m) == 0);
51 }
52
53 void
mrb_mruby_numeric_ext_gem_init(mrb_state * mrb)54 mrb_mruby_numeric_ext_gem_init(mrb_state* mrb)
55 {
56 struct RClass *i = mrb_module_get(mrb, "Integral");
57
58 mrb_define_method(mrb, i, "allbits?", mrb_int_allbits, MRB_ARGS_REQ(1));
59 mrb_define_method(mrb, i, "anybits?", mrb_int_anybits, MRB_ARGS_REQ(1));
60 mrb_define_method(mrb, i, "nobits?", mrb_int_nobits, MRB_ARGS_REQ(1));
61
62 #ifndef MRB_WITHOUT_FLOAT
63 mrb_define_const(mrb, mrb->float_class, "RADIX", mrb_fixnum_value(MRB_FLT_RADIX));
64 mrb_define_const(mrb, mrb->float_class, "MANT_DIG", mrb_fixnum_value(MRB_FLT_MANT_DIG));
65 mrb_define_const(mrb, mrb->float_class, "EPSILON", mrb_float_value(mrb, MRB_FLT_EPSILON));
66 mrb_define_const(mrb, mrb->float_class, "DIG", mrb_fixnum_value(MRB_FLT_DIG));
67 mrb_define_const(mrb, mrb->float_class, "MIN_EXP", mrb_fixnum_value(MRB_FLT_MIN_EXP));
68 mrb_define_const(mrb, mrb->float_class, "MIN", mrb_float_value(mrb, MRB_FLT_MIN));
69 mrb_define_const(mrb, mrb->float_class, "MIN_10_EXP", mrb_fixnum_value(MRB_FLT_MIN_10_EXP));
70 mrb_define_const(mrb, mrb->float_class, "MAX_EXP", mrb_fixnum_value(MRB_FLT_MAX_EXP));
71 mrb_define_const(mrb, mrb->float_class, "MAX", mrb_float_value(mrb, MRB_FLT_MAX));
72 mrb_define_const(mrb, mrb->float_class, "MAX_10_EXP", mrb_fixnum_value(MRB_FLT_MAX_10_EXP));
73 #endif /* MRB_WITHOUT_FLOAT */
74 }
75
76 void
mrb_mruby_numeric_ext_gem_final(mrb_state * mrb)77 mrb_mruby_numeric_ext_gem_final(mrb_state* mrb)
78 {
79 }
80