/
fixnums.sls
344 lines (287 loc) · 7.81 KB
/
fixnums.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#| License
Copyright (c) 2007-2015 Llewellyn Pritchard
All rights reserved.
This source code is subject to terms and conditions of the BSD License.
See docs/license.txt. |#
(library (ironscheme typed fixnums)
(export
fixnum?
fixnum-width
least-fixnum
greatest-fixnum
fx=?
fx>?
fx<?
fx>=?
fx<=?
fxzero?
fxpositive?
fxnegative?
fxodd?
fxeven?
fxmax
fxmin
fx+
fx*
fx-
fxdiv-and-mod
fxdiv
fxmod
fxdiv0-and-mod0
fxdiv0
fxmod0
fxnot
fxand
fxior
fxxor
fxif
fxbit-count
fxlength
fxfirst-bit-set
fxbit-set?
fxcopy-bit
fxbit-field
fxcopy-bit-field
fxarithmetic-shift
fxarithmetic-shift-left
fxarithmetic-shift-right
fxrotate-bit-field
fxreverse-bit-field
fxadd1
fxsub1)
(import
(ironscheme core)
(ironscheme clr)
(ironscheme typed)
(ironscheme unsafe)
(ironscheme integrable)
(ironscheme syntax utils)
(ironscheme syntax shorthand)
(except (ironscheme)
fixnum-width
fxif
fxcopy-bit
fxbit-field
fxcopy-bit-field
fxarithmetic-shift
fxarithmetic-shift-left
fxarithmetic-shift-right
fxrotate-bit-field
fxbit-count
fxlength
fxfirst-bit-set
fxbit-set?
fxmod
fxmod0
fxdiv-and-mod
fxdiv0-and-mod0
fxdiv0
fxdiv
fxand
fxior
fxxor
fx=?
fx<?
fx>?
fx<=?
fx>=?
fxnot
fxzero?
fxpositive?
fxnegative?
fxodd?
fxeven?
greatest-fixnum
least-fixnum
fxmax
fxmin
fx-
fx+
fx*
fxreverse-bit-field
fxadd1
fxsub1))
(define-syntax define-fx
(lambda (x)
(define (pred? name)
(clr-call String EndsWith (symbol->string (syntax->datum name)) "?"))
(syntax-case x ()
[(_ (name formals ...) body body* ...)
(with-syntax (((type ...) (map (lambda (x) (datum->syntax x 'Int32)) #'(formals ...)))
(ret-type (datum->syntax #'name (if (pred? #'name) 'Boolean 'Int32))))
#'(define: (name (formals : type) ... -> ret-type)
body body* ...))])))
(define-fx (fixnum-width) 32)
(define-fx (greatest-fixnum) #x7fffffff)
(define-fx (least-fixnum) #x-80000000)
(define-fx (fxadd1 x)
($fx+ x 1))
(define-fx (fxsub1 x)
($fx- x 1))
(define-fx (fx+ x1 x2)
($fx+ x1 x2))
(define-fx (fx* x1 x2)
($fx* x1 x2))
(define fx-
(case-lambda:
[((x1 : Int32) -> Int32)
($fx- x1)]
[((x1 : Int32) (x2 : Int32) -> Int32)
($fx- x1 x2)]))
(define-fx (fxarithmetic-shift x k)
(cond
[($fx<? k 0)
($fxarithmetic-shift-right x ($fx- k))]
[else
($fxarithmetic-shift-left x k)]))
(define-fx (fxbit-count x)
(cond
[($fx<? x 0)
($fxnot (fxbit-count ($fxnot x)))]
[else
(let: f ((count 0)(x x) -> Int32)
(if (not ($fx=? 0 x))
(f ($fx+ count 1)
($fxand x ($fx- x 1)))
count))]))
(define-fx (fxlength x)
(if ($fx<? x 0)
(fxlength ($fxnot x))
(let: f ((count 0)(x x) -> Int32)
(if ($fx<? 0 x)
(f ($fx+ count 1) ($fxarithmetic-shift-right x 1))
count))))
(define-fx (fxfirst-bit-set x)
(if ($fx=? x 0)
-1
(let: f ((count 0)(x x) -> Int32)
(if (not ($fx=? 0 x))
(if ($fx=? 1 ($fxand 1 x))
count
(f ($fx+ count 1)
($fxarithmetic-shift-right x 1)))
count))))
(define-fx (fxbit-set? x k)
($fx=? 1 ($fxand 1 ($fxarithmetic-shift-right x k))))
(define-fx (fxnot x1)
($fxnot x1))
(define-syntax-case (define-fx-comparer name)
(with-syntax ((uname (syntax-format "$~a" #'name #'name)))
#'(define name
(case-lambda:
[((x1 : Int32) (x2 : Int32) -> Boolean)
(uname x1 x2)]
;; this is bit hard for now...
#;[(x1 x2 . rest)
(let f ((a x1)(b (cons x2 rest)))
(cond
[(null? b) #t]
[(name a ($car b))
(f ($car b) ($cdr b))]
[else #f]))]))))
(define-fx-comparer fx=?)
(define-fx-comparer fx<?)
(define-fx-comparer fx<=?)
(define-fx-comparer fx>?)
(define-fx-comparer fx>=?)
(define-syntax-case (define-fx-bitop name id)
(with-syntax ((uname (syntax-format "$~a" #'name #'name)))
#'(define name
(case-lambda:
[(-> Int32) id]
[((x : Int32) -> Int32) x]
[((x1 : Int32) (x2 : Int32) -> Int32)
(uname x1 x2)]))))
(define-fx-bitop fxand -1)
(define-fx-bitop fxior 0)
(define-fx-bitop fxxor 0)
(define-fx (fxdiv x1 x2)
(cond
[($fx=? 0 x1) 0]
[($fx<? 0 x1)
($fxdiv x1 x2)]
[($fx<? 0 x2)
($fx- ($fxdiv ($fx+ x1 1) x2) 1)]
[else
($fx+ ($fxdiv ($fx+ x1 1) x2) 1)]))
(define-fx (fxmod x1 x2)
($fx- 0 ($fx- ($fx* (fxdiv x1 x2) x2) x1)))
;; untyped
(define (fxdiv-and-mod x1 x2)
(import (rnrs))
(let ((d (fxdiv x1 x2)))
(values d ($fx- 0 ($fx- ($fx* d x2) x1)))))
(define-fx (fxdiv0 x1 x2)
(let* ((d (fxdiv x1 x2))
(m ($fx- 0 ($fx- ($fx* d x2) x1)))
(halfx2 ($fxdiv x2 2))
(abshalfx2 (if ($fx<=? 0 halfx2)
halfx2
($fx- 0 halfx2))))
(cond
[($fx<? m ($fx+ abshalfx2 ($fxand x2 1)))
d]
[($fx>? x2 0)
($fx+ d 1)]
[else
($fx- d 1)])))
(define-fx (fxmod0 x1 x2)
($fx- 0 ($fx- ($fx* (fxdiv0 x1 x2) x2) x1)))
;; untyped
(define (fxdiv0-and-mod0 x1 x2)
(let ((d (fxdiv0 x1 x2)))
(values d ($fx- 0 ($fx- ($fx* d x2) x1)))))
(define-fx (fxpositive? r)
($fx<? 0 r))
(define-fx (fxnegative? r)
($fx>? 0 r))
(define-fx (fxzero? r)
($fx=? 0 r))
(define-fx (fxeven? n)
($fx=? 0 ($fxand n 1)))
(define-fx (fxodd? n)
($fx=? 1 ($fxand n 1)))
(define-fx (fxmax a b)
(if ($fx<? a b) b a))
(define-fx (fxmin a b)
(if ($fx>? a b) b a))
(define-fx (fxif fx1 fx2 fx3)
($fxior ($fxand fx1 fx2)
($fxand ($fxnot fx1) fx3)))
(define-fx (fxcopy-bit fx1 fx2 fx3)
(fxif ($fxarithmetic-shift-left 1 fx2)
($fxarithmetic-shift-left fx3 fx2) fx1))
(define-fx (fxbit-field fx1 fx2 fx3)
($fxarithmetic-shift-right
($fxand fx1 ($fxnot ($fxarithmetic-shift-left -1 fx3)))
fx2))
(define-fx (fxcopy-bit-field to start end from)
(fxif
($fxand
($fxarithmetic-shift-left -1 start)
($fxnot ($fxarithmetic-shift-left -1 end)))
($fxarithmetic-shift-left from start)
to))
(define-fx (fxarithmetic-shift-left fx1 fx2)
($fxarithmetic-shift-left fx1 fx2))
(define-fx (fxarithmetic-shift-right fx1 fx2)
($fxarithmetic-shift-right fx1 fx2))
(define-fx (fxrotate-bit-field n start end count)
(let* ((width ($fx- end start))
(field1 (fxbit-field n start ($fx- end count)))
(field2 (fxbit-field n start end)))
(fxcopy-bit-field n start end
($fxior
($fxarithmetic-shift-left field1 count)
($fxarithmetic-shift-right field2 ($fx- width count))))))
;; from larceny
(define-fx (fxreverse-bit-field x1 start end)
(do ((width ($fx- end start) ($fx- width 1))
(bits (fxbit-field x1 start end)
($fxarithmetic-shift-right bits 1))
(rbits 0
($fxior ($fxarithmetic-shift-left rbits 1)
($fxand bits 1))))
(($fx=? width 0)
(fxcopy-bit-field x1 start end rbits))))
#;(clr-static-call System.Diagnostics.Debugger Break))