Debian Codegraph @

lib/scheme/fixnum.scm

raw
1 ;;;
2 ;;; scheme.fixnum - Fixnums (R7RS Tangerine)
3 ;;;
4 ;;; Copyright (c) 2017-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Originally SRFI-143
35 (define-module scheme.fixnum
36 (use srfi.60 :only (bitwise-if rotate-bit-field reverse-bit-field))
37 (use scheme.division :only (balanced/))
38 (export fx-width fx-greatest fx-least
39 fixnum? fx=? fx<? fx<=? fx>? fx>=?
40 fxzero? fxpositive? fxnegative? fxodd? fxeven?
41 fxmax fxmin
42 fx+ fx- fxneg fx* fxquotient fxremainder fxabs fxsquare fxsqrt
43 fxnot fxand fxior fxxor fxarithmetic-shift
44 fxarithmetic-shift-left fxarithmetic-shift-right
45 fxbit-count fxlength fxif fxbit-set?
46 fxcopy-bit fxfirst-set-bit
47 fxbit-field fxbit-field-rotate fxbit-field-reverse
48 fx+/carry fx-/carry fx*/carry))
49 (select-module scheme.fixnum)
50
51 ;; We don't do define-constant, for these may differ among platforms
52 ;; and making them constant would interfere with cross-compilation
53 (define fx-width (fixnum-width))
54 (define fx-greatest (greatest-fixnum))
55 (define fx-least (least-fixnum))
56
57 ;; fixnum? - builtin
58
59 ;; In Gauche, using standard operators is the most efficient.
60 (define-inline fx=? =)
61 (define-inline fx<? <)
62 (define-inline fx<=? <=)
63 (define-inline fx>? >)
64 (define-inline fx>=? >=)
65 (define-inline fxzero? zero?)
66 (define-inline fxpositive? positive?)
67 (define-inline fxnegative? negative?)
68 (define-inline fxodd? odd?)
69 (define-inline fxeven? even?)
70 (define-inline fxmax max)
71 (define-inline fxmin min)
72
73 (define-inline (fx+ i j) (+ i j))
74 (define-inline (fx- i j) (- i j))
75 (define-inline (fxneg i) (- i))
76 (define-inline (fx* i j) (* i j))
77 (define-inline (fxquotient i j) (quotient i j))
78 (define-inline (fxremainder i j) (remainder i j))
79 (define-inline (fxabs i) (abs i))
80 (define-inline (fxsquare i) (square i))
81 (define-inline (fxsqrt i) (exact-integer-sqrt i))
82
83 (define-inline (fxnot i) (lognot i))
84 (define-inline fxand logand)
85 (define-inline fxior logior)
86 (define-inline fxxor logxor)
87 (define-inline (fxarithmetic-shift i c) (ash i c))
88 (define-inline (fxarithmetic-shift-left i c) (ash i c))
89 (define-inline (fxarithmetic-shift-right i c) (ash i (- c)))
90 (define-inline (fxbit-count i) (logcount i))
91 (define-inline (fxlength i) (integer-length i))
92 (define-inline (fxif mask i j) (bitwise-if mask i j))
93 (define-inline (fxbit-set? index i) (logbit? index i))
94 (define-inline (fxcopy-bit index i boolean) (copy-bit index i boolean))
95 (define-inline (fxfirst-set-bit i) (twos-exponent-factor i))
96 (define-inline (fxbit-field i start end) (bit-field i start end))
97 (define-inline (fxbit-field-rotate i count start end)
98 (rotate-bit-field i count start end))
99 (define-inline (fxbit-field-reverse i start end)
100 (reverse-bit-field i start end))
101
102 ;; Procedures that require additional definitions
103 ;; Can be more efficient.
104
105 (define *modulo* (ash 2 (- (fixnum-width) 1)))
106
107 (define (fx+/carry i j k)
108 (let1 v (+ i j k)
109 (receive (q r) (balanced/ v *modulo*)
110 (values r q))))
111
112 (define (fx-/carry i j k)
113 (let1 v (- i j k)
114 (receive (q r) (balanced/ v *modulo*)
115 (values r q))))
116
117 (define (fx*/carry i j k)
118 (let1 v (+ (* i j) k)
119 (receive (q r) (balanced/ v *modulo*)
120 (values r q))))