-
Notifications
You must be signed in to change notification settings - Fork 6
/
ops-racket.rkt
93 lines (75 loc) · 2.87 KB
/
ops-racket.rkt
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
#lang racket
(provide (all-defined-out))
(define-syntax-rule (<< x y bit)
(if (and (>= y 0) (< y bit))
(let ([mask (sub1 (arithmetic-shift 1 (- bit y)))])
(finitize (arithmetic-shift (bitwise-and x mask) y) bit))
0))
(define-syntax-rule (>> x y)
(if (>= y 0)
(arithmetic-shift x (- y))
(if (>= x 0) 0 -1)))
(define-syntax-rule (>>> x y bit)
(if (= y 0)
x
(let ([unsigned-x (bitwise-and x (sub1 (arithmetic-shift 1 bit)))])
(>> unsigned-x y))))
(define (finitize num bit)
(let* ([mask (arithmetic-shift -1 bit)]
[masked (bitwise-and (bitwise-not mask) num)])
(if (= (bitwise-and masked (arithmetic-shift 1 (sub1 bit))) 0)
masked
(bitwise-ior mask masked))))
(define-syntax-rule (get-field* f o) (get-field f o))
(define-syntax-rule (is-a?* o type) (is-a? o type))
(define-syntax-rule (send* o f ...) (send o f ...))
;;;;;;;;;;;;;;;;;;;;; assert ;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax-rule (assert-return c message val) val)
(define-syntax assert
(syntax-rules ()
((assert x)
(unless x
(raise (exn "racket: assert fail" (current-continuation-marks)))))
((assert x y)
(unless x
(raise (exn (format "racket: assert fail: ~a" y) (current-continuation-marks)))))))
(define-syntax-rule (for/all ([a b] ...) expr)
(let ([a b] ...) expr))
(define-syntax-rule (for*/all ([a b] ...) expr)
(let* ([a b] ...) expr))
;;;;;;;;;;;;;;;;;;;;; vector ;;;;;;;;;;;;;;;;;;;;;;;;
;; TODO: do we need this?
(define (vector-copy-len vec start len)
;(pretty-display `(vector-copy ,start ,len))
(for/vector ([i len]) (vector-ref vec (+ start i))))
(define (vector-copy-len! dest dest-start src
src-start len)
(vector-copy! dest dest-start src src-start (+ src-start len)))
(define (vector-extract a b shift)
;(pretty-display `(vector-extract ,a ,b ,shift))
(define len (vector-length a))
(define pos (- len shift))
(define vec (make-vector len))
(for ([i (in-range pos)])
;(pretty-display `(first ,i ,(+ shift i)))
(vector-set! vec i (vector-ref a (+ shift i))))
(for ([i (in-range shift)])
;(pretty-display `(second ,(+ pos i) ,i))
(vector-set! vec (+ pos i) (vector-ref b i)))
;(pretty-display `(vector-extract-ret ,vec))
vec)
;;;;;;;;;;;;;;;;;;;;; lookup table ;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax-rule (lookup-table assoc)
(make-hash assoc))
(define-syntax lt-ref
(syntax-rules ()
[(lt-ref table key) (hash-ref table key)]
[(lt-ref table key default) (hash-ref table key default)]))
;;;;;;;;;;;;;;;;;;;;; multiplication ;;;;;;;;;;;;;;;;;;;;;;;;
(define (smmul x y bit)
(finitize (arithmetic-shift (* x y) (- bit)) bit))
(define (ummul x y bit)
(let ([mask (sub1 (arithmetic-shift 1 bit))])
(finitize
(arithmetic-shift (* (bitwise-and x mask) (bitwise-and y mask)) (- bit))
bit)))