add math.gcd()

This commit is contained in:
Irmen de Jong
2026-01-22 23:30:00 +01:00
parent 492cd686a0
commit dfad83650d
10 changed files with 115 additions and 12 deletions
@@ -240,6 +240,7 @@ math {
direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte
direction_qd (ubyte quadrant @A, ubyte xdelta @X, ubyte ydelta @Y) -> ubyte @A
direction_sc (byte x1, byte y1, byte x2, byte y2) -> ubyte
gcd (uword a, uword b) -> uword
interpolate (ubyte v, ubyte inputMin, ubyte inputMax, ubyte outputMin, ubyte outputMax) -> ubyte
lerp (ubyte v0, ubyte v1, ubyte t) -> ubyte
lerpw (uword v0, uword v1, uword t) -> uword
@@ -369,6 +369,7 @@ math {
direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte
direction_qd (ubyte quadrant @A, ubyte xdelta @X, ubyte ydelta @Y) -> ubyte @A
direction_sc (byte x1, byte y1, byte x2, byte y2) -> ubyte
gcd (uword a, uword b) -> uword
interpolate (ubyte v, ubyte inputMin, ubyte inputMax, ubyte outputMin, ubyte outputMax) -> ubyte
interpolatef alias for: floats.interpolate
lerp (ubyte v0, ubyte v1, ubyte t) -> ubyte
@@ -545,6 +545,7 @@ math {
direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte
direction_qd (ubyte quadrant @A, ubyte xdelta @X, ubyte ydelta @Y) -> ubyte @A
direction_sc (byte x1, byte y1, byte x2, byte y2) -> ubyte
gcd (uword a, uword b) -> uword
interpolate (ubyte v, ubyte inputMin, ubyte inputMax, ubyte outputMin, ubyte outputMax) -> ubyte
interpolatef alias for: floats.interpolate
lerp (ubyte v0, ubyte v1, ubyte t) -> ubyte
@@ -294,6 +294,7 @@ math {
direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte
direction_qd (ubyte quadrant @A, ubyte xdelta @X, ubyte ydelta @Y) -> ubyte @A
direction_sc (byte x1, byte y1, byte x2, byte y2) -> ubyte
gcd (uword a, uword b) -> uword
interpolate (ubyte v, ubyte inputMin, ubyte inputMax, ubyte outputMin, ubyte outputMax) -> ubyte
interpolatef alias for: floats.interpolate
lerp (ubyte v0, ubyte v1, ubyte t) -> ubyte
@@ -228,6 +228,7 @@ math {
direction (ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte
direction_qd (ubyte quadrant, ubyte xdelta, ubyte ydelta) -> ubyte
direction_sc (byte x1, byte y1, byte x2, byte y2) -> ubyte
gcd (uword aa, uword bb) -> uword
interpolate (ubyte v, ubyte inputMin, ubyte inputMax, ubyte outputMin, ubyte outputMax) -> ubyte
lerp (ubyte v0, ubyte v1, ubyte t) -> ubyte
lerpf alias for: floats.lerp
+4
View File
@@ -65,6 +65,10 @@ divmod (dividend, divisor, quotient, remainder)
All values are ubytes or all are uwords.
The last two arguments must be variables to receive the quotient and remainder results, respectively.
gcd (a, b)
Returns the GCD (greatest common divisor) of uwords a and b
The routine is efficient and uses bit shifts instead of divisions.
max (x, y)
Returns the largest of x and y. Supported for integer types only, for floats use ``floats.maxf()`` instead.
+1
View File
@@ -10,6 +10,7 @@ Weird Heisenbug
Future Things and Ideas
^^^^^^^^^^^^^^^^^^^^^^^
- add a -warnimplicitcasts and -warnexplicitcasts?
- make builtin functions capable of returning multiple values, then make divmod() return the 2 results rather than accepting 2 extra variables as arguments
- then also introduce lmh(longvalue) -or whatever sensible name- builtin function that returns the low, mid, hi (bank) bytes of a long.
- add a -profile option (for now X16 only) that instruments the start (and returns?) -of every prog8 subroutine with code that dumps to the X16 emulator debug console: name of sub, stack pointer (for call depth!), emudbg cycle count. Save/restore all used registers! Start of program must set cycle count to zero.