Loading...
Searching...
No Matches
math.h File Reference

SNES Fixed-Point Math. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define FIX(x)   ((fixed)((x) << 8))
 Convert integer to fixed-point.
 
#define FIX_FRAC(x)   ((u8)((x) & 0xFF))
 Get fractional part of fixed-point.
 
#define FIX_MAKE(i, f)   ((fixed)(((i) << 8) | (f)))
 Create fixed-point from integer and fraction.
 
#define UNFIX(x)   ((s16)((x) >> 8))
 Convert fixed-point to integer (truncate)
 
#define UNFIX_ROUND(x)   ((s16)(((x) + 128) >> 8))
 Convert fixed-point to integer (rounded)
 

Typedefs

typedef s16 fixed
 8.8 signed fixed-point type
 

Functions

u16 div16 (u16 dividend, u16 divisor)
 Safe 16-bit division.
 
fixed fixAbs (fixed x)
 Absolute value of fixed-point.
 
fixed fixClamp (fixed x, fixed min, fixed max)
 Clamp value to range.
 
fixed fixCos (u8 angle)
 Get cosine value for angle.
 
fixed fixDiv (fixed a, fixed b)
 Divide two fixed-point values.
 
fixed fixLerp (fixed a, fixed b, u8 t)
 Linear interpolation.
 
fixed fixMul (fixed a, fixed b)
 Multiply two fixed-point values.
 
fixed fixSin (u8 angle)
 Get sine value for angle.
 
u16 mod16 (u16 dividend, u16 divisor)
 Get remainder of division.
 
u16 mul16 (u16 a, u16 b)
 Safe 16-bit multiplication.
 

Detailed Description

SNES Fixed-Point Math.

Fixed-point arithmetic for smooth movement and physics.

Overview

This module provides:

  • 8.8 fixed-point type and macros
  • Sine/cosine lookup tables (256 angles = full circle)
  • Fixed-point multiplication
  • Integer multiplication (safe alternative to compiler's *)

Fixed-Point Format

The fixed type is 16-bit signed (s16) in 8.8 format:

  • High byte: integer part (-128 to 127)
  • Low byte: fractional part (0-255, representing 0.0 to 0.996)
fixed pos_x = FIX(100); // 100.0
fixed velocity = FIX(1) / 4; // 0.25 (64 in fixed)
pos_x = pos_x + velocity; // 100.25
s16 screen_x = UNFIX(pos_x); // 100 (truncated)
static s16 pos_x
Definition main.c:177
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:49
#define UNFIX(x)
Convert fixed-point to integer (truncate)
Definition math.h:83
#define FIX(x)
Convert integer to fixed-point.
Definition math.h:71
s16 fixed
8.8 signed fixed-point type
Definition math.h:59

Angles

Angles are 8-bit values (0-255) representing 0-360 degrees:

  • 0 = 0°, 64 = 90°, 128 = 180°, 192 = 270°
u8 angle = 64; // 90 degrees
fixed dx = fixSin(angle); // 1.0 (256)
fixed dy = fixCos(angle); // 0.0 (0)
player_x = player_x + fixMul(speed, dx);
static s16 player_x
Player X position in screen coordinates.
Definition main.c:55
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:46
fixed fixCos(u8 angle)
Get cosine value for angle.
fixed fixMul(fixed a, fixed b)
Multiply two fixed-point values.
fixed fixSin(u8 angle)
Get sine value for angle.
Author
OpenSNES Team

Macro Definition Documentation

◆ FIX

#define FIX (   x)    ((fixed)((x) << 8))

Convert integer to fixed-point.

Parameters
xInteger value (-128 to 127)
Returns
Fixed-point representation
fixed pos = FIX(50); // 50.0 in fixed-point
fixed half = FIX(1) / 2; // 0.5

◆ FIX_FRAC

#define FIX_FRAC (   x)    ((u8)((x) & 0xFF))

Get fractional part of fixed-point.

Parameters
xFixed-point value
Returns
Fractional part (0-255)

◆ FIX_MAKE

#define FIX_MAKE (   i,
 
)    ((fixed)(((i) << 8) | (f)))

Create fixed-point from integer and fraction.

Parameters
iInteger part
fFractional part (0-255)
Returns
Fixed-point value
fixed half = FIX_MAKE(0, 128); // 0.5
fixed quarter = FIX_MAKE(0, 64); // 0.25
#define FIX_MAKE(i, f)
Create fixed-point from integer and fraction.
Definition math.h:115

◆ UNFIX

#define UNFIX (   x)    ((s16)((x) >> 8))

Convert fixed-point to integer (truncate)

Parameters
xFixed-point value
Returns
Integer part (truncated toward zero)
fixed pos = FIX(50) + 128; // 50.5
s16 screen_pos = UNFIX(pos); // 50

◆ UNFIX_ROUND

#define UNFIX_ROUND (   x)    ((s16)(((x) + 128) >> 8))

Convert fixed-point to integer (rounded)

Parameters
xFixed-point value
Returns
Integer part (rounded to nearest)
fixed pos = FIX(50) + 128; // 50.5
s16 screen_pos = UNFIX_ROUND(pos); // 51
#define UNFIX_ROUND(x)
Convert fixed-point to integer (rounded)
Definition math.h:95

Typedef Documentation

◆ fixed

typedef s16 fixed

8.8 signed fixed-point type

Range: -128.0 to 127.996 (approximately) Precision: 1/256 = 0.00390625

Function Documentation

◆ div16()

u16 div16 ( u16  dividend,
u16  divisor 
)

Safe 16-bit division.

Parameters
dividendNumber to divide
divisorNumber to divide by (must not be zero)
Returns
Quotient
Warning
Returns 0 if divisor is 0

◆ fixAbs()

fixed fixAbs ( fixed  x)

Absolute value of fixed-point.

Parameters
xFixed-point value
Returns
Absolute value

◆ fixClamp()

fixed fixClamp ( fixed  x,
fixed  min,
fixed  max 
)

Clamp value to range.

Parameters
xValue to clamp
minMinimum value
maxMaximum value
Returns
Clamped value

◆ fixCos()

fixed fixCos ( u8  angle)

Get cosine value for angle.

Parameters
angle8-bit angle (0-255 = 0°-360°)
Returns
Cosine value in 8.8 fixed-point (-256 to 256)
u8 angle = 0; // 0 degrees
fixed cos_val = fixCos(angle); // 256 = 1.0

◆ fixDiv()

fixed fixDiv ( fixed  a,
fixed  b 
)

Divide two fixed-point values.

Parameters
aDividend (fixed-point)
bDivisor (fixed-point, must not be zero)
Returns
Quotient in fixed-point format
fixed distance = FIX(100);
fixed time = FIX(5);
fixed speed = fixDiv(distance, time); // 20.0
fixed fixDiv(fixed a, fixed b)
Divide two fixed-point values.
Warning
Division by zero returns 0

◆ fixLerp()

fixed fixLerp ( fixed  a,
fixed  b,
u8  t 
)

Linear interpolation.

Parameters
aStart value
bEnd value
tInterpolation factor (0-256 = 0.0-1.0)
Returns
Interpolated value
fixed start = FIX(0);
fixed end = FIX(100);
fixed mid = fixLerp(start, end, 128); // 50.0
fixed fixLerp(fixed a, fixed b, u8 t)
Linear interpolation.

◆ fixMul()

fixed fixMul ( fixed  a,
fixed  b 
)

Multiply two fixed-point values.

Parameters
aFirst fixed-point operand
bSecond fixed-point operand
Returns
Product in fixed-point format
fixed speed = FIX(2);
fixed scale = FIX(1) / 2; // 0.5
fixed result = fixMul(speed, scale); // 1.0
Note
Uses 32-bit intermediate for accuracy

◆ fixSin()

fixed fixSin ( u8  angle)

Get sine value for angle.

Parameters
angle8-bit angle (0-255 = 0°-360°)
Returns
Sine value in 8.8 fixed-point (-256 to 256, i.e., -1.0 to 1.0)
u8 angle = 64; // 90 degrees
fixed sin_val = fixSin(angle); // 256 = 1.0
Note
Table-based lookup, very fast

◆ mod16()

u16 mod16 ( u16  dividend,
u16  divisor 
)

Get remainder of division.

Parameters
dividendNumber to divide
divisorNumber to divide by (must not be zero)
Returns
Remainder

◆ mul16()

u16 mul16 ( u16  a,
u16  b 
)

Safe 16-bit multiplication.

Multiplies two 16-bit values safely. Use this instead of the compiler's * operator for important calculations, as the compiler's runtime multiplication can have bugs.

Parameters
aFirst operand
bSecond operand
Returns
Product (may overflow for large values)
u16 result = mul16(width, height);
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:52
u16 mul16(u16 a, u16 b)
Safe 16-bit multiplication.