Commit b422aedf

b422aedfc2ccMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +3 added

Description

feat: standalone bigint library (adapted from mruby-bigint)

Extract core mpz (multi-precision integer) operations from mruby-bigint and add a thin Spinel API wrapper.

Core (from mruby-bigint, public domain fgmp):

  • mpz_add, mpz_sub, mpz_mul, mpz_mdiv, mpz_mmod
  • mpz_pow, mpz_cmp, mpz_sizeinbase
  • Open-addressing with Karatsuba multiplication for large numbers

Spinel API:

  • sp_bigint_new_int, sp_bigint_new_str
  • sp_bigint_add/sub/mul/div/mod/pow/cmp
  • sp_bigint_to_int, sp_bigint_to_s

Adapter (mruby_shim.h):

  • Minimal type shim replacing mruby headers
  • mrb_malloc → malloc, mrb_raise → fprintf+exit

Verified: 2^100, fib(100), basic arithmetic all match Ruby.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
b422aedfc2cc
Parent
96029f8eda9a

lib/mruby_shim.haddedView file

1+/*
2+ * Minimal shim to compile mruby-bigint without the full mruby runtime.
3+ * Provides just enough types and macros for bigint.c to compile standalone.
4+ */
5+#ifndef MRUBY_SHIM_H
6+#define MRUBY_SHIM_H
7+
8+#include <stdio.h>
9+#include <stdlib.h>
10+#include <stdint.h>
11+#include <string.h>
12+#include <assert.h>
13+
14+/* Basic mruby types */
15+typedef int64_t mrb_int;
16+typedef uint64_t mrb_uint;
17+typedef double mrb_float;
18+typedef int mrb_bool;
19+#define TRUE 1
20+#define FALSE 0
21+#define MRB_INT_MAX INT64_MAX
22+#define MRB_INT_MIN INT64_MIN
23+#define MRB_INT_BIT 64
24+
25+/* mrb_state stub */
26+typedef struct mrb_state {
27+ int dummy;
28+} mrb_state;
29+
30+/* Memory allocation */
31+#define mrb_malloc(mrb, sz) malloc(sz)
32+#define mrb_realloc(mrb, p, sz) realloc(p, sz)
33+#define mrb_free(mrb, p) free(p)
34+#define mrb_malloc_simple(mrb, sz) malloc(sz)
35+
36+/* Error handling */
37+#define mrb_raise(mrb, cls, msg) do { fprintf(stderr, "%s\n", msg); exit(1); } while(0)
38+#define mrb_int_zerodiv(mrb) do { fprintf(stderr, "divided by 0\n"); exit(1); } while(0)
39+#define E_RANGE_ERROR 0
40+#define E_RUNTIME_ERROR 0
41+#define E_ARGUMENT_ERROR 0
42+#define E_TYPE_ERROR 0
43+
44+/* mrb_value stub - tagged union */
45+typedef struct mrb_value {
46+ union {
47+ mrb_int i;
48+ mrb_float f;
49+ void *p;
50+ } value;
51+ int tt;
52+} mrb_value;
53+
54+enum mrb_vtype {
55+ MRB_TT_FALSE = 0,
56+ MRB_TT_TRUE,
57+ MRB_TT_INTEGER,
58+ MRB_TT_FLOAT,
59+ MRB_TT_STRING,
60+ MRB_TT_BIGINT,
61+};
62+
63+#define mrb_integer(v) ((v).value.i)
64+#define mrb_float(v) ((v).value.f)
65+#define mrb_ptr(v) ((v).value.p)
66+#define mrb_type(v) ((v).tt)
67+#define mrb_integer_p(v) ((v).tt == MRB_TT_INTEGER)
68+#define mrb_float_p(v) ((v).tt == MRB_TT_FLOAT)
69+#define mrb_bigint_p(v) ((v).tt == MRB_TT_BIGINT)
70+#define mrb_fixnum_value(i) ((mrb_value){{.i=(i)}, MRB_TT_INTEGER})
71+#define mrb_int_value(mrb, i) mrb_fixnum_value(i)
72+
73+/* Object header stub */
74+#define MRB_OBJECT_HEADER uint32_t flags; uint32_t _pad
75+#define mrb_obj_ptr(v) ((struct RBasic*)mrb_ptr(v))
76+#define mrb_static_assert_object_size(t) /* nop */
77+#define mrb_assert(x) assert(x)
78+
79+/* String */
80+#define RSTRING_PTR(s) ((const char*)mrb_ptr(s))
81+#define RSTRING_LEN(s) ((mrb_int)strlen(RSTRING_PTR(s)))
82+
83+/* nil value */
84+static inline mrb_value mrb_nil_value(void) {
85+ mrb_value v; v.tt = MRB_TT_FALSE; v.value.i = 0; return v;
86+}
87+
88+/* MRB_ENSURE stub - execute body, no exception handling */
89+#define MRB_ENSURE(mrb, exc, body, data) \
90+ { mrb_value exc = body(mrb, data); (void)exc; } if(0)
91+
92+/* Bigint object creation */
93+struct RBasic {
94+ MRB_OBJECT_HEADER;
95+};
96+
97+#endif /* MRUBY_SHIM_H */