fa/std/native_math.fa
Ari Archer a699dc4f05
Imrprove math lib
Signed-off-by: Ari Archer <ari.web.xyz@gmail.com>
2022-07-20 07:27:03 +03:00

134 lines
2.2 KiB
Text

--<
WARNING: This is an old times deprecated library, use math.fa
Naming:
(nothing) -- Signed
u -- Unsigned
a -- Absolute
s -- Semi-(un)signed
>--
--<
Semi-unsigned modulo (%) operator
Takes:
INT: n >0
INT: d
Returns:
INT: result
>--
fun smod [ int int ] int eo
as __mod_n as __mod_d
label __calculate_mod
@__mod_n deref 0 grt if
@__mod_n deref @__mod_d deref add
ret
end
@__mod_n @__mod_d deref @__mod_n deref sub point
goto __calculate_mod
end
--<
Unsigned absolute division by 10
SRC=C:
unsigned q, r;
q = (n >> 1) + (n >> 2);
q = q + (q >> 4);
q = q + (q >> 8);
q = q + (q >> 16);
q = q >> 3;
r = n - (((q << 2) + q) << 1);
return q + (r > 9);
Takes:
INT: Input
Returns:
INT: Input / 10
>--
fun uadiv10 [ int ] int eo
as __div10_n
0 as __div10_q
@__div10_q
1 @__div10_n deref shr
2 @__div10_n deref shr
add
point
@__div10_q
@__div10_q deref
4 @__div10_q deref shr
add
point
@__div10_q
@__div10_q deref
8 @__div10_q deref shr
add
point
@__div10_q
@__div10_q deref
16 @__div10_q deref shr
add
point
@__div10_q
3 @__div10_q deref shr
point
-- 2 @__div10_q deref shl as ___r1
-- @___r1 deref @__div10_q deref add as ___r2
-- 1 @___r2 deref shl as ___r3
-- @___r3 deref @__div10_n deref sub as ___r4
1
2 @__div10_q deref shl
@__div10_q deref add
shl
@__div10_n deref sub
as __div10_r
@__div10_q deref
9
@__div10_r deref
grt add
end
--<
Unsigned absolute division
Takes:
INT: A >=0
INT: B >0
Returns:
INT: A / B
>--
fun uadiv [ int int ] int eo
as __udiv_a as __udiv_b
0 as __udiv_c
label __udiv_resolve
@__udiv_a deref
@__udiv_b deref
grt if
@__udiv_c deref
ret
end
@__udiv_c @__udiv_c deref 1 add point
@__udiv_a @__udiv_b deref @__udiv_a deref sub
point
goto __udiv_resolve
end