2022-04-21 22:45:54 +00:00
|
|
|
floats {
|
|
|
|
; the floating point functions shared across compiler targets
|
2023-06-29 22:29:50 +00:00
|
|
|
%option merge, no_symbol_prefixing
|
2022-04-21 22:45:54 +00:00
|
|
|
|
2023-12-07 20:25:03 +00:00
|
|
|
asmsub print_f(float value @FAC1) clobbers(A,X,Y) {
|
2023-12-02 11:36:49 +00:00
|
|
|
; ---- prints the floating point value (without a newline). No leading space (unlike BASIC)!
|
2022-04-21 22:45:54 +00:00
|
|
|
%asm {{
|
2023-12-07 20:25:03 +00:00
|
|
|
jsr str_f
|
2022-04-21 22:45:54 +00:00
|
|
|
ldy #0
|
2023-12-02 11:36:49 +00:00
|
|
|
- lda (P8ZP_SCRATCH_W1),y
|
2023-12-07 20:25:03 +00:00
|
|
|
beq +
|
2023-04-28 21:13:03 +00:00
|
|
|
jsr cbm.CHROUT
|
2023-12-07 20:25:03 +00:00
|
|
|
iny
|
2022-04-21 22:45:54 +00:00
|
|
|
bne -
|
2023-07-15 13:18:26 +00:00
|
|
|
+ rts
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-12-07 20:25:03 +00:00
|
|
|
asmsub str_f(float value @FAC1) clobbers(X) -> str @AY {
|
|
|
|
; ---- converts the floating point value to a string. No leading space!
|
|
|
|
%asm {{
|
|
|
|
jsr FOUT
|
|
|
|
sta P8ZP_SCRATCH_W1
|
|
|
|
sty P8ZP_SCRATCH_W1+1
|
|
|
|
ldy #0
|
|
|
|
lda (P8ZP_SCRATCH_W1),y
|
|
|
|
cmp #' '
|
|
|
|
bne +
|
|
|
|
inc P8ZP_SCRATCH_W1
|
|
|
|
bne +
|
|
|
|
inc P8ZP_SCRATCH_W1+1
|
|
|
|
+ lda P8ZP_SCRATCH_W1
|
|
|
|
ldy P8ZP_SCRATCH_W1+1
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:45:54 +00:00
|
|
|
sub pow(float value, float power) -> float {
|
|
|
|
%asm {{
|
2022-05-04 20:31:45 +00:00
|
|
|
stx P8ZP_SCRATCH_W1
|
|
|
|
sty P8ZP_SCRATCH_W1+1
|
2022-04-21 22:45:54 +00:00
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr floats.CONUPK
|
|
|
|
lda #<power
|
|
|
|
ldy #>power
|
|
|
|
jsr floats.FPWR
|
2022-05-04 20:31:45 +00:00
|
|
|
ldx P8ZP_SCRATCH_W1
|
|
|
|
ldy P8ZP_SCRATCH_W1+1
|
2022-04-21 22:45:54 +00:00
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sin(float angle) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<angle
|
|
|
|
ldy #>angle
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp SIN
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cos(float angle) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<angle
|
|
|
|
ldy #>angle
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp COS
|
2022-04-21 22:45:54 +00:00
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub tan(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp TAN
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub atan(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp ATN
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ln(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp LOG
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub log2(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
|
|
|
jsr LOG
|
|
|
|
jsr MOVEF
|
|
|
|
lda #<FL_LOG2_const
|
|
|
|
ldy #>FL_LOG2_const
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp FDIVT
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rad(float angle) -> float {
|
|
|
|
; -- convert degrees to radians (d * pi / 180)
|
|
|
|
%asm {{
|
|
|
|
lda #<angle
|
|
|
|
ldy #>angle
|
|
|
|
jsr MOVFM
|
|
|
|
lda #<_pi_div_180
|
|
|
|
ldy #>_pi_div_180
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp FMULT
|
2022-04-21 22:45:54 +00:00
|
|
|
_pi_div_180 .byte 123, 14, 250, 53, 18 ; pi / 180
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub deg(float angle) -> float {
|
|
|
|
; -- convert radians to degrees (d * (1/ pi * 180))
|
|
|
|
%asm {{
|
|
|
|
lda #<angle
|
|
|
|
ldy #>angle
|
|
|
|
jsr MOVFM
|
|
|
|
lda #<_one_over_pi_div_180
|
|
|
|
ldy #>_one_over_pi_div_180
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp FMULT
|
2022-04-21 22:45:54 +00:00
|
|
|
rts
|
|
|
|
_one_over_pi_div_180 .byte 134, 101, 46, 224, 211 ; 1 / (pi * 180)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub round(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
|
|
|
jsr FADDH
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp INT
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub floor(float value) -> float {
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp INT
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub ceil(float value) -> float {
|
|
|
|
; -- ceil: tr = int(f); if tr==f -> return else return tr+1
|
|
|
|
%asm {{
|
|
|
|
lda #<value
|
|
|
|
ldy #>value
|
|
|
|
jsr MOVFM
|
|
|
|
ldx #<fmath_float1
|
|
|
|
ldy #>fmath_float1
|
|
|
|
jsr MOVMF
|
|
|
|
jsr INT
|
|
|
|
lda #<fmath_float1
|
|
|
|
ldy #>fmath_float1
|
|
|
|
jsr FCOMP
|
|
|
|
cmp #0
|
|
|
|
beq +
|
|
|
|
lda #<FL_ONE_const
|
|
|
|
ldy #>FL_ONE_const
|
|
|
|
jsr FADD
|
2023-07-15 13:18:26 +00:00
|
|
|
+ rts
|
2022-04-21 22:45:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:41:23 +00:00
|
|
|
sub rndseedf(float seed) {
|
|
|
|
if seed>0
|
|
|
|
seed = -seed ; make sure fp seed is always negative
|
|
|
|
|
|
|
|
%asm {{
|
|
|
|
lda #<seed
|
|
|
|
ldy #>seed
|
|
|
|
jsr MOVFM ; load float into fac1
|
|
|
|
lda #-1
|
2023-07-15 13:18:26 +00:00
|
|
|
jmp floats.RND
|
2022-11-06 21:41:23 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-04-29 15:52:15 +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>maximum
|
|
|
|
value=maximum
|
|
|
|
if value>minimum
|
|
|
|
return value
|
|
|
|
return minimum
|
|
|
|
}
|
|
|
|
|
2023-12-26 14:19:49 +00:00
|
|
|
inline asmsub push(float value @FAC1) {
|
|
|
|
%asm {{
|
|
|
|
jsr floats.pushFAC1
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline asmsub pop() -> float @FAC1 {
|
|
|
|
%asm {{
|
|
|
|
clc
|
|
|
|
jsr floats.popFAC
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:45:54 +00:00
|
|
|
}
|