| 1 |
;; SRFI-143 r6rs library implementation |
| 2 |
;; |
| 3 |
;; Implements the fixnum operators specified in SRFI-143 using a combination of |
| 4 |
;; R6RS sepcified version and Chez Scheme provided operators where R6RS does |
| 5 |
;; not include them. These are in the helpers library and non-Chez Scheme |
| 6 |
;; versions are included for supporting other R6RS implementations. |
| 7 |
;; |
| 8 |
;; Copyright (c) 2018 - 2020 Andrew W. Keep |
| 9 |
;; Copyright (c) 2020 Amirouche Boubekki |
| 10 |
|
| 11 |
(library (srfi :143 fixnums) |
| 12 |
(export |
| 13 |
fx-width fx-greatest fx-least |
| 14 |
|
| 15 |
fixnum? fx=? fx<? fx>? fx<=? fx>=? fxzero? fxpositive? fxnegative? fxodd? |
| 16 |
fxeven? fxmax fxmin |
| 17 |
|
| 18 |
fx+ fx- fxneg fx* fxquotient fxremainder fxabs fxsquare fxsqrt |
| 19 |
|
| 20 |
fx+/carry fx-/carry fx*/carry |
| 21 |
|
| 22 |
fxnot fxand fxior fxxor fxarithmetic-shift fxarithmetic-shift-left |
| 23 |
fxarithmetic-shift-right fxbit-count fxlength fxif fxbit-set? fxcopy-bit |
| 24 |
fxfirst-set-bit fxbit-field fxbit-field-rotate fxbit-field-reverse) |
| 25 |
(import (except (rnrs) fxbit-set? fxcopy-bit) |
| 26 |
(prefix (only (rnrs) fxbit-set? fxcopy-bit) r6:) |
| 27 |
(srfi :143 helpers)) |
| 28 |
|
| 29 |
(define fx-width (fixnum-width)) |
| 30 |
(define fx-greatest (greatest-fixnum)) |
| 31 |
(define fx-least (least-fixnum)) |
| 32 |
|
| 33 |
(define (fxbit-set? index b) |
| 34 |
(r6:fxbit-set? b index)) |
| 35 |
|
| 36 |
(define (fxcopy-bit index i c) |
| 37 |
(if (fxnegative? index) |
| 38 |
(r6:fxcopy-bit i (fx+ fx-width index) (if c 1 0)) |
| 39 |
(r6:fxcopy-bit i index (if c 1 0)))) |
| 40 |
|
| 41 |
(define fxneg (lambda (i) (fx- i))) |
| 42 |
|
| 43 |
(define fxsquare (lambda (i) (fx* i i))) |
| 44 |
|
| 45 |
(define fxsqrt (lambda (i) (exact-integer-sqrt i))) |
| 46 |
|
| 47 |
(define fxfirst-set-bit (lambda (i) (fxfirst-bit-set i))) |
| 48 |
|
| 49 |
(define (fxbit-field-rotate i count start end) |
| 50 |
(if (fxnegative? count) |
| 51 |
(fxrotate-bit-field i start end (+ count (- end start))) |
| 52 |
(fxrotate-bit-field i start end count))) |
| 53 |
|
| 54 |
(define fxbit-field-reverse (lambda (i s e) (fxreverse-bit-field i s e)))) |