2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
|
|
|
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
|
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 04:20:42 -04:00
|
|
|
|
|
|
|
(library (ikarus fixnums)
|
2007-05-05 04:33:58 -04:00
|
|
|
(export fxzero? fxadd1 fxsub1 fxlognot fx+ fx- fx* fxquotient
|
2007-09-15 03:16:55 -04:00
|
|
|
fx+/carry fx*/carry fx-/carry
|
2007-05-05 04:39:25 -04:00
|
|
|
fxremainder fxmodulo fxlogor fxlogand fxlogxor fxsll fxsra
|
2007-09-13 16:39:13 -04:00
|
|
|
fx= fx< fx<= fx> fx>=
|
|
|
|
fx=? fx<? fx<=? fx>? fx>=?
|
2007-09-15 03:27:28 -04:00
|
|
|
fxior fxand fxxor fxnot fxif
|
2007-09-13 19:25:47 -04:00
|
|
|
fxpositive? fxnegative?
|
|
|
|
fxeven? fxodd?
|
|
|
|
fixnum->string
|
2007-09-15 00:14:47 -04:00
|
|
|
fxarithmetic-shift-left fxarithmetic-shift-right fxarithmetic-shift
|
2007-09-15 00:24:38 -04:00
|
|
|
fxmin fxmax
|
2007-09-13 19:25:47 -04:00
|
|
|
error@fx+)
|
2007-05-05 04:20:42 -04:00
|
|
|
(import
|
2007-05-06 18:13:57 -04:00
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $chars)
|
2007-09-13 16:39:13 -04:00
|
|
|
(ikarus system $pairs)
|
2007-05-06 18:13:57 -04:00
|
|
|
(ikarus system $strings)
|
2007-09-13 19:25:47 -04:00
|
|
|
(prefix (only (ikarus) fx+) sys:)
|
2007-05-05 04:33:58 -04:00
|
|
|
(except (ikarus) fxzero? fxadd1 fxsub1 fxlognot fx+ fx- fx*
|
2007-05-05 04:36:52 -04:00
|
|
|
fxquotient fxremainder fxmodulo fxlogor fxlogand
|
2007-05-05 06:26:09 -04:00
|
|
|
fxlogxor fxsll fxsra fx= fx< fx<= fx> fx>=
|
2007-09-13 16:39:13 -04:00
|
|
|
fx=? fx<? fx<=? fx>? fx>=?
|
2007-09-15 03:27:28 -04:00
|
|
|
fxior fxand fxxor fxnot fxif
|
2007-09-13 19:25:47 -04:00
|
|
|
fxpositive? fxnegative?
|
|
|
|
fxeven? fxodd?
|
2007-09-15 00:14:47 -04:00
|
|
|
fxarithmetic-shift-left fxarithmetic-shift-right fxarithmetic-shift
|
2007-09-15 03:16:55 -04:00
|
|
|
fx+/carry fx*/carry fx-/carry
|
2007-09-15 00:24:38 -04:00
|
|
|
fxmin fxmax
|
2007-05-05 06:26:09 -04:00
|
|
|
fixnum->string))
|
2007-05-05 04:20:42 -04:00
|
|
|
|
|
|
|
(define fxzero?
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[(eq? x 0) #t]
|
|
|
|
[(fixnum? x) #f]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fxzero? "not a fixnum" x)])))
|
2007-05-05 04:20:42 -04:00
|
|
|
|
|
|
|
(define fxadd1
|
|
|
|
(lambda (n)
|
|
|
|
(if (fixnum? n)
|
|
|
|
($fxadd1 n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxadd1 "not a fixnum" n))))
|
2007-05-05 04:20:42 -04:00
|
|
|
|
|
|
|
(define fxsub1
|
|
|
|
(lambda (n)
|
|
|
|
(if (fixnum? n)
|
|
|
|
($fxsub1 n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsub1 "not a fixnum" n))))
|
2007-05-05 04:20:42 -04:00
|
|
|
|
2007-05-05 04:21:48 -04:00
|
|
|
(define fxlognot
|
|
|
|
(lambda (x)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxlognot "not a fixnum" x))
|
2007-05-05 04:21:48 -04:00
|
|
|
($fxlognot x)))
|
2007-05-05 04:31:53 -04:00
|
|
|
|
2007-09-13 19:25:47 -04:00
|
|
|
(define fxnot
|
|
|
|
(lambda (x)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxnot "not a fixnum" x))
|
2007-09-13 19:25:47 -04:00
|
|
|
($fxlognot x)))
|
|
|
|
|
|
|
|
(define error@fx+
|
|
|
|
(lambda (x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fx+ "overflow when adding numbers" x y)
|
|
|
|
(die 'fx+ "not a fixnum" y))
|
|
|
|
(die 'fx+ "not a fixnum" x))))
|
2007-09-13 19:25:47 -04:00
|
|
|
|
2007-05-05 04:31:53 -04:00
|
|
|
(define fx+
|
|
|
|
(lambda (x y)
|
2007-09-13 19:25:47 -04:00
|
|
|
(sys:fx+ x y)))
|
2007-05-05 04:31:53 -04:00
|
|
|
|
|
|
|
(define fx-
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fx- "not a fixnum" x))
|
2007-05-05 04:31:53 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fx- "not a fixnum" y))
|
2007-05-05 04:31:53 -04:00
|
|
|
($fx- x y)))
|
|
|
|
|
|
|
|
(define fx*
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fx* "not a fixnum" x))
|
2007-05-05 04:31:53 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fx* "not a fixnum" y))
|
2007-05-05 04:31:53 -04:00
|
|
|
($fx* x y)))
|
2007-05-05 04:39:25 -04:00
|
|
|
|
2007-09-15 03:27:28 -04:00
|
|
|
|
2007-09-13 16:39:13 -04:00
|
|
|
(define false-loop
|
|
|
|
(lambda (who ls)
|
|
|
|
(if (pair? ls)
|
|
|
|
(if (fixnum? ($car ls))
|
|
|
|
(false-loop who ($cdr ls))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a fixnum" ($car ls)))
|
2007-09-13 16:39:13 -04:00
|
|
|
#f)))
|
|
|
|
|
|
|
|
(define-syntax fxcmp
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ who $op)
|
|
|
|
(case-lambda
|
|
|
|
[(x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" x))
|
2007-09-13 16:39:13 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" y))
|
2007-09-13 16:39:13 -04:00
|
|
|
($op x y)]
|
|
|
|
[(x y . ls)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($op x y)
|
|
|
|
(let f ([x y] [ls ls])
|
|
|
|
(if (pair? ls)
|
|
|
|
(let ([y ($car ls)] [ls ($cdr ls)])
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($op x y)
|
|
|
|
(f y ls)
|
|
|
|
(false-loop 'who ls))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" y)))
|
2007-09-13 16:39:13 -04:00
|
|
|
#t))
|
|
|
|
(false-loop 'who ls))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" y))
|
|
|
|
(die 'who "not a fixnum" x))]
|
2007-09-13 16:39:13 -04:00
|
|
|
[(x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(if (fixnum? x) #t (die 'who "not a fixnum" x))])]))
|
2007-05-05 04:39:25 -04:00
|
|
|
|
2007-09-13 16:39:13 -04:00
|
|
|
(define fx= (fxcmp fx= $fx=))
|
|
|
|
(define fx< (fxcmp fx< $fx<))
|
|
|
|
(define fx<= (fxcmp fx<= $fx<=))
|
|
|
|
(define fx> (fxcmp fx> $fx>))
|
|
|
|
(define fx>= (fxcmp fx>= $fx>=))
|
|
|
|
(define fx=? (fxcmp fx=? $fx=))
|
|
|
|
(define fx<? (fxcmp fx<? $fx<))
|
|
|
|
(define fx<=? (fxcmp fx<=? $fx<=))
|
|
|
|
(define fx>? (fxcmp fx>? $fx>))
|
|
|
|
(define fx>=? (fxcmp fx>=? $fx>=))
|
|
|
|
|
2007-05-05 04:39:25 -04:00
|
|
|
|
2007-05-05 04:33:58 -04:00
|
|
|
(define fxquotient
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxquotient "not a fixnum" x))
|
2007-05-05 04:33:58 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxquotient "not a fixnum" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
(when ($fxzero? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxquotient "zero dividend" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
($fxquotient x y)))
|
|
|
|
|
|
|
|
(define fxremainder
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxremainder "not a fixnum" x))
|
2007-05-05 04:33:58 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxremainder "not a fixnum" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
(when ($fxzero? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxremainder "zero dividend" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
(let ([q ($fxquotient x y)])
|
|
|
|
($fx- x ($fx* q y)))))
|
|
|
|
|
|
|
|
(define fxmodulo
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmodulo "not a fixnum" x))
|
2007-05-05 04:33:58 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmodulo "not a fixnum" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
(when ($fxzero? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmodulo "zero dividend" y))
|
2007-05-05 04:33:58 -04:00
|
|
|
($fxmodulo x y)))
|
|
|
|
|
2007-09-13 19:25:47 -04:00
|
|
|
(define-syntax fxbitop
|
|
|
|
(syntax-rules ()
|
|
|
|
[(_ who $op identity)
|
|
|
|
(case-lambda
|
|
|
|
[(x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
($op x y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" y))
|
|
|
|
(die 'who "not a fixnum" x))]
|
2007-09-13 19:25:47 -04:00
|
|
|
[(x y . ls)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(let f ([a ($op x y)] [ls ls])
|
|
|
|
(cond
|
|
|
|
[(pair? ls)
|
|
|
|
(let ([b ($car ls)])
|
|
|
|
(if (fixnum? b)
|
|
|
|
(f ($op a b) ($cdr ls))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" b)))]
|
2007-09-13 19:25:47 -04:00
|
|
|
[else a]))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'who "not a fixnum" y))
|
|
|
|
(die 'who "not a fixnum" x))]
|
|
|
|
[(x) (if (fixnum? x) x (die 'who "not a fixnum" x))]
|
2007-09-13 19:25:47 -04:00
|
|
|
[() identity])]))
|
|
|
|
|
|
|
|
(define fxlogor (fxbitop fxlogor $fxlogor 0))
|
|
|
|
(define fxlogand (fxbitop fxlogand $fxlogand -1))
|
|
|
|
(define fxlogxor (fxbitop fxlogxor $fxlogxor 0))
|
|
|
|
(define fxior (fxbitop fxior $fxlogor 0))
|
|
|
|
(define fxand (fxbitop fxand $fxlogand -1))
|
|
|
|
(define fxxor (fxbitop fxxor $fxlogxor 0))
|
2007-05-05 04:36:52 -04:00
|
|
|
|
2007-09-15 03:27:28 -04:00
|
|
|
(define (fxif x y z)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if (fixnum? z)
|
|
|
|
($fxlogor
|
|
|
|
($fxlogand x y)
|
|
|
|
($fxlogand ($fxlognot x) z))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxif "not a fixnum" z))
|
|
|
|
(die 'fxif "not a fixnum" y))
|
|
|
|
(die 'fxif "not a fixnum" x)))
|
2007-09-15 03:27:28 -04:00
|
|
|
|
2007-05-05 04:36:52 -04:00
|
|
|
(define fxsra
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsra "not a fixnum" x))
|
2007-05-05 04:36:52 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsra "not a fixnum" y))
|
2007-05-05 04:36:52 -04:00
|
|
|
(unless ($fx>= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsra "negative shift not allowed" y))
|
2007-05-05 04:36:52 -04:00
|
|
|
($fxsra x y)))
|
|
|
|
|
2007-09-15 00:14:47 -04:00
|
|
|
|
|
|
|
(define fxarithmetic-shift-right
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-right "not a fixnum" x))
|
2007-09-15 00:14:47 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-right "not a fixnum" y))
|
2007-09-15 00:14:47 -04:00
|
|
|
(unless ($fx>= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-right "negative shift not allowed" y))
|
2007-09-15 00:14:47 -04:00
|
|
|
($fxsra x y)))
|
|
|
|
|
2007-05-05 04:36:52 -04:00
|
|
|
(define fxsll
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsll "not a fixnum" x))
|
2007-05-05 04:36:52 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsll "not a fixnum" y))
|
2007-05-05 04:36:52 -04:00
|
|
|
(unless ($fx>= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxsll "negative shift not allowed" y))
|
2007-05-05 04:36:52 -04:00
|
|
|
($fxsll x y)))
|
2007-05-05 04:31:53 -04:00
|
|
|
|
2007-09-15 00:14:47 -04:00
|
|
|
|
|
|
|
(define fxarithmetic-shift-left
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-left "not a fixnum" x))
|
2007-09-15 00:14:47 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-left "not a fixnum" y))
|
2007-09-15 00:14:47 -04:00
|
|
|
(unless ($fx>= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift-left "negative shift not allowed" y))
|
2007-09-15 00:14:47 -04:00
|
|
|
($fxsll x y)))
|
|
|
|
|
|
|
|
(define fxarithmetic-shift
|
|
|
|
(lambda (x y)
|
|
|
|
(unless (fixnum? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift "not a fixnum" x))
|
2007-09-15 00:14:47 -04:00
|
|
|
(unless (fixnum? y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxarithmetic-shift "not a fixnum" y))
|
2007-09-15 00:14:47 -04:00
|
|
|
(if ($fx>= y 0)
|
|
|
|
($fxsll x y)
|
2007-09-15 01:54:45 -04:00
|
|
|
(if ($fx< x -100) ;;; arbitrary number < (fixnum-width)
|
|
|
|
($fxsra x 32)
|
|
|
|
($fxsra x ($fx- 0 y))))))
|
2007-09-15 00:14:47 -04:00
|
|
|
|
2007-09-13 19:25:47 -04:00
|
|
|
(define (fxpositive? x)
|
|
|
|
(if (fixnum? x)
|
|
|
|
($fx> x 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxpositive? "not a fixnum" x)))
|
2007-09-13 19:25:47 -04:00
|
|
|
|
|
|
|
(define (fxnegative? x)
|
|
|
|
(if (fixnum? x)
|
|
|
|
($fx< x 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxnegative? "not a fixnum" x)))
|
2007-09-13 19:25:47 -04:00
|
|
|
|
|
|
|
(define (fxeven? x)
|
|
|
|
(if (fixnum? x)
|
|
|
|
($fxzero? ($fxlogand x 1))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxeven? "not a fixnum" x)))
|
2007-09-13 19:25:47 -04:00
|
|
|
|
|
|
|
(define (fxodd? x)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(not ($fxzero? ($fxlogand x 1)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxodd? "not a fixnum" x)))
|
2007-09-13 19:25:47 -04:00
|
|
|
|
2007-09-15 00:24:38 -04:00
|
|
|
(define fxmin
|
|
|
|
(case-lambda
|
|
|
|
[(x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx< x y) x y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmin "not a fixnum" y))
|
|
|
|
(die 'fxmin "not a fixnum" x))]
|
2007-09-15 00:24:38 -04:00
|
|
|
[(x y z . ls)
|
|
|
|
(fxmin (fxmin x y)
|
|
|
|
(if (fixnum? z)
|
|
|
|
(let f ([z z] [ls ls])
|
|
|
|
(if (null? ls)
|
|
|
|
z
|
|
|
|
(let ([a ($car ls)])
|
|
|
|
(if (fixnum? a)
|
|
|
|
(if ($fx< a z)
|
|
|
|
(f a ($cdr ls))
|
|
|
|
(f z ($cdr ls)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmin "not a fixnum" a)))))
|
|
|
|
(die 'fxmin "not a fixnum" z)))]
|
|
|
|
[(x) (if (fixnum? x) x (die 'fxmin "not a fixnum" x))]))
|
2007-09-15 00:24:38 -04:00
|
|
|
|
|
|
|
(define fxmax
|
|
|
|
(case-lambda
|
|
|
|
[(x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx> x y) x y)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmax "not a fixnum" y))
|
|
|
|
(die 'fxmax "not a fixnum" x))]
|
2007-09-15 00:24:38 -04:00
|
|
|
[(x y z . ls)
|
|
|
|
(fxmax (fxmax x y)
|
|
|
|
(if (fixnum? z)
|
|
|
|
(let f ([z z] [ls ls])
|
|
|
|
(if (null? ls)
|
|
|
|
z
|
|
|
|
(let ([a ($car ls)])
|
|
|
|
(if (fixnum? a)
|
|
|
|
(if ($fx> a z)
|
|
|
|
(f a ($cdr ls))
|
|
|
|
(f z ($cdr ls)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmax "not a fixnum" a)))))
|
|
|
|
(die 'fxmax "not a fixnum" z)))]
|
|
|
|
[(x) (if (fixnum? x) x (die 'fxmax "not a fixnum" x))]))
|
2007-09-15 00:24:38 -04:00
|
|
|
|
2007-09-15 03:16:55 -04:00
|
|
|
(define (fx*/carry fx1 fx2 fx3)
|
|
|
|
(let ([s0 ($fx+ ($fx* fx1 fx2) fx3)])
|
|
|
|
(values
|
|
|
|
s0
|
|
|
|
(sra (+ (* fx1 fx2) (- fx3 s0)) (fixnum-width)))))
|
|
|
|
|
|
|
|
(define (fx+/carry fx1 fx2 fx3)
|
|
|
|
(let ([s0 ($fx+ ($fx+ fx1 fx2) fx3)])
|
|
|
|
(values
|
|
|
|
s0
|
|
|
|
(sra (+ (+ fx1 fx2) (- fx3 s0)) (fixnum-width)))))
|
|
|
|
|
|
|
|
(define (fx-/carry fx1 fx2 fx3)
|
|
|
|
(let ([s0 ($fx- ($fx- fx1 fx2) fx3)])
|
|
|
|
(values
|
|
|
|
s0
|
|
|
|
(sra (- (- fx1 fx2) (+ s0 fx3)) (fixnum-width)))))
|
|
|
|
|
2007-05-05 06:26:09 -04:00
|
|
|
(module (fixnum->string)
|
2007-11-07 22:28:42 -05:00
|
|
|
(define mapping-string "0123456789ABCDEF")
|
2007-05-05 06:26:09 -04:00
|
|
|
(define f
|
2007-11-07 20:02:32 -05:00
|
|
|
(lambda (n i j radix)
|
2007-05-05 06:26:09 -04:00
|
|
|
(cond
|
|
|
|
[($fxzero? n)
|
|
|
|
(values (make-string i) j)]
|
|
|
|
[else
|
2007-11-07 20:02:32 -05:00
|
|
|
(let* ([q ($fxquotient n radix)]
|
|
|
|
[c ($string-ref mapping-string
|
|
|
|
($fx- n ($fx* q radix)))])
|
2007-05-05 06:26:09 -04:00
|
|
|
(call-with-values
|
2007-11-07 20:02:32 -05:00
|
|
|
(lambda () (f q ($fxadd1 i) j radix))
|
2007-05-05 06:26:09 -04:00
|
|
|
(lambda (str j)
|
2007-11-07 20:02:32 -05:00
|
|
|
(string-set! str j c)
|
|
|
|
(values str ($fxadd1 j)))))])))
|
|
|
|
(define $fixnum->string
|
|
|
|
(lambda (x radix)
|
2007-05-05 06:26:09 -04:00
|
|
|
(cond
|
|
|
|
[($fxzero? x) "0"]
|
2007-11-07 20:02:32 -05:00
|
|
|
[($fx> x 0)
|
2007-05-05 06:26:09 -04:00
|
|
|
(call-with-values
|
2007-11-07 20:02:32 -05:00
|
|
|
(lambda () (f x 0 0 radix))
|
2007-05-05 06:26:09 -04:00
|
|
|
(lambda (str j) str))]
|
2007-11-07 20:02:32 -05:00
|
|
|
[($fx= x (least-fixnum))
|
|
|
|
(string-append
|
|
|
|
($fixnum->string ($fxquotient x radix) radix)
|
|
|
|
($fixnum->string ($fx- radix ($fxmodulo x radix)) radix))]
|
2007-05-05 06:26:09 -04:00
|
|
|
[else
|
|
|
|
(call-with-values
|
2007-11-07 20:02:32 -05:00
|
|
|
(lambda () (f ($fx- 0 x) 1 1 radix))
|
2007-05-05 06:26:09 -04:00
|
|
|
(lambda (str j)
|
|
|
|
($string-set! str 0 #\-)
|
2007-11-07 20:02:32 -05:00
|
|
|
str))])))
|
|
|
|
(define fixnum->string
|
|
|
|
(case-lambda
|
|
|
|
[(x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (fixnum? x) (die 'fixnum->string "not a fixnum" x))
|
2007-11-07 20:02:32 -05:00
|
|
|
($fixnum->string x 10)]
|
|
|
|
[(x r)
|
2007-12-15 08:22:49 -05:00
|
|
|
(unless (fixnum? x) (die 'fixnum->string "not a fixnum" x))
|
2007-11-07 20:02:32 -05:00
|
|
|
(case r
|
|
|
|
[(2) ($fixnum->string x 2)]
|
|
|
|
[(8) ($fixnum->string x 8)]
|
|
|
|
[(10) ($fixnum->string x 10)]
|
|
|
|
[(16) ($fixnum->string x 16)]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'fixnum->string "invalid radix" r)])])))
|
2007-05-05 06:26:09 -04:00
|
|
|
|
2007-09-15 03:16:55 -04:00
|
|
|
|
2007-05-05 04:20:42 -04:00
|
|
|
)
|
2007-11-11 20:43:59 -05:00
|
|
|
|
|
|
|
(library (ikarus fixnums div-and-mod)
|
2007-11-11 21:52:27 -05:00
|
|
|
(export fxdiv fxmod fxdiv-and-mod fxdiv0 fxmod0 fxdiv0-and-mod0)
|
2007-11-11 20:43:59 -05:00
|
|
|
(import
|
|
|
|
(ikarus system $fx)
|
2007-11-11 21:52:27 -05:00
|
|
|
(except (ikarus) fxdiv fxmod fxdiv-and-mod fxdiv0 fxmod0 fxdiv0-and-mod0))
|
2007-11-11 20:43:59 -05:00
|
|
|
|
|
|
|
(define ($fxdiv-and-mod n m)
|
|
|
|
(let ([d0 ($fxquotient n m)])
|
|
|
|
(let ([m0 ($fx- n ($fx* d0 m))])
|
|
|
|
(if ($fx>= m0 0)
|
|
|
|
(values d0 m0)
|
|
|
|
(if ($fx>= m 0)
|
|
|
|
(values ($fx- d0 1) ($fx+ m0 m))
|
|
|
|
(values ($fx+ d0 1) ($fx- m0 m)))))))
|
|
|
|
|
|
|
|
(define ($fxdiv n m)
|
|
|
|
(let ([d0 ($fxquotient n m)])
|
|
|
|
(if ($fx>= n ($fx* d0 m))
|
|
|
|
d0
|
|
|
|
(if ($fx>= m 0)
|
|
|
|
($fx- d0 1)
|
|
|
|
($fx+ d0 1)))))
|
|
|
|
|
|
|
|
(define ($fxmod n m)
|
|
|
|
(let ([d0 ($fxquotient n m)])
|
|
|
|
(let ([m0 ($fx- n ($fx* d0 m))])
|
|
|
|
(if ($fx>= m0 0)
|
|
|
|
m0
|
|
|
|
(if ($fx>= m 0)
|
|
|
|
($fx+ m0 m)
|
|
|
|
($fx- m0 m))))))
|
|
|
|
|
|
|
|
(define (fxdiv-and-mod x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv-and-mod "division by 0")
|
2007-11-11 20:43:59 -05:00
|
|
|
($fxdiv-and-mod x y))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv-and-mod "not a fixnum" y))
|
|
|
|
(die 'fxdiv-and-mod "not a fixnum" x)))
|
2007-11-11 20:43:59 -05:00
|
|
|
|
|
|
|
(define (fxdiv x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv "division by 0")
|
2007-11-11 20:43:59 -05:00
|
|
|
($fxdiv x y))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv "not a fixnum" y))
|
|
|
|
(die 'fxdiv "not a fixnum" x)))
|
2007-11-11 20:43:59 -05:00
|
|
|
|
|
|
|
(define (fxmod x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmod "modision by 0")
|
2007-11-11 20:43:59 -05:00
|
|
|
($fxmod x y))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmod "not a fixnum" y))
|
|
|
|
(die 'fxmod "not a fixnum" x)))
|
2007-11-11 21:52:27 -05:00
|
|
|
|
|
|
|
(define ($fxdiv0-and-mod0 n m)
|
|
|
|
(let ([d0 (quotient n m)])
|
|
|
|
(let ([m0 (- n (* d0 m))])
|
|
|
|
(if (>= m 0)
|
|
|
|
(if (< (* m0 2) m)
|
|
|
|
(if (<= (* m0 -2) m)
|
|
|
|
(values d0 m0)
|
|
|
|
(values (- d0 1) (+ m0 m)))
|
|
|
|
(values (+ d0 1) (- m0 m)))
|
|
|
|
(if (> (* m0 -2) m)
|
|
|
|
(if (>= (* m0 2) m)
|
|
|
|
(values d0 m0)
|
|
|
|
(values (+ d0 1) (- m0 m)))
|
|
|
|
(values (- d0 1) (+ m0 m)))))))
|
|
|
|
|
|
|
|
(define ($fxdiv0 n m)
|
|
|
|
(let ([d0 (quotient n m)])
|
|
|
|
(let ([m0 (- n (* d0 m))])
|
|
|
|
(if (>= m 0)
|
|
|
|
(if (< (* m0 2) m)
|
|
|
|
(if (<= (* m0 -2) m)
|
|
|
|
d0
|
|
|
|
(- d0 1))
|
|
|
|
(+ d0 1))
|
|
|
|
(if (> (* m0 -2) m)
|
|
|
|
(if (>= (* m0 2) m)
|
|
|
|
d0
|
|
|
|
(+ d0 1))
|
|
|
|
(- d0 1))))))
|
|
|
|
|
|
|
|
(define ($fxmod0 n m)
|
|
|
|
(let ([d0 (quotient n m)])
|
|
|
|
(let ([m0 (- n (* d0 m))])
|
|
|
|
(if (>= m 0)
|
|
|
|
(if (< (* m0 2) m)
|
|
|
|
(if (<= (* m0 -2) m)
|
|
|
|
m0
|
|
|
|
(+ m0 m))
|
|
|
|
(- m0 m))
|
|
|
|
(if (> (* m0 -2) m)
|
|
|
|
(if (>= (* m0 2) m)
|
|
|
|
m0
|
|
|
|
(- m0 m))
|
|
|
|
(+ m0 m))))))
|
|
|
|
|
|
|
|
(define (fxdiv0-and-mod0 x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0-and-mod0 "division by 0")
|
2007-11-11 21:52:27 -05:00
|
|
|
(let-values ([(d m) ($fxdiv0-and-mod0 x y)])
|
|
|
|
(if (and (fixnum? d) (fixnum? m))
|
|
|
|
(values d m)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0-and-mod0
|
2007-11-11 21:52:27 -05:00
|
|
|
"results not representable as fixnums"
|
|
|
|
x y))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0-and-mod0 "not a fixnum" y))
|
|
|
|
(die 'fxdiv0-and-mod0 "not a fixnum" x)))
|
2007-11-11 21:52:27 -05:00
|
|
|
|
|
|
|
(define (fxdiv0 x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0 "division by 0")
|
2007-11-11 21:52:27 -05:00
|
|
|
(let ([d ($fxdiv0 x y)])
|
|
|
|
(if (fixnum? d)
|
|
|
|
d
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0
|
2007-11-11 21:52:27 -05:00
|
|
|
"result not representable as fixnum"
|
|
|
|
x y))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxdiv0 "not a fixnum" y))
|
|
|
|
(die 'fxdiv0 "not a fixnum" x)))
|
2007-11-11 21:52:27 -05:00
|
|
|
|
|
|
|
(define (fxmod0 x y)
|
|
|
|
(if (fixnum? x)
|
|
|
|
(if (fixnum? y)
|
|
|
|
(if ($fx= y 0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmod0 "division by 0")
|
2007-11-11 21:52:27 -05:00
|
|
|
(let ([d ($fxmod0 x y)])
|
|
|
|
(if (fixnum? d)
|
|
|
|
d
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmod0
|
2007-11-11 21:52:27 -05:00
|
|
|
"result not representable as fixnum"
|
|
|
|
x y))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'fxmod0 "not a fixnum" y))
|
|
|
|
(die 'fxmod0 "not a fixnum" x)))
|
2007-11-11 20:43:59 -05:00
|
|
|
)
|
|
|
|
|
2007-11-15 06:33:21 -05:00
|
|
|
|