Ti Constructor
 
Loading...
Searching...
No Matches
fast_math.c File Reference

Fast math operations, and some 16-bit fixed point path. More...

#include "textio.c"

Macros

#define FX(R)   (int)((R) > 0 ? (R)*256 : 0xFFFF-((-(R)) *256) )
 Inline compile time fixed point number.
 

Functions

char FX_floor_int (int f) __naked
 Fixed point number to char number.
 
int FX_floor (int f) __naked
 Round fixed point number.
 
char sqrt_rounded (int x) __naked
 Gets the square root of a 16-bit intager to a 8 bit char.
 
long mul_int (int x, int y) __naked
 Multiple two 16-bit ints together.
 
int div_int (int x, int y) __naked
 Divide two 16-bit ints together.
 
int FX_mul (int x, int y) __naked
 Multiple two 16-bit fixed point numbers.
 
int FX_div (int x, int y) __naked
 Divide two 16-bit fixed point numbers.
 
int FX_sqrt (int x) __naked
 Take the sqrt of a fixed point number.
 
int FX_abs (int x) __naked
 Take the abs of a fixed point number.
 
void FX_number (int x) __naked
 Print a fixed point number to the screen.
 
void __fast_16_bit_sqrt_asm () __naked
 Assembly 16-bit Square Root routine
 
void __mult_de_bc () __naked
 Assembly 16-bit multiplication routine
 
void __div_32_by_16 () __naked
 Assembly 32-bit by 16-bit division routine
 
void __div_ac_de () __naked
 Assembly 16-bit division routine
 

Fixed point trig functions (requires #define FX_TRIG)

const int FX_sine_lookup [] = {4, 13, 22, 31, 40, 48, 57, 66, 74, 83, 91, 100, 108, 116, 124, 131, 139, 146, 154, 161, 167, 174, 181, 187, 193, 198, 204, 209, 214, 219, 223, 228, 232, 235, 238, 242, 244, 247, 249, 251, 252, 254, 255, 255, 255, 255, 255, 255, 254, 252, 251, 249, 247, 244, 242, 238, 235, 232, 228, 223, 219, 214, 209, 204, 198, 193, 187, 181, 174, 167, 161, 154, 146, 139, 131, 124, 116, 108, 100, 91, 83, 74, 66, 57, 48, 40, 31, 22, 13, 4, -4, -13, -22, -31, -40, -48, -57, -66, -74, -83, -91, -100, -108, -116, -124, -131, -139, -146, -154, -161, -167, -174, -181, -187, -193, -198, -204, -209, -214, -219, -223, -228, -232, -235, -238, -242, -244, -247, -249, -251, -252, -254, -255, -255, -255, -255, -255, -255, -254, -252, -251, -249, -247, -244, -242, -238, -235, -232, -228, -223, -219, -214, -209, -204, -198, -193, -187, -181, -174, -167, -161, -154, -146, -139, -131, -124, -116, -108, -100, -91, -83, -74, -66, -57, -48, -40, -31, -22, -13, -4}
 Fixed point sine lookup table. Takes up 360 bytes

 
int FX_sine (int deg)
 Fixed point sine operation.
 
int FX_cos (int deg)
 Fixed point cosine operation.
 
int FX_tan (int deg)
 Fixed point tangent operation.
 

Detailed Description

Fast math operations, and some 16-bit fixed point path.

Faster then fixedpoint.c but more complex and less precise. Uses 16 bit (int) for fixed point math (8 for decimal and 8 for fraction). It should be noted that if you are worried about the size of your program, this is quite a large file.

Mostly sourced from https://wikiti.brandonw.net/index.php?title=Category:Z80_Routines

Optional #defines

Macro Definition Documentation

◆ FX

#define FX ( R)    (int)((R) > 0 ? (R)*256 : 0xFFFF-((-(R)) *256) )

Inline compile time fixed point number.

Parameters
[R]Number to be converted
Returns
Fixed point number

This is used to create a fixed point number at compile time, highly NOT recommended to use at run time, or with anything other than a literal.

Function Documentation

◆ __div_32_by_16()

void __div_32_by_16 ( )

Assembly 32-bit by 16-bit division routine

ONLY CALL FROM ASM

IN: ACIX=dividend, DE=divisor OUT: ACIX=quotient, DE=divisor, HL=remainder, B=0

◆ __div_ac_de()

void __div_ac_de ( )

Assembly 16-bit division routine

ONLY CALL FROM ASM

This assembly routine divides ac by de and places the quotient in ac and the remainder in hl

◆ __fast_16_bit_sqrt_asm()

void __fast_16_bit_sqrt_asm ( )

Assembly 16-bit Square Root routine

ONLY CALL FROM ASM

la is the 16-bit operand and D is the 8-bit result.

◆ __mult_de_bc()

void __mult_de_bc ( )

Assembly 16-bit multiplication routine

ONLY CALL FROM ASM

bc by de and places the result in dehl.

◆ div_int()

int div_int ( int x,
int y )

Divide two 16-bit ints together.

Parameters
[x]A 16-bit int (NOT fixed point number)
[y]Another 16-bit int (NOT fixed point number)
Returns
A 16-bit int of x/y

◆ FX_abs()

int FX_abs ( int x)

Take the abs of a fixed point number.

Parameters
[x]A 16-bit fixed point number
Returns
The fixed point result of abs(x)

Taken from https://learn.cemetech.net/index.php?title=Z80:Math_Routines#absHL

◆ FX_cos()

int FX_cos ( int deg)

Fixed point cosine operation.

Parameters
[deg]A degree as an INTEGER
Returns
The fixed point cosine value

◆ FX_div()

int FX_div ( int x,
int y )

Divide two 16-bit fixed point numbers.

Parameters
[x]A 16-bit fixed point number
[y]Another 16-bit fixed point number
Returns
The fixed point result of x/y

Takes about 1800 t-states (+-500)

◆ FX_floor()

int FX_floor ( int f)

Round fixed point number.

Parameters
[f]Number to be rounded
Returns
Another fixed point number

◆ FX_floor_int()

char FX_floor_int ( int f)

Fixed point number to char number.

Parameters
[f]Number to be converted
Returns
char

◆ FX_mul()

int FX_mul ( int x,
int y )

Multiple two 16-bit fixed point numbers.

Parameters
[x]A 16-bit fixed point number
[y]Another 16-bit fixed point number
Returns
The fixed point result of x*y

◆ FX_number()

void FX_number ( int x)

Print a fixed point number to the screen.

Parameters
[x]A 16-bit fixed point number

◆ FX_sine()

int FX_sine ( int deg)

Fixed point sine operation.

Parameters
[deg]A degree as an INTEGER
Returns
The fixed point sine value

◆ FX_sqrt()

int FX_sqrt ( int x)

Take the sqrt of a fixed point number.

Parameters
[x]A 16-bit fixed point number
Returns
The fixed point result of sqrt(x)

◆ FX_tan()

int FX_tan ( int deg)

Fixed point tangent operation.

Parameters
[deg]A degree as an INTEGER
Returns
The fixed point tangent value

◆ mul_int()

long mul_int ( int x,
int y )

Multiple two 16-bit ints together.

Parameters
[x]A 16-bit int (NOT fixed point number)
[y]Another 16-bit int (NOT fixed point number)
Returns
A long(32-bit) value of x*y

◆ sqrt_rounded()

char sqrt_rounded ( int x)

Gets the square root of a 16-bit intager to a 8 bit char.

Parameters
[x]A 16-bit INTEGER (NOT fixed point number)
Returns
A rounded char of the sqrt(x)