1
0
Fork 0
forked from AbleOS/ableos
ableos_time/sysdata/libraries/stn/src/math.hb

55 lines
1.7 KiB
Plaintext
Raw Normal View History

2024-10-12 15:39:09 -05:00
abs := fn($Expr: type, x: Expr): Expr {
mask := x >> @intcast(@sizeof(Expr) - 1)
2024-08-20 07:03:39 -05:00
return (x ^ mask) - mask
}
2024-10-12 15:39:09 -05:00
min := fn($Expr: type, a: Expr, b: Expr): Expr {
c := a - b
2024-10-12 15:39:09 -05:00
return b + (c & c >> @intcast(@sizeof(Expr) - 1))
}
2024-10-12 15:39:09 -05:00
max := fn($Expr: type, a: Expr, b: Expr): Expr {
c := a - b
2024-10-12 15:39:09 -05:00
return a - (c & c >> @intcast(@sizeof(Expr) - 1))
}
signum := fn($Expr: type, x: Expr): int {
if x > @as(Expr, @intcast(0)) {
return 1
} else if x < @as(Expr, @intcast(0)) {
return -1
} else {
return 0
}
}
signincl := fn($Expr: type, x: Expr): int {
if x > @as(Expr, @intcast(0)) {
return 1
}
return -1
}
2024-10-12 15:39:09 -05:00
Vec2 := fn($Expr: type): type {
return struct {x: Expr, y: Expr}
}
SIN_TABLE := [int].(0, 174, 348, 523, 697, 871, 1045, 1218, 1391, 1564, 1736, 1908, 2079, 2249, 2419, 2588, 2756, 2923, 3090, 3255, 3420, 3583, 3746, 3907, 4067, 4226, 4384, 4540, 4695, 4848, 5000, 5150, 5299, 5446, 5591, 5735, 5877, 6018, 6156, 6293, 6427, 6560, 6691, 6819, 6946, 7071, 7193, 7313, 7431, 7547, 7660, 7771, 7880, 7986, 8090, 8191, 8290, 8386, 8480, 8571, 8660, 8746, 8829, 8910, 8987, 9063, 9135, 9205, 9271, 9335, 9396, 9455, 9510, 9563, 9612, 9659, 9702, 9743, 9781, 9816, 9848, 9877, 9902, 9925, 9945, 9961, 9975, 9986, 9993, 9998, 10000)
sin_i := fn(theta_deg: int, amplitude: int): int {
theta := theta_deg % 360
if theta < 0 {
theta += 360
}
quadrant := theta / 90
theta = theta % 90
sign := 1 - ((quadrant & 2) >> 1) * 2
complement := quadrant & 1
index := theta * (1 - complement) + (90 - theta) * complement
sin_value := SIN_TABLE[index] * sign
return (sin_value * amplitude + 5000) / 10000
}
cos_i := fn(theta_deg: int, amplitude: int): int {
return @inline(sin_i, theta_deg + 90, amplitude)
2024-08-20 07:03:39 -05:00
}