| 1 |
#include <mruby.h> |
| 2 |
#include <mruby/class.h> |
| 3 |
#include <mruby/numeric.h> |
| 4 |
#include <mruby/internal.h> |
| 5 |
#include <mruby/presym.h> |
| 6 |
|
| 7 |
#ifdef MRB_NO_FLOAT |
| 8 |
# error Complex conflicts with 'MRB_NO_FLOAT' configuration |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifdef MRB_USE_FLOAT32 |
| 12 |
#define F(x) x##f |
| 13 |
#else |
| 14 |
#define F(x) x |
| 15 |
#endif |
| 16 |
|
| 17 |
struct mrb_complex { |
| 18 |
mrb_float real; |
| 19 |
mrb_float imaginary; |
| 20 |
}; |
| 21 |
|
| 22 |
#if defined(MRB_32BIT) && !defined(MRB_USE_FLOAT32) |
| 23 |
|
| 24 |
struct RComplex { |
| 25 |
MRB_OBJECT_HEADER; |
| 26 |
struct mrb_complex *p; |
| 27 |
}; |
| 28 |
|
| 29 |
static struct mrb_complex* |
| 30 |
complex_ptr(mrb_state *mrb, mrb_value v) |
| 31 |
{ |
| 32 |
struct RComplex *r = (struct RComplex*)mrb_obj_ptr(v); |
| 33 |
|
| 34 |
if (!r->p) { |
| 35 |
mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized complex"); |
| 36 |
} |
| 37 |
return r->p; |
| 38 |
} |
| 39 |
|
| 40 |
#else |
| 41 |
#define COMPLEX_INLINE |
| 42 |
struct RComplex { |
| 43 |
MRB_OBJECT_HEADER; |
| 44 |
struct mrb_complex r; |
| 45 |
}; |
| 46 |
#define complex_ptr(mrb, v) (&((struct RComplex*)mrb_obj_ptr(v))->r) |
| 47 |
#endif |
| 48 |
|
| 49 |
mrb_static_assert_object_size(struct RComplex); |
| 50 |
|
| 51 |
static struct RBasic* |
| 52 |
complex_alloc(mrb_state *mrb, struct RClass *c, struct mrb_complex **p) |
| 53 |
{ |
| 54 |
struct RComplex *s; |
| 55 |
s = MRB_OBJ_ALLOC(mrb, MRB_TT_COMPLEX, c); |
| 56 |
#ifdef COMPLEX_INLINE |
| 57 |
*p = &s->r; |
| 58 |
#else |
| 59 |
*p = s->p = (struct mrb_complex*)mrb_malloc(mrb, sizeof(struct mrb_complex)); |
| 60 |
#endif |
| 61 |
return (struct RBasic*)s; |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
mrb_complex_get(mrb_state *mrb, mrb_value cpx, mrb_float *r, mrb_float *i) |
| 66 |
{ |
| 67 |
struct mrb_complex *c = complex_ptr(mrb, cpx); |
| 68 |
|
| 69 |
*r = c->real; |
| 70 |
*i = c->imaginary; |
| 71 |
} |
| 72 |
|
| 73 |
mrb_value |
| 74 |
mrb_complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary) |
| 75 |
{ |
| 76 |
struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Complex)); |
| 77 |
struct mrb_complex *p; |
| 78 |
struct RBasic *comp = complex_alloc(mrb, c, &p); |
| 79 |
p->real = real; |
| 80 |
p->imaginary = imaginary; |
| 81 |
MRB_SET_FROZEN_FLAG(comp); |
| 82 |
|
| 83 |
return mrb_obj_value(comp); |
| 84 |
} |
| 85 |
|
| 86 |
#define complex_new(mrb, real, imag) mrb_complex_new(mrb, real, imag) |
| 87 |
|
| 88 |
void |
| 89 |
mrb_complex_copy(mrb_state *mrb, mrb_value x, mrb_value y) |
| 90 |
{ |
| 91 |
struct mrb_complex *p1 = complex_ptr(mrb, x); |
| 92 |
struct mrb_complex *p2 = complex_ptr(mrb, y); |
| 93 |
p1->real = p2->real; |
| 94 |
p1->imaginary = p2->imaginary; |
| 95 |
} |
| 96 |
|
| 97 |
static mrb_value |
| 98 |
complex_real(mrb_state *mrb, mrb_value self) |
| 99 |
{ |
| 100 |
struct mrb_complex *p = complex_ptr(mrb, self); |
| 101 |
return mrb_float_value(mrb, p->real); |
| 102 |
} |
| 103 |
|
| 104 |
static mrb_value |
| 105 |
complex_imaginary(mrb_state *mrb, mrb_value self) |
| 106 |
{ |
| 107 |
struct mrb_complex *p = complex_ptr(mrb, self); |
| 108 |
return mrb_float_value(mrb, p->imaginary); |
| 109 |
} |
| 110 |
|
| 111 |
static mrb_value |
| 112 |
complex_s_rect(mrb_state *mrb, mrb_value self) |
| 113 |
{ |
| 114 |
mrb_float real, imaginary = 0.0; |
| 115 |
|
| 116 |
mrb_get_args(mrb, "f|f", &real, &imaginary); |
| 117 |
return complex_new(mrb, real, imaginary); |
| 118 |
} |
| 119 |
|
| 120 |
mrb_value |
| 121 |
mrb_complex_to_f(mrb_state *mrb, mrb_value self) |
| 122 |
{ |
| 123 |
struct mrb_complex *p = complex_ptr(mrb, self); |
| 124 |
|
| 125 |
if (p->imaginary != 0) { |
| 126 |
mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Float", self); |
| 127 |
} |
| 128 |
|
| 129 |
return mrb_float_value(mrb, p->real); |
| 130 |
} |
| 131 |
|
| 132 |
mrb_value |
| 133 |
mrb_complex_to_i(mrb_state *mrb, mrb_value self) |
| 134 |
{ |
| 135 |
struct mrb_complex *p = complex_ptr(mrb, self); |
| 136 |
|
| 137 |
#ifdef MRB_USE_BIGINT |
| 138 |
if (p->imaginary != 0) { |
| 139 |
mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Integer", self); |
| 140 |
} |
| 141 |
if (!FIXABLE_FLOAT(p->real)) { |
| 142 |
return mrb_bint_new_float(mrb, p->real); |
| 143 |
} |
| 144 |
#else |
| 145 |
if (p->imaginary != 0 || !FIXABLE_FLOAT(p->real)) { |
| 146 |
mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Integer", self); |
| 147 |
} |
| 148 |
#endif |
| 149 |
return mrb_int_value(mrb, (mrb_int)p->real); |
| 150 |
} |
| 151 |
|
| 152 |
static mrb_value |
| 153 |
complex_to_c(mrb_state *mrb, mrb_value self) |
| 154 |
{ |
| 155 |
return self; |
| 156 |
} |
| 157 |
|
| 158 |
mrb_bool |
| 159 |
mrb_complex_eq(mrb_state *mrb, mrb_value x, mrb_value y) |
| 160 |
{ |
| 161 |
struct mrb_complex *p1 = complex_ptr(mrb, x); |
| 162 |
|
| 163 |
switch (mrb_type(y)) { |
| 164 |
case MRB_TT_COMPLEX: |
| 165 |
{ |
| 166 |
struct mrb_complex *p2 = complex_ptr(mrb, y); |
| 167 |
|
| 168 |
if (p1->real == p2->real && p1->imaginary == p2->imaginary) { |
| 169 |
return TRUE; |
| 170 |
} |
| 171 |
return FALSE; |
| 172 |
} |
| 173 |
case MRB_TT_INTEGER: |
| 174 |
if (p1->imaginary != 0) return FALSE; |
| 175 |
return p1->real == mrb_integer(y); |
| 176 |
case MRB_TT_FLOAT: |
| 177 |
if (p1->imaginary != 0) return FALSE; |
| 178 |
return p1->real == mrb_float(y); |
| 179 |
|
| 180 |
default: |
| 181 |
return mrb_equal(mrb, y, x); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
static mrb_value |
| 186 |
complex_eq(mrb_state *mrb, mrb_value x) |
| 187 |
{ |
| 188 |
mrb_value y = mrb_get_arg1(mrb); |
| 189 |
return mrb_bool_value(mrb_complex_eq(mrb, x, y)); |
| 190 |
} |
| 191 |
|
| 192 |
mrb_value |
| 193 |
mrb_complex_add(mrb_state *mrb, mrb_value x, mrb_value y) |
| 194 |
{ |
| 195 |
struct mrb_complex *p1 = complex_ptr(mrb, x); |
| 196 |
|
| 197 |
switch (mrb_type(y)) { |
| 198 |
case MRB_TT_COMPLEX: |
| 199 |
{ |
| 200 |
struct mrb_complex *p2 = complex_ptr(mrb, y); |
| 201 |
return mrb_complex_new(mrb, p1->real+p2->real, p1->imaginary+p2->imaginary); |
| 202 |
} |
| 203 |
|
| 204 |
default: |
| 205 |
{ |
| 206 |
mrb_float z = mrb_as_float(mrb, y); |
| 207 |
return mrb_complex_new(mrb, p1->real+z, p1->imaginary); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static mrb_value |
| 213 |
complex_add(mrb_state *mrb, mrb_value x) |
| 214 |
{ |
| 215 |
mrb_value y = mrb_get_arg1(mrb); |
| 216 |
return mrb_complex_add(mrb, x, y); |
| 217 |
} |
| 218 |
|
| 219 |
mrb_value |
| 220 |
mrb_complex_sub(mrb_state *mrb, mrb_value x, mrb_value y) |
| 221 |
{ |
| 222 |
struct mrb_complex *p1 = complex_ptr(mrb, x); |
| 223 |
|
| 224 |
switch (mrb_type(y)) { |
| 225 |
case MRB_TT_COMPLEX: |
| 226 |
{ |
| 227 |
struct mrb_complex *p2 = complex_ptr(mrb, y); |
| 228 |
return mrb_complex_new(mrb, p1->real-p2->real, p1->imaginary-p2->imaginary); |
| 229 |
} |
| 230 |
|
| 231 |
default: |
| 232 |
{ |
| 233 |
mrb_float z = mrb_as_float(mrb, y); |
| 234 |
return mrb_complex_new(mrb, p1->real-z, p1->imaginary); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static mrb_value |
| 240 |
complex_sub(mrb_state *mrb, mrb_value x) |
| 241 |
{ |
| 242 |
mrb_value y = mrb_get_arg1(mrb); |
| 243 |
return mrb_complex_sub(mrb, x, y); |
| 244 |
} |
| 245 |
|
| 246 |
mrb_value |
| 247 |
mrb_complex_mul(mrb_state *mrb, mrb_value x, mrb_value y) |
| 248 |
{ |
| 249 |
struct mrb_complex *p1 = complex_ptr(mrb, x); |
| 250 |
|
| 251 |
switch (mrb_type(y)) { |
| 252 |
case MRB_TT_COMPLEX: |
| 253 |
{ |
| 254 |
struct mrb_complex *p2 = complex_ptr(mrb, y); |
| 255 |
return mrb_complex_new(mrb, p1->real*p2->real - p1->imaginary*p2->imaginary, |
| 256 |
p1->real*p2->imaginary + p2->real*p1->imaginary); |
| 257 |
} |
| 258 |
|
| 259 |
default: |
| 260 |
{ |
| 261 |
mrb_float z = mrb_as_float(mrb, y); |
| 262 |
return mrb_complex_new(mrb, p1->real*z, p1->imaginary*z); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
static mrb_value |
| 268 |
complex_mul(mrb_state *mrb, mrb_value x) |
| 269 |
{ |
| 270 |
mrb_value y = mrb_get_arg1(mrb); |
| 271 |
return mrb_complex_mul(mrb, x, y); |
| 272 |
} |
| 273 |
|
| 274 |
/* Arithmetic on (significand, exponent) pairs avoids premature overflow in |
| 275 |
complex division */ |
| 276 |
struct float_pair { |
| 277 |
mrb_float s; |
| 278 |
int x; |
| 279 |
}; |
| 280 |
|
| 281 |
static void |
| 282 |
add_pair(struct float_pair *s, struct float_pair const *a, |
| 283 |
struct float_pair const *b) |
| 284 |
{ |
| 285 |
if (b->s == 0.0F) { |
| 286 |
*s = *a; |
| 287 |
} |
| 288 |
else if (a->s == 0.0F) { |
| 289 |
*s = *b; |
| 290 |
} |
| 291 |
else if (a->x >= b->x) { |
| 292 |
s->s = a->s + F(ldexp)(b->s, b->x - a->x); |
| 293 |
s->x = a->x; |
| 294 |
} |
| 295 |
else { |
| 296 |
s->s = F(ldexp)(a->s, a->x - b->x) + b->s; |
| 297 |
s->x = b->x; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
static void |
| 302 |
mul_pair(struct float_pair *p, struct float_pair const *a, |
| 303 |
struct float_pair const *b) |
| 304 |
{ |
| 305 |
p->s = a->s * b->s; |
| 306 |
p->x = a->x + b->x; |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
div_pair(struct float_pair *q, struct float_pair const *a, |
| 311 |
struct float_pair const *b) |
| 312 |
{ |
| 313 |
q->s = mrb_div_float(a->s, b->s); |
| 314 |
q->x = a->x - b->x; |
| 315 |
} |
| 316 |
|
| 317 |
mrb_value |
| 318 |
mrb_complex_div(mrb_state *mrb, mrb_value self, mrb_value rhs) |
| 319 |
{ |
| 320 |
struct mrb_complex *a, *b; |
| 321 |
|
| 322 |
a = complex_ptr(mrb, self); |
| 323 |
if (mrb_type(rhs) != MRB_TT_COMPLEX) { |
| 324 |
if (mrb_integer_p(rhs) && mrb_integer(rhs) == 0) { |
| 325 |
mrb_int_zerodiv(mrb); |
| 326 |
} |
| 327 |
mrb_float f = mrb_as_float(mrb, rhs); |
| 328 |
return complex_new(mrb, mrb_div_float(a->real, f), mrb_div_float(a->imaginary, f)); |
| 329 |
} |
| 330 |
|
| 331 |
struct float_pair ar, ai, br, bi; |
| 332 |
struct float_pair br2, bi2; |
| 333 |
struct float_pair div; |
| 334 |
struct float_pair ar_br, ai_bi; |
| 335 |
struct float_pair ai_br, ar_bi; |
| 336 |
struct float_pair zr, zi; |
| 337 |
|
| 338 |
b = complex_ptr(mrb, rhs); |
| 339 |
|
| 340 |
/* Split floating-point components into significand and exponent */ |
| 341 |
ar.s = F(frexp)(a->real, &ar.x); |
| 342 |
ai.s = F(frexp)(a->imaginary, &ai.x); |
| 343 |
br.s = F(frexp)(b->real, &br.x); |
| 344 |
bi.s = F(frexp)(b->imaginary, &bi.x); |
| 345 |
|
| 346 |
/* Perform arithmetic on (significand, exponent) pairs to produce |
| 347 |
the result: */ |
| 348 |
|
| 349 |
/* the divisor */ |
| 350 |
mul_pair(&br2, &br, &br); |
| 351 |
mul_pair(&bi2, &bi, &bi); |
| 352 |
add_pair(&div, &br2, &bi2); |
| 353 |
|
| 354 |
/* real component */ |
| 355 |
mul_pair(&ar_br, &ar, &br); |
| 356 |
mul_pair(&ai_bi, &ai, &bi); |
| 357 |
add_pair(&zr, &ar_br, &ai_bi); |
| 358 |
div_pair(&zr, &zr, &div); |
| 359 |
|
| 360 |
/* imaginary component */ |
| 361 |
mul_pair(&ai_br, &ai, &br); |
| 362 |
mul_pair(&ar_bi, &ar, &bi); |
| 363 |
ar_bi.s = -ar_bi.s; |
| 364 |
add_pair(&zi, &ai_br, &ar_bi); |
| 365 |
div_pair(&zi, &zi, &div); |
| 366 |
|
| 367 |
/* assemble the result */ |
| 368 |
return complex_new(mrb, F(ldexp)(zr.s, zr.x), F(ldexp)(zi.s, zi.x)); |
| 369 |
} |
| 370 |
|
| 371 |
static mrb_value |
| 372 |
complex_div(mrb_state *mrb, mrb_value x) |
| 373 |
{ |
| 374 |
mrb_value y = mrb_get_arg1(mrb); |
| 375 |
return mrb_complex_div(mrb, x, y); |
| 376 |
} |
| 377 |
|
| 378 |
static mrb_value |
| 379 |
complex_hash(mrb_state *mrb, mrb_value cpx) |
| 380 |
{ |
| 381 |
struct mrb_complex *c = complex_ptr(mrb, cpx); |
| 382 |
uint32_t hash = mrb_byte_hash((uint8_t*)&c->real, sizeof(mrb_float)); |
| 383 |
hash = mrb_byte_hash_step((uint8_t*)&c->imaginary, sizeof(mrb_float), hash); |
| 384 |
return mrb_int_value(mrb, hash); |
| 385 |
} |
| 386 |
|
| 387 |
static mrb_value |
| 388 |
nil_to_c(mrb_state *mrb, mrb_value self) |
| 389 |
{ |
| 390 |
return complex_new(mrb, 0, 0); |
| 391 |
} |
| 392 |
|
| 393 |
void mrb_mruby_complex_gem_init(mrb_state *mrb) |
| 394 |
{ |
| 395 |
struct RClass *comp; |
| 396 |
|
| 397 |
comp = mrb_define_class_id(mrb, MRB_SYM(Complex), mrb_class_get_id(mrb, MRB_SYM(Numeric))); |
| 398 |
MRB_SET_INSTANCE_TT(comp, MRB_TT_UNDEF); |
| 399 |
|
| 400 |
mrb_undef_class_method(mrb, comp, "new"); |
| 401 |
mrb_define_class_method(mrb, comp, "rectangular", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); |
| 402 |
mrb_define_class_method(mrb, comp, "rect", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); |
| 403 |
mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); |
| 404 |
mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE()); |
| 405 |
mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE()); |
| 406 |
mrb_define_method(mrb, comp, "to_f", mrb_complex_to_f, MRB_ARGS_NONE()); |
| 407 |
mrb_define_method(mrb, comp, "to_i", mrb_complex_to_i, MRB_ARGS_NONE()); |
| 408 |
mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE()); |
| 409 |
mrb_define_method(mrb, comp, "+", complex_add, MRB_ARGS_REQ(1)); |
| 410 |
mrb_define_method(mrb, comp, "-", complex_sub, MRB_ARGS_REQ(1)); |
| 411 |
mrb_define_method(mrb, comp, "*", complex_mul, MRB_ARGS_REQ(1)); |
| 412 |
mrb_define_method(mrb, comp, "/", complex_div, MRB_ARGS_REQ(1)); |
| 413 |
mrb_define_method(mrb, comp, "quo", complex_div, MRB_ARGS_REQ(1)); |
| 414 |
mrb_define_method(mrb, comp, "==", complex_eq, MRB_ARGS_REQ(1)); |
| 415 |
mrb_define_method(mrb, comp, "hash", complex_hash, MRB_ARGS_NONE()); |
| 416 |
mrb_define_method(mrb, mrb->nil_class, "to_c", nil_to_c, MRB_ARGS_NONE()); |
| 417 |
} |
| 418 |
|
| 419 |
void |
| 420 |
mrb_mruby_complex_gem_final(mrb_state* mrb) |
| 421 |
{ |
| 422 |
} |