/
declared-numerics.lisp
1125 lines (1005 loc) · 41.6 KB
/
declared-numerics.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; -*- Mode:Common-Lisp; Package:GBBOPEN-TOOLS; Syntax:common-lisp -*-
;;;; *-* File: /usr/local/gbbopen/source/tools/declared-numerics.lisp *-*
;;;; *-* Edited-By: cork *-*
;;;; *-* Last-Edit: Sun Mar 31 12:57:35 2013 *-*
;;;; *-* Machine: phoenix.corkills.org *-*
;;;; **************************************************************************
;;;; **************************************************************************
;;;; *
;;;; * Shorthand Declared Numeric Operations and Values
;;;; *
;;;; **************************************************************************
;;;; **************************************************************************
;;;
;;; Written by: Dan Corkill
;;;
;;; Copyright (C) 2002-2012, Dan Corkill <corkill@GBBopen.org>
;;; Part of the GBBopen Project.
;;; Licensed under Apache License 2.0 (see LICENSE for license information).
;;;
;;; Porting Notice:
;;;
;;; The implementation-specific numeric type predicates fixnump,
;;; single-float-p, and double-float-p need to be imported or defined.
;;;
;;; Infinity and -infinity values are implementation dependent and must be
;;; defined for each new port. These are generally IEEE 754 number-format
;;; values for infinity (INF) and negative infinity (-INF). Many
;;; implementations use read-time evaluation (#.) notation to represent these
;;; (a non-evaluating, standard reader syntax is sorely needed!). Lispworks
;;; uses an extended number syntax with a doubled exponent sign (with the
;;; second sign positive) to encode infinite values:
;;; 1e++0 positive infinity
;;; -1e++0 negative infinity
;;; Lispworks also uses a similar extension (but with a negative second
;;; exponent sign) to encode not-a-numbers (NaNs):
;;; 1e+-0 NaN
;;; The sign of the first exponent sign (+ or -) doesn't matter in
;;; determining the value (so 1e-+0 is also infinity and 1d--0 is also NaN).
;;; The type of the value can be encoded just as with any other float
;;; (1s++0, 1d++0, etc.).
;;;
;;; We proposed wider adoption of the Lispworks encoding scheme in other
;;; Common Lisp implementations and provided patches for Clozure CL, CMUCL,
;;; and SBCL to use the Lispworks representation. Our proposal met with
;;; mixed opinions, and Nikodemus Siivola suggested an alternative approach
;;; using PRINT-OBJECT and a #@ dispatch macro. This is the approach that is
;;; currently being used in GBBopen, but it has three issues:
;;; 1. Printing infinite values using #@ does not work in Lispworks, as we
;;; are unable to use PRINT-OBJECT.
;;; 2. Digitool MCL has an existing #@ dispatch that we have to work around
;;; 3. Other packages might also want to use the #@ dispatch for other
;;; purposes
;;; Until CL implementations "standardize" on a portable, non-read-eval-based,
;;; mechanism for infinite values, this is our best attempt.
;;;
;;; Most Common Lisp implementations map double-float numbers to the the
;;; 64-bit IEEE 754 double format and single-float numbers to the 32-bit IEEE
;;; 754 single format.
;;;
;;; Digitool's Macintosh Common Lisp (MCL) maps both the double-float and
;;; single-float types to the 64-bit IEEE 754 double format (*only* the
;;; short-float type maps to the IEEE 754 single format).
;;;
;;; Prior to their 5.0 release, Lispworks supported only the IEEE 754 double
;;; format, so double-float and single-float type declarations were
;;; equivalent.
;;;
;;; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;;;
;;; 07-04-02 File created. (Corkill)
;;; 02-20-04 Added full-safety escape feature. (Corkill)
;;; 03-13-04 Added infinity values. (Corkill)
;;; 04-13-04 Added SBCL & CMUCL infinity values. (Corkill)
;;; 05-21-04 Export FIXNUMP, SINGLE-FLOAT-P, and DOUBLE-FLOAT-P. (Corkill)
;;; 01-11-05 Added fixnum infinity values. (Corkill)
;;; 06-08-05 Added CLISP support. (sds)
;;; 08-23-05 Added COERCE&, COERCE$&, COERCE$, COERCE$$, and COERCE$$. (Corkill)
;;; 10-31-05 Add non-*read-eval*, *print-readably* printing for infinity and
;;; NaN values for SBCL, Clozure CL, & CMUCL. (Corkill)
;;; 11-02-05 Added CormanLisp support (faking infinity for now). (Corkill)
;;; 11-27-05 Changed infinity reading/printing to #@ dispatching macro (as
;;; suggested by Nikodemus Siivola); no special NaN I/O. (Corkill)
;;; 01-18-06 Added declared-numeric ABS functions. (Corkill)
;;; 02-13-06 Added GCL support. (Corkill)
;;; 03-24-06 Added infinity-not-available feature. (Corkill)
;;; 05-08-06 Added support for the Scieneer CL. (dtc)
;;; 11-15-06 Added short-float support. (Corkill)
;;; 06-29-08 Changed most operators from macros to functions with
;;; compiler-macros. (Corkill)
;;; 08-03-09 Added declared-numeric COMPARE functions. (Corkill)
;;; 11-03-12 Added REM&, REM$&, REM$, REM$$, REM$$$. (Corkill)
;;;
;;; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
(in-package :gbbopen-tools)
(eval-when (:compile-toplevel :load-toplevel :execute)
(import
#+abcl
'(ext:fixnump)
#+allegro
'(excl:fixnump excl:single-float-p excl:double-float-p)
#+clisp
'(sys::fixnump sys::short-float-p sys::single-float-p sys::double-float-p
sys::long-float-p)
#+clozure
'(ccl:fixnump ccl::short-float-p ccl::double-float-p)
#+cmu
'(extensions:fixnump lisp::short-float-p kernel:single-float-p
kernel:double-float-p kernel:long-float-p)
#+cormanlisp
'(lisp::fixnump lisp::short-float-p lisp::single-float-p
lisp::double-float-p lisp::long-float-p)
#+digitool-mcl
'(ccl:fixnump ccl::double-float-p)
#+ecl
'(si:fixnump)
#+gcl
'(system:fixnump)
#+lispworks
'(lispworks:fixnump lispworks:short-float-p hcl:single-float-p
hcl:double-float-p lispworks:long-float-p)
#+sbcl
'(sb-int:fixnump sb-int:short-float-p sb-int:single-float-p
sb-int:double-float-p sb-int:long-float-p)
#+scl
'(ext:fixnump lisp::short-float-p kernel:single-float-p
kernel:double-float-p kernel:long-float-p)
#-(or abcl allegro clisp clozure cmu cormanlisp digitool-mcl ecl gcl
lispworks sbcl scl)
(need-to-port (fixnump short-float-p single-float-p double-float-p
long-float-p))))
;;; CLs that don't have short-float-p predicates:
#+(or abcl allegro ecl gcl)
(defun short-float-p (obj)
(typep obj 'short-float))
#+(or abcl allegro ecl gcl)
(defcm short-float-p (obj)
`(typep ,obj 'short-float))
;;; CLs that don't have single-float-p predicates:
#+(or abcl clozure digitool-mcl ecl gcl)
(defun single-float-p (obj)
(typep obj 'single-float))
#+(or abcl clozure digitool-mcl ecl gcl)
(defcm single-float-p (obj)
`(typep ,obj 'single-float))
;;; CLs that don't have double-float-p predicates:
#+(or abcl ecl gcl)
(defun double-float-p (obj)
(typep obj 'double-float))
#+(or abcl ecl gcl)
(defcm double-float-p (obj)
`(typep ,obj 'double-float))
;;; CLs that don't have long-float-p predicates:
#+(or abcl allegro clozure ecl gcl)
(defun long-float-p (obj)
(typep obj 'long-float))
#+(or abcl allegro clozure ecl gcl)
(defcm long-float-p (obj)
`(typep ,obj 'long-float))
;;; ---------------------------------------------------------------------------
(eval-when (:compile-toplevel :load-toplevel :execute)
(export '(;; Numeric types
fixnump short-float-p single-float-p double-float-p long-float-p
;; Coercion:
coerce& coerce$& coerce$ coerce$$ coerce$$$
;; Declared fixnum ops:
& /& *& +& -& 1+& 1-&
/=& <& <=& =& >& >=&
bounded-value& ceiling& compare& decf& decf&-after evenp&
floor& fceiling& ffloor& fround& ftruncate&
incf& incf&-after max& min& minusp&
abs& mod& oddp& plusp& rem& round& truncate& zerop&
;; Declared short-float ops:
$& /$& *$& +$& -$& 1+$& 1-$&
/=$& <$& <=$& =$& >$& >=$&
bounded-value$& ceiling$& compare$& decf$& decf$&-after evenp$&
floor$& fceiling$& ffloor$& fround$& ftruncate$&
incf$& incf$&-after max$& min$& minusp$&
abs$& mod$& oddp$& plusp$& rem$& round$& truncate$& zerop$&
;; Declared single-float ops:
$ /$ *$ +$ -$ 1+$ 1-$
/=$ <$ <=$ =$ >$ >=$
bounded-value$ ceiling$ compare$ decf$ decf$-after evenp$
floor$ fceiling$ ffloor$ fround$ ftruncate$
incf$ incf$-after max$ min$ minusp$
abs$ mod$ oddp$ plusp$ rem$ round$ truncate$ zerop$
;; Declared double-float ops:
$$ /$$ *$$ +$$ -$$ 1+$$ 1-$$
/=$$ <$$ <=$$ =$$ >$$ >=$$
bounded-value$$ ceiling$$ compare$$ decf$$ decf$$-after evenp$$
floor$$ fceiling$$ ffloor$$ fround$$ ftruncate$$
incf$$ incf$$-after max$$ min$$ minusp$$
abs$$ mod$$ oddp$$ plusp$$ rem$$ round$$ truncate$$ zerop$$
;; Declared long-float ops:
$$$ /$$$ *$$$ +$$$ -$$$ 1+$$$ 1-$$$
/=$$$ <$$$ <=$$$ =$$$ >$$$ >=$$$
bounded-value$$$ ceiling$$$ compare$$$ decf$$$ decf$$$-after evenp$$$
floor$$$ fceiling$$$ ffloor$$$ fround$$$ ftruncate$$$
incf$$$ incf$$$-after max$$$ min$$$ minusp$$$
abs$$$ mod$$$ oddp$$$ plusp$$$ rem$$$ round$$$ truncate$$$ zerop$$$
;; Infinite values:
infinity -infinity infinity& -infinity&
infinity$ -infinity$ infinity$& -infinity$&
infinity$$ -infinity$$ infinity$$$ -infinity$$$
;; Infinity reader escape hook (undocumented):
*inf-reader-escape-hook*
;; Infinity reader macro-character checker (undocumented):
check-for-inf-reader
;; Infinity reader macro-character setting (undocumented):
set-inf-reader-dispatch-macro-character)))
;;; ---------------------------------------------------------------------------
;;; The fastest (/ fixnum fixnum) => fixnum [completely-fixnum division]
;;; operator for each CL (as determined by :cl-timing tests) -- either
;;; /&, floor&, or truncate&.
;;;
;;; Programmers must take care in using /& only where fixnum results will be
;;; created.
(defconstant fastest-fixnum-div-operator
;; When timings are very close, truncate& is preferred. Tested on x86 and
;; PPC architectures (could vary on others--reports welcomed!).
(or #+abcl 'truncate&
#+allegro 'truncate&
#+clisp 'floor&
#+clozure 'truncate&
#+cmu 'truncate&
#+digitool-mcl 'truncate&
#+ecl 'floor&
#+lispworks 'truncate&
#+sbcl 'truncate&
#+scl 'truncate&
#-(or abcl
allegro
clisp
clozure
cmu
digitool-mcl
ecl
lispworks
sbcl
scl)
(need-to-port fastest-fixnum-div-operator)))
;;; ---------------------------------------------------------------------------
;;; Warn if the CL implementation doesn't have at least 29-bit fixnums and
;;; note if the fixnum size supports unsigned-byte 32 values, pushing
;;; features accordingly:
(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((fixnum-size (1+ (integer-length most-positive-fixnum))))
(cond
((> fixnum-size 32)
(pushnew :fixnum-size-supports-unsigned-byte-32 *features*))
((< fixnum-size 29)
(pushnew :fixnum-size-below-29 *features*)))))
#+fixnum-size-below-29
(defun small-fixnum-warning ()
(let ((fixnum-size #.(1+ (integer-length most-positive-fixnum))))
(warn "Fixnums on ~a (~a) are only ~s bits long."
fixnum-size
(lisp-implementation-type)
(machine-type))))
#+fixnum-size-below-29
(small-fixnum-warning)
;;; ---------------------------------------------------------------------------
;;; Check if various floats are not implemented distinctly (also run at
;;; compile time in order to push features during compilation)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun check-for-numeric-type (declared-type feature)
(let ((actual-type (type-of (coerce 1.0l0 declared-type))))
(when (eq actual-type declared-type)
(pushnew feature *features*))))
(check-for-numeric-type 'short-float ':has-short-float)
(check-for-numeric-type 'single-float ':has-single-float)
(check-for-numeric-type 'double-float ':has-double-float)
(check-for-numeric-type 'long-float ':has-long-float))
;;; ---------------------------------------------------------------------------
;;; Warn if various floats are not implemented distinctly
(defun warn-numeric-type (declared-type feature)
(unless (member feature *features*)
(let ((actual-type (type-of (coerce 1.0l0 declared-type))))
(warn "~s is equivalent to ~s on ~a~@[ running on ~a~]."
declared-type
actual-type
(lisp-implementation-type)
(machine-type)))))
(warn-numeric-type 'short-float ':has-short-float)
(warn-numeric-type 'single-float ':has-single-float)
(warn-numeric-type 'double-float ':has-double-float)
(warn-numeric-type 'long-float ':has-long-float)
;;; ---------------------------------------------------------------------------
;;; Allows a function to be called from the #@ inf-reader function, if the
;;; object read is not one of the legal infinite-value names:
(declaim (special *inf-reader-escape-hook*))
(unless (boundp '*inf-reader-escape-hook*)
(setf *inf-reader-escape-hook* nil))
;;; ---------------------------------------------------------------------------
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun dn-defcm-expander (type op args result values-types)
;;; Builds a form declaring all the arguments to `op' to be `type.' If
;;; `result' is true then the type of the result of the operation is also
;;; declared.
(let ((form `(,op ,.(flet ((fn (x) `(the ,type ,x)))
(declare (dynamic-extent #'fn))
(mapcar #'fn args)))))
(if (and result
(not (member ':full-safety *features* :test #'eq)))
`(the ,(if values-types
(cons 'values values-types)
type)
,form)
form)))
(defmacro defdn (dn-symbol op &optional result values-types)
(let ((type (ecase dn-symbol
(& 'fixnum)
($& 'short-float)
($ 'single-float)
($$ 'double-float)
($$$ 'long-float)))
(dn-op (intern (concatenate 'simple-string
(symbol-name op)
(symbol-name dn-symbol)))))
`(progn
(defcm ,dn-op (&rest args)
(dn-defcm-expander ',type ',op args ',result ',values-types))
(defun ,dn-op (&rest args)
(declare (dynamic-extent args))
;; optimize this someday
(apply ',op args))))))
;;; ===========================================================================
;;; Fixnum Operations
(defmacro & (arg)
;;; Wraps (the fixnum ...) around `arg'
(if (feature-present-p ':full-safety)
`,arg
`(the fixnum ,arg)))
(defun unable-to-coerce-to-fixnum-error (value)
(error "Unable to coerce ~s to a fixnum" value))
(defun coerce& (arg)
;; avoid truncate call if not required:
(if (typep arg 'fixnum)
arg
;; Allow (coerce& 1.0) for symmetry with other declared-numeric coerce
;; operators, even though CL doesn't allow either (coerce 1.0 'integer)
;; or (coerce 1.0 'fixnum):
(multiple-value-bind (result remainder)
(truncate arg)
(unless (and (zerop remainder)
(typep result 'fixnum))
(unable-to-coerce-to-fixnum-error arg))
result)))
(defcm coerce& (arg)
(with-once-only-bindings (arg)
(with-gensyms (result remainder)
;; avoid truncate call if not required:
`(if (typep ,arg 'fixnum)
,arg
(multiple-value-bind (,result ,remainder)
(truncate ,arg)
(unless (and (zerop ,remainder)
(typep ,result 'fixnum))
(unable-to-coerce-to-fixnum-error ,arg))
,result)))))
(defdn & + t)
(defdn & 1+ t)
(defdn & - t)
(defdn & 1- t)
(defdn & * t)
(defdn & / t) ;; avoid using this one!!!
(defdn & =)
(defdn & /=)
(defdn & <)
(defdn & <=)
(defdn & >)
(defdn & >=)
(defdn & min t)
(defdn & max t)
(defdn & zerop)
(defdn & plusp)
(defdn & minusp)
(defdn & evenp)
(defdn & oddp)
(defdn & abs t)
(defdn & mod t)
(defdn & rem t)
(defdn & floor t (fixnum fixnum))
(defdn & ceiling t (fixnum fixnum))
(defdn & truncate t (fixnum fixnum))
(defdn & round t (fixnum fixnum))
(defdn & ffloor t (float fixnum))
(defdn & fceiling t (float fixnum))
(defdn & ftruncate t (float fixnum))
(defdn & fround t (float fixnum))
(define-modify-macro incf& (&optional (increment 1)) +&)
(define-modify-macro decf& (&optional (increment 1)) -&)
(defmacro incf&-after (place &optional (increment 1) &environment env)
;;; Like incf&, but returns the original value of `place' (the value before
;;; the incf was done)
(incf/decf-after-builder place increment env '+& 'incf&))
(defmacro decf&-after (place &optional (increment 1) &environment env)
;;; Like decf&, but returns the original value of `place' (the value before
;;; the decf was done)
(incf/decf-after-builder place increment env '-& 'decf&))
(defun bounded-value& (min n max)
(max& min (min& n max)))
(defcm bounded-value& (min n max)
`(max& ,min (min& ,n ,max)))
(defun compare& (a b)
;; Users must be careful that the result is a fixnum!
(-& a b))
;;; ===========================================================================
;;; Short-Float Operations
(defmacro $& (arg)
;;; Wraps (the short-float ...) around `arg'
(if (feature-present-p ':full-safety)
`,arg
`(the short-float ,arg)))
(defun coerce$& (arg)
;; avoid coercion if not required (some CLs will coerce anyway):
(if (typep arg 'short-float)
arg
(coerce arg 'short-float)))
(defcm coerce$& (arg)
(with-once-only-bindings (arg)
;; avoid coercion if not required (some CLs will coerce anyway):
`(if (typep ,arg 'short-float)
,arg
(coerce ,arg 'short-float))))
(defdn $& + t)
(defdn $& 1+ t)
(defdn $& - t)
(defdn $& 1- t)
(defdn $& * t)
(defdn $& / t)
(defdn $& =)
(defdn $& /=)
(defdn $& <)
(defdn $& <=)
(defdn $& >)
(defdn $& >=)
(defdn $& min t)
(defdn $& max t)
(defdn $& zerop)
(defdn $& plusp)
(defdn $& minusp)
(defdn $& evenp)
(defdn $& oddp)
(defdn $& abs t)
(defdn $& mod t)
(defdn $& rem t)
(defdn $& floor t (fixnum short-float))
(defdn $& ceiling t (fixnum short-float))
(defdn $& truncate t (fixnum short-float))
(defdn $& round t (fixnum short-float))
(defdn $& ffloor t (short-float short-float))
(defdn $& fceiling t (short-float short-float))
(defdn $& ftruncate t (short-float short-float))
(defdn $& fround t (short-float short-float))
(define-modify-macro incf$& (&optional (increment 1.0s0)) +$&)
(define-modify-macro decf$& (&optional (increment 1.0s0)) -$&)
(defmacro incf$&-after (place &optional (increment 1.0s0) &environment env)
;;; Like incf$&, but returns the original value of `place' (the value before
;;; the incf was done)
(incf/decf-after-builder place increment env '+$& 'incf$&))
(defmacro decf$&-after (place &optional (increment 1.0s0) &environment env)
;;; Like decf$&, but returns the original value of `place' (the value before
;;; the decf was done)
(incf/decf-after-builder place increment env '-$& 'decf$&))
(defun bounded-value$& (min n max)
(max$& min (min$& n max)))
(defcm bounded-value$& (min n max)
`(max$& ,min (min$& ,n ,max)))
(defun compare$& (a b)
(cond ((<$& a b) -1)
((>$& a b) 1)
(t 0)))
;;; ===========================================================================
;;; Single-Float Operations
(defmacro $ (arg)
;;; Wraps (the single-float ...) around `arg'
(if (feature-present-p ':full-safety)
`,arg
`(the single-float ,arg)))
(defun coerce$ (arg) (coerce arg 'single-float))
(defcm coerce$ (arg)
(with-once-only-bindings (arg)
;; avoid coercion if not required (some CLs will coerce anyway):
`(if (typep ,arg 'single-float)
,arg
(coerce ,arg 'single-float))))
(defdn $ + t)
(defdn $ 1+ t)
(defdn $ - t)
(defdn $ 1- t)
(defdn $ * t)
(defdn $ / t)
(defdn $ =)
(defdn $ /=)
(defdn $ <)
(defdn $ <=)
(defdn $ >)
(defdn $ >=)
(defdn $ min t)
(defdn $ max t)
(defdn $ zerop)
(defdn $ plusp)
(defdn $ minusp)
(defdn $ evenp)
(defdn $ oddp)
(defdn $ abs t)
(defdn $ mod t)
(defdn $ rem t)
(defdn $ floor t (fixnum single-float))
(defdn $ ceiling t (fixnum single-float))
(defdn $ truncate t (fixnum single-float))
(defdn $ round t (fixnum single-float))
(defdn $ ffloor t (single-float single-float))
(defdn $ fceiling t (single-float single-float))
(defdn $ ftruncate t (single-float single-float))
(defdn $ fround t (single-float single-float))
(define-modify-macro incf$ (&optional (increment 1.0f0)) +$)
(define-modify-macro decf$ (&optional (increment 1.0f0)) -$)
(defmacro incf$-after (place &optional (increment 1.0f0) &environment env)
;;; Like incf$, but returns the original value of `place' (the value before
;;; the incf was done)
(incf/decf-after-builder place increment env '+$ 'incf$))
(defmacro decf$-after (place &optional (increment 1.0f0) &environment env)
;;; Like decf$, but returns the original value of `place' (the value before
;;; the decf was done)
(incf/decf-after-builder place increment env '-$ 'decf$))
(defun bounded-value$ (min n max)
(max$ min (min$ n max)))
(defcm bounded-value$ (min n max)
`(max$ ,min (min$ ,n ,max)))
(defun compare$ (a b)
(cond ((<$ a b) -1)
((>$ a b) 1)
(t 0)))
;;; ===========================================================================
;;; Double-Float Operations
(defmacro $% (arg)
;;; Wraps (the double-float ...) around `arg'
(if (feature-present-p ':full-safety)
`,arg
`(the double-float ,arg)))
(defun coerce$$ (arg) (coerce arg 'double-float))
(defcm coerce$$ (arg)
(with-once-only-bindings (arg)
;; avoid coercion if not required (some CLs will coerce anyway):
`(if (typep ,arg 'double-float)
,arg
(coerce ,arg 'double-float))))
(defdn $$ + t)
(defdn $$ 1+ t)
(defdn $$ - t)
(defdn $$ 1- t)
(defdn $$ * t)
(defdn $$ / t)
(defdn $$ =)
(defdn $$ /=)
(defdn $$ <)
(defdn $$ <=)
(defdn $$ >)
(defdn $$ >=)
(defdn $$ min t)
(defdn $$ max t)
(defdn $$ zerop)
(defdn $$ plusp)
(defdn $$ minusp)
(defdn $$ evenp)
(defdn $$ oddp)
(defdn $$ abs t)
(defdn $$ mod t)
(defdn $$ rem t)
(defdn $$ floor t (fixnum double-float))
(defdn $$ ceiling t (fixnum double-float))
(defdn $$ truncate t (fixnum double-float))
(defdn $$ round t (fixnum double-float))
(defdn $$ ffloor t (double-float double-float))
(defdn $$ fceiling t (double-float double-float))
(defdn $$ ftruncate t (double-float double-float))
(defdn $$ fround t (double-float double-float))
(define-modify-macro incf$$ (&optional (increment 1.0d0)) +$$)
(define-modify-macro decf$$ (&optional (increment 1.0d0)) -$$)
(defmacro incf$$-after (place &optional (increment 1.0d0) &environment env)
;;; Like incf$$, but returns the original value of `place' (the value before
;;; the incf was done)
(incf/decf-after-builder place increment env '+$$ 'incf$$))
(defmacro decf$$-after (place &optional (increment 1.0d0) &environment env)
;;; Like decf$$, but returns the original value of `place' (the value before
;;; the decf was done)
(incf/decf-after-builder place increment env '-$$ 'decf$$))
(defun bounded-value$$ (min n max)
(max$$ min (min$$ n max)))
(defcm bounded-value$$ (min n max)
`(max$$ ,min (min$$ ,n ,max)))
(defun compare$$ (a b)
(cond ((<$$ a b) -1)
((>$$ a b) 1)
(t 0)))
;;; ===========================================================================
;;; Long-Float Operations
(defmacro $$$ (arg)
;;; Wraps (the long-float ...) around `arg'
(if (feature-present-p ':full-safety)
`,arg
`(the long-float ,arg)))
(defun coerce$$$ (arg) (coerce arg 'long-float))
(defcm coerce$$$ (arg)
(with-once-only-bindings (arg)
;; avoid coercion if not required (some CLs will coerce anyway):
`(if (typep ,arg 'long-float)
,arg
(coerce ,arg 'long-float))))
(defdn $$$ + t)
(defdn $$$ 1+ t)
(defdn $$$ - t)
(defdn $$$ 1- t)
(defdn $$$ * t)
(defdn $$$ / t)
(defdn $$$ =)
(defdn $$$ /=)
(defdn $$$ <)
(defdn $$$ <=)
(defdn $$$ >)
(defdn $$$ >=)
(defdn $$$ min t)
(defdn $$$ max t)
(defdn $$$ zerop)
(defdn $$$ plusp)
(defdn $$$ minusp)
(defdn $$$ evenp)
(defdn $$$ oddp)
(defdn $$$ abs t)
(defdn $$$ mod t)
(defdn $$$ rem t)
(defdn $$$ floor t (fixnum long-float))
(defdn $$$ ceiling t (fixnum long-float))
(defdn $$$ truncate t (fixnum long-float))
(defdn $$$ round t (fixnum long-float))
(defdn $$$ ffloor t (long-float long-float))
(defdn $$$ fceiling t (long-float long-float))
(defdn $$$ ftruncate t (long-float long-float))
(defdn $$$ fround t (long-float long-float))
(define-modify-macro incf$$$ (&optional (increment 1.0l0)) +$$$)
(define-modify-macro decf$$$ (&optional (increment 1.0l0)) -$$$)
(defmacro incf$$$-after (place &optional (increment 1.0l0) &environment env)
;;; Like incf$$$, but returns the original value of `place' (the value before
;;; the incf was done)
(incf/decf-after-builder place increment env '+$$$ 'incf$$$))
(defmacro decf$$$-after (place &optional (increment 1.0l0) &environment env)
;;; Like decf$$$, but returns the original value of `place' (the value before
;;; the decf was done)
(incf/decf-after-builder place increment env '-$$$ 'decf$$$))
(defun bounded-value$$$ (min n max)
(max$$$ min (min$$$ n max)))
(defcm bounded-value$$$ (min n max)
`(max$$$ ,min (min$$$ ,n ,max)))
(defun compare$$$ (a b)
(cond ((<$$$ a b) -1)
((>$$$ a b) 1)
(t 0)))
;;; ===========================================================================
;;; Infinity Values
;;;
;;; Infinity values are not required by the CL standard, but most they are
;;; provided in most CL implementations. We also want to be able to save and
;;; communicate infinity values, and using implementation-specific #.
;;; representations won't work between implementations or if *READ-EVAL*
;;; is turned off. We like the text-representation approach used by
;;; Lispworks:
;;; 1e++0 => positive infinity
;;; -1e++0 => negative infinity
;;; 1e+-0 => not-a-number
;;; but we have been unable to influence a broader adoption in other CL
;;; implementations. So, in GBBopen, we resort to a #@ dispatch macro
;;; mechanism (see below), which has its own problems. This is an area
;;; where some defacto standardization is sorely needed.
;;;
;;; Note:
;;; * CLISP, ECL (if configured without ieee-fp), and GCL do not support
;;; IEEE 754 infinity representations
(eval-when (:compile-toplevel :load-toplevel :execute)
#+(or clisp
cormanlisp
(and ecl (not ieee-floating-point))
gcl)
(pushnew ':infinity-not-available *features*))
(eval-when (:compile-toplevel :load-toplevel :execute)
;; --------------------------------------------------------------------------
;; Fixnum infinity approximations:
(defconstant infinity& most-positive-fixnum)
(defconstant -infinity& most-negative-fixnum)
;; --------------------------------------------------------------------------
;; Double-float infinities (defined first, so that we can use them in
;; defining other types, when needed)
(defconstant infinity$$
#+abcl ext:double-float-positive-infinity
#+allegro excl::*infinity-double*
#+clozure #.(unwind-protect
(progn
(ccl:set-fpu-mode :division-by-zero nil)
(/ 0d0))
(ccl:set-fpu-mode :division-by-zero t))
#+cmu ext:double-float-positive-infinity
#+digitool-mcl #.(unwind-protect
(progn
(ccl:set-fpu-mode :division-by-zero nil)
(/ 0d0))
(ccl:set-fpu-mode :division-by-zero t))
#+(and ecl (not infinity-not-available)) si:double-float-positive-infinity
#+lispworks #.(read-from-string "10E999")
#+sbcl sb-ext:double-float-positive-infinity
#+scl ext:double-float-positive-infinity
;; We have to fake infinity
#+infinity-not-available most-positive-double-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port infinity$$))
(defconstant -infinity$$
#+abcl ext:double-float-negative-infinity
#+allegro excl::*negative-infinity-double*
#+clozure #.(unwind-protect
(progn
(ccl:set-fpu-mode :division-by-zero nil)
(/ -0d0))
(ccl:set-fpu-mode :division-by-zero t))
#+cmu ext:double-float-negative-infinity
#+digitool-mcl #.(unwind-protect
(progn
(ccl:set-fpu-mode :division-by-zero nil)
(/ -0d0))
(ccl:set-fpu-mode :division-by-zero t))
#+(and ecl (not infinity-not-available)) si:double-float-negative-infinity
#+lispworks #.(read-from-string "-10E999")
#+sbcl sb-ext:double-float-negative-infinity
#+scl ext:double-float-negative-infinity
;; We have to fake negative infinity
#+infinity-not-available most-negative-double-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port -infinity$$))
;; --------------------------------------------------------------------------
;; Single-float infinities:
(defconstant infinity$
#+abcl ext:single-float-positive-infinity
#+allegro excl::*infinity-single*
#+clozure (coerce infinity$$ 'single-float)
#+cmu ext:single-float-positive-infinity
#+digitool-mcl (coerce infinity$$ 'single-float)
#+(and ecl (not infinity-not-available)) si:single-float-positive-infinity
#+lispworks (coerce infinity$$ 'single-float)
#+sbcl sb-ext:single-float-positive-infinity
#+scl ext:single-float-positive-infinity
;; We have to fake infinity
#+infinity-not-available most-positive-single-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port infinity$))
(defconstant -infinity$
#+abcl ext:single-float-negative-infinity
#+allegro excl::*negative-infinity-single*
#+clozure (coerce -infinity$$ 'single-float)
#+cmu ext:single-float-negative-infinity
#+digitool-mcl (coerce -infinity$$ 'single-float)
#+(and ecl (not infinity-not-available)) si:single-float-negative-infinity
#+lispworks (coerce -infinity$$ 'single-float)
#+sbcl sb-ext:single-float-negative-infinity
#+scl ext:single-float-negative-infinity
;; We have to fake negative infinity
#+infinity-not-available most-negative-single-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port -infinity$))
;; --------------------------------------------------------------------------
;; Short-float infinities:
(defconstant infinity$&
#+abcl infinity$
#+allegro infinity$
#+clozure (coerce infinity$$ 'short-float)
#+cmu ext:short-float-positive-infinity
#+digitool-mcl (coerce infinity$$ 'short-float)
#+(and ecl (not infinity-not-available)) si:short-float-positive-infinity
#+lispworks (coerce infinity$$ 'short-float)
#+sbcl sb-ext:short-float-positive-infinity
#+scl ext:short-float-positive-infinity
;; We have to fake infinity
#+infinity-not-available most-positive-short-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port infinity$&))
(defconstant -infinity$&
#+abcl -infinity$
#+allegro -infinity$
#+clozure (coerce -infinity$$ 'short-float)
#+cmu ext:short-float-negative-infinity
#+digitool-mcl (coerce -infinity$$ 'short-float)
#+(and ecl (not infinity-not-available)) si:short-float-negative-infinity
#+lispworks (coerce -infinity$$ 'short-float)
#+sbcl sb-ext:short-float-negative-infinity
#+scl ext:short-float-negative-infinity
;; We have to fake negative infinity
#+infinity-not-available most-negative-short-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port -infinity$&))
;; --------------------------------------------------------------------------
;; Long-float infinities:
(defconstant infinity$$$
#+abcl infinity$$
#+allegro infinity$$
#+clozure (coerce infinity$$ 'long-float)
#+cmu ext:long-float-positive-infinity
#+digitool-mcl (coerce infinity$$ 'long-float)
#+(and ecl (not infinity-not-available)) si:long-float-positive-infinity
#+lispworks (coerce infinity$$ 'long-float)
#+sbcl sb-ext:long-float-positive-infinity
#+scl ext:long-float-positive-infinity
;; We have to fake infinity
#+infinity-not-available most-positive-long-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port infinity$$$))
(defconstant -infinity$$$
#+abcl -infinity$$
#+allegro -infinity$$
#+clozure (coerce -infinity$$ 'long-float)
#+cmu ext:long-float-negative-infinity
#+digitool-mcl (coerce -infinity$$ 'long-float)
#+(and ecl (not infinity-not-available)) si:long-float-negative-infinity
#+lispworks (coerce -infinity$$ 'long-float)
#+sbcl sb-ext:long-float-negative-infinity
#+scl ext:long-float-negative-infinity
;; We have to fake negative infinity
#+infinity-not-available most-positive-long-float
#-(or abcl allegro clozure cmu digitool-mcl
(and ecl (not infinity-not-available))
lispworks sbcl scl infinity-not-available)
(need-to-port -infinity$$$))
;; --------------------------------------------------------------------------
;; Generic infinities:
(defconstant infinity infinity$)
(defconstant -infinity -infinity$))
;;; ---------------------------------------------------------------------------
;;; INF input & output
;;;
;;; Until CL implementations "standardize" on a portable,
;;; non-read-eval-based, mechanism for infinite values, we roll our own
;;; using PRINT-OBJECT and a #@ dispatch macro. (Thanks to Nikodemus
;;; Siivola for suggesting this approach.)
;;;
;;; The Lispworks printer does not call our PRINT-OBJECT methods, because
;;; the methods violate the conforming program rules that state that the
;;; consequences are undefined for a method on a standardized generic
;;; function which is applicable when all of the arguments are direct
;;; instances of standardized classes. We've yet to figure out a work
;;; around.
;;;
;;; Loading Clozure's COCOA-bridge after loading GBBopen Tools, will
;;; overwrite the #@ inf-reader, requiring a call to
;;; SET-INF-READER-DISPATCH-MACRO-CHARACTER to restore infinite-value
;;; reading.
(defun inf-reader (stream sub-char infix-parameter)
(declare (ignore sub-char infix-parameter))
(let ((what (with-standard-io-syntax ; protect against foolery
(read stream t nil 't))))
(flet ((illegal-value-error (what)
(error "Illegal infinite-value specifier: #@~s" what)))
(typecase what
(symbol
(flet ((symbol-equal (a b)
(string-equal (symbol-name a) (symbol-name b))))
(cond
((symbol-equal what 'short-float-infinity) infinity$&)
((symbol-equal what 'single-float-infinity) infinity$)
((symbol-equal what 'double-float-infinity) infinity$$)
((symbol-equal what 'long-float-infinity) infinity$$$)
((symbol-equal what 'short-float-negative-infinity) -infinity$&)
((symbol-equal what 'single-float-negative-infinity) -infinity$)
((symbol-equal what 'double-float-negative-infinity) -infinity$$)
((symbol-equal what 'long-float-negative-infinity) -infinity$$$)
(*inf-reader-escape-hook*
(funcall *inf-reader-escape-hook* what))
(t (illegal-value-error what)))))
;; Maintain COCOA-bridge #@string extension:
#+clozure
(string
(if (fboundp (find-symbol "objc-#@-reader"
(load-time-value (find-package :ccl))))
(funcall (macro-function 'ccl:@) what)
(illegal-value-error what)))