/[oct]/oct/qd-rep.lisp
ViewVC logotype

Contents of /oct/qd-rep.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Thu Jul 17 17:26:43 2008 UTC (5 years, 9 months ago) by rtoy
Branch: MAIN
CVS Tags: GIT_CONVERSION, HEAD
Changes since 1.14: +7 -1 lines
Add or cleanup more docstrings.
1 ;;;; -*- Mode: lisp -*-
2 ;;;;
3 ;;;; Copyright (c) 2007 Raymond Toy
4 ;;;;
5 ;;;; Permission is hereby granted, free of charge, to any person
6 ;;;; obtaining a copy of this software and associated documentation
7 ;;;; files (the "Software"), to deal in the Software without
8 ;;;; restriction, including without limitation the rights to use,
9 ;;;; copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ;;;; copies of the Software, and to permit persons to whom the
11 ;;;; Software is furnished to do so, subject to the following
12 ;;;; conditions:
13 ;;;;
14 ;;;; The above copyright notice and this permission notice shall be
15 ;;;; included in all copies or substantial portions of the Software.
16 ;;;;
17 ;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 ;;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 ;;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 ;;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 ;;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 ;;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 ;;;; OTHER DEALINGS IN THE SOFTWARE.
25
26 (in-package #:octi)
27
28 ;;; This file contains the actual representation of a %quad-double
29 ;;; number. The only real requirement for a %quad-double number is an
30 ;;; object that can hold four double-float values.
31 ;;;
32 ;;; This object is created by %MAKE-QD-D. The four double-float
33 ;;; elements of a %quad-double are accessed via QD-0, QD-1, QD-2, and
34 ;;; QD-3. A convenience function, QD-PARTS, is also provided to
35 ;;; return all four values at once.
36
37 ;; All of the following functions should be inline to reduce consing.
38 #+(and cmu (not oct-array))
39 (declaim (inline
40 qd-0 qd-1 qd-2 qd-3
41 %make-qd-d
42 qd-parts))
43
44 #+(and cmu (not oct-array))
45 (progn
46 ;; For CMUCL (at least recent enough versions that support
47 ;; double-double-float), we can use a (complex double-double-float) to
48 ;; hold our 4 double-float values. This has a nice advantage: Much of
49 ;; the arithmetic can be done without consing, provided the key
50 ;; functions are inline.
51 (deftype %quad-double ()
52 '(complex double-double-float))
53
54 ;; QD-0, QD-1, QD-2, and QD-3 extract the various parts of a
55 ;; quad-double. QD-0 is the most significant part and QD-3 is the
56 ;; least.
57 (defun qd-0 (q)
58 "Return the most significant double-float in the %QUAD-DOUBLE number Q"
59 (declare (type %quad-double q)
60 (optimize (speed 3)))
61 (kernel:double-double-hi (realpart q)))
62 (defun qd-1 (q)
63 "Return the second most significant double-float in the %QUAD-DOUBLE number Q"
64 (declare (type %quad-double q)
65 (optimize (speed 3)))
66 (kernel:double-double-lo (realpart q)))
67 (defun qd-2 (q)
68 "Return the third most significant double-float in the %QUAD-DOUBLE number Q"
69 (declare (type %quad-double q)
70 (optimize (speed 3)))
71 (kernel:double-double-hi (imagpart q)))
72 (defun qd-3 (q)
73 "Return the fourth most significant (least significant) double-float in the
74 %QUAD-DOUBLE number Q"
75 (declare (type %quad-double q)
76 (optimize (speed 3)))
77 (kernel:double-double-lo (imagpart q)))
78
79 (eval-when (:compile-toplevel :load-toplevel :execute)
80 (defun %make-qd-d (a0 a1 a2 a3)
81 "Make a %QUAD-DOUBLE from four double-floats, exactly using the given
82 values. No check is made to see if the values make sense. A0 is
83 the most significant part and A3 is the least.
84 "
85 (declare (double-float a0 a1
86 a2 a3))
87 (complex (kernel:%make-double-double-float a0 a1)
88 (kernel:%make-double-double-float a2 a3)))
89 )
90
91 (defmacro %store-qd-d (target q0 q1 q2 q3)
92 (declare (ignore target))
93 `(%make-qd-d ,q0 ,q1, q2, q3))
94
95 (defun qd-parts (qd)
96 "Extract the four doubles comprising a quad-double and return them
97 as multiple values. The most significant double is the first value."
98 (declare (type %quad-double qd))
99 (let ((re (realpart qd))
100 (im (imagpart qd)))
101 (values (kernel:double-double-hi re)
102 (kernel:double-double-lo re)
103 (kernel:double-double-hi im)
104 (kernel:double-double-lo im))))
105
106 ) ; end progn
107
108 #+oct-array
109 (progn
110 ;; For Lisp's without a double-double-float type, I think the best we
111 ;; can do is a simple-array of four double-floats. Even with
112 ;; inlining, I think there will lots of consing when working with this
113 ;; type.
114 ;;
115 ;; A defstruct would also work but I think a simple-array is the
116 ;; simplest and smallest representation.
117 (deftype %quad-double ()
118 '(simple-array double-float (4)))
119
120 #||
121 (defun qd-0 (q)
122 (declare (type %quad-double q)
123 (optimize (speed 3)))
124 (aref q 0))
125
126 (defun qd-1 (q)
127 (declare (type %quad-double q)
128 (optimize (speed 3)))
129 (aref q 1))
130
131 (defun qd-2 (q)
132 (declare (type %quad-double q)
133 (optimize (speed 3)))
134 (aref q 2))
135
136 (defun qd-3 (q)
137 (declare (type %quad-double q)
138 (optimize (speed 3)))
139 (aref q 3))
140
141 ||#
142
143 (defmacro qd-0 (q)
144 `(aref ,q 0))
145
146 (defmacro qd-1 (q)
147 `(aref ,q 1))
148
149 (defmacro qd-2 (q)
150 `(aref ,q 2))
151
152 (defmacro qd-3 (q)
153 `(aref ,q 3))
154
155 #+(or)
156 (eval-when (:compile-toplevel :load-toplevel :execute)
157 (defun %make-qd-d (a0 a1 a2 a3)
158 "Make a %quad-double from 4 double-floats, exactly using the given
159 values. No check is made to see if the values make sense. A0 is
160 the most significant part and A3 is the least.
161 "
162 (declare (double-float a0 a1
163 a2 a3)
164 (optimize (speed 3)))
165 (let ((a (make-array 4 :element-type 'double-float)))
166 (setf (aref a 0) a0)
167 (setf (aref a 1) a1)
168 (setf (aref a 2) a2)
169 (setf (aref a 3) a3)
170 a))
171 )
172
173 (defmacro %make-qd-d (a0 a1 a2 a3)
174 (let ((a (gensym)))
175 `(let ((,a (make-array 4 :element-type 'double-float)))
176 (setf (aref ,a 0) ,a0)
177 (setf (aref ,a 1) ,a1)
178 (setf (aref ,a 2) ,a2)
179 (setf (aref ,a 3) ,a3)
180 ,a)))
181
182 (defmacro %store-qd-d (target q0 q1 q2 q3)
183 (let ((dest (gensym "TARGET-")))
184 `(let ((,dest ,target))
185 (declare (type %quad-double ,dest))
186 (setf (aref ,dest 0) ,q0)
187 (setf (aref ,dest 1) ,q1)
188 (setf (aref ,dest 2) ,q2)
189 (setf (aref ,dest 3) ,q3)
190 ,dest)))
191
192 (defun qd-parts (qd)
193 "Extract the four doubles comprising a quad-double and return them
194 as multiple values. The most significant double is the first value."
195 (declare (type %quad-double qd))
196 (values (aref qd 0)
197 (aref qd 1)
198 (aref qd 2)
199 (aref qd 3)))
200
201 ) ; end progn
202
203 (defmacro with-qd-parts ((a0 a1 a2 a3) qd &body body)
204 (let ((q (gensym)))
205 `(let* ((,q ,qd)
206 (,a0 (qd-0 ,q))
207 (,a1 (qd-1 ,q))
208 (,a2 (qd-2 ,q))
209 (,a3 (qd-3 ,q)))
210 ,@body)))
211
212
213 ;; Some simple support for infinity and NaN. For CMUCL, we can import
214 ;; the desired functions from the EXTENSIONS package.
215
216 ;; Implementation for Allegro
217 #+allegro
218 (progn
219 (defmacro float-infinity-p (x)
220 `(= (abs ,x) #.excl::*infinity-double*))
221
222 (defun float-nan-p (x)
223 (excl::nan-p x))
224
225 (defun float-trapping-nan-p (x)
226 nil)
227 ) ; end progn
228
229
230 ;; Default implementation. Assume we can't recognize any of these.
231
232 #-(or cmu allegro)
233 (progn
234 (defun float-infinity-p (x)
235 (declare (ignore x))
236 nil)
237 (defun float-nan-p (x)
238 (declare (ignore x))
239 nil)
240 (defun float-trapping-nan-p (x)
241 (declare (ignore x))
242 nil)
243 ) ; end progn
244
245
246 ;; Define some compiler macros to transform add-qd to add-qd-t
247 ;; directly. For CMU without :oct-array, we always replace the
248 ;; parameter C with NIL because we don't use it. For other Lisps, we
249 ;; create the necessary object and call add-qd-t.
250 ;;
251 ;; Do the same mul-qd and other similar functions.
252 (macrolet
253 ((frob (qd qd-t)
254 #-oct-array
255 `(define-compiler-macro ,qd (a b &optional c)
256 (if c
257 `(setf ,c (,',qd-t ,a ,b nil))
258 `(,',qd-t ,a ,b nil)))
259 #+oct-array
260 `(define-compiler-macro ,qd (a b &optional c)
261 (if c
262 `(,',qd-t ,a ,b ,c)
263 `(,',qd-t ,a ,b (%make-qd-d 0d0 0d0 0d0 0d0))))))
264 (frob add-qd add-qd-t)
265 (frob mul-qd mul-qd-t)
266 (frob div-qd div-qd-t)
267 (frob add-qd-d add-qd-d-t)
268 (frob mul-qd-d mul-qd-d-t))
269
270 #+(and cmu (not oct-array))
271 (define-compiler-macro sub-qd (a b &optional c)
272 (if c
273 `(setf ,c (add-qd-t ,a (neg-qd ,b) nil))
274 `(add-qd-t ,a (neg-qd ,b) nil)))
275
276 #-(and cmu (not oct-array))
277 (define-compiler-macro sub-qd (a b &optional c)
278 (if c
279 `(add-qd-t ,a (neg-qd ,b) ,c)
280 `(add-qd-t ,a (neg-qd ,b) (%make-qd-d 0d0 0d0 0d0 0d0))))
281
282 #+(and cmu (not oct-array))
283 (define-compiler-macro sqr-qd (a &optional c)
284 (if c
285 `(setf ,c (sqr-qd-t ,a nil))
286 `(sqr-qd-t ,a nil)))
287
288 #-(and cmu (not oct-array))
289 (define-compiler-macro sqr-qd (a &optional c)
290 (if c
291 `(sqr-qd-t ,a ,c)
292 `(sqr-qd-t ,a (%make-qd-d 0d0 0d0 0d0 0d0))))
293
294 #+(and cmu (not oct-array))
295 (define-compiler-macro add-d-qd (a b &optional c)
296 (if c
297 `(setf ,c (add-qd-d ,b ,a))
298 `(add-qd-d ,b ,a)))
299
300 #-(and cmu (not oct-array))
301 (define-compiler-macro add-d-qd (a b &optional c)
302 (if c
303 `(add-qd-d ,b ,a ,c)
304 `(add-qd-d ,b ,a (%make-qd-d 0d0 0d0 0d0 0d0))))
305
306 #+(and cmu (not oct-array))
307 (define-compiler-macro sub-qd-d (a b &optional c)
308 (if c
309 `(setf ,c (add-qd-d ,a (cl:- ,b)))
310 `(add-qd-d ,a (cl:- ,b))))
311
312 #-(and cmu (not oct-array))
313 (define-compiler-macro sub-qd-d (a b &optional c)
314 (if c
315 `(add-qd-d ,a (cl:- ,b) ,c)
316 `(add-qd-d ,a (cl:- ,b) (%make-qd-d 0d0 0d0 0d0 0d0))))
317
318 #+(and cmu (not oct-array))
319 (define-compiler-macro sub-d-qd (a b &optional c)
320 (if c
321 `(setf ,c (add-d-qd ,a (neg-qd ,b)))
322 `(add-d-qd ,a (neg-qd ,b))))
323
324 #-(and cmu (not oct-array))
325 (define-compiler-macro sub-d-qd (a b &optional c)
326 (if c
327 `(add-d-qd ,a (neg-qd ,b) ,c)
328 `(add-d-qd ,a (neg-qd ,b) (%make-qd-d 0d0 0d0 0d0 0d0))))
329
330 #+(and cmu (not oct-array))
331 (define-compiler-macro neg-qd (a &optional c)
332 (if c
333 `(setf ,c (neg-qd-t ,a nil))
334 `(neg-qd-t ,a nil)))
335
336 #-(and cmu (not oct-array))
337 (define-compiler-macro neg-qd (a &optional c)
338 (if c
339 `(neg-qd-t ,a ,c)
340 `(neg-qd-t ,a (%make-qd-d 0d0 0d0 0d0 0d0))))
341

  ViewVC Help
Powered by ViewVC 1.1.5