mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-21 22:59:16 +02:00
Bignum-compatible version of ash
This commit is contained in:
parent
c0a5daf898
commit
afa12cec5f
1 changed files with 50 additions and 43 deletions
89
srfi/60.scm
89
srfi/60.scm
|
@ -203,50 +203,57 @@
|
||||||
(ash from start)
|
(ash from start)
|
||||||
to))
|
to))
|
||||||
|
|
||||||
(define-c ash
|
;(define-c ash
|
||||||
|
; "(void* data, int argc, closure _, object k, object x, object y)"
|
||||||
|
; "Cyc_check_int(data, x);
|
||||||
|
; Cyc_check_int(data,y);
|
||||||
|
; int bf = (int)unbox_number(x);
|
||||||
|
; int shift = (int)unbox_number(y);
|
||||||
|
; //int i;
|
||||||
|
; if (shift > 0) {
|
||||||
|
; bf <<= shift;
|
||||||
|
; } else {
|
||||||
|
; bf >>= abs(shift);
|
||||||
|
; }
|
||||||
|
;// if (shift > 0) {
|
||||||
|
;// for (i = 0; i < shift; i++) {
|
||||||
|
;// bf *= 2;
|
||||||
|
;// }
|
||||||
|
;// } else {
|
||||||
|
;// for (i = 0; i < abs(shift); i++) {
|
||||||
|
;// bf /= 2;
|
||||||
|
;// }
|
||||||
|
;// }
|
||||||
|
; return_closcall1(data, k, obj_int2obj(bf))")
|
||||||
|
|
||||||
|
(define-c ash
|
||||||
"(void* data, int argc, closure _, object k, object x, object y)"
|
"(void* data, int argc, closure _, object k, object x, object y)"
|
||||||
"Cyc_check_int(data, x);
|
"Cyc_check_int(data, x);
|
||||||
Cyc_check_int(data,y);
|
Cyc_check_fixnum(data,y);
|
||||||
int bf = (int)unbox_number(x);
|
int shift, i;
|
||||||
int shift = (int)unbox_number(y);
|
//int result;
|
||||||
//int i;
|
alloc_bignum(data, bn);
|
||||||
if (shift > 0) {
|
|
||||||
bf <<= shift;
|
|
||||||
} else {
|
|
||||||
bf >>= abs(shift);
|
|
||||||
}
|
|
||||||
// if (shift > 0) {
|
|
||||||
// for (i = 0; i < shift; i++) {
|
|
||||||
// bf *= 2;
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// for (i = 0; i < abs(shift); i++) {
|
|
||||||
// bf /= 2;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
return_closcall1(data, k, obj_int2obj(bf))")
|
|
||||||
|
|
||||||
;; (define-c ash
|
if (Cyc_is_bignum(x) == boolean_t){
|
||||||
;; "(void* data, int argc, closure _, object k, object x, object y)"
|
mp_copy(&bignum_value(x), &bignum_value(bn));
|
||||||
;; "Cyc_check_int(data, x);
|
} else {
|
||||||
;; Cyc_check_fixnum(data,y);
|
Cyc_int2bignum((int)unbox_number(x), &bignum_value(bn));
|
||||||
;; int shift;
|
}
|
||||||
;; //int result;
|
|
||||||
;; alloc_bignum(data, bn);
|
// Inefficient but always works without overflow
|
||||||
;;
|
// Should be able to do pure fixnum math in some cases, though
|
||||||
;; if (obj_is_int(x)) {
|
shift = (int)unbox_number(y);
|
||||||
;; Cyc_int2bignum(obj_obj2int(x), &bignum_value(bn));
|
if (shift > 0) {
|
||||||
;; } else {
|
for (i = 0; i < shift; i++) {
|
||||||
;; mp_copy(&bignum_value(x), &bignum_value(bn));
|
mp_mul_2(&bignum_value(bn), &bignum_value(bn));
|
||||||
;; }
|
}
|
||||||
;;
|
} else {
|
||||||
;; shift = (int)unbox_number(y);
|
for (i = 0; i < abs(shift); i++) {
|
||||||
;; if (shift > 0) {
|
mp_div_2(&bignum_value(bn), &bignum_value(bn));
|
||||||
;; mp_lshd(&bignum_value(bn), shift);
|
}
|
||||||
;; } else {
|
}
|
||||||
;; mp_rshd(&bignum_value(bn), abs(shift));
|
|
||||||
;; }
|
return_closcall1(data, k, Cyc_bignum_normalize(data, bn));")
|
||||||
;; return_closcall1(data, k, Cyc_bignum_normalize(data, bn));")
|
|
||||||
|
|
||||||
(define arithmetic-shift ash)
|
(define arithmetic-shift ash)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue