-
Notifications
You must be signed in to change notification settings - Fork 7
/
numbers.lisp
266 lines (228 loc) · 9.5 KB
/
numbers.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
;;; -*- Syntax: Common-Lisp; Base: 10 -*-
;;;
;;; Copyright (c) 2024 Gary Palter
;;;
;;; Licensed under the MIT License;
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; https://opensource.org/license/mit
(in-package #:forth)
(defconstant +true+ -1)
(defconstant +false+ 0)
(declaim (inline truep))
(defun truep (x) (not (zerop x)))
(declaim (inline falsep))
(defun falsep (x) (zerop x))
(defconstant +most-positive-single-cell+ (1- (dpb 1 (byte 1 63) 0)))
(defconstant +most-negative-single-cell+ (- (dpb 1 (byte 1 63) 0)))
(defconstant +maximum-unsigned-single-cell+ (1- (dpb 1 (byte 1 64) 0)))
(defconstant +most-positive-double-cell+ (1- (dpb 1 (byte 1 127) 0)))
(defconstant +most-negative-double-cell+ (- (dpb 1 (byte 1 127) 0)))
(defconstant +maximum-unsigned-double-cell+ (1- (dpb 1 (byte 1 128) 0)))
(defun interpret-number (token base &key (allow-floats? t) (signal-overflow? t))
(flet ((interpret-base-prefix ()
(let ((ch (aref token 0)))
(cond ((eql ch #\#) (values 10 1))
((eql ch #\$) (values 16 1))
((eql ch #\%) (values 2 1))
(t (values base 0))))))
(cond ((and allow-floats?
(= base 10)
(let ((ch (aref token 0)))
(or (digit-char-p ch)
(eql ch #\+)
(eql ch #\-)))
(= (count #\E token :test #'char-equal) 1))
;; When BASE is 10 and the string contains exactly one "E", try floating point
(let* ((token (if (char-equal (aref token (1- (length token))) #\E)
(concatenate 'string token "0")
token))
(float (with-standard-io-syntax
(let ((*read-default-float-format* 'double-float)
(*read-eval* nil))
(read-from-string token)))))
(if (floatp float)
(values :float float)
(values nil nil))))
;; 'c' is interpreted as a character literal
((and (= (length token) 3)
(eql (aref token 0) #\')
(eql (aref token 2) #\'))
(values :single (forth-char (aref token 1))))
((and (= (count #\. token :test #'eql) 1)
(eql (aref token (1- (length token))) #\.))
;; If the string end with a period, try a double precision integer
(handler-case
(multiple-value-bind (base start)
(interpret-base-prefix)
(let ((value (parse-integer token :radix base :start start :end (1- (length token)))))
(cond ((<= +most-negative-double-cell+ value +most-positive-double-cell+)
(values :double value))
(signal-overflow?
(forth-exception :out-of-range "Value too large for a double integer"))
(t
(values nil nil)))))
(parse-error ()
(values nil nil))))
(t
;; Otherwise, try a single precision integer
(handler-case
(multiple-value-bind (base start)
(interpret-base-prefix)
(let ((value (parse-integer token :radix base :start start)))
(cond ((<= +most-negative-single-cell+ value +most-positive-single-cell+)
(values :single value))
(signal-overflow?
(forth-exception :out-of-range "Value too large for an integer"))
(t
(values nil nil)))))
(parse-error ()
(values nil nil)))))))
(declaim (inline cell-signed))
(defun cell-signed (cell)
(cond ((fixnump cell) cell)
((<= +most-negative-single-cell+ cell +most-positive-single-cell+)
cell)
(t
(let ((raw (ldb (byte 64 0) cell)))
(if (zerop (ldb (byte 1 63) raw))
raw
(- raw (dpb 1 (byte 1 64) 0)))))))
(define-compiler-macro cell-signed (&whole form &environment env cell)
(if (constantp cell env)
(cell-signed cell)
form))
(declaim (inline cell-unsigned))
(defun cell-unsigned (cell)
(ldb (byte 64 0) cell))
(define-compiler-macro cell-unsigned (&whole form &environment env cell)
(if (constantp cell env)
(cell-unsigned cell)
form))
(declaim (inline double-components))
(defun double-components (double)
(values (ldb (byte 64 0) double) (ldb (byte 64 64) double)))
(declaim (inline double-cell-signed))
(defun double-cell-signed (low-cell high-cell)
(let ((value (dpb high-cell (byte 64 64) (ldb (byte 64 0) low-cell))))
(if (<= +most-negative-double-cell+ value +most-positive-double-cell+)
value
(let ((raw (ldb (byte 128 0) value)))
(if (zerop (ldb (byte 1 127) raw))
raw
(- raw (dpb 1 (byte 1 128) 0)))))))
(define-compiler-macro double-cell-signed (&whole form &environment env low-cell high-cell)
(if (and (constantp low-cell env) (constantp high-cell env))
(double-cell-signed low-cell high-cell)
form))
(declaim (inline double-cell-unsigned))
(defun double-cell-unsigned (low-cell high-cell)
(dpb high-cell (byte 64 64) (ldb (byte 64 0) low-cell)))
(define-compiler-macro double-cell-unsigned (&whole form &environment env low-cell high-cell)
(if (and (constantp low-cell env) (constantp high-cell env))
(double-cell-unsigned low-cell high-cell)
form))
(declaim (inline quad-byte-signed))
(defun quad-byte-signed (value)
(let ((raw (ldb (byte 32 0) value)))
(if (zerop (ldb (byte 1 31) raw))
raw
(- raw (dpb 1 (byte 1 32) 0)))))
(define-compiler-macro quad-byte-signed (&whole form &environment env value)
(if (constantp value env)
(quad-byte-signed value)
form))
(declaim (inline quad-byte-unsigned))
(defun quad-byte-unsigned (value)
(ldb (byte 32 0) value))
(define-compiler-macro quad-byte-unsigned (&whole form &environment env value)
(if (constantp value env)
(quad-byte-unsigned value)
form))
(declaim (inline double-byte-signed))
(defun double-byte-signed (value)
(let ((raw (ldb (byte 16 0) value)))
(if (zerop (ldb (byte 1 15) raw))
raw
(- raw (dpb 1 (byte 1 16) 0)))))
(define-compiler-macro double-byte-signed (&whole form &environment env value)
(if (constantp value env)
(double-byte-signed value)
form))
(declaim (inline double-byte-unsigned))
(defun double-byte-unsigned (value)
(ldb (byte 16 0) value))
(define-compiler-macro double-byte-unsigned (&whole form &environment env value)
(if (constantp value env)
(double-byte-unsigned value)
form))
;;; Floating Point
(declaim (inline >single-float))
(defun >single-float (x)
(float x 1.0d0))
(defun decode-single-float (f)
(multiple-value-bind (significand exponent sign)
(integer-decode-float f)
(let ((exponent (+ exponent 127 23)))
(when (and (= exponent 1) (zerop (ldb (byte 1 23) significand)))
;; Zero or a subnormal number
(setf exponent 0))
(dpb (if (minusp sign) 1 0) (byte 1 31) (dpb exponent (byte 8 23) (ldb (byte 23 0) significand))))))
(defun encode-single-float (n)
(let ((sign (ldb (byte 1 31) n))
(exponent (ldb (byte 8 23) n))
(significand (ldb (byte 23 0) n)))
(cond ((= exponent 255)
;; Encoding of either a NaN or an infinity
(forth-exception :floating-out-of-range))
((zerop exponent)
;; Encoding of zero or a subnormal number
(setf exponent 1))
(t
(setf significand (dpb 1 (byte 1 23) significand))))
(let ((absolute (scale-float (float significand 1.0e0) (- exponent 127 23))))
(if (zerop sign)
absolute
(- absolute)))))
(declaim (inline >double-float))
(defun >double-float (x)
(float x 1.0d0))
(defun decode-double-float (f)
(multiple-value-bind (significand exponent sign)
(integer-decode-float f)
(let ((exponent (+ exponent 1023 52)))
(when (and (= exponent 1) (zerop (ldb (byte 1 52) significand)))
;; Zero or a subnormal number
(setf exponent 0))
(dpb (if (minusp sign) 1 0) (byte 1 63) (dpb exponent (byte 11 52) (ldb (byte 52 0) significand))))))
(defun encode-double-float (n)
(let ((sign (ldb (byte 1 63) n))
(exponent (ldb (byte 11 52) n))
(significand (ldb (byte 52 0) n)))
(cond ((= exponent 2047)
;; Encoding of either a NaN or an infinity
(forth-exception :floating-out-of-range))
((zerop exponent)
;; Encoding of zero or a subnormal number
(setf exponent 1))
(t
(setf significand (dpb 1 (byte 1 52) significand))))
(let ((absolute (scale-float (float significand 1.0d0) (- exponent 1023 52))))
(if (zerop sign)
absolute
(- absolute)))))
;;; CL-Forth uses double precision floating point as its internal representation of float values
(defconstant +most-positive-native-float+ most-positive-double-float)
(declaim (inline native-float))
(defun native-float (x)
(float x 1.0d0))
(declaim (inline decode-native-float))
(defun decode-native-float (f)
(decode-double-float f))
(declaim (inline encode-native-float))
(defun encode-native-float (n)
(encode-double-float n))
(defmacro with-native-float-format (() &body body)
`(let ((*read-default-float-format* 'double-float))
,@body))