-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection_iterator-support.dats.m4
151 lines (133 loc) · 3.49 KB
/
bisection_iterator-support.dats.m4
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
(*
Copyright © 2023 Barry Schwartz
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License, as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License
along with this program. If not, see
<https://www.gnu.org/licenses/>.
*)
include(`common-macros.m4')m4_include(`ats2-xprelude-macros.m4')
(*------------------------------------------------------------------*)
#define ATS_DYNLOADFLAG 0
#define ATS_PACKNAME "ats2-xprelude.bisection_iterator"
#include "share/atspre_staload.hats"
#include "xprelude/HATS/xprelude.hats"
staload "xprelude/SATS/exrat.sats"
staload _ = "xprelude/DATS/exrat.dats"
staload "xprelude/SATS/bisection_iterator.sats"
(*------------------------------------------------------------------*)
typedef _bisection_iterator =
'{
numerator = exrat,
denominator = exrat,
backwards = bool
}
assume bisection_iterator = _bisection_iterator
implement
bisection_iterator_forwards_make () =
'{
numerator = g0i2f 0,
denominator = g0i2f 1,
backwards = false
}
implement
bisection_iterator_backwards_make () =
'{
numerator = g0i2f 0,
denominator = g0i2f 1,
backwards = true
}
implement
bisection_iterator_done iter =
let
val '{
numerator = numer,
denominator = denom,
backwards = _
} = iter
in
(numer = denom) && iseqz (pred denom)
end
implement
bisection_iterator_interval iter =
if bisection_iterator_done iter then
let
val msg = "bisection_iterator_interval:past_end"
in
$raise IllegalArgExn msg
end
else
let
val '{
numerator = numer,
denominator = denom,
backwards = backwards
} = iter
in
if ~backwards then
@(numer / denom, (succ numer) / denom)
else
let
val top = denom - numer
in
@((pred top) / denom, top / denom)
end
end
implement
bisection_iterator_bisect iter =
if bisection_iterator_done iter then
let
val msg = "bisection_iterator_bisect:past_end"
in
$raise IllegalArgExn msg
end
else
let
val '{
numerator = numer,
denominator = denom,
backwards = backwards
} = iter
in
'{
numerator = mul_2exp (numer, 1),
denominator = mul_2exp (denom, 1),
backwards = backwards
}
end
implement
bisection_iterator_next iter =
if bisection_iterator_done iter then
let
val msg = "bisection_iterator_next:past_end"
in
$raise IllegalArgExn msg
end
else
let
val '{
numerator = numer,
denominator = denom,
backwards = backwards
} = iter
val numer = succ numer
val shift_amount =
(g0u2i (pred (exrat_numerator_ffs numer))) : intmax
in
'{
numerator = div_2exp (numer, shift_amount),
denominator = div_2exp (denom, shift_amount),
backwards = backwards
}
end
(*------------------------------------------------------------------*)
dnl
dnl local variables:
dnl mode: ATS
dnl end: