Repository navigation
Expand file tree
/
Copy pathstdlib.lyte
More file actions
268 lines (232 loc) · 6.29 KB
/
Copy pathstdlib.lyte
File metadata and controls
268 lines (232 loc) · 6.29 KB
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
// Lyte standard library
// --- Math utility functions ---
fract(x: f32) -> f32 { x - floor(x) }
fract(x: f64) -> f64 { x - floor(x) }
mod(x: f32, y: f32) -> f32 { x - y * floor(x / y) }
mod(x: f64, y: f64) -> f64 { x - y * floor(x / y) }
// Float `%`. No backend has a primitive float remainder instruction, so the
// checker resolves `x % y` on floats to these overloads. Semantics match C's
// fmod and Rust's `%`: the result takes the sign of x. Use mod() above for
// floored modulo, whose result takes the sign of y. Unlike a true fmod, this
// loses precision when x / y is large enough to round.
__mod(x: f32, y: f32) -> f32 {
let q = x / y
var t = floor(q)
if q < 0.0 { t = ceil(q) }
x - y * t
}
__mod(x: f64, y: f64) -> f64 {
let q = x / y
var t = floor(q)
if q < 0.0f64 { t = ceil(q) }
x - y * t
}
clamp(x: f32, lo: f32, hi: f32) -> f32 { min(max(x, lo), hi) }
clamp(x: f64, lo: f64, hi: f64) -> f64 { min(max(x, lo), hi) }
step(edge: f32, x: f32) -> f32 {
if x < edge { return 0.0 }
return 1.0
}
step(edge: f64, x: f64) -> f64 {
if x < edge { return 0.0f64 }
return 1.0f64
}
smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
var t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0)
t * t * (3.0 - 2.0 * t)
}
smoothstep(edge0: f64, edge1: f64, x: f64) -> f64 {
var t = clamp((x - edge0) / (edge1 - edge0), 0.0f64, 1.0f64)
t * t * (3.0f64 - 2.0f64 * t)
}
mix(a: f32, b: f32, t: f32) -> f32 { a + t * (b - a) }
mix(a: f64, b: f64, t: f64) -> f64 { a + t * (b - a) }
// --- String functions ---
println(s: [i8]) {
for i in 0 .. s.len {
if s[i] != 0 as i8 {
putc(s[i] as i32)
}
}
putc(10)
}
strcpy(dst: [i8], src: [i8]) {
for i in 0 .. dst.len {
if i < src.len {
dst[i] = src[i]
} else {
dst[i] = 0 as i8
}
}
}
strlen(s: [i8]) -> i32 {
var n = 0
for i in 0 .. s.len {
if s[i] != 0 as i8 {
n = n + 1
}
}
return n
}
itoa(dst: [i8], n: i32) {
var num = n
if n < 0 {
num = 0 - n
}
var div = 1
var temp = num / 10
while temp > 0 {
div = div * 10
temp = temp / 10
}
var pos = 0
if n < 0 {
pos = 1
}
for i in 0 .. dst.len {
if i == 0 && n < 0 {
dst[i] = 45 as i8
} else if i >= pos && div > 0 {
dst[i] = ((num / div) % 10 + 48) as i8
div = div / 10
} else {
dst[i] = 0 as i8
}
}
}
ftoa(dst: [i8], n: f32) {
var num = n
if n < 0.0 {
num = 0.0 - n
}
var int_part = num as i32
var frac_part = ((num - (int_part as f32)) * 1000000.0 + 0.5) as i32
// count digits in integer part
var div = 1
var temp = int_part / 10
while temp > 0 {
div = div * 10
temp = temp / 10
}
// phase 0: sign, phase 1: integer digits, phase 2: dot, phase 3: frac digits, phase 4: null
var phase = 1
if n < 0.0 {
phase = 0
}
var frac_div = 100000
for i in 0 .. dst.len {
if phase == 0 {
dst[i] = 45 as i8
phase = 1
} else if phase == 1 {
// phase leaves 1 as soon as div reaches 0, and div starts at 1.
assume div != 0
dst[i] = ((int_part / div) % 10 + 48) as i8
div = div / 10
if div == 0 {
phase = 2
}
} else if phase == 2 {
dst[i] = 46 as i8
phase = 3
} else if phase == 3 {
// phase leaves 3 as soon as frac_div reaches 0.
assume frac_div != 0
dst[i] = ((frac_part / frac_div) % 10 + 48) as i8
frac_div = frac_div / 10
if frac_div == 0 {
phase = 4
}
} else {
dst[i] = 0 as i8
}
}
}
// --- Comparison interface and implementations ---
interface Compare<A> {
cmp(lhs: A, rhs: A) -> i32
}
cmp(lhs: i32, rhs: i32) -> i32 { lhs - rhs }
cmp(lhs: f32, rhs: f32) -> i32 {
if lhs < rhs { return 0 - 1 }
if lhs > rhs { return 1 }
return 0
}
// --- Sorting (in-place via slices, generic) ---
// Push larger half, loop on smaller: caps stack depth at log2(a.len).
quicksort<T>(a: [T]) where Compare<T> {
var lo_stk: [i32; 64]
var hi_stk: [i32; 64]
lo_stk[0] = 0
hi_stk[0] = a.len - 1
var top = 1
while top > 0 {
top = top - 1
assume top >= 0 && top < 64
var cur_lo = lo_stk[top]
var cur_hi = hi_stk[top]
while cur_lo < cur_hi {
assume cur_lo >= 0
assume cur_hi >= 0
assume cur_hi < a.len
let pivot_val = a[cur_hi]
var i = cur_lo
for j in cur_lo .. cur_hi {
if cmp(a[j], pivot_val) < 0 {
var tmp = a[i]
a[i] = a[j]
a[j] = tmp
i = i + 1
}
}
var tmp = a[i]
a[i] = a[cur_hi]
a[cur_hi] = tmp
var pivot = i
let left_hi = pivot - 1
let right_lo = pivot + 1
let left_size = left_hi - cur_lo
let right_size = cur_hi - right_lo
assume top >= 0 && top < 64
if left_size > right_size {
lo_stk[top] = cur_lo
hi_stk[top] = left_hi
top = top + 1
cur_lo = right_lo
} else {
lo_stk[top] = right_lo
hi_stk[top] = cur_hi
top = top + 1
cur_hi = left_hi
}
}
}
}
sort<T>(a: [T]) where Compare<T> {
quicksort(a)
}
// --- String functions ---
strcat(dst: [i8], src: [i8]) {
var j = 0
var copying = false
for i in 0 .. dst.len {
if copying {
if j >= 0 && j < src.len {
if src[j] != 0 as i8 {
dst[i] = src[j]
j = j + 1
}
}
} else {
if dst[i] == 0 as i8 {
copying = true
if j >= 0 && j < src.len {
if src[j] != 0 as i8 {
dst[i] = src[j]
j = j + 1
}
}
}
}
}
}