Debian Codegraph @

scheme/ikarus.fixnums.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus fixnums)
18 (export fxzero? fxadd1 fxsub1 fxlognot fx+ fx- fx* fxquotient
19 fx+/carry fx*/carry fx-/carry
20 fxremainder fxmodulo fxlogor fxlogand fxlogxor fxsll fxsra
21 fx= fx< fx<= fx> fx>=
22 fx=? fx<? fx<=? fx>? fx>=?
23 fxior fxand fxxor fxnot fxif
24 fxpositive? fxnegative?
25 fxeven? fxodd?
26 fixnum->string
27 fxarithmetic-shift-left fxarithmetic-shift-right fxarithmetic-shift
28 fxmin fxmax
29 error@fx+ error@fx* error@fx- error@fxadd1 error@fxsub1
30
31 error@fxarithmetic-shift-left
32 error@fxarithmetic-shift-right
33 )
34 (import
35 (ikarus system $fx)
36 (ikarus system $chars)
37 (ikarus system $pairs)
38 (ikarus system $strings)
39 (prefix (only (ikarus) fx+ fx* fx-) sys:)
40 (except (ikarus) fxzero? fxadd1 fxsub1 fxlognot fx+ fx- fx*
41 fxquotient fxremainder fxmodulo fxlogor fxlogand
42 fxlogxor fxsll fxsra fx= fx< fx<= fx> fx>=
43 fx=? fx<? fx<=? fx>? fx>=?
44 fxior fxand fxxor fxnot fxif
45 fxpositive? fxnegative?
46 fxeven? fxodd?
47 fxarithmetic-shift-left fxarithmetic-shift-right fxarithmetic-shift
48 fx+/carry fx*/carry fx-/carry
49 fxmin fxmax
50 fixnum->string))
51
52 (define (die/overflow who . args)
53 (raise
54 (condition
55 (make-implementation-restriction-violation)
56 (make-who-condition who)
57 (make-message-condition "overflow")
58 (make-irritants-condition args))))
59
60 (define fxzero?
61 (lambda (x)
62 (cond
63 [(eq? x 0) #t]
64 [(fixnum? x) #f]
65 [else (die 'fxzero? "not a fixnum" x)])))
66
67
68 (define fxlognot
69 (lambda (x)
70 (unless (fixnum? x)
71 (die 'fxlognot "not a fixnum" x))
72 ($fxlognot x)))
73
74 (define fxnot
75 (lambda (x)
76 (unless (fixnum? x)
77 (die 'fxnot "not a fixnum" x))
78 ($fxlognot x)))
79
80 (define (make-fx-error who)
81 (case-lambda
82 [(x y)
83 (if (fixnum? x)
84 (if (fixnum? y)
85 (die/overflow who x y)
86 (die who "not a fixnum" y))
87 (die who "not a fixnum" x))]
88 [(x)
89 (if (fixnum? x)
90 (die/overflow who x)
91 (die who "not a fixnum" x))]))
92
93 (define error@fx+ (make-fx-error 'fx+))
94 (define error@fx- (make-fx-error 'fx-))
95 (define error@fx* (make-fx-error 'fx*))
96 (define error@fxadd1 (make-fx-error 'fxadd1))
97 (define error@fxsub1 (make-fx-error 'fxsub1))
98
99 (define (fx+ x y) (sys:fx+ x y))
100
101 (define (fx* x y) (sys:fx* x y))
102
103 (define fx-
104 (case-lambda
105 [(x y) (sys:fx- x y)]
106 [(x) (sys:fx- x)]))
107
108 (define fxadd1
109 (lambda (n)
110 (import (ikarus))
111 (fxadd1 n)))
112
113 (define fxsub1
114 (lambda (n)
115 (import (ikarus))
116 (fxsub1 n)))
117
118 (define false-loop
119 (lambda (who ls)
120 (if (pair? ls)
121 (if (fixnum? ($car ls))
122 (false-loop who ($cdr ls))
123 (die who "not a fixnum" ($car ls)))
124 #f)))
125
126 (define-syntax fxcmp
127 (syntax-rules ()
128 [(_ who $op)
129 (case-lambda
130 [(x y)
131 (unless (fixnum? x)
132 (die 'who "not a fixnum" x))
133 (unless (fixnum? y)
134 (die 'who "not a fixnum" y))
135 ($op x y)]
136 [(x y . ls)
137 (if (fixnum? x)
138 (if (fixnum? y)
139 (if ($op x y)
140 (let f ([x y] [ls ls])
141 (if (pair? ls)
142 (let ([y ($car ls)] [ls ($cdr ls)])
143 (if (fixnum? y)
144 (if ($op x y)
145 (f y ls)
146 (false-loop 'who ls))
147 (die 'who "not a fixnum" y)))
148 #t))
149 (false-loop 'who ls))
150 (die 'who "not a fixnum" y))
151 (die 'who "not a fixnum" x))]
152 [(x)
153 (if (fixnum? x) #t (die 'who "not a fixnum" x))])]))
154
155 (define fx= (fxcmp fx= $fx=))
156 (define fx< (fxcmp fx< $fx<))
157 (define fx<= (fxcmp fx<= $fx<=))
158 (define fx> (fxcmp fx> $fx>))
159 (define fx>= (fxcmp fx>= $fx>=))
160 (define fx=? (fxcmp fx=? $fx=))
161 (define fx<? (fxcmp fx<? $fx<))
162 (define fx<=? (fxcmp fx<=? $fx<=))
163 (define fx>? (fxcmp fx>? $fx>))
164 (define fx>=? (fxcmp fx>=? $fx>=))
165
166
167 (define fxquotient
168 (lambda (x y)
169 (unless (fixnum? x)
170 (die 'fxquotient "not a fixnum" x))
171 (unless (fixnum? y)
172 (die 'fxquotient "not a fixnum" y))
173 (when ($fxzero? y)
174 (die 'fxquotient "zero dividend" y))
175 (if (eq? y -1)
176 (if (eq? x (least-fixnum))
177 (die/overflow 'fxquotient x y)
178 ($fx- 0 x))
179 ($fxquotient x y))))
180
181 (define fxremainder
182 (lambda (x y)
183 (unless (fixnum? x)
184 (die 'fxremainder "not a fixnum" x))
185 (unless (fixnum? y)
186 (die 'fxremainder "not a fixnum" y))
187 (when ($fxzero? y)
188 (die 'fxremainder "zero dividend" y))
189 (let ([q ($fxquotient x y)])
190 ($fx- x ($fx* q y)))))
191
192 (define fxmodulo
193 (lambda (x y)
194 (unless (fixnum? x)
195 (die 'fxmodulo "not a fixnum" x))
196 (unless (fixnum? y)
197 (die 'fxmodulo "not a fixnum" y))
198 (when ($fxzero? y)
199 (die 'fxmodulo "zero dividend" y))
200 ($fxmodulo x y)))
201
202 (define-syntax fxbitop
203 (syntax-rules ()
204 [(_ who $op identity)
205 (case-lambda
206 [(x y)
207 (if (fixnum? x)
208 (if (fixnum? y)
209 ($op x y)
210 (die 'who "not a fixnum" y))
211 (die 'who "not a fixnum" x))]
212 [(x y . ls)
213 (if (fixnum? x)
214 (if (fixnum? y)
215 (let f ([a ($op x y)] [ls ls])
216 (cond
217 [(pair? ls)
218 (let ([b ($car ls)])
219 (if (fixnum? b)
220 (f ($op a b) ($cdr ls))
221 (die 'who "not a fixnum" b)))]
222 [else a]))
223 (die 'who "not a fixnum" y))
224 (die 'who "not a fixnum" x))]
225 [(x) (if (fixnum? x) x (die 'who "not a fixnum" x))]
226 [() identity])]))
227
228 (define fxlogor (fxbitop fxlogor $fxlogor 0))
229 (define fxlogand (fxbitop fxlogand $fxlogand -1))
230 (define fxlogxor (fxbitop fxlogxor $fxlogxor 0))
231 (define fxior (fxbitop fxior $fxlogor 0))
232 (define fxand (fxbitop fxand $fxlogand -1))
233 (define fxxor (fxbitop fxxor $fxlogxor 0))
234
235 (define (fxif x y z)
236 (if (fixnum? x)
237 (if (fixnum? y)
238 (if (fixnum? z)
239 ($fxlogor
240 ($fxlogand x y)
241 ($fxlogand ($fxlognot x) z))
242 (die 'fxif "not a fixnum" z))
243 (die 'fxif "not a fixnum" y))
244 (die 'fxif "not a fixnum" x)))
245
246 (define fxsra
247 (lambda (x y)
248 (unless (fixnum? x)
249 (die 'fxsra "not a fixnum" x))
250 (unless (fixnum? y)
251 (die 'fxsra "not a fixnum" y))
252 (unless ($fx>= y 0)
253 (die 'fxsra "negative shift not allowed" y))
254 ($fxsra x y)))
255
256
257 (define fxarithmetic-shift-right
258 (lambda (x y)
259 (import (ikarus))
260 (fxarithmetic-shift-right x y)))
261
262 (define fxsll
263 (lambda (x y)
264 (unless (fixnum? x)
265 (die 'fxsll "not a fixnum" x))
266 (unless (fixnum? y)
267 (die 'fxsll "not a fixnum" y))
268 (unless ($fx>= y 0)
269 (die 'fxsll "negative shift not allowed" y))
270 ($fxsll x y)))
271
272
273 (define (error@fxarithmetic-shift who x y)
274 (unless (fixnum? x)
275 (die who "not a fixnum" x))
276 (unless (fixnum? y)
277 (die who "not a fixnum" y))
278 (unless ($fx>= y 0)
279 (die who "negative shift not allowed" y))
280 (unless ($fx< y (fixnum-width))
281 (die who "shift is not less than fixnum-width" y))
282 (die/overflow who x y))
283
284 (define (error@fxarithmetic-shift-left x y)
285 (error@fxarithmetic-shift 'arithmetic-shift-left x y))
286
287 (define (error@fxarithmetic-shift-right x y)
288 (error@fxarithmetic-shift 'arithmetic-shift-right x y))
289
290 (define fxarithmetic-shift-left
291 (lambda (x y)
292 (import (ikarus))
293 (fxarithmetic-shift-left x y)))
294
295 (define fxarithmetic-shift
296 (lambda (x y)
297 (import (ikarus))
298 (define (err str x) (die 'fxarithmetic-shift str x))
299 (unless (fixnum? x) (err "not a fixnum" x))
300 (unless (fixnum? y) (err "not a fixnum" y))
301 (if ($fx>= y 0)
302 (if ($fx< y (fixnum-width))
303 (let ([r ($fxsll x y)])
304 (if ($fx= x ($fxsra r y))
305 r
306 (die/overflow 'fxarithmetic-shift x y)))
307 (err "invalid shift amount" y))
308 (if ($fx> y (- (fixnum-width)))
309 ($fxsra x ($fx- 0 y))
310 (err "invalid shift amount" y)))))
311
312 (define (fxpositive? x)
313 (if (fixnum? x)
314 ($fx> x 0)
315 (die 'fxpositive? "not a fixnum" x)))
316
317 (define (fxnegative? x)
318 (if (fixnum? x)
319 ($fx< x 0)
320 (die 'fxnegative? "not a fixnum" x)))
321
322 (define (fxeven? x)
323 (if (fixnum? x)
324 ($fxzero? ($fxlogand x 1))
325 (die 'fxeven? "not a fixnum" x)))
326
327 (define (fxodd? x)
328 (if (fixnum? x)
329 (not ($fxzero? ($fxlogand x 1)))
330 (die 'fxodd? "not a fixnum" x)))
331
332 (define fxmin
333 (case-lambda
334 [(x y)
335 (if (fixnum? x)
336 (if (fixnum? y)
337 (if ($fx< x y) x y)
338 (die 'fxmin "not a fixnum" y))
339 (die 'fxmin "not a fixnum" x))]
340 [(x y z . ls)
341 (fxmin (fxmin x y)
342 (if (fixnum? z)
343 (let f ([z z] [ls ls])
344 (if (null? ls)
345 z
346 (let ([a ($car ls)])
347 (if (fixnum? a)
348 (if ($fx< a z)
349 (f a ($cdr ls))
350 (f z ($cdr ls)))
351 (die 'fxmin "not a fixnum" a)))))
352 (die 'fxmin "not a fixnum" z)))]
353 [(x) (if (fixnum? x) x (die 'fxmin "not a fixnum" x))]))
354
355 (define fxmax
356 (case-lambda
357 [(x y)
358 (if (fixnum? x)
359 (if (fixnum? y)
360 (if ($fx> x y) x y)
361 (die 'fxmax "not a fixnum" y))
362 (die 'fxmax "not a fixnum" x))]
363 [(x y z . ls)
364 (fxmax (fxmax x y)
365 (if (fixnum? z)
366 (let f ([z z] [ls ls])
367 (if (null? ls)
368 z
369 (let ([a ($car ls)])
370 (if (fixnum? a)
371 (if ($fx> a z)
372 (f a ($cdr ls))
373 (f z ($cdr ls)))
374 (die 'fxmax "not a fixnum" a)))))
375 (die 'fxmax "not a fixnum" z)))]
376 [(x) (if (fixnum? x) x (die 'fxmax "not a fixnum" x))]))
377
378 (define-syntax define-fx
379 (syntax-rules ()
380 [(_ (name arg* ...) body)
381 (define (name arg* ...)
382 (unless (fixnum? arg*) (die 'name "not a fixnum" arg*)) ...
383 body)]))
384
385 (define-fx (fx*/carry fx1 fx2 fx3)
386 (let ([s0 ($fx+ ($fx* fx1 fx2) fx3)])
387 (values
388 s0
389 (sra (+ (* fx1 fx2) (- fx3 s0)) (fixnum-width)))))
390
391 (define-fx (fx+/carry fx1 fx2 fx3)
392 (let ([s0 ($fx+ ($fx+ fx1 fx2) fx3)])
393 (values
394 s0
395 (sra (+ (+ fx1 fx2) (- fx3 s0)) (fixnum-width)))))
396
397 (define-fx (fx-/carry fx1 fx2 fx3)
398 (let ([s0 ($fx- ($fx- fx1 fx2) fx3)])
399 (values
400 s0
401 (sra (- (- fx1 fx2) (+ s0 fx3)) (fixnum-width)))))
402
403 (module (fixnum->string)
404 (define mapping-string "0123456789ABCDEF")
405 (define f
406 (lambda (n i j radix)
407 (cond
408 [($fxzero? n)
409 (values (make-string i) j)]
410 [else
411 (let* ([q ($fxquotient n radix)]
412 [c ($string-ref mapping-string
413 ($fx- n ($fx* q radix)))])
414 (call-with-values
415 (lambda () (f q ($fxadd1 i) j radix))
416 (lambda (str j)
417 (string-set! str j c)
418 (values str ($fxadd1 j)))))])))
419 (define $fixnum->string
420 (lambda (x radix)
421 (cond
422 [($fxzero? x) (string #\0)]
423 [($fx> x 0)
424 (call-with-values
425 (lambda () (f x 0 0 radix))
426 (lambda (str j) str))]
427 [($fx= x (least-fixnum))
428 (string-append
429 ($fixnum->string ($fxquotient x radix) radix)
430 ($fixnum->string ($fx- radix ($fxmodulo x radix)) radix))]
431 [else
432 (call-with-values
433 (lambda () (f ($fx- 0 x) 1 1 radix))
434 (lambda (str j)
435 ($string-set! str 0 #\-)
436 str))])))
437 (define fixnum->string
438 (case-lambda
439 [(x)
440 (unless (fixnum? x) (die 'fixnum->string "not a fixnum" x))
441 ($fixnum->string x 10)]
442 [(x r)
443 (unless (fixnum? x) (die 'fixnum->string "not a fixnum" x))
444 (case r
445 [(2) ($fixnum->string x 2)]
446 [(8) ($fixnum->string x 8)]
447 [(10) ($fixnum->string x 10)]
448 [(16) ($fixnum->string x 16)]
449 [else (die 'fixnum->string "invalid radix" r)])])))
450
451
452 )
453
454 (library (ikarus fixnums div-and-mod)
455 (export fxdiv fxmod fxdiv-and-mod fxdiv0 fxmod0 fxdiv0-and-mod0)
456 (import
457 (ikarus system $fx)
458 (except (ikarus) fxdiv fxmod fxdiv-and-mod fxdiv0 fxmod0 fxdiv0-and-mod0))
459
460 (define ($fxdiv-and-mod n m)
461 (let ([d0 ($fxquotient n m)])
462 (let ([m0 ($fx- n ($fx* d0 m))])
463 (if ($fx>= m0 0)
464 (values d0 m0)
465 (if ($fx>= m 0)
466 (values ($fx- d0 1) ($fx+ m0 m))
467 (values ($fx+ d0 1) ($fx- m0 m)))))))
468
469 (define ($fxdiv n m)
470 (let ([d0 ($fxquotient n m)])
471 (if ($fx>= n ($fx* d0 m))
472 d0
473 (if ($fx>= m 0)
474 ($fx- d0 1)
475 ($fx+ d0 1)))))
476
477 (define ($fxmod n m)
478 (let ([d0 ($fxquotient n m)])
479 (let ([m0 ($fx- n ($fx* d0 m))])
480 (if ($fx>= m0 0)
481 m0
482 (if ($fx>= m 0)
483 ($fx+ m0 m)
484 ($fx- m0 m))))))
485
486 (define-syntax define-div-proc
487 (syntax-rules ()
488 [(_ who $unsafe-op overflow-check?)
489 (define (who x y)
490 (if (fixnum? x)
491 (if (fixnum? y)
492 (if ($fx> y 0)
493 ($unsafe-op x y)
494 (if ($fx= y 0)
495 (die 'who "division by 0" x y)
496 (if (and overflow-check? ($fx= y -1))
497 (if ($fx= x (least-fixnum))
498 (die 'who "result not representable as fixnum"
499 x y)
500 ($unsafe-op x y))
501 ($unsafe-op x y))))
502 (die 'who "not a fixnum" y))
503 (die 'who "not a fixnum" x)))]))
504
505 (define-div-proc fxdiv $fxdiv #t)
506 (define-div-proc fxmod $fxmod #f)
507 (define-div-proc fxdiv-and-mod $fxdiv-and-mod #t)
508
509
510 (define ($fxdiv0-and-mod0 n m)
511 (let ([d0 (quotient n m)])
512 (let ([m0 (- n (* d0 m))])
513 (if (>= m 0)
514 (if (< (* m0 2) m)
515 (if (<= (* m0 -2) m)
516 (values d0 m0)
517 (values (- d0 1) (+ m0 m)))
518 (values (+ d0 1) (- m0 m)))
519 (if (> (* m0 -2) m)
520 (if (>= (* m0 2) m)
521 (values d0 m0)
522 (values (+ d0 1) (- m0 m)))
523 (values (- d0 1) (+ m0 m)))))))
524
525 (define ($fxdiv0 n m)
526 (let ([d0 (quotient n m)])
527 (let ([m0 (- n (* d0 m))])
528 (if (>= m 0)
529 (if (< (* m0 2) m)
530 (if (<= (* m0 -2) m)
531 d0
532 (- d0 1))
533 (+ d0 1))
534 (if (> (* m0 -2) m)
535 (if (>= (* m0 2) m)
536 d0
537 (+ d0 1))
538 (- d0 1))))))
539
540 (define ($fxmod0 n m)
541 (let ([d0 (quotient n m)])
542 (let ([m0 (- n (* d0 m))])
543 (if (>= m 0)
544 (if (< (* m0 2) m)
545 (if (<= (* m0 -2) m)
546 m0
547 (+ m0 m))
548 (- m0 m))
549 (if (> (* m0 -2) m)
550 (if (>= (* m0 2) m)
551 m0
552 (- m0 m))
553 (+ m0 m))))))
554
555 (define (fxdiv0-and-mod0 x y)
556 (if (fixnum? x)
557 (if (fixnum? y)
558 (if ($fx= y 0)
559 (die 'fxdiv0-and-mod0 "division by 0")
560 (let-values ([(d m) ($fxdiv0-and-mod0 x y)])
561 (if (and (fixnum? d) (fixnum? m))
562 (values d m)
563 (die 'fxdiv0-and-mod0
564 "results not representable as fixnums"
565 x y))))
566 (die 'fxdiv0-and-mod0 "not a fixnum" y))
567 (die 'fxdiv0-and-mod0 "not a fixnum" x)))
568
569 (define (fxdiv0 x y)
570 (if (fixnum? x)
571 (if (fixnum? y)
572 (if ($fx= y 0)
573 (die 'fxdiv0 "division by 0")
574 (let ([d ($fxdiv0 x y)])
575 (if (fixnum? d)
576 d
577 (die 'fxdiv0
578 "result not representable as fixnum"
579 x y))))
580 (die 'fxdiv0 "not a fixnum" y))
581 (die 'fxdiv0 "not a fixnum" x)))
582
583 (define (fxmod0 x y)
584 (if (fixnum? x)
585 (if (fixnum? y)
586 (if ($fx= y 0)
587 (die 'fxmod0 "division by 0")
588 (let ([d ($fxmod0 x y)])
589 (if (fixnum? d)
590 d
591 (die 'fxmod0
592 "result not representable as fixnum"
593 x y))))
594 (die 'fxmod0 "not a fixnum" y))
595 (die 'fxmod0 "not a fixnum" x)))
596 )
597
598
599 (library (ikarus fixnums unsafe)
600 (export $fxzero? $fxadd1 $fxsub1
601 $fx+ $fx* $fx- $fx= $fx< $fx<= $fx> $fx>=
602 $fxsll $fxsra $fxlogor $fxlogand $fxlognot)
603 (import (ikarus))
604 (define $fxzero? fxzero?)
605 (define $fxadd1 fxadd1)
606 (define $fxsub1 fxsub1)
607 (define $fx+ fx+)
608 (define $fx* fx*)
609 (define $fx- fx-)
610 (define $fx= fx=)
611 (define $fx< fx<)
612 (define $fx<= fx<=)
613 (define $fx> fx>)
614 (define $fx>= fx>=)
615 (define $fxsll fxsll)
616 (define $fxsra fxsra)
617 (define $fxlogor fxlogor)
618 (define $fxlogand fxlogand)
619 (define $fxlognot fxlognot))
620
621
622
623
624
625