File: rnd.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (1140 lines) | stat: -rw-r--r-- 35,025 bytes parent folder | download | duplicates (6)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
;;;***************************************************************
;;;An ACL2 Library of Floating Point Arithmetic

;;;David M. Russinoff
;;;Advanced Micro Devices, Inc.
;;;February, 1998
;;;***************************************************************

;put all the new defuns in this book together at the t

(in-package "ACL2")

(local (include-book "float"))
(local (include-book "trunc"))
(local (include-book "away"))
(local (include-book "near"))
(local (include-book "near+"))
(local (include-book "sticky"))
(local (include-book "bitn")) ; for roundup
(local (include-book "land")) ; for roundup
(local (include-book "lior")) ; for roundup

;; Necessary functions:

(local ; ACL2 primitive
 (defun natp (x)
   (declare (xargs :guard t))
   (and (integerp x)
        (<= 0 x))))

(defund fl (x)
  (declare (xargs :guard (real/rationalp x)))
  (floor x 1))

(defund cg (x)
  (declare (xargs :guard (real/rationalp x)))
  (- (fl (- x))))

(defun expo-measure (x)
;  (declare (xargs :guard (and (real/rationalp x) (not (equal x 0)))))
  (cond ((not (rationalp x)) 0)
	((< x 0) '(2 . 0))
	((< x 1) (cons 1 (fl (/ x))))
	(t (fl x))))

(defund expo (x)
  (declare (xargs :guard t
                  :measure (expo-measure x)))
  (cond ((or (not (rationalp x)) (equal x 0)) 0)
	((< x 0) (expo (- x)))
	((< x 1) (1- (expo (* 2 x))))
	((< x 2) 0)
	(t (1+ (expo (/ x 2))))))

;could redefine to divide by the power of 2 (instead of making it a negative power of 2)...
(defund sig (x)
  (declare (xargs :guard t))
  (if (rationalp x)
      (if (< x 0)
          (- (* x (expt 2 (- (expo x)))))
        (* x (expt 2 (- (expo x)))))
    0))

;make defund?
(defun sgn (x)
  (declare (xargs :guard t))
  (if (or (not (rationalp x)) (equal x 0))
      0
    (if (< x 0)
        -1
      1)))

(defund exactp (x n)
;  (declare (xargs :guard (and (real/rationalp x) (integerp n))))
  (integerp (* (sig x) (expt 2 (1- n)))))

(defun fp+ (x n)
  (+ x (expt 2 (- (1+ (expo x)) n))))

(defund trunc (x n)
  (declare (xargs :guard (integerp n)))
  (* (sgn x) (fl (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n))))

(defund away (x n)
  (* (sgn x) (cg (* (expt 2 (1- n)) (sig x))) (expt 2 (- (1+ (expo x)) n))))


(defund re (x)
  (- x (fl x)))

(defund near (x n)
  (let ((z (fl (* (expt 2 (1- n)) (sig x))))
	(f (re (* (expt 2 (1- n)) (sig x)))))
    (if (< f 1/2)
	(trunc x n)
      (if (> f 1/2)
	  (away x n)
	(if (evenp z)
	    (trunc x n)
	  (away x n))))))

(defund near+ (x n)
  (if (< (re (* (expt 2 (1- n)) (sig x)))
	 1/2)
      (trunc x n)
    (away x n)))

(defund sticky (x n)
  (cond ((exactp x (1- n)) x)
	(t (+ (trunc x (1- n))
              (* (sgn x) (expt 2 (1+ (- (expo x) n))))))))

(defund bits (x i j)
  (declare (xargs :guard (and (natp x)
                              (natp i)
                              (natp j))
                  :verify-guards nil))
  (mbe :logic (if (or (not (integerp i))
                      (not (integerp j)))
                  0
                (fl (/ (mod x (expt 2 (1+ i))) (expt 2 j))))
       :exec  (if (< i j)
                  0
                (logand (ash x (- j)) (1- (ash 1 (1+ (- i j))))))))

(defund bitn (x n)
  (declare (xargs :guard (and (natp x)
                              (natp n))
                  :verify-guards nil))
  (mbe :logic (bits x n n)
       :exec  (if (evenp (ash x (- n))) 0 1)))

;;
;; New stuff:
;;

;Typically, we may plan to have inf and minf enabled, but we have a few lemmas about them anyway..

(defund inf (x n)
  (if (>= x 0)
      (away x n)
    (trunc x n)))

(defund minf (x n)
  (if (>= x 0)
      (trunc x n)
    (away x n)))

(defund IEEE-MODE-P (mode)
  (member mode '(trunc inf minf near)))

(defund rounding-mode-p (mode)
  (or (IEEE-mode-p mode) (equal mode 'away) (equal mode 'near+)))

(defund rnd (x mode n)
  (case mode
    (away (away x n))
    (near+ (near+ x n))
    (trunc (trunc x n))
    (inf (inf x n))
    (minf (minf x n))
    (near (near x n))
    (otherwise 0)))

(defund flip (m)
  (case m
    (inf 'minf)
    (minf 'inf)
    (t m)))

;rounding constant..
(defun rnd-const (e mode n)
  (case mode
    ((near near+) (expt 2 (- e n)))
    ((inf away) (1- (expt 2 (1+ (- e n)))))
    (otherwise 0)))

(defthmd inf-minus
  (equal (inf (* -1 x) n)
         (* -1 (minf x n)))
  :hints (("Goal" :in-theory (enable inf minf))))

(defthmd minf-minus
  (equal (minf (* -1 x) n)
         (* -1 (inf x n)))
  :hints (("Goal" :in-theory (enable inf minf))))

(defthm inf-shift
  (implies (and (rationalp x)
                (integerp n)
                (integerp k))
           (= (inf (* x (expt 2 k)) n)
              (* (inf x n) (expt 2 k))))
  :hints (("Goal" :in-theory (enable inf)
           )))

(defthm minf-shift
  (implies (and (rationalp x)
                (integerp n)
                (integerp k))
           (= (minf (* x (expt 2 k)) n)
              (* (minf x n) (expt 2 k))))
  :hints (("Goal"  :in-theory (enable minf))))

(defthm ieee-mode-p-implies-rounding-mode-p
  (implies (IEEE-mode-p mode)
           (rounding-mode-p mode))
  :hints (("Goal" :in-theory (enable rounding-mode-p)))
  :rule-classes (:rewrite; :forward-chaining
                 ))

(defthm rationalp-rnd
  (rationalp (rnd x mode n))
  :hints (("Goal" :in-theory (enable rnd)))
  :rule-classes (:type-prescription))

(defthmd rnd-minus
  (equal (rnd (* -1 x) mode n)
         (* -1 (rnd x (flip mode) n)))
  :hints (("Goal" :in-theory (enable rnd flip  minf-minus inf-minus near+-minus))))

(local (defthm rnd-const-thm-1
         (implies (and (integerp n)
                       (> n 1)
                       (integerp x)
                       (> x 0)
                       (>= (expo x) n))
                  (= (near x n)
                     (if (and (exactp x (1+ n))
                              (not (exactp x n)))
                         (trunc (+ x (rnd-const (expo x) 'near n)) (1- n))
                       (trunc (+ x (rnd-const (expo x) 'near n)) n))))
         :rule-classes ()
         :hints (("Goal"
                  :use ((:instance near-trunc))))))

(local (defthm rnd-const-thm-2
    (implies (and (integerp n)
		  (> n 1)
		  (integerp x)
		  (> x 0)
		  (>= (expo x) n))
	     (= (away x n)
		(trunc (+ x (rnd-const (expo x) 'inf n)) n)))
  :rule-classes ()
  :hints (("Goal" :in-theory (enable exactp2)
		  :use ((:instance away-imp (m (1+ (expo x)))))))))

(local (defthm rnd-const-thm-3
    (implies (and (integerp n)
		  (> n 1)
		  (integerp x)
		  (> x 0)
		  (>= (expo x) n))
	     (= (near+ x n)
		(trunc (+ x (rnd-const (expo x) 'near+ n)) n)))
  :rule-classes ()
  :hints (("Goal" :in-theory (enable exactp2)
		  :use ((:instance near+trunc))))))

(defthm RND-CONST-THM
    (implies (and (rounding-mode-p mode)
		  (integerp n)
		  (> n 1)
		  (integerp x)
		  (> x 0)
		  (>= (expo x) n))
	     (= (rnd x mode n)
		(if (and (eql mode 'near)
			 (exactp x (1+ n))
			 (not (exactp x n)))
		    (trunc (+ x (rnd-const (expo x) mode n)) (1- n))
		  (trunc (+ x (rnd-const (expo x) mode n)) n))))
  :rule-classes ()
  :hints (("Goal"  :in-theory (enable inf minf rnd rounding-mode-p ieee-mode-p)
           :use (rnd-const-thm-1 rnd-const-thm-2 rnd-const-thm-3))))

(defun roundup (x mode n)
; Returns T when we should add an ulp after truncating x to n digits.
  (case mode
    (near+ (= (bitn x (- (expo x) n)) 1))
    (near (and (= (bitn x (- (expo x) n)) 1)
               (or (not (exactp x (1+ n)))
                   (= (bitn x (- (1+ (expo x)) n)) 1))))
    ((inf away) (not (exactp x n)))
    (otherwise nil)))

(local
 (defthm exactp-preserved-up ; could perhaps manage with exactp-<= instead
   (implies (and (integerp n)
                 (< 0 n)
                 (exactp x n))
            (exactp x (1+ n)))
   :hints (("Goal" :in-theory (enable exactp)
            :expand ((expt 2 n))))))

(local (include-book "merge"))

(local
 (defthm roundup-thm-1
   (implies (and (rounding-mode-p mode)
                 (not (eq mode 'near))
                 (not (eq mode 'near+))
                 (integerp n)
                 (> n 1)
                 (integerp x)
                 (> x 0)
                 (>= (expo x) n))
            (= (rnd x mode n)
               (if (roundup x mode n)
                   (fp+ (trunc x n) n)
                 (trunc x n))))
   :hints (("Goal"  :in-theory (enable inf minf rnd rounding-mode-p
                                       ieee-mode-p)
            :use (trunc-away trunc-exactp-a away-exactp-a)))))

(local (include-book "bits-trunc"))

(local
 (defthm roundup-thm-2-1-1-1
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        0))
            (equal (trunc x n)
                   (trunc x (1- n))))
   :hints (("Goal" :use ((:instance bits-trunc-2 (k n)
                                    (n (1+ (expo x))))
                         (:instance bits-trunc-2 (k (1- n))
                                    (n (1+ (expo x))))
                         (:instance bits-plus-bitn (n (expo x))
                                    (m (+ 1 (expo x) (* -1 n)))))
            :expand ((expt 2 (+ 2 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-1-1
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        0))
            (exactp (trunc x n) (1- n)))
   :hints (("Goal" :use roundup-thm-2-1-1-1))))

(local
 (defthm roundup-thm-2-1-2
   (implies (and (not (exactp x n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (exactp x (+ 1 n)))
            (equal (+ x (expt 2 (+ (expo x) (* -1 n))))
                   (+ (trunc x n)
                      (expt 2 (1+ (+ (expo x) (* -1 n)))))))
   :hints (("Goal" :use trunc-away-a
            :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-1
   (implies (and (not (exactp x n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (exactp x (+ 1 n))
                 (not (equal (bitn x (+ 1 (expo x) (* -1 n)))
                             1)))
            (equal (trunc (+ x (expt 2 (+ (expo x) (* -1 n))))
                          (1- n))
                   (trunc x n)))
   :hints (("Goal" :in-theory (enable plus-trunc-corollary
                                      expt-strong-monotone)
            :use roundup-thm-2-1-2))))

(local
 (defthm roundup-thm-2-2
   (implies (and (not (exactp x n))
                 (exactp x (+ 1 n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x)))
            (equal (bitn x (+ (expo x) (* -1 n)))
                   1))
   :hints (("Goal" :use ((:instance exact-k+1
                                    (n (1+ (expo x)))
                                    (k (+ (expo x) (* -1 n)))))))))

(local
 (encapsulate
  ()

  (local
   (defthm roundup-thm-2-3-1-1-1
     (implies (and (not (exactp x n))
                   (exactp x (+ 1 n))
                   (integerp n)
                   (< 1 n)
                   (integerp x)
                   (< 0 x)
                   (<= n (expo x))
                   (equal (bitn x (+ 1 (expo x) (* -1 n)))
                          1))
              (equal (bits x (expo x)
                           (+ 1 (expo x) (* -1 n)))
                     (+ 1
                        (* 2
                           (bits x (expo x)
                                 (+ 2 (expo x) (* -1 n)))))))
     :hints (("Goal" :use ((:instance bits-plus-bitn (n (expo x))
                                      (m (+ 1 (expo x) (* -1 n)))))))))

  (defthm roundup-thm-2-3-1-1
    (implies (and (not (exactp x n))
                  (exactp x (+ 1 n))
                  (integerp n)
                  (< 1 n)
                  (integerp x)
                  (< 0 x)
                  (<= n (expo x))
                  (equal (bitn x (+ 1 (expo x) (* -1 n)))
                         1))
             (equal x
                    (+ (trunc x (1- n))
                       (expt 2 (+ (expo x) (* -1 n)))
                       (expt 2 (+ 1 (expo x) (* -1 n))))))
    :hints (("Goal"
             :use ((:instance bits-trunc-2 (n (1+ (expo x)))
                              (k (1+ n)))
                   (:instance bits-plus-bitn (n (expo x))
                              (m (+ (expo x) (* -1 n))))
                   (:instance bits-plus-bitn (n (expo x))
                              (m (+ 1 (expo x) (* -1 n))))
                   (:instance bits-trunc-2 (n (1+ (expo x)))
                              (k (1- n))))
             :expand
             ((expt 2 (+ 2 (expo x) (* -1 n)))))
            ;; BOZO!! We can't put the following as part of Goal's :expand hint.
            ("Subgoal 4" :expand ((expt 2 (+ 1 (expo x) (* -1 n)))))
            ("Subgoal 1" :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
    :rule-classes nil)))

(local
 (defthm roundup-thm-2-3-1-2
   (implies (and (not (exactp x n))
                 (exactp x (+ 1 n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        1))
            (equal (+ x (expt 2 (+ (expo x) (* -1 n))))
                   (+ (trunc x (1- n))
                      (expt 2 (+ 2 (expo x) (* -1 n))))))
   :hints (("Goal" :use roundup-thm-2-3-1-1
            :expand ((expt 2 (+ 2 (expo x) (* -1 n)))))
           ("Subgoal 1" ; !! BOZO: avoid infinite by using separate subgoal hint
            :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-3-1
   (implies (and (not (exactp x n))
                 (exactp x (+ 1 n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        1))
            (exactp (+ x (expt 2 (+ (expo x) (* -1 n))))
                    (1- n)))
   :hints (("Goal" :use (roundup-thm-2-3-1-1
                         roundup-thm-2-3-1-2
                         (:instance fp+2
                                    (x (trunc x (1- n)))
                                    (n (1- n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-3-2
   (implies (and (not (exactp x n))
                 (exactp x (+ 1 n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        1))
            (equal (+ (trunc x n)
                      (expt 2 (+ 1 (expo x) (* -1 n))))
                   (+ x (expt 2 (+ (expo x) (* -1 n))))))
   :hints (("Goal" :use trunc-away-a
            :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-3
   (implies (and (not (exactp x n))
                 (exactp x (+ 1 n))
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ 1 (expo x) (* -1 n)))
                        1))
            (equal (trunc (+ x (expt 2 (+ (expo x) (* -1 n))))
                          (1- n))
                   (+ (trunc x n)
                      (expt 2 (+ 1 (expo x) (* -1 n))))))
   :hints (("Goal" :use (roundup-thm-2-3-1
                         roundup-thm-2-3-2
                         (:instance trunc-exactp-a
                                    (x (+ x (expt 2 (+ (expo x) (* -1 n)))))
                                    (n (1- n))))))))

; We need a special case of the following lemma for roundup-thm-2-4, so let's
; prove a nice version to include in the library.

(defthmd trunc-split
  (implies (and (= n (1+ (expo x)))
                (>= x 0)
                (integerp m)
                (> m k)
                (integerp k)
                (> k 0))
           (equal (trunc x m)
                  (+ (trunc x k)
                     (* (expt 2 (- n m))
                        (bits x (1- (- n k)) (- n m))))))
  :hints (("Goal" :use ((:instance expt-split (r 2)
                                   (i (+ m (* -1 k)))
                                   (j (+ 1 (expo x) (* -1 m))))
                        bits-trunc-2
                        (:instance bits-trunc-2 (k m)
                                   (n (1+ (expo x))))
                        (:instance bits-plus-bits (n (expo x))
                                   (m (- (1+ (expo x)) m))
                                   (p (- (1+ (expo x)) k)))))))

(defund trunc-rem (x n)
  (- x (trunc x n)))

(defthm trunc-rem-small
  (implies (and (integerp n)
                (<= 0 n)
                (rationalp x)
                (<= 0 x))
           (< (trunc-rem x n)
              (expt 2 (+ 1 (expo x) (* -1 n)))))
  :hints (("Goal" :use (trunc-diff-pos trunc-rem)))
  :rule-classes :linear)

(defthm trunc-rem-nonnegative
  (implies (and (integerp n)
                (<= 0 n)
                (rationalp x)
                (<= 0 x))
           (and (rationalp (trunc-rem x n))
                (<= 0 (trunc-rem x n))))
  :hints (("Goal" :use (trunc-lower-pos trunc-rem)))
  :rule-classes :type-prescription)

; First, break x into the high n bits, the next bit, and the rest.

(local
 (defthm roundup-thm-2-4-1
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        1))
            (equal x
                   (+ (trunc x n)
                      (expt 2 (- (expo x) n))
                      (trunc-rem x (1+ n)))))
   :hints (("Goal" :use ((:instance trunc-split
                                    (n (1+ (expo x)))
                                    (m (1+ n))
                                    (k n)))
            :in-theory (enable trunc-rem bitn)))
   :rule-classes nil))

; Next, trivially introduce fp+.  A key fact is that (exactp (fp+ (trunc x n)
; n) n), by fp+2 and trunc-exact-b.  We need that fact in order to apply
; plus-trunc-corollary.

(local
 (defthm roundup-thm-2-4-2
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        1))
            (equal (+ x (expt 2 (+ (expo x) (* -1 n))))
                   (+ (fp+ (trunc x n) n)
                      (trunc-rem x (1+ n)))))
   :hints (("Goal" :use (roundup-thm-2-4-1)
            :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm expt-2-+-constant
   (implies (and (syntaxp (quotep k))
                 (integerp n)
                 (<= 0 n)
                 (integerp k)
                 (<= 0 k))
            (equal (expt 2 (+ k n))
                   (* 2 (expt 2 (+ (1- k) n)))))
   :hints (("Goal" :expand ((expt 2 (+ k n)))))))

; Note: fp+-positive was first discovered at about this point.

(local
 (defthm roundup-thm-2-4-3
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        1))
            (equal (trunc (+ (fp+ (trunc x n) n)
                             (trunc-rem x (1+ n)))
                          n)
                   (fp+ (trunc x n) n)))
   :hints (("Goal" :use ((:instance fp+2 (x (trunc x n)))
                         (:instance plus-trunc-corollary
                                    (x (fp+ (trunc x n) n))
                                    (y (trunc-rem x (1+ n))))
                         (:instance trunc-rem-small (n (1+ n)))
                         (:instance fp+2-2 (x (trunc x n))))
            :in-theory (disable fp+ plus-trunc-corollary)))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-4
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        1))
            (equal (trunc (+ x (expt 2 (+ (expo x) (* -1 n))))
                          n)
                   (+ (trunc x n)
                      (expt 2 (+ 1 (expo x) (* -1 n))))))
   :hints (("Goal" :use (roundup-thm-2-4-2 roundup-thm-2-4-3)))))

(local
 (defthm roundup-thm-2-5-1
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        0))
            (equal x
                   (+ (trunc x n)
                      (trunc-rem x (1+ n)))))
   :hints (("Goal" :use ((:instance trunc-split
                                    (n (1+ (expo x)))
                                    (m (1+ n))
                                    (k n)))
            :in-theory (enable trunc-rem bitn)))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-5-2
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (equal (bitn x (+ (expo x) (* -1 n)))
                        0))
            (equal (+ x (expt 2 (+ (expo x) (* -1 n))))
                   (+ (trunc x n)
                      (expt 2 (+ (expo x) (* -1 n)))
                      (trunc-rem x (1+ n)))))
   :hints (("Goal" :use (roundup-thm-2-5-1)
            :expand ((expt 2 (+ 1 (expo x) (* -1 n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-5-3
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x)))
            (< (+ (expt 2 (+ (expo x) (* -1 n)))
                  (trunc-rem x (1+ n)))
               (expt 2 (+ 1 (expo x) (* -1 n)))))
   :hints (("Goal" :expand ((expt 2 (+ 1 (expo x) (* -1 n))))
            :use ((:instance trunc-rem-small (n (1+ n))))))
   :rule-classes nil))

(local
 (defthm roundup-thm-2-5
   (implies (and (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x))
                 (not (equal (bitn x (+ (expo x) (* -1 n)))
                             1)))
            (equal (trunc (+ x (expt 2 (+ (expo x) (* -1 n))))
                          n)
                   (trunc x n)))
   :hints (("Goal" :use ((:instance plus-trunc-corollary
                                    (x (trunc x n))
                                    (y (+ (trunc-rem x (+ 1 n))
                                          (expt 2 (+ (expo x) (* -1 n))))))
                         roundup-thm-2-5-2
                         roundup-thm-2-5-3)
            :in-theory (disable fp+ plus-trunc-corollary)))))

(local
 (defthm roundup-thm-2-6
   (implies (and (exactp x n)
                 (integerp n)
                 (< 1 n)
                 (integerp x)
                 (< 0 x)
                 (<= n (expo x)))
            (equal (bitn x (+ (expo x) (* -1 n)))
                   0))
   :hints (("Goal" :use ((:instance exact-bits-1
                                    (n (1+ (expo x)))
                                    (k (- (1+ (expo x)) n)))
                         (:instance exact-bits-3
                                    (k (- (1+ (expo x)) n))))))))

(local
 (defthm roundup-thm-2
   (implies (and (eq mode 'near)
                 (integerp n)
                 (> n 1)
                 (integerp x)
                 (> x 0)
                 (>= (expo x) n))
            (= (rnd x mode n)
               (if (roundup x mode n)
                   (fp+ (trunc x n) n)
                 (trunc x n))))
   :hints (("Goal"  :in-theory (enable rnd)
            :use (near-exactp-a rnd-const-thm-1)))
   :rule-classes ()))

(local
 (defthm roundup-thm-3
   (implies (and (eq mode 'near+)
                 (integerp n)
                 (> n 1)
                 (integerp x)
                 (> x 0)
                 (>= (expo x) n))
            (= (rnd x mode n)
               (if (roundup x mode n)
                   (fp+ (trunc x n) n)
                 (trunc x n))))
   :hints (("Goal"  :in-theory (enable rnd)
            :use near+trunc))
   :rule-classes ()))

(defthm roundup-thm
    (implies (and (rounding-mode-p mode)
		  (integerp n)
		  (> n 1)
		  (integerp x)
		  (> x 0)
		  (>= (expo x) n))
	     (= (rnd x mode n)
                (if (roundup x mode n)
                    (fp+ (trunc x n) n)
                  (trunc x n))))
  :hints (("Goal"  :in-theory (enable rounding-mode-p)
           :use (roundup-thm-1
                 roundup-thm-2
                 roundup-thm-3)))
  :rule-classes ())

;rephrase?
(defthmd rnd-sticky
  (implies (and (> n (1+ k))
                (rounding-mode-p mode)
                (rationalp x) (> x 0)
                (integerp k) (> k 0)
                (integerp n) )
           (equal (rnd (sticky x n) mode k)
                  (rnd x mode k)))
  :hints (("Goal" :in-theory (enable rnd minf inf)
           :use (sticky-pos
                 (:instance trunc-sticky (m k))
                 (:instance away-sticky (m k))
                 (:instance near-sticky (m k))
                 (:instance near+-sticky (m k))))))

(defthm rnd-shift
  (implies (and (rationalp x)
                (integerp n)
                (rounding-mode-p mode)
                (integerp k))
           (= (rnd (* x (expt 2 k)) mode n)
              (* (rnd x mode n) (expt 2 k))))
  :rule-classes ()
  :hints (("goal" :in-theory (enable rnd IEEE-MODE-P
                                     rounding-mode-p)
           :use (trunc-shift
                 away-shift
                 near-shift
                 near+-shift
                 inf-shift
                 minf-shift
                 ))))
;elim <-- why?
(defthm expo-rnd
    (implies (and (rationalp x)
		  (not (= x 0))
		  (integerp n)
		  (> n 0)
		  (rounding-mode-p mode)
		  (not (= (abs (rnd x mode n))
			  (expt 2 (1+ (expo x))))))
	     (= (expo (rnd x mode n))
		(expo x)))
  :rule-classes ()
  :hints (("goal" :in-theory (enable rounding-mode-p
                                     ieee-mode-p near near+ rnd minf inf)
		  :use (expo-trunc expo-away))))

;better rule-classes?
(defthm rnd-pos
  (implies (and (< 0 x)
                (rationalp x)
                (integerp n)
                (> n 0)
                (rounding-mode-p mode))
           (> (rnd x mode n) 0))
  :rule-classes (:type-prescription)
  :hints (("goal" :in-theory (enable rounding-mode-p ieee-mode-p near rnd inf minf)
           :use ())))

(defthm rnd-0
  (equal (rnd 0 mode n)
         0)
  :hints (("Goal" :in-theory (enable rnd rounding-mode-p ieee-mode-p inf minf)
           :use (trunc-0 away-0))))

;better rule-classes?
(defthm rnd-neg
  (implies (and (< x 0)
                (rationalp x)
                (integerp n)
                (> n 0)
                (rounding-mode-p mode))
           (< (rnd x mode n) 0))
  :rule-classes (:type-prescription)
  :hints (("Goal" :in-theory (enable rnd rounding-mode-p ieee-mode-p inf minf)
           :use (
                 near-neg))))

;would like to not open minf, inf here?
(defthm rnd-non-pos
  (implies (<= x 0)
           (<= (rnd x mode n) 0))
  :hints (("goal" :in-theory (enable rnd near+ inf minf)))
  :rule-classes (:rewrite :type-prescription :linear))

;would like to not open minf, inf here?
;add to lib?
(defthm rnd-non-neg
  (implies (<= 0 x)
           (<= 0 (rnd x mode n)))
  :hints (("goal" :in-theory (enable rnd near+ inf minf)))
  :rule-classes (:rewrite :type-prescription :linear))

(defthm sgn-rnd
  (implies (and; (rationalp x)
                (rounding-mode-p mode)
                (integerp n)
                (> n 0)
                )
           (equal (sgn (rnd x mode n))
                  (sgn x)))
  :hints (("Goal" :in-theory (enable ieee-mode-p rounding-mode-p rnd near+ inf minf)
           :use (sgn-trunc
                 sgn-away
                 sgn-near-2))))

;enable?
(defthmd rnd-exactp-a
  (implies (and (rationalp x)
                (rounding-mode-p mode)
                (integerp n)
                (> n 0))
           (equal (equal x (rnd x mode n))
                  (exactp x n)))
  :hints (("Goal" :in-theory (enable ieee-mode-p rounding-mode-p rnd near+ minf inf)
           :use (near-exactp-a
                 trunc-exactp-a
                 away-exactp-a))))

(defthm rnd-exactp-b
    (implies (< 0 n)
	     (exactp (rnd x mode n) n))
  :hints (("Goal" :in-theory (enable rnd near+ minf inf))))

(defthm rnd-exactp-c
    (implies (and (rationalp x)
		  (> x 0)
		  (rounding-mode-p mode)
		  (integerp n)
		  (> n 0)
		  (rationalp a)
		  (exactp a n)
		  (>= a x))
	     (>= a (rnd x mode n)))
  :hints (("Goal" :in-theory (enable ieee-mode-p rnd minf inf)
		  :use (near-exactp-c
			away-exactp-c
			trunc-upper-pos))))

(defthm rnd-exactp-d
    (implies (and (rationalp x)
		  (> x 0)
		  (rounding-mode-p mode)
		  (integerp n)
		  (> n 0)
		  (rationalp a)
		  (exactp a n)
		  (<= a x))
	     (<= a (rnd x mode n)))
  :hints (("Goal" :in-theory (enable ieee-mode-p rounding-mode-p rnd minf inf)
		  :use (near-exactp-c
			trunc-exactp-c
			away-lower-pos))))


(defthm rnd<=away
    (implies (and (rationalp x)
		  (> x 0)
		  (rounding-mode-p mode)
		  (integerp n)
		  (> n 0))
	     (<= (rnd x mode n) (away x n)))
  :rule-classes ()
  :hints (("Goal" :in-theory (enable ieee-mode-p rnd minf inf)
		  :use (trunc-upper-pos
			away-lower-pos
			near-choice))))

(defthm rnd>=trunc
    (implies (and (rationalp x)
		  (> x 0)
		  (rounding-mode-p mode)
		  (integerp n)
		  (> n 0))
	     (>= (rnd x mode n) (trunc x n)))
  :rule-classes ()
  :hints (("Goal" :in-theory (enable ieee-mode-p rounding-mode-p rnd minf inf)
		  :use (trunc-upper-pos
			away-lower-pos
			near-choice))))

(defthm rnd-monotone
    (implies (and (<= x y)
                  (rationalp x)
		  (rationalp y)
		  (< 0 x)
		  (rounding-mode-p mode)
		  (integerp n)
		  (> n 0))
	     (<= (rnd x mode n) (rnd y mode n)))
  :hints (("Goal" :in-theory (enable ieee-mode-p rnd minf inf)
		  :use (trunc-monotone
			away-monotone
			near-monotone))))

(defthm exactp-rnd
  (implies (and (rationalp x)
                (rounding-mode-p mode)
                (integerp n)
                (> n 0))
           (exactp (rnd x mode n) n))
  :hints (("Goal" :in-theory (enable ieee-mode-p rounding-mode-p rnd inf minf))))

(defthm rnd-choice
  (implies (rounding-mode-p mode)
           (or (equal (rnd x mode n) (rnd x 'away n))
               (equal (rnd x mode n) (rnd x 'trunc n))))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable rnd near+ inf minf near rounding-mode-p ieee-mode-p)
                              '(re evenp))))
  :rule-classes nil)

(defthm ieee-mode-p-flip
    (implies (ieee-mode-p m)
	     (ieee-mode-p (flip m)))
    :hints (("Goal" :in-theory (enable ieee-mode-p flip))))

(defthm rounding-mode-p-flip
    (implies (rounding-mode-p m)
	     (rounding-mode-p (flip m)))
    :hints (("Goal" :in-theory (enable ieee-mode-p flip))))


(defthm expo-rnd-bnd
    (implies (and (rationalp x)
		  (integerp n)
		  (> n 0)
		  (rounding-mode-p mode))
	     (>= (expo (rnd x mode n))
		 (expo x)))
  :rule-classes ()
  :hints (("Goal" :in-theory (disable expo-minus)
           :use (expo-rnd
			(:instance expo-minus (x (rnd x mode n)))))))

(defthm plus-inf
    (implies (and (rationalp x)
		  (>= x 0)
		  (rationalp y)
		  (>= y 0)
		  (integerp k)
		  (exactp x (+ k (- (expo x) (expo y)))))
	     (= (+ x (inf y k))
		(inf (+ x y) (+ k (- (expo (+ x y)) (expo y))))))
  :rule-classes ()
  :hints (("goal" :in-theory (enable inf)
           :use plus-away)))

(defthm plus-minf
    (implies (and (rationalp x)
		  (>= x 0)
		  (rationalp y)
		  (>= y 0)
		  (integerp k)
		  (exactp x (+ k (- (expo x) (expo y)))))
	     (= (+ x (minf y k))
		(minf (+ x y) (+ k (- (expo (+ x y)) (expo y))))))
  :rule-classes ()
  :hints (("goal" :in-theory (enable minf)
           :use plus-trunc)))

;make alt form too?
; add to lib?
(defthm plus-rnd
  (implies (and (rationalp x)
                (>= x 0)
                (rationalp y)
                (>= y 0)
                (integerp k)
                (exactp x (+ -1 k (- (expo x) (expo y))))
                (rounding-mode-p mode))
           (= (+ x (rnd y mode k))
              (rnd (+ x y)
                   mode
                   (+ k (- (expo (+ x y)) (expo y))))))
  :rule-classes nil
  :hints (("Goal" :in-theory (enable rnd ieee-mode-p ROUNDING-MODE-P)
           :use (plus-near
                 plus-near+
                 plus-away
                 plus-trunc
                 plus-minf
                 plus-inf
                 (:instance exactp-<= (m (+ -1 k (- (expo x) (expo y))))
                            (n (+  k (- (expo x) (expo y)))))))))

(defthm rnd-rarely-zero
  (implies (and (rationalp x)
                (integerp k)
                (case-split (< 0 k))
                (case-split (rounding-mode-p mode)))
           (equal (equal (rnd x mode k) 0)
                  (equal x 0)
           ))
  :hints (("Goal" :in-theory (enable rnd near+ minf inf near ROUNDING-MODE-P ieee-mode-p))))

;add to lib?
(defthm flip-flip
  (equal (flip (flip mode))
         mode)
  :hints (("Goal" :in-theory (enable flip))))

;add to lib?
(defthm inf-lower-bound
  (implies (and (rationalp x)
                (integerp n))
           (>= (inf x n) x))
  :hints (("Goal" :in-theory (enable inf)
           :use trunc-upper-bound))
  :rule-classes (:rewrite :linear))

;add to lib?
(defthm minf-upper-bound
  (implies (and (rationalp x)
                (integerp n))
           (<= (minf x n) x))
  :hints (("Goal" :in-theory (set-difference-theories
                              (enable minf)
                              '(abs-away))
           :use away-lower-bound))
  :rule-classes (:rewrite :linear))


;add to lib?
(defthm rnd-diff
  (implies (and (rationalp x)
                (integerp n)
                (> n 0)
                (rounding-mode-p mode))
           (< (abs (- x (rnd x mode n))) (expt 2 (- (1+ (expo x)) n))))
  :hints (("Goal" :in-theory (enable rnd near near+ inf minf ieee-mode-p rounding-mode-p)
           :use (trunc-diff away-diff))))