-
Notifications
You must be signed in to change notification settings - Fork 6
/
gen-support.lisp
181 lines (166 loc) · 6.09 KB
/
gen-support.lisp
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
(in-package "NAPA-FFT.GEN")
(defmacro define-inline-function (name (&rest args) &body body)
`(progn
(declaim (inline ,name))
(defun ,name (,@args)
,@body)
(define-compiler-macro ,name (&rest arg-forms)
`((lambda (,@',args) ,@',body)
,@arg-forms))))
(define-inline-function mul+i (x)
(declare (type complex-sample x))
#+ (and sbcl complex-float-vops)
(sb-vm::swap-complex (conjugate x))
#- (and sbcl complex-float-vops)
(* x #c(0 1d0)))
(define-inline-function mul-i (x)
(declare (type complex-sample x))
#+ (and sbcl complex-float-vops)
(conjugate (sb-vm::swap-complex x))
#- (and sbcl complex-float-vops)
(* x #c(0 -1d0)))
(define-inline-function mul+/-sqrt+i (x scale)
(declare (type complex-sample x)
(type double-float scale))
#+ (and sbcl complex-float-vops)
(let ((x (* x scale)))
(+ x (sb-vm::swap-complex (conjugate x))))
#- (and sbcl complex-float-vops)
(* x (complex scale scale)))
(define-inline-function mul+/-sqrt-i (x scale)
(declare (type complex-sample x)
(type double-float scale))
#+ (and sbcl complex-float-vops)
(let ((x (* x scale)))
(- x (sb-vm::swap-complex (conjugate x))))
#- (and sbcl complex-float-vops)
(* x (complex scale (- scale))))
(define-inline-function %scale (x scale)
(declare (type complex-sample x)
(type double-float scale)
(muffle-conditions sb-ext:code-deletion-note))
(case scale
(1d0 x)
(-1d0 (- x))
(t (* x scale))))
(define-inline-function %window (x window i)
(declare (type complex-sample x)
(type (or null (simple-array * 1)) window)
(type index i)
(muffle-conditions sb-ext:code-deletion-note))
(if window
(* x (aref window i))
x))
(defun mul-root (x root &optional default)
(setf root (mod root 1))
(case root
(0 x)
(1/2
`(- ,x))
(1/4
`(mul+i ,x))
(3/4
`(mul-i ,x))
((1/8 5/8)
`(mul+/-sqrt+i
,x
,(case root
(1/8 (/ (sqrt 2d0) 2d0))
(5/8 (- (/ (sqrt 2d0) 2d0))))))
((3/8 7/8)
`(mul+/-sqrt-i
,x
,(case root
(3/8 (- (/ (sqrt 2d0) 2d0)))
(7/8 (/ (sqrt 2d0) 2d0)))))
(t
(assert default)
`(* ,x ,default))))
(defun butterfly (i j)
(setf (values (@ i) (@ j))
(op (complex-sample complex-sample)
`(lambda (x y)
(values (+ x y)
(- x y)))
(@ i) (@ j))))
(defun rotate (i k root)
(setf (@ i)
(op (complex-sample)
`(lambda (x)
,(mul-root 'x root
(and k
`(aref twiddle ,k))))
(@ i))))
(defun scale (i scale window &optional (window-i
`(+ window-start ,i)))
(setf (@ i)
(op (complex-sample)
`(lambda (x)
(%window (%scale x ,scale)
,window
,(if window
window-i
0)))
(@ i))))
(defun emit-unrolled-for (count bindings body)
`(progn
,@(loop
for i below count
collect
`(symbol-macrolet ,(loop for (name start stride) in bindings
collect (if (numberp stride)
`(,name (+ ,start ,(* stride i)))
`(,name (+ ,start (* ,stride ,i)))))
,@body))
nil))
(define-symbol-macro %blocking-factor% 4)
(define-symbol-macro %unroll-count% 8)
(defmacro for ((count &rest bindings) &body body
&environment env)
(let ((bindings (loop for binding in bindings
collect
(destructuring-bind (name &optional (start 0) (stride 1))
(if (consp binding)
binding
(list binding))
(list name start stride))))
(unroll (macroexpand-1 '%unroll-count% env))
(blocking (macroexpand-1 '%blocking-factor% env)))
(cond ((and (integerp count)
(<= count unroll)
(not (position-if-not #'atom bindings :key #'second))
(not (position-if-not #'constantp bindings :key #'third)))
(emit-unrolled-for count bindings body))
((and (integerp count)
(> blocking 1)
(zerop (mod count blocking))
(not (find-if-not #'constantp bindings :key #'third)))
(let ((gensyms (mapcar (lambda (binding)
(make-symbol (symbol-name
(first binding))))
bindings)))
`(loop for ,(gensym "DUMMY") of-type index
from ,(truncate count
blocking)
above 0
,@(loop for (name start stride) in bindings
for gensym in gensyms
append `(for ,gensym of-type index from ,start
by ,(* stride blocking)))
do (progn
,@(loop
for i below blocking
collect
`(symbol-macrolet
,(loop for (name start stride) in bindings
for gensym in gensyms
collect (if (numberp stride)
`(,name (+ ,gensym ,(* stride i)))
`(,name (+ ,gensym (* ,stride ,i)))))
,@body))))))
(t
`(loop for ,(gensym "DUMMY") of-type index from ,count above 0
,@(loop for (name start stride) in bindings
append `(for ,name of-type index from ,start by ,stride))
do (locally
,@body))))))