File: float-tran.lisp

package info (click to toggle)
cmucl 21d-2.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 45,364 kB
  • sloc: lisp: 378,758; ansic: 30,678; asm: 2,977; sh: 1,417; makefile: 352; csh: 31
file content (212 lines) | stat: -rw-r--r-- 7,834 bytes parent folder | download | duplicates (3)
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
;; Tests for various float transformations.

(defpackage :float-tran-tests
  (:use :cl :lisp-unit))

(in-package "FLOAT-TRAN-TESTS")

(define-test decode-float-sign
  "Test type derivation of the sign from decode-float"
  (assert-equalp (c::make-member-type :members (list 1f0 -1f0))
		 (c::decode-float-sign-derive-type-aux
		  (c::specifier-type 'single-float)))
  (assert-equalp (c::make-member-type :members (list 1d0 -1d0))
		 (c::decode-float-sign-derive-type-aux
		  (c::specifier-type 'double-float)))
  (assert-equalp (c::make-member-type :members (list 1f0))
		 (c::decode-float-sign-derive-type-aux
		  (c::specifier-type '(single-float (0f0))))))

(define-test decode-float-exp
  "Test type derivation of the exponent from decode-float"
  (assert-equalp (c::specifier-type '(integer -148 128))
		 (c::decode-float-exp-derive-type-aux
		  (c::specifier-type 'single-float)))
  (assert-equalp (c::specifier-type '(integer -1073 1024))
		 (c::decode-float-exp-derive-type-aux
		  (c::specifier-type 'double-float)))
  #+double-double
  (assert-equalp (c::specifier-type '(integer -1073 1024))
		 (c::decode-float-exp-derive-type-aux
		  (c::specifier-type 'kernel:double-double-float)))
  (assert-equalp (c::specifier-type '(integer 2 8))
		 (c::decode-float-exp-derive-type-aux
		  (c::specifier-type '(double-float 2d0 128d0)))))
				    
(define-test log2-single-transform
  "Test tranform of (log x 2) to (kernel::log2 x)"
  (let ((test-fun
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float (0f0)) x))
		     (log x 2)))))
    ;; test-fun should have transformed (log x 2) to kernel::log2
    (assert-true (search "log2" (with-output-to-string (*standard-output*)
				  (disassemble test-fun)))))
  (let ((test-fun
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float 0f0) x))
		     (log x 2)))))
    ;; test-fun should not have transformed (log x 2) to kernel::log2
    ;; because x can be -0 for which log should return a complex
    ;; result.
    (assert-false (search "log2" (with-output-to-string (*standard-output*)
				   (disassemble test-fun)))))
  (let ((test-fun
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float 0f0) x))
		     (log x 2d0)))))
    ;; test-fun should not have transformed (log x 2) to kernel::log2
    ;; because the result should be a double due to floating-point
    ;; contagion.
    (assert-false (search "log2" (with-output-to-string (*standard-output*)
				   (disassemble test-fun))))))

(define-test log2-double-transform
  "Test tranform of (log x 2) to (kernel::log2 x)"
  (let ((test-fun-good
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float (0d0)) x))
		     (log x 2)))))
    ;; test-fun should have transformed (log x 2) to kernel::log2
    (assert-true (search "log2" (with-output-to-string (*standard-output*)
				  (disassemble test-fun-good)))))
  (let ((test-fun-bad
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float 0d0) x))
		     (log x 2)))))
    ;; test-fun should not have transformed (log x 2) to kernel::log2
    ;; because x can be -0 for which log should return a complex
    ;; result.
    (assert-false (search "log2" (with-output-to-string (*standard-output*)
				   (disassemble test-fun-bad)))))
  (let ((test-fun-good-2
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float (0d0)) x))
		     (log x 2f0)))))
    ;; test-fun should have transformed (log x 2) to kernel::log2
    (assert-true (search "log2" (with-output-to-string (*standard-output*)
				  (disassemble test-fun-good-2))))))

(define-test log10-single-transform
  "Test tranform of (log x 10) to (kernel::log2 x)"
  (let ((test-fun-good
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float (0f0)) x))
		     (log x 10)))))
    ;; test-fun should have transformed (log x 2) to kernel:%log10
    (assert-true (search "log10" (with-output-to-string (*standard-output*)
				  (disassemble test-fun-good)))))
  (let ((test-fun-bad
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float 0f0) x))
		     (log x 10)))))
    ;; test-fun should not have transformed (log x 2) to kernel:%log10
    ;; because x can be -0 for which log should return a complex
    ;; result.
    (assert-false (search "log10" (with-output-to-string (*standard-output*)
				   (disassemble test-fun-bad)))))
  (let ((test-fun-bad-2
	  (compile nil
		   (lambda (x)
		     (declare (type (single-float (0f0)) x))
		     (log x 10d0)))))
    ;; test-fun should not have transformed (log x 2) to kernel:%log10
    ;; because the result should be a double due to floating-point
    ;; contagion.
    (assert-false (search "log10" (with-output-to-string (*standard-output*)
				   (disassemble test-fun-bad-2))))))

(define-test log10-double-transform
  "Test tranform of (log x 10) to (kernel:%log10 x)"
  (let ((test-fun-good
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float (0d0)) x))
		     (log x 10)))))
    ;; test-fun should have transformed (log x 10) to kernel:%log10
    (assert-true (search "log10" (with-output-to-string (*standard-output*)
				  (disassemble test-fun-good)))))
  (let ((test-fun-bad
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float 0d0) x))
		     (log x 10)))))
    ;; test-fun should not have transformed (log x 10) to kernel:%log10
    ;; because x can be -0 for which log should return a complex
    ;; result.
    (assert-false (search "log10" (with-output-to-string (*standard-output*)
				   (disassemble test-fun-bad)))))
  (let ((test-fun-good-2
	  (compile nil
		   (lambda (x)
		     (declare (type (double-float (0d0)) x))
		     (log x 10f0)))))
    ;; test-fun should have transformed (log x 10) to kernel:%log10
    (assert-true (search "log10" (with-output-to-string (*standard-output*)
				   (disassemble test-fun-good-2))))))

(define-test scale-float-transform.single
  (let ((xfrm-scale
	  (compile nil
		   (lambda (x n)
		     (declare (single-float x)
			      (type (integer -149 127) n))
		     (scale-float x n))))
	(scale
	  (compile nil
		   (lambda (x n)
		     (declare (single-float x)
			      (type (signed-byte 32) n))
		     (scale-float x n)))))
    ;; If the deftransform for scale-float was applied, (scale-float
    ;; most-positive-single-float 2) is done as a multiplication which
    ;; will overflow.  The operation will be '*.  If the deftransform
    ;; was not applied, the overflow will still be signaled, but the
    ;; operation will be 'scale-float.
    (assert-eql '*
		(handler-case 
		    (funcall xfrm-scale most-positive-single-float 2)
		  (arithmetic-error (c)
	            (arithmetic-error-operation c))))
    (assert-eql 'scale-float
		(handler-case
		    (funcall scale most-positive-single-float 2)
		  (arithmetic-error (c)
		    (arithmetic-error-operation c))))))

(define-test scale-float-transform.double
  (let ((xfrm-scale
	  (compile nil
		   (lambda (x n)
		     (declare (double-float x)
			      (type (integer -1074 1023) n))
		     (scale-float x n))))
	(scale
	  (compile nil
		   (lambda (x n)
		     (declare (double-float x)
			      (type (signed-byte 32) n))
		     (scale-float x n)))))
    ;; If the deftransform for scale-float was applied, (scale-float
    ;; most-positive-double-float 2) is done as a multiplication which
    ;; will overflow.  The operation will be '*.  If the deftransform
    ;; was not applied, the overflow will still be signaled, but the
    ;; operation will be 'scale-float.
    (assert-eql '*
		(handler-case 
		    (funcall xfrm-scale most-positive-double-float 2)
		  (arithmetic-error (c)
	            (arithmetic-error-operation c))))
    (assert-eql 'scale-float
		(handler-case
		    (funcall scale most-positive-double-float 2)
		  (arithmetic-error (c)
		    (arithmetic-error-operation c))))))