2022-04-23 00:15:51 +00:00
|
|
|
; Prog8 definitions for floating point handling on the VirtualMachine
|
|
|
|
|
2023-12-26 22:37:59 +00:00
|
|
|
%option enable_floats, ignore_unused
|
2022-04-23 00:15:51 +00:00
|
|
|
|
2024-11-21 23:44:00 +00:00
|
|
|
sys {
|
|
|
|
%option merge ; add some constants to sys
|
|
|
|
|
|
|
|
const float MAX_FLOAT = 1.7976931348623157e+308
|
|
|
|
const float MIN_FLOAT = -1.7976931348623157e+308
|
|
|
|
}
|
|
|
|
|
|
|
|
txt {
|
|
|
|
%option merge ; add function to txt
|
|
|
|
|
|
|
|
alias print_f = floats.print
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-23 00:15:51 +00:00
|
|
|
floats {
|
|
|
|
|
2024-11-21 21:12:32 +00:00
|
|
|
const float π = 3.141592653589793
|
|
|
|
const float PI = π
|
|
|
|
const float TWOPI = 2*π
|
|
|
|
const float E = 2.718281828459045
|
|
|
|
const float EPSILON = 4.9E-324
|
2023-12-05 00:34:41 +00:00
|
|
|
|
2022-04-23 00:15:51 +00:00
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub print(float value) {
|
2023-12-07 22:10:27 +00:00
|
|
|
; ---- prints the floating point value (without a newline and no leading spaces).
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2023-12-29 02:12:44 +00:00
|
|
|
loadm.f fr65535,floats.print.value
|
2024-07-06 16:49:03 +00:00
|
|
|
syscall 15 (fr65535.f)
|
2022-04-29 22:10:07 +00:00
|
|
|
return
|
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub tostr(float value) -> str {
|
2023-12-07 22:10:27 +00:00
|
|
|
; ---- converts the floating point value to a string (no leading spaces)
|
|
|
|
str @shared buffer=" "*20
|
|
|
|
%ir {{
|
2023-12-29 02:12:44 +00:00
|
|
|
load.w r65535,floats.tostr.buffer
|
|
|
|
loadm.f fr65535,floats.tostr.value
|
2024-07-06 16:49:03 +00:00
|
|
|
syscall 34 (r65535.w, fr65535.f)
|
2023-12-29 02:12:44 +00:00
|
|
|
load.w r0,floats.tostr.buffer
|
2023-12-07 22:10:27 +00:00
|
|
|
returnr.w r0
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub parse(str value) -> float {
|
2023-08-16 12:47:20 +00:00
|
|
|
; -- parse a string value of a number to float
|
|
|
|
%ir {{
|
2023-12-29 02:12:44 +00:00
|
|
|
loadm.w r65535,floats.parse.value
|
2024-07-06 16:49:03 +00:00
|
|
|
syscall 32 (r65535.w): fr0.f
|
2023-08-16 12:47:20 +00:00
|
|
|
returnr.f fr0
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-04-23 00:15:51 +00:00
|
|
|
sub pow(float value, float power) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.pow.value
|
|
|
|
loadm.f fr1,floats.pow.power
|
2022-05-11 20:09:46 +00:00
|
|
|
fpow.f fr0,fr1
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub sin(float angle) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.sin.angle
|
2022-04-30 22:09:03 +00:00
|
|
|
fsin.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-29 22:10:07 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cos(float angle) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.cos.angle
|
2022-04-30 22:09:03 +00:00
|
|
|
fcos.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub tan(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.tan.value
|
2022-04-30 22:09:03 +00:00
|
|
|
ftan.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub atan(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.atan.value
|
2022-04-30 22:09:03 +00:00
|
|
|
fatan.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 18:03:36 +00:00
|
|
|
; two-argument arctangent that returns an angle in the correct quadrant
|
|
|
|
; for the signs of x and y, normalized to the range [0, 2π]
|
|
|
|
sub atan2(float y, float x) -> float {
|
|
|
|
float atn = atan(y / x)
|
|
|
|
if x < 0 atn += π
|
|
|
|
if atn < 0 atn += 2*π
|
|
|
|
return atn
|
|
|
|
}
|
|
|
|
|
|
|
|
; reciprocal functions
|
|
|
|
sub secant(float value) -> float { return 1.0 / cos(value) }
|
|
|
|
sub csc(float value) -> float { return 1.0 / sin(value) }
|
|
|
|
sub cot(float value) -> float { return 1.0 / tan(value) }
|
|
|
|
|
2022-04-23 00:15:51 +00:00
|
|
|
sub ln(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.ln.value
|
2022-04-30 22:09:03 +00:00
|
|
|
fln.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub log2(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.log2.value
|
2022-05-02 18:16:45 +00:00
|
|
|
flog.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rad(float angle) -> float {
|
|
|
|
; -- convert degrees to radians (d * pi / 180)
|
2022-04-30 22:09:03 +00:00
|
|
|
return angle * PI / 180.0
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub deg(float angle) -> float {
|
|
|
|
; -- convert radians to degrees (d * (1/ pi * 180))
|
2022-04-30 22:09:03 +00:00
|
|
|
return angle * 180.0 / PI
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub round(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.round.value
|
2022-04-30 22:09:03 +00:00
|
|
|
fround.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub floor(float value) -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.floor.value
|
2022-04-30 22:09:03 +00:00
|
|
|
ffloor.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub ceil(float value) -> float {
|
|
|
|
; -- ceil: tr = int(f); if tr==f -> return else return tr+1
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2022-09-15 21:08:41 +00:00
|
|
|
loadm.f fr0,floats.ceil.value
|
2022-04-30 22:09:03 +00:00
|
|
|
fceil.f fr0,fr0
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub rnd() -> float {
|
2022-09-30 12:05:11 +00:00
|
|
|
%ir {{
|
2024-07-06 16:49:03 +00:00
|
|
|
syscall 22 () : fr0.f
|
2023-04-11 20:28:19 +00:00
|
|
|
returnr.f fr0
|
2022-04-30 22:09:03 +00:00
|
|
|
}}
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|
2022-10-22 11:33:35 +00:00
|
|
|
|
2023-12-29 02:12:44 +00:00
|
|
|
sub rndseed(float seed) {
|
2022-10-22 11:33:35 +00:00
|
|
|
%ir {{
|
2023-12-29 02:12:44 +00:00
|
|
|
loadm.f fr65535,floats.rndseed.seed
|
2024-07-06 16:49:03 +00:00
|
|
|
syscall 19 (fr65535.f)
|
2023-05-12 21:26:36 +00:00
|
|
|
return
|
2022-10-22 11:33:35 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-05-17 21:03:59 +00:00
|
|
|
|
2023-05-17 21:18:14 +00:00
|
|
|
sub minf(float f1, float f2) -> float {
|
|
|
|
if f1<f2
|
|
|
|
return f1
|
|
|
|
return f2
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub maxf(float f1, float f2) -> float {
|
|
|
|
if f1>f2
|
|
|
|
return f1
|
|
|
|
return f2
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-17 21:03:59 +00:00
|
|
|
sub clampf(float value, float minimum, float maximum) -> float {
|
|
|
|
if value<minimum
|
|
|
|
value=minimum
|
|
|
|
if value<maximum
|
|
|
|
return value
|
|
|
|
return maximum
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:54:35 +00:00
|
|
|
sub normalize(float value) -> float {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-12-26 14:19:49 +00:00
|
|
|
sub push(float value) {
|
2023-12-26 14:30:18 +00:00
|
|
|
; note: this *should* be inlined, however since the VM has separate program counter and value stacks, this also works
|
2023-12-26 14:19:49 +00:00
|
|
|
%ir {{
|
|
|
|
loadm.f fr65535,floats.push.value
|
|
|
|
push.f fr65535
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pop() -> float {
|
2023-12-26 14:30:18 +00:00
|
|
|
; note: this *should* be inlined, however since the VM has separate program counter and value stacks, this also works
|
2023-12-26 14:19:49 +00:00
|
|
|
%ir {{
|
|
|
|
pop.f fr65535
|
|
|
|
returnr.f fr65535
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2024-11-01 01:05:48 +00:00
|
|
|
sub lerp(float v0, float v1, float t) -> float {
|
|
|
|
; Linear interpolation (LERP)
|
|
|
|
; Precise method, which guarantees v = v1 when t = 1.
|
|
|
|
; returns an interpolation between two inputs (v0, v1) for a parameter t in the closed unit interval [0, 1]
|
|
|
|
return (1 - t) * v0 + t * v1
|
|
|
|
}
|
|
|
|
|
|
|
|
sub lerp_fast(float v0, float v1, float t) -> float {
|
|
|
|
; Linear interpolation (LERP)
|
|
|
|
; Imprecise (but slightly faster) method, which does not guarantee v = v1 when t = 1
|
|
|
|
; returns an interpolation between two inputs (v0, v1) for a parameter t in the closed unit interval [0, 1]
|
|
|
|
return v0 + t * (v1 - v0)
|
|
|
|
}
|
|
|
|
|
2022-04-23 00:15:51 +00:00
|
|
|
}
|