File: bigint.c

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (70 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * If placed under the "mruby/examples/mrbgems/mruby-YOUR-bigint" directory,
 * this file is available under the Creative Commons Zero License (CC0).
 * Note that file is incomplete.
 *
 * TODO: If this file is copied and another implementation is written,
 * remove this comment block from the copied file.
 */

#include <mruby.h>
#include <mruby/numeric.h>

/*
 * The "mruby/internal.h" file should be placed after the other mruby header files.
 */
#include <mruby/internal.h>

/*
 * The "mruby/presym.h" file is placed at the end of the mruby header file.
 */
#include <mruby/presym.h>

/*
 * Define your own struct RBigint.
 *
 * - Object type must be MRB_TT_BIGINT.
 * - If the structure is named RBigint, MRB_OBJ_ALLOC() can be used as is.
 */
struct RBigint {
  /*
   * Put MRB_OBJECT_HEADER before the first member of the structure.
   */
  MRB_OBJECT_HEADER;

  /*
   * Up to 3 words can be freely configured.
   */
  size_t len;
  size_t capa;
  uintptr_t *num;
};

/*
 * Assert with mrb_static_assert_object_size() that the entire structure is within 6 words.
 */
mrb_static_assert_object_size(struct RBigint);

/*
 * The lower 16 bits of the object flags (`obj->flags`) can be used freely by the GEM author.
 */
#define MY_BIGINT_NEGATIVE_FLAG 1
#define MY_BIGINT_NEGATIVE_P(obj) ((obj)->flags & MY_BIGINT_NEGATIVE_FLAG)

/*
 * Implement the functions declared in `#ifdef MRUBY_USE_BIGINT ... #endif` in the "mruby/internal.h" file.
 */

mrb_value
mrb_bint_new_int(mrb_state *mrb, mrb_int x)
{
  struct RBigint *obj = MRB_OBJ_ALLOC(mrb, MRB_TT_BIGINT, mrb->integer_class);

  ...

  return mrb_obj_value(obj);
}

/*
 * The implementation function continues...
 */