Debian Codegraph @

src/lsp/numlib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; number routines
12
13 (in-package "SYSTEM")
14
15 #-ecl-min
16 (ffi:clines "#include <math.h>")
17 #+(and (not ecl-min) complex-float)
18 (ffi:clines "#include <complex.h>")
19
20 #.
21 (flet ((binary-search (f min max)
22 (do ((new (/ (+ min max) 2) (/ (+ min max) 2)))
23 ((>= min max)
24 max)
25 (if (funcall f new)
26 (if (= new max)
27 (return max)
28 (setq max new))
29 (if (= new min)
30 (return max)
31 (setq min new)))))
32 (epsilon+ (x)
33 (/= (float 1 x) (+ (float 1 x) x)))
34 (epsilon- (x)
35 (/= (float 1 x) (- (float 1 x) x))))
36 #+ecl-min
37 (si::trap-fpe 'last nil)
38 `(eval-when (compile load eval)
39 (defconstant short-float-epsilon
40 ,(binary-search #'epsilon+ (coerce 0 'short-float) (coerce 1 'short-float))
41 "The smallest postive short-float E that satisfies
42 (not (= (float 1 E) (+ (float 1 E) E)))")
43 (defconstant single-float-epsilon
44 ,(binary-search #'epsilon+ (coerce 0 'single-float) (coerce 1 'single-float))
45 "The smallest postive single-float E that satisfies
46 (not (= (float 1 E) (+ (float 1 E) E)))")
47 (defconstant double-float-epsilon
48 ,(binary-search #'epsilon+ (coerce 0 'double-float) (coerce 1 'double-float))
49 "The smallest postive double-float E that satisfies
50 (not (= (float 1 E) (+ (float 1 E) E)))")
51 (defconstant long-float-epsilon
52 ,(binary-search #'epsilon+ (coerce 0 'long-float) (coerce 1 'long-float))
53 "The smallest postive long-float E that satisfies
54 (not (= (float 1 E) (+ (float 1 E) E)))")
55 (defconstant short-float-negative-epsilon
56 ,(binary-search #'epsilon- (coerce 0 'short-float) (coerce 1 'short-float))
57 "The smallest positive short-float E that satisfies
58 (not (= (float 1 E) (- (float 1 E) E)))")
59 (defconstant single-float-negative-epsilon
60 ,(binary-search #'epsilon- (coerce 0 'single-float) (coerce 1 'single-float))
61 "The smallest positive single-float E that satisfies
62 (not (= (float 1 E) (- (float 1 E) E)))")
63 (defconstant double-float-negative-epsilon
64 ,(binary-search #'epsilon- (coerce 0 'double-float) (coerce 1 'double-float))
65 "The smallest positive double-float E that satisfies
66 (not (= (float 1 E) (- (float 1 E) E)))")
67 (defconstant long-float-negative-epsilon
68 ,(binary-search #'epsilon- (coerce 0 'long-float) (coerce 1 'long-float))
69 "The smallest positive long-float E that satisfies
70 (not (= (float 1 E) (- (float 1 E) E)))")
71 ))
72
73 #+ieee-floating-point
74 (locally (declare (notinline -))
75 (let ((bits (si::trap-fpe 'last nil)))
76 (unwind-protect
77 (progn
78 (let ((a (/ (coerce 1 'short-float) (coerce 0.0 'short-float))))
79 (defconstant short-float-positive-infinity a)
80 (defconstant short-float-negative-infinity (- a)))
81 (let ((a (/ (coerce 1 'single-float) (coerce 0.0 'single-float))))
82 (defconstant single-float-positive-infinity a)
83 (defconstant single-float-negative-infinity (- a)))
84 (let ((a (/ (coerce 1 'double-float) (coerce 0.0 'double-float))))
85 (defconstant double-float-positive-infinity a)
86 (defconstant double-float-negative-infinity (- a)))
87 (let ((a (/ (coerce 1 'long-float) (coerce 0.0 'long-float))))
88 (defconstant long-float-positive-infinity a)
89 (defconstant long-float-negative-infinity (- a))))
90 (si::trap-fpe bits t))))
91
92 (defconstant imag-one #C(0.0 1.0))
93
94 (defun isqrt (i)
95 "Args: (integer)
96 Returns the integer square root of INTEGER."
97 (unless (and (integerp i) (>= i 0))
98 (error 'type-error :datum i :expected-type 'unsigned-byte))
99 (if (zerop i)
100 0
101 (let ((n (integer-length i)))
102 (do ((x (ash 1 (ceiling n 2)))
103 (y))
104 (nil)
105 (setq y (floor i x))
106 (when (<= x y)
107 (return x))
108 (setq x (floor (+ x y) 2))))))
109
110 (defun phase (x)
111 "Args: (number)
112 Returns the angle part (in radians) of the polar representation of NUMBER.
113 Returns zero for non-complex numbers."
114 (if (zerop x)
115 (if (eq x 0)
116 0.0
117 (float 0 (realpart x)))
118 (atan (imagpart x) (realpart x))))
119
120 (defun signum (x)
121 "Args: (number)
122 Returns a number that represents the sign of NUMBER. Returns NUMBER If it is
123 zero. Otherwise, returns the value of (/ NUMBER (ABS NUMBER))"
124 (if (complexp x)
125 (if (zerop x)
126 x
127 (cis (atan (imagpart x) (realpart x))))
128 (let ((result (cond ((> x 0) 1)
129 ((< x 0) -1)
130 (t ; x is 0 or NaN
131 x))))
132 (if (floatp x)
133 (float result x)
134 result))))
135
136 (defun cis (x)
137 "Args: (radians)
138 Returns a complex number whose realpart and imagpart are the values of (COS
139 RADIANS) and (SIN RADIANS) respectively."
140 (declare (ext:check-arguments-type))
141 (exp (* imag-one x)))
142
143 #-ecl-min
144 (eval-when (:compile-toplevel)
145 (defmacro c-num-op (name arg restriction &body gencomplex)
146 `(progn
147 (when (rationalp ,arg)
148 (setf ,arg (float ,arg)))
149 (typecase ,arg
150 (single-float
151 (if (or ,restriction #+ieee-floating-point (ext:float-nan-p ,arg))
152 (ffi::c-inline (,arg) (:float) :float
153 ,(format nil "~af(#0)" name)
154 :one-liner t)
155 #+complex-float
156 (ffi::c-inline (,arg) (:float) :csfloat
157 ,(format nil "c~af(#0 + I*0.0f)" name)
158 :one-liner t)
159 #-complex-float
160 (progn ,@gencomplex)))
161 (double-float
162 (if (or ,restriction #+ieee-floating-point (ext:float-nan-p ,arg))
163 (ffi::c-inline (,arg) (:double) :double
164 ,(format nil "~a(#0)" name)
165 :one-liner t)
166 #+complex-float
167 (ffi::c-inline (,arg) (:double) :cdfloat
168 ,(format nil "c~a(#0 + I*0.0)" name)
169 :one-liner t)
170 #-complex-float
171 (progn ,@gencomplex)))
172 (long-float
173 (if (or ,restriction #+ieee-floating-point (ext:float-nan-p ,arg))
174 (ffi::c-inline (,arg) (:long-double) :long-double
175 ,(format nil "~al(#0)" name)
176 :one-liner t)
177 #+complex-float
178 (ffi::c-inline (,arg) (:long-double) :clfloat
179 ,(format nil "c~al(#0 + I*0.0l)" name)
180 :one-liner t)
181 #-complex-float
182 (progn ,@gencomplex)))
183 #+complex-float
184 ((complex single-float)
185 (ffi::c-inline (,arg) (:csfloat) :csfloat
186 ,(format nil "c~af(#0)" name)
187 :one-liner t))
188 #+complex-float
189 ((complex double-float)
190 (ffi::c-inline (,arg) (:cdfloat) :cdfloat
191 ,(format nil "c~a(#0)" name)
192 :one-liner t))
193 #+complex-float
194 ((complex long-float)
195 (ffi::c-inline (,arg) (:clfloat) :clfloat
196 ,(format nil "c~al(#0)" name)
197 :one-liner t))
198 (complex
199 #+complex-float
200 (ffi::c-inline ((float (realpart ,arg))
201 (float (imagpart ,arg)))
202 (:float :float) :csfloat
203 ,(format nil "c~af(#0 + I*(#1))" name)
204 :one-liner t)
205 #-complex-float
206 ,@gencomplex)
207 (otherwise
208 (error 'type-error :datum ,arg :expected-type 'number))))))
209
210 ;;; Branch cuts and signed zeros
211 ;;;
212 ;;; The value of the following multi-valued complex functions along
213 ;;; their branch cuts is chosen depending on the sign of zero when
214 ;;; applicable. For example, the imaginary part of (asin (complex x y))
215 ;;; for x real and x>1 is positive for y=+0.0 and negative for
216 ;;; y=-0.0, consistent with approaching the branch cut at y=0 from
217 ;;; above and respectively below.
218 ;;; Note that this differs from the specification in the ANSI
219 ;;; standard, which gives only one value (the ANSI standard is silent
220 ;;; about signed zeros with regards to branch cuts). We take this
221 ;;; approach because it is mathematically more sensible and consistent
222 ;;; with the specification for complex numbers in the C programming
223 ;;; language. -- mg 2022-01-06
224
225 (defun asin (x)
226 "Args: (number)
227 Returns the arc sine of NUMBER."
228 #+ecl-min
229 (complex-asin x)
230 #-ecl-min
231 (c-num-op "asin" x
232 (<= -1.0 x 1.0)
233 (complex-asin x)))
234
235 ;; Ported from CMUCL
236 #+(or ecl-min (not complex-float))
237 (defun complex-asin (z)
238 (declare (number z)
239 (si::c-local))
240 (let* ((re-z (realpart z))
241 (im-z (imagpart z))
242 ;; add real and imaginary parts separately for correct signed
243 ;; zero handling around the branch cuts
244 (sqrt-1+z (sqrt (complex (+ 1 re-z) im-z)))
245 (sqrt-1-z (sqrt (complex (- 1 re-z) (- im-z)))))
246 (complex (atan (realpart z) (realpart (* sqrt-1-z sqrt-1+z)))
247 (asinh (imagpart (* (conjugate sqrt-1-z) sqrt-1+z))))))
248
249 (defun acos (x)
250 "Args: (number)
251 Returns the arc cosine of NUMBER."
252 #+ecl-min
253 (complex-acos x)
254 #-ecl-min
255 (c-num-op "acos" x
256 (<= -1.0 x 1.0)
257 (complex-acos x)))
258
259 ;; Ported from CMUCL
260 #+(or ecl-min (not complex-float))
261 (defun complex-acos (z)
262 (declare (number z)
263 (si::c-local))
264 (let* ((re-z (realpart z))
265 (im-z (imagpart z))
266 ;; add real and imaginary parts separately for correct signed
267 ;; zero handling around the branch cuts
268 (sqrt-1+z (sqrt (complex (+ 1 re-z) im-z)))
269 (sqrt-1-z (sqrt (complex (- 1 re-z) (- im-z)))))
270 (complex (* 2 (atan (realpart sqrt-1-z) (realpart sqrt-1+z)))
271 (asinh (imagpart (* (conjugate sqrt-1+z) sqrt-1-z))))))
272
273 (defun asinh (x)
274 "Args: (number)
275 Returns the hyperbolic arc sine of NUMBER."
276 ;; (log (+ x (sqrt (+ 1.0 (* x x)))))
277 #+ecl-min
278 (complex-asinh x)
279 #-ecl-min
280 (c-num-op "asinh" x
281 t
282 (complex-asinh x)))
283
284 ;; Ported from CMUCL
285 #+(or ecl-min (not complex-float))
286 (defun complex-asinh (z)
287 (declare (number z) (si::c-local))
288 (let* ((iz (complex (- (imagpart z)) (realpart z)))
289 (result (complex-asin iz)))
290 (complex (imagpart result)
291 (- (realpart result)))))
292
293 (defun acosh (x)
294 "Args: (number)
295 Returns the hyperbolic arc cosine of NUMBER."
296 ;; (log (+ x (sqrt (* (1- x) (1+ x)))))
297 #+ecl-min
298 (complex-acosh x)
299 #-ecl-min
300 (c-num-op "acosh" x
301 (<= 1.0 x)
302 (complex-acosh x)))
303
304 ;; Ported from CMUCL
305 #+(or ecl-min (not complex-float))
306 (defun complex-acosh (z)
307 (declare (number z) (si::c-local))
308 (let* ((re-z (realpart z))
309 (im-z (imagpart z))
310 ;; add real and imaginary parts separately for correct signed
311 ;; zero handling around the branch cuts
312 (sqrt-z+1 (sqrt (complex (+ re-z 1) im-z)))
313 (sqrt-z-1 (sqrt (complex (- re-z 1) im-z))))
314 (complex (asinh (realpart (* (conjugate sqrt-z-1)
315 sqrt-z+1)))
316 (* 2 (atan (imagpart sqrt-z-1) (realpart sqrt-z+1))))))
317
318 (defun atanh (x)
319 "Args: (number)
320 Returns the hyperbolic arc tangent of NUMBER."
321 #+ecl-min
322 (complex-atanh x)
323 #-ecl-min
324 (c-num-op "atanh" x
325 (<= -1.0 x 1.0)
326 (complex-atanh x)))
327
328 ;; Ported from CMUCL
329 #+(or ecl-min (not complex-float))
330 (defun complex-atanh (z)
331 (declare (number z) (si::c-local))
332 (let* ((re-z (realpart z))
333 (im-z (imagpart z))
334 ;; add real and imaginary parts separately for correct signed
335 ;; zero handling around the branch cuts
336 (log-1+z (log (complex (+ 1 re-z) im-z)))
337 (log-1-z (log (complex (- 1 re-z) (- im-z)))))
338 (/ (- log-1+z log-1-z) 2)))
339
340 (defun ffloor (x &optional (y 1.0f0))
341 "Args: (number &optional (divisor 1))
342 Same as FLOOR, but returns a float as the first value."
343 (multiple-value-bind (i r) (floor x y)
344 (values (if (floatp r) (float i r) (float i)) r)))
345
346 (defun fceiling (x &optional (y 1.0f0))
347 "Args: (number &optional (divisor 1))
348 Same as CEILING, but returns a float as the first value."
349 (multiple-value-bind (i r) (ceiling x y)
350 (values (if (floatp r) (float i r) (float i)) r)))
351
352 (defun ftruncate (x &optional (y 1.0f0))
353 "Args: (number &optional (divisor 1))
354 Same as TRUNCATE, but returns a float as the first value."
355 (multiple-value-bind (i r) (truncate x y)
356 (values (if (floatp r) (float i r) (float i)) r)))
357
358 (defun fround (x &optional (y 1.0f0))
359 "Args: (number &optional (divisor 1))
360 Same as ROUND, but returns a float as the first value."
361 (multiple-value-bind (i r) (round x y)
362 (values (if (floatp r) (float i r) (float i)) r)))
363
364 (defun logtest (x y)
365 "Args: (integer1 integer2)
366 Equivalent to (NOT (ZEROP (LOGAND INTEGER1 INTEGER2)))."
367 (not (zerop (logand x y))))
368
369
370 (defun byte (size position)
371 "Args: (size position)
372 Returns a byte specifier of integers. The value specifies the SIZE-bits byte
373 starting the least-significant-bit but POSITION bits of integers. In ECL, a
374 byte specifier is represented by a dotted pair (SIZE . POSITION)."
375 (cons size position))
376
377 (defun byte-size (bytespec)
378 "Args: (byte)
379 Returns the size part (in ECL, the car part) of the byte specifier BYTE."
380 (car bytespec))
381
382 (defun byte-position (bytespec)
383 "Args: (byte)
384 Returns the position part (in ECL, the cdr part) of the byte specifier BYTE."
385 (cdr bytespec))
386
387 (defun ldb (bytespec integer)
388 "Args: (bytespec integer)
389 Extracts a byte from INTEGER at the specified byte position, right-justifies
390 the byte, and returns the result as an integer."
391 (logand (ash integer (- (byte-position bytespec)))
392 (lognot (ash -1 (byte-size bytespec)))))
393
394 (defun ldb-test (bytespec integer)
395 "Args: (bytespec integer)
396 Returns T if at least one bit of the specified byte is 1; NIL otherwise."
397 (not (zerop (mask-field bytespec integer))))
398
399 (defun mask-field (bytespec integer)
400 "Args: (bytespec integer)
401 Extracts the specified byte from INTEGER and returns the result as an integer."
402 (logand (ash (lognot (ash -1 (byte-size bytespec)))
403 (byte-position bytespec))
404 integer))
405
406 (defun dpb (newbyte bytespec integer)
407 "Args: (newbyte bytespec integer)
408 Replaces the specified byte of INTEGER with NEWBYTE (an integer) and returns
409 the result."
410 (let* ((pos (byte-position bytespec))
411 (size (byte-size bytespec))
412 (mask (ash (lognot (ash -1 size)) pos)))
413 (logior (logandc2 integer mask)
414 (logand (ash newbyte pos) mask))))
415
416 (defun deposit-field (newbyte bytespec integer)
417 "Args: (integer1 bytespec integer2)
418 Returns an integer represented by the bit sequence obtained by replacing the
419 specified bits of INTEGER2 with the specified bits of INTEGER1."
420 (let* ((pos (byte-position bytespec))
421 (size (byte-size bytespec))
422 (mask (ash (lognot (ash -1 size)) pos)))
423 (logior (logandc2 integer mask)
424 (logand newbyte mask))))
425
426 (defun single-float-bits (num)
427 (ffi:c-inline (num) (:float) :uint32-t "ecl_float_bits(#0)" :one-liner t))
428
429 (defun bits-single-float (num)
430 (ffi:c-inline (num) (:uint32-t) :float "ecl_bits_float(#0)" :one-liner t))
431
432 (defun double-float-bits (num)
433 (ffi:c-inline (num) (:double) :uint64-t "ecl_double_bits(#0)" :one-liner t))
434
435 (defun bits-double-float (num)
436 (ffi:c-inline (num) (:uint64-t) :double "ecl_bits_double(#0)" :one-liner t))
437
438 ;;; XXX long double may have 64, 80, 96 or 128 bits (possibly more). The layout
439 ;;; in the memory is also an unknown, so we punt here. -- jd 2022-07-07
440
441
442 (defun long-float-bits (num)
443 #+long-float (declare (ignore num))
444 #+long-float (error "Operation not supported.")
445 #-long-float (double-float-bits num))
446
447 (defun bits-long-float (num)
448 #+long-float (declare (ignore num))
449 #+long-float (error "Operation not supported.")
450 #-long-float (bits-double-float num))