3
(set-enforce-redundancy t)
5
(local (include-book "../support/top"))
9
(set-inhibit-warnings "theory") ; avoid warning in the next event
10
(local (in-theory nil))
11
;(set-inhibit-warnings) ; restore theory warnings (optional)
14
;;;**********************************************************************
16
;;;**********************************************************************
19
(declare (xargs :guard (integerp n)))
20
(* (sgn x) (fl (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n))))
22
(defthmd trunc-rewrite
23
(implies (and (rationalp x)
28
(fl (* (expt 2 (- (1- n) (expo x))) (abs x)))
29
(expt 2 (- (1+ (expo x)) n))))))
32
;BOZO now a rewrite rule too; okay?
33
(defthm trunc-positive
35
(case-split (rationalp x))
36
(case-split (integerp n))
40
:rule-classes (:rewrite :linear))
43
;BOZO now a rewrite rule too; okay?
44
(defthm trunc-negative
46
(case-split (rationalp x))
47
(case-split (integerp n))
50
:rule-classes (:rewrite :linear))
53
(equal (trunc 0 n) 0))
60
(equal (sgn (trunc x n))
64
(equal (trunc (* -1 x) n)
68
(equal (abs (trunc x n))
69
(* (fl (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n)))))
72
(defthm trunc-exactp-a
73
(exactp (trunc x n) n))
75
(defthm trunc-exactp-b
76
(implies (and (rationalp x)
79
(iff (= x (trunc x n))
83
(defthmd trunc-exactp-c
84
(implies (and (exactp a n)
93
(defthmd trunc-integer-type-prescription
94
(implies (and (>= (expo x) n)
95
(case-split (integerp n))
97
(integerp (trunc x n)))
98
:rule-classes :type-prescription)
100
(defthmd trunc-upper-bound
101
(implies (and (rationalp x)
103
(<= (abs (trunc x n)) (abs x)))
104
:rule-classes :linear)
106
(defthmd trunc-upper-pos
107
(implies (and (<= 0 x)
111
:rule-classes :linear)
113
(defthmd trunc-lower-1
114
(implies (and (rationalp x)
116
(> (abs (trunc x n)) (- (abs x) (expt 2 (- (1+ (expo x)) n)))))
117
:rule-classes :linear)
119
(defthmd trunc-lower-pos
120
(implies (and (rationalp x)
124
(> (trunc x n) (* x (- 1 (expt 2 (- 1 n))))))
125
:rule-classes :linear)
127
(defthmd trunc-lower-3
128
(implies (and (rationalp x)
131
(>= (abs (trunc x n)) (* (abs x) (- 1 (expt 2 (- 1 n))))))
132
:rule-classes :linear)
134
(defthmd trunc-lower-4
135
(implies (and (rationalp x)
138
(>= (trunc x n) (- x (* (abs x) (expt 2 (- 1 n))))))
139
:rule-classes :linear)
142
(implies (and (rationalp x)
145
(< (abs (- x (trunc x n))) (expt 2 (- (1+ (expo x)) n))))
148
(defthm trunc-diff-pos
149
(implies (and (rationalp x)
153
(< (- x (trunc x n)) (expt 2 (- (1+ (expo x)) n))))
156
(defthm trunc-diff-expo
157
(implies (and (rationalp x)
161
(<= (expo (- x (trunc x n))) (- (expo x) n)))
164
(defthmd trunc-monotone
165
(implies (and (<= x y)
170
(<= (trunc x n) (trunc y n)))
171
:rule-classes :linear)
174
(implies (integerp n)
175
(equal (trunc (* x (expt 2 k)) n)
176
(* (trunc x n) (expt 2 k)))))
179
(implies (and (< 0 n)
183
(equal (expo (trunc x n))
187
(implies (and (>= n m)
191
(equal (trunc (trunc x n) m)
195
(implies (and (rationalp x)
200
(exactp x (+ k (- (expo x) (expo y)))))
202
(trunc (+ x y) (+ k (- (expo (+ x y)) (expo y))))))
205
(defthm plus-trunc-alt
206
(implies (and (exactp x (+ j (expo x) (- (expo (+ x y)))))
214
(trunc y (+ j (- (expo (+ x y))) (expo y))))))
217
(defthmd plus-trunc-corollary
218
(implies (and (< y (expt 2 (- (1+ (expo x)) n)))
225
(= (trunc (+ x y) n) x)))
228
(implies (and (rationalp y)
237
(= (trunc (+ (expt 2 e) (trunc y k)) m)
238
(trunc (+ (expt 2 e) y) m)))
242
(implies (and (rationalp x)
248
;;this isn't really needed, but it won't hurt me.
250
(= e (- (1+ (expo x)) n))
251
(= z (trunc (- x (trunc x n)) n)))
252
(= (- (trunc x (+ n k)) (trunc x n))
253
(* (1- (sig (trunc (+ (expt 2 e) z) (1+ k))))
259
(implies (and (= n (1+ (expo x)))
266
(bits x (1- n) (- n k)))))
270
(implies (and (>= x (expt 2 (1- n)))
273
(integerp m) (>= m n)
278
(land x (- (expt 2 m) (expt 2 (- n k))) n)))
283
(implies (and (integerp n) (> n 0)
284
(rationalp x) (> x 0)
287
(= (- x (expt 2 (- (expo x) n)))
292
(implies (and (= n (1+ (expo x)))
301
(bits x (1- (- n k)) (- n m)))))))
303
;;;**********************************************************************
305
;;;**********************************************************************
308
(* (sgn x) (cg (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n))))
310
(defthmd away-rewrite
311
(implies (and (rationalp x)
316
(cg (* (expt 2 (- (1- n) (expo x))) (abs x)))
317
(expt 2 (- (1+ (expo x)) n))))))
320
;BOZO wasn't a rewrite rule..
321
(defthm away-positive
322
(implies (and (< 0 x)
323
(case-split (rationalp x))
326
:rule-classes (:rewrite :linear))
329
;BOZO wasn't a rewrite rule..
330
(defthm away-negative
331
(implies (and (< x 0)
332
(case-split (rationalp x))
335
:rule-classes (:rewrite :linear))
338
(equal (away 0 n) 0))
341
(equal (sgn (away x n))
345
(= (away (* -1 x) n) (* -1 (away x n))))
348
(implies (and (rationalp x)
350
(equal (abs (away x n))
351
(* (cg (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n))))))
355
(defthm away-exactp-a
356
(implies (case-split (< 0 n))
357
(exactp (away x n) n)))
359
(defthm away-exactp-b
360
(implies (and (rationalp x)
363
(iff (= x (away x n))
368
(defthmd away-exactp-c
369
(implies (and (exactp a n)
378
(defthmd away-lower-bound
379
(implies (and (case-split (rationalp x))
380
(case-split (integerp n)))
381
(>= (abs (away x n)) (abs x)))
382
:rule-classes :linear)
384
(defthmd away-lower-pos
385
(implies (and (>= x 0)
386
(case-split (rationalp x))
387
(case-split (integerp n)))
389
:rule-classes :linear)
391
;; ;; (defthmd expo-away-lower-bound
392
;; ;; (implies (and (rationalp x)
395
;; ;; (>= (expo (away x n)) (expo x)))
396
;; ;; :rule-classes :linear)
399
(defthmd expo-away-lower-bound
400
(implies (and (rationalp x)
402
(>= (expo (away x n)) (expo x)))
403
:rule-classes :linear)
406
(defthmd away-upper-1
407
(implies (and (rationalp x)
410
(< (abs (away x n)) (+ (abs x) (expt 2 (- (1+ (expo x)) n)))))
411
:rule-classes :linear)
413
(defthmd away-upper-2
414
(implies (and (rationalp x)
418
(< (abs (away x n)) (* (abs x) (+ 1 (expt 2 (- 1 n))))))
419
:rule-classes :linear)
421
(defthmd away-upper-3
422
(implies (and (rationalp x)
425
(<= (abs (away x n)) (* (abs x) (+ 1 (expt 2 (- 1 n))))))
426
:rule-classes :linear)
429
(implies (and (rationalp x)
432
(< (abs (- (away x n) x)) (expt 2 (- (1+ (expo x)) n))))
433
:rule-classes :linear)
435
(defthmd away-diff-pos
436
(implies (and (rationalp x)
440
(< (- (away x n) x) (expt 2 (- (1+ (expo x)) n))))
441
:rule-classes :linear)
443
(defthmd away-diff-expo
444
(implies (and (rationalp x)
448
(<= (expo (- (away x n) x)) (- (expo x) n)))
449
:rule-classes :linear)
451
(defthm away-exactp-d
452
(implies (and (rationalp x)
456
(<= (abs (away x n)) (expt 2 (1+ (expo x)))))
461
(implies (and (rationalp x)
463
(not (= (abs (away x n)) (expt 2 (1+ (expo x))))))
464
(equal (expo (away x n))
469
;; (implies (and (rationalp x)
473
;; (not (= (abs (away x n)) (expt 2 (1+ (expo x))))))
474
;; (equal (expo (away x n))
479
(defthmd away-monotone
480
(implies (and (rationalp x)
484
(<= (away x n) (away y n)))
485
:rule-classes :linear)
488
(implies (integerp n)
489
(= (away (* x (expt 2 k)) n)
490
(* (away x n) (expt 2 k)))))
493
(implies (and (rationalp x)
499
(equal (away (away x n) m)
503
(implies (and (rationalp x)
512
(expt 2 (- (1+ (expo x)) n))
513
(- (expt 2 (- (1+ (expo x)) m))))
518
(implies (and (exactp x (+ k (- (expo x) (expo y))))
526
(+ k (- (expo (+ x y)) (expo y))))))
529
(defthm plus-away-alt
530
(implies (and (exactp x (+ j (expo x) (- (expo (+ x y)))))
538
(away y (+ j (- (expo (+ x y))) (expo y))))))
541
(defthmd plus-away-corollary
542
(implies (and (< y (expt 2 (- (1+ (expo x)) n)))
553
(implies (and (integerp n) (> n 0)
554
(rationalp x) (> x 0)
558
(+ x (expt 2 (- (expo x) n)))))
562
(implies (and (rationalp x) (> x 0)
567
(expt 2 (+ (expo x) 1 (- n))))))
571
(defthm minus-trunc-4
572
(implies (and (rationalp x)
579
(> (+ k (- (expo (- x y)) (expo y))) 0)
580
(= n (+ k (- (expo x) (expo y))))
582
(equal (- x (trunc y k))
583
(away (- x y) (+ k (- (expo (- x y)) (expo y))))))
586
;BOZO move to away section?
587
(defthm minus-trunc-5
588
(implies (and (rationalp x)
595
(> (+ k (- (expo (- x y)) (expo y))) 0)
596
(= n (+ k (- (expo x) (expo y))))
598
(equal (- x (trunc y k))
599
(- (trunc (- y x) (+ k (- (expo (- x y)) (expo y)))))))
603
;;;**********************************************************************
605
;;;**********************************************************************
611
(let ((z (fl (* (expt 2 (1- n)) (sig x))))
612
(f (re (* (expt 2 (1- n)) (sig x)))))
623
(implies (and (< (abs (- x (trunc x n)))
624
(abs (- (away x n) x)))
633
(implies (and (> (abs (- x (trunc x n))) (abs (- (away x n) x)))
641
(or (= (near x n) (trunc x n))
642
(= (near x n) (away x n)))
646
(implies (and (< 0 x)
651
:rule-classes (:TYPE-PRESCRIPTION :LINEAR))
654
(implies (and (< x 0)
660
:rule-classes (:TYPE-PRESCRIPTION :LINEAR))
667
(implies (and (rationalp x)
670
(equal (sgn (near x n))
674
(equal (near (* -1 x) n)
677
(defthm near-exactp-b
678
(implies (and (rationalp x)
681
(iff (= x (near x n))
685
(defthm near-exactp-a
687
(exactp (near x n) n)))
689
(defthmd near-exactp-c
690
(implies (and (exactp a n)
700
(defthmd near-exactp-d
701
(implies (and (rationalp x)
711
(implies (and (rationalp x)
715
(<= (near x n) (away x n)))
719
(implies (and (rationalp x)
723
(>= (near x n) (trunc x n)))
726
;was called monotone-near
727
(defthm near-monotone
728
(implies (and (<= x y)
734
(<= (near x n) (near y n))))
737
(implies (and (rationalp x)
740
(= (near (* x (expt 2 k)) n)
741
(* (near x n) (expt 2 k)))))
744
(implies (and (exactp y n)
749
(>= (abs (- x y)) (abs (- x (near x n)))))
752
(defund near-witness (x y n)
753
(if (= (expo x) (expo y))
754
(/ (+ (near x n) (near y n)) 2)
757
(defthm near-near-lemma
758
(implies (and (rationalp x)
764
(not (= (near x n) (near y n))))
765
(and (<= x (near-witness x y n))
766
(<= (near-witness x y n) y)
767
(exactp (near-witness x y n) (1+ n))))
771
(implies (and (rationalp x)
783
(<= (near y k) (near x k)))
787
(implies (and (integerp n) (> n 0)
790
(<= (abs (- x (near x n)))
791
(expt 2 (- (expo x) n))))
795
(implies (and (rationalp x) (> x 0)
796
(rationalp a) (> a 0)
799
(> x (+ a (expt 2 (- (expo a) n)))))
800
(>= (near x n) (+ a (expt 2 (- (1+ (expo a)) n)))))
804
(implies (and (rationalp x) (> x 0)
805
(rationalp a) (> a 0)
808
(< x (+ a (expt 2 (- (expo a) n)))))
813
(implies (and (rationalp x) (> x 0)
814
(rationalp a) (> a 0)
818
(> x (- a (expt 2 (- (expo x) n)))))
823
(implies (and (rationalp x) ;; (> x 0)
827
(exactp (near x n) (1- n)))
831
(implies (and (rationalp x) (> x 0)
833
(>= (+ x (expt 2 (- (expo x) n)))
834
(expt 2 (1+ (expo x)))))
836
(expt 2 (1+ (expo x)))))
840
(implies (and (rationalp x) (> x 0)
842
(>= (+ x (expt 2 (- (expo x) n)))
843
(expt 2 (1+ (expo x)))))
844
(= (trunc (+ x (expt 2 (- (expo x) n))) n)
845
(expt 2 (1+ (expo x)))))
849
(implies (and (rationalp x) (> x 0)
850
(integerp n) (> n 1))
852
(if (and (exactp x (1+ n)) (not (exactp x n)))
853
(trunc (+ x (expt 2 (- (expo x) n))) (1- n))
854
(trunc (+ x (expt 2 (- (expo x) n))) n))))
858
;;;**********************************************************************
860
;;;**********************************************************************
863
(if (< (re (* (expt 2 (1- n)) (sig x)))
869
(implies (and (rationalp x)
874
(trunc (+ x (expt 2 (- (expo x) n))) n)))
878
(implies (and (rationalp x)
882
(* (sgn x) (near+ (abs x) n))))
886
(implies (and (rationalp x)
889
(equal (sgn (near+ x n))
893
(implies (and (rationalp x)
898
:rule-classes :linear)
902
(implies (and (< x 0)
907
:rule-classes :linear)
910
(implies (and (case-split (< 0 n))
911
(case-split (rationalp x))
912
(case-split (integerp n))
914
(equal (equal (near+ x n) 0)
919
(equal (near+ 0 n) 0))
922
(= (near+ (* -1 x) n) (* -1 (near+ x n))))
925
(implies (and (rationalp x)
928
(= (near+ (* x (expt 2 k)) n)
929
(* (near+ x n) (expt 2 k)))))
933
(or (= (near+ x n) (trunc x n))
934
(= (near+ x n) (away x n)))
938
(implies (and (rationalp x)
940
(< (abs (- x (trunc x n))) (abs (- (away x n) x))))
941
(= (near+ x n) (trunc x n)))
945
(implies (and (rationalp x)
947
(> (abs (- x (trunc x n))) (abs (- (away x n) x))))
948
(= (near+ x n) (away x n)))
952
(implies (and (rationalp x)
956
(<= (near+ x n) (away x n)))
960
(implies (and (rationalp x)
964
(>= (near+ x n) (trunc x n)))
969
(implies (and (exactp y n)
974
(>= (abs (- x y)) (abs (- x (near+ x n)))))
979
(implies (and (integerp n)
982
(<= (abs (- x (near+ x n)))
983
(expt 2 (- (expo x) n))))
987
;was called monotone-near+
989
;; (defthm near+-monotone
990
;; (implies (and (<= x y)
996
;; (<= (near+ x n) (near+ y n))))
998
(defthm near+-monotone
999
(implies (and (<= x y)
1004
(<= (near+ x n) (near+ y n))))
1008
(implies (and (rationalp x) (> x 0)
1009
(integerp n) (> n 1)
1010
(>= (+ x (expt 2 (- (expo x) n)))
1011
(expt 2 (1+ (expo x)))))
1013
(expt 2 (1+ (expo x)))))
1016
(defthm near+-exactp-b
1017
(implies (and (rationalp x)
1020
(iff (= x (near+ x n))
1024
(defthm near+-exactp-a
1025
(implies (and (rationalp x)
1028
(exactp (near+ x n) n)))
1030
(defthm near+-exactp-c
1031
(implies (and (rationalp x)
1037
(>= a (near+ x n))))
1039
(defthm near+-exactp-d
1040
(implies (and (rationalp x)
1046
(<= a (near+ x n))))
1048
;; (defthm near+-exactp-c
1049
;; (implies (and (rationalp x)
1056
;; (>= a (near+ x n))))
1058
;; (defthm near+-exactp-d
1059
;; (implies (and (rationalp x)
1066
;; (<= a (near+ x n))))
1069
;;;**********************************************************************
1071
;;;**********************************************************************
1073
(defund sticky (x n)
1074
(cond ((exactp x (1- n)) x)
1075
(t (+ (trunc x (1- n))
1076
(* (sgn x) (expt 2 (1+ (- (expo x) n))))))))
1079
(implies (rationalp x)
1081
(* (sgn x) (expt 2 (expo x))))))
1084
(implies (and (< 0 x)
1089
:rule-classes :linear)
1092
(equal (sticky 0 n) 0))
1094
(defthmd sticky-minus
1095
(equal (sticky (* -1 x) n) (* -1 (sticky x n))))
1097
(defthm sticky-shift
1098
(implies (and (rationalp x)
1099
(integerp n) (> n 0)
1101
(= (sticky (* (expt 2 k) x) n)
1102
(* (expt 2 k) (sticky x n))))
1105
(defthm sticky-exactp
1106
(implies (and (rationalp x) ;; (>= x 0)
1107
(integerp n) (> n 0))
1108
(exactp (sticky x n) n))
1111
(defthm sticky-exactp-m
1112
(implies (and (rationalp x)
1117
(iff (exactp (sticky x n) m)
1122
(implies (and (rationalp x) ;; (> x 0)
1123
(integerp n) (> n 0))
1124
(= (expo (sticky x n))
1128
(defthm trunc-sticky
1129
(implies (and (rationalp x) ;; (> x 0)
1130
(integerp m) (> m 0)
1131
(integerp n) (> n m))
1132
(= (trunc (sticky x n) m)
1137
(implies (and (rationalp x) ;; (> x 0)
1138
(integerp m) (> m 0)
1139
(integerp n) (> n m))
1140
(= (away (sticky x n) m)
1145
(implies (and (rationalp x) ;; (> x 0)
1146
(integerp m) (> m 0)
1147
(integerp n) (> n m))
1148
(= (away (sticky x n) m)
1154
(implies (and (rationalp x) ;; (> x 0)
1155
(integerp m) (> m 0)
1156
(integerp n) (> n (1+ m)))
1157
(= (near (sticky x n) m)
1162
(defthm near+-sticky
1163
(implies (and (rationalp x)
1164
(integerp m) (> m 0)
1165
(integerp n) (> n (1+ m)))
1166
(= (near+ (sticky x n) m)
1171
(defthm sticky-sticky
1172
(implies (and (rationalp x)
1177
(= (sticky (sticky x n) m)
1182
(defthm sticky-plus-original ;; Fri Oct 13 14:40:05 2006
1183
(implies (and (rationalp x)
1188
(= k1 (+ k (- (expo x) (expo y))))
1189
(= k2 (+ k (- (expo (+ x y)) (expo y))))
1194
(= (+ x (sticky y k))
1195
(sticky (+ x y) k2)))
1198
(defthm minus-sticky
1199
(implies (and (rationalp x)
1204
(= k1 (+ k (- (expo x) (expo y))))
1205
(= k2 (+ k (- (expo (- x y)) (expo y))))
1210
(= (- x (sticky y k))
1211
(sticky (- x y) k2)))
1214
(defthm sticky-lemma
1215
(implies (and (rationalp x)
1219
(= k1 (+ k (- (expo x) (expo y))))
1220
(= k2 (+ k (- (expo (+ x y)) (expo y))))
1225
(= (+ x (sticky y k))
1226
(sticky (+ x y) k2)))
1229
(defthmd sticky-monotone
1230
(implies (and (<= x y)
1234
(<= (sticky x n) (sticky y n)))
1235
:rule-classes :linear)
1237
;;;**********************************************************************
1239
;;;**********************************************************************
1241
;was called "odd" but that name conflicted with another function we wanted (a recursive version of oddp)
1243
(let ((z (fl (* (expt 2 (1- n)) (sig x)))))
1245
(* (sgn x) (1+ z) (expt 2 (- (1+ (expo x)) n)))
1246
(* (sgn x) z (expt 2 (- (1+ (expo x)) n))))))
1249
(implies (and (< 0 x)
1257
(implies (and (rationalp x)
1261
(>= (oddr x n) (trunc x n)))
1265
(defthmd oddr-rewrite
1266
(implies (and (< 0 x)
1271
(let ((z (fl (* (expt 2 (- (1- n) (expo x))) x))))
1273
(* (1+ z) (expt 2 (- (1+ (expo x)) n)))
1274
(* z (expt 2 (- (1+ (expo x)) n))))))))
1277
(implies (and (rationalp x)
1283
(expt 2 (- (1+ (expo x)) n)))))
1287
(implies (and (rationalp x)
1291
(equal (expo (oddr x n)) (expo x))))
1294
(implies (and (rationalp x)
1298
(exactp (oddr x n) n))
1301
(defthm not-exactp-oddr
1302
(implies (and (rationalp x)
1306
(not (exactp (oddr x n) (1- n))))
1310
(implies (and (rationalp x)
1316
(= (trunc (oddr x n) m)
1321
(+ k (- (expo (+ x y)) (expo y))))
1324
(implies (and (rationalp x)
1330
(> (+ (1- k) (- (expo x) (expo y))) 0)
1331
(exactp x (+ (1- k) (- (expo x) (expo y)))))
1333
(oddr (+ x y) (kp k x y))))
1336
(defthm trunc-trunc-oddr
1337
(implies (and (rationalp x)
1345
(>= (trunc x k) (trunc (oddr y m) k)))
1348
(defthm away-away-oddr
1349
(implies (and (rationalp x)
1357
(>= (away x k) (away (oddr y m) k)))
1360
(defthm near-near-oddr
1361
(implies (and (rationalp x)
1369
(>= (near x k) (near (oddr y m) k)))
1373
;;;**********************************************************************
1374
;;; IEEE Rounding (most theorems also apply to AWAY and NEAR+)
1375
;;;**********************************************************************
1387
(defund ieee-mode-p (mode)
1388
(member mode '(trunc inf minf near)))
1390
(defun common-rounding-mode-p (mode)
1391
(or (IEEE-mode-p mode) (equal mode 'away) (equal mode 'near+)))
1393
(defthmd ieee-mode-p-implies-common-rounding-mode-p
1394
(implies (IEEE-mode-p mode)
1395
(common-rounding-mode-p mode)))
1397
(defund rnd (x mode n)
1407
(defthm rationalp-rnd
1408
(rationalp (rnd x mode n))
1409
:rule-classes (:type-prescription))
1411
; Unlike the above, we leave the following two as rewrite rules because we may
1412
; want to use the rewriter to relieve their hypotheses.
1416
(<= (rnd x mode n) 0))
1417
:rule-classes (:rewrite :type-prescription :linear))
1421
(<= 0 (rnd x mode n)))
1422
:rule-classes (:rewrite :type-prescription :linear))
1425
(implies (and (< 0 x)
1429
(common-rounding-mode-p mode))
1430
(> (rnd x mode n) 0))
1431
:RULE-CLASSES (:TYPE-PRESCRIPTION))
1434
(implies (and (< x 0)
1438
(common-rounding-mode-p mode))
1439
(< (rnd x mode n) 0))
1440
:RULE-CLASSES (:TYPE-PRESCRIPTION))
1443
(equal (rnd 0 mode n)
1447
(implies (and; (rationalp x)
1448
(common-rounding-mode-p mode)
1451
(equal (sgn (rnd x mode n))
1460
(defthm ieee-mode-p-flip
1461
(implies (ieee-mode-p m)
1462
(ieee-mode-p (flip m))))
1464
(defthm common-rounding-mode-p-flip
1465
(implies (common-rounding-mode-p m)
1466
(common-rounding-mode-p (flip m))))
1468
;a very similar rule was called rnd-flip
1471
(equal (rnd (* -1 x) mode n)
1472
(* -1 (rnd x (flip mode) n))))
1474
(defthm rnd-exactp-b
1475
(implies (and (rationalp x)
1476
(common-rounding-mode-p mode)
1479
(equal (equal x (rnd x mode n))
1482
(defthm rnd-exactp-a
1484
(exactp (rnd x mode n) n)))
1486
(defthmd rnd-exactp-c
1487
(implies (and (rationalp x)
1489
(common-rounding-mode-p mode)
1495
(>= a (rnd x mode n))))
1497
(defthmd rnd-exactp-d
1498
(implies (and (rationalp x)
1500
(common-rounding-mode-p mode)
1506
(<= a (rnd x mode n))))
1509
(implies (and (rationalp x)
1511
(common-rounding-mode-p mode)
1513
(<= (rnd x mode n) (away x n)))
1517
(implies (and (rationalp x)
1519
(common-rounding-mode-p mode)
1522
(>= (rnd x mode n) (trunc x n)))
1526
(defthmd rnd-monotone
1527
(implies (and (<= x y)
1530
(common-rounding-mode-p mode)
1533
(<= (rnd x mode n) (rnd y mode n))))
1536
(implies (and (rationalp x)
1537
(common-rounding-mode-p mode)
1540
(exactp (rnd x mode n) n)))
1543
(implies (and (rationalp x)
1545
(common-rounding-mode-p mode)
1547
(= (rnd (* x (expt 2 k)) mode n)
1548
(* (rnd x mode n) (expt 2 k))))
1552
(implies (and (rationalp x)
1556
(common-rounding-mode-p mode)
1557
(not (= (abs (rnd x mode n))
1558
(expt 2 (1+ (expo x))))))
1559
(= (expo (rnd x mode n))
1563
(defthm expo-rnd-bnd
1564
(implies (and (rationalp x)
1567
(common-rounding-mode-p mode))
1568
(>= (expo (rnd x mode n))
1572
(defun rnd-const (e mode n)
1574
((near near+) (expt 2 (- e n)))
1575
((inf away) (1- (expt 2 (1+ (- e n)))))
1578
(defthm rnd-const-thm
1579
(implies (and (common-rounding-mode-p mode)
1586
(if (and (eql mode 'near)
1589
(trunc (+ x (rnd-const (expo x) mode n)) (1- n))
1590
(trunc (+ x (rnd-const (expo x) mode n)) n))))
1593
(defun roundup (x mode n)
1594
; Returns T when we should add an ulp after truncating x to n digits.
1596
(near+ (= (bitn x (- (expo x) n)) 1))
1597
(near (and (= (bitn x (- (expo x) n)) 1)
1598
(or (not (exactp x (1+ n)))
1599
(= (bitn x (- (1+ (expo x)) n)) 1))))
1600
((inf away) (not (exactp x n)))
1604
(implies (and (common-rounding-mode-p mode)
1611
(if (roundup x mode n)
1617
(implies (and (common-rounding-mode-p mode)
1623
(equal (rnd (sticky x n) mode m)
1627
(implies (and (rationalp x)
1630
(common-rounding-mode-p mode))
1631
(< (abs (- x (rnd x mode n))) (expt 2 (- (1+ (expo x)) n)))))
1634
(implies (and (rationalp x)
1639
(exactp x (+ -1 k (- (expo x) (expo y))))
1640
(common-rounding-mode-p mode))
1641
(= (+ x (rnd y mode k))
1644
(+ k (- (expo (+ x y)) (expo y))))))
1649
;;;**********************************************************************
1650
;;; Denormal Rounding
1651
;;;**********************************************************************
1653
(defund drnd-original (x mode n k)
1654
(- (rnd (+ x (* (sgn x) (expt 2 (- 2 (expt 2 (1- k)))))) mode n)
1655
(* (sgn x) (expt 2 (- 2 (expt 2 (1- k)))))))
1657
(defthm drnd-original-0
1658
(equal (drnd-original 0 mode n k)
1661
; a very similar rule was called drnd-original-flip
1662
(defthmd drnd-original-minus
1663
(equal (drnd-original (* -1 x) mode n k)
1664
(* -1 (drnd-original x (flip mode) n k))))
1666
(defthm drnd-original-sticky
1667
(implies (and (common-rounding-mode-p mode)
1675
(<= (expo x) (- 1 (expt 2 (1- k))))
1676
(<= (expo x) (- m (+ n (expt 2 (1- k))))))
1677
(equal (drnd-original (sticky x m) mode n k)
1678
(drnd-original x mode n k)))
1681
(defthm drnd-original-tiny-equal
1682
(implies (and (ieee-mode-p m)
1688
(< (abs x) (expt 2 (- 2 (+ n (expt 2 (1- k))))))
1690
(< (abs y) (expt 2 (- 2 (+ n (expt 2 (1- k))))))
1691
(equal (sgn x) (sgn y)))
1692
(equal (drnd-original x m n k)
1693
(drnd-original y m n k)))
1698
(expt 2 (- 1 (bias q))))
1700
;;These next three show that spn really is what it claims to be
1702
(defthm positive-spn
1704
:rule-classes ( :linear))
1708
(implies (and (integerp p)
1712
(nrepp (spn q) p q)))
1717
(expt 2 (+ 2 (- (bias q)) (- p))))
1719
;;These next three show that spd really is what it claims to be
1721
(defthm positive-spd
1722
(implies (and (integerp p)
1726
:rule-classes :linear)
1728
;; (defthm positive-spd
1729
;; (implies (and (integerp p)
1734
;; :rule-classes :linear)
1737
(implies (and (integerp p)
1741
(drepp (spd p q) p q)))
1743
(defthm smallest-spd
1744
(implies (and (integerp p)
1749
(>= (abs r) (spd p q))))
1751
;DRND returns a denormal, or zero, or the smallest normal:
1753
(defthm drnd-original-type
1754
(implies (and (rationalp x)
1755
(<= (abs x) (spn k))
1760
(common-rounding-mode-p mode))
1761
(or (drepp (drnd-original x mode n k) n k)
1762
(= (drnd-original x mode n k) 0)
1763
(= (drnd-original x mode n k) (* (sgn x) (spn k)))))
1766
(defthmd drnd-original-rewrite
1767
(implies (and (rationalp x)
1768
(<= (abs x) (spn k))
1769
(common-rounding-mode-p mode)
1774
(equal (drnd-original x mode n k)
1781
(defthm drnd-original-of-drepp-is-NOP
1782
(implies (and (drepp x n k)
1787
(common-rounding-mode-p mode))
1788
(equal (drnd-original x mode n k)
1791
(defthm drnd-original-spn-is-spn-general
1792
(implies (and (= (abs x) (spn k))
1793
(common-rounding-mode-p mode)
1799
(= (drnd-original x mode n k) x)))
1801
(defthm drnd-original-trunc-never-goes-away-from-zero
1802
(implies (and (integerp n)
1807
(<= (abs x) (spn k)))
1808
(<= (abs (drnd-original x 'trunc n k))
1811
(defthm drnd-original-away-never-goes-toward-zero
1812
(implies (and (integerp n)
1817
(<= (abs x) (spn k)))
1818
(>= (abs (drnd-original x 'away n k))
1821
(defthm drnd-original-inf-never-goes-down
1822
(implies (and (integerp n)
1827
(<= (abs x) (spn k)))
1828
(>= (drnd-original x 'inf n k)
1831
(defthm drnd-original-minf-never-goes-up
1832
(implies (and (integerp n)
1837
(<= (abs x) (spn k)))
1838
(<= (drnd-original x 'minf n k)
1841
(defthm drnd-original-trunc-skips-no-denormals
1842
(implies (and (integerp n)
1847
(<= (abs x) (spn k))
1849
(<= (abs a) (abs x))
1852
(abs (drnd-original x 'trunc n k)))))
1854
(defthm drnd-original-away-skips-no-denormals
1855
(implies (and (integerp n)
1860
(<= (abs x) (spn k))
1862
(>= (abs a) (abs x))
1864
(>= (abs a) (abs (drnd-original x 'away n k)))))
1867
(defthm drnd-original-inf-skips-no-denormals
1868
(implies (and (integerp n)
1873
(<= (abs x) (spn k))
1876
(>= a (drnd-original x 'inf n k))))
1878
(defthm drnd-original-minf-skips-no-denormals
1879
(implies (and (integerp n)
1884
(<= (abs x) (spn k))
1887
(<= a (drnd-original x 'minf n k))))
1889
(defthm drnd-original-diff
1890
(implies (and (rationalp x)
1891
(<= (ABS X) (SPN K))
1896
(common-rounding-mode-p mode))
1897
(< (abs (- x (drnd-original x mode n k))) (spd n k))))
1899
(defund next-denormal (x n k)
1902
;;NEXT-DENORMAL behaves as expected:
1904
(defthm denormal-spacing
1905
(implies (and (integerp n)
1913
(>= x+ (next-denormal x n k))))
1915
(defthm no-denormal-is-closer-than-what-drnd-original-near-returns
1916
(implies (and (rationalp x)
1917
(<= (abs x) (spn k))
1923
(>= (abs (- x a)) (abs (- x (drnd-original x 'near n k))))))