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

59 lines
1.2 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}
}
2024-10-26 03:23:28 -05:00
SIN_TABLE := @as([int; 91], @bitcast(@embed("./assets/sin_table")))
2024-10-26 03:23:28 -05:00
sin := fn(theta: int, amplitude: uint): int {
if theta < 0 {
2024-10-26 03:23:28 -05:00
theta += (-theta / 360 + 1) * 360
} else if theta >= 360 {
theta -= theta / 360 * 360
}
quadrant := theta / 90
2024-10-26 03:23:28 -05:00
index := theta % 90
2024-10-26 03:23:28 -05:00
if @as(u8, @intcast(quadrant)) == @as(u8, 1) {
index = 90 - index
}
2024-10-26 03:23:28 -05:00
value := SIN_TABLE[@bitcast(index)]
if quadrant >= 2 {
value = -value
}
2024-10-26 03:23:28 -05:00
return (value * @bitcast(amplitude) + 5000) / 10000
}
2024-10-26 03:23:28 -05:00
cos := fn(theta: int, amplitude: uint): int {
return @inline(sin, theta + 90, amplitude)
2024-08-20 07:03:39 -05:00
}