379 lines
9.5 KiB
C
379 lines
9.5 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#pragma once
|
|
#include "dusk.h"
|
|
|
|
typedef int32_t float_t;
|
|
|
|
#define FIXED248_FRACTION_BITS 8
|
|
#define FIXED248_HIGH_MULTIPLIER (1 << FIXED248_FRACTION_BITS)
|
|
#define FIXED248_MIN INT32_MIN
|
|
#define FIXED248_MAX INT32_MAX
|
|
#define FIXED248(i, f) ((float_t)( \
|
|
((i) << FIXED248_FRACTION_BITS) + \
|
|
(((f) * FIXED248_HIGH_MULTIPLIER) / 100) \
|
|
))
|
|
#define FIXED248_ONE (FIXED248(1, 0))
|
|
#define FIXED248_ZERO (FIXED248(0, 0))
|
|
#define FX248_PI 804
|
|
#define FX248_HALF_PI 402
|
|
#define FX248_3PI_4 603
|
|
#define FX248_NEG_PI -804
|
|
|
|
/**
|
|
* Convert an int32_t value to a float_t value.
|
|
*
|
|
* @param b The int32_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Fromi32(const int32_t b);
|
|
|
|
/**
|
|
* Convert a uint32_t value to a float_t value.
|
|
*
|
|
* @param b The uint32_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Fromu32(const uint32_t b);
|
|
|
|
/**
|
|
* Convert a float_t value to a float_t value.
|
|
*
|
|
* @param b The float_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Fromf32(const float_t b);
|
|
|
|
/**
|
|
* Convert a uint16_t value to a float_t value.
|
|
*
|
|
* @param b The uint16_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Fromu16(const uint16_t b);
|
|
|
|
/**
|
|
* Convert a uint8_t value to a float_t value.
|
|
*
|
|
* @param b The uint8_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Fromu8(const uint8_t b);
|
|
|
|
|
|
|
|
/**
|
|
* Convert a float_t value to an int32_t value.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted int32_t value.
|
|
*/
|
|
int32_t fx248Toi32(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to a uint32_t value.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint32_t value.
|
|
*/
|
|
uint32_t fx248Tou32(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to a float_t value.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted float_t value.
|
|
*/
|
|
float_t fx248Tof32(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to a uint16_t value.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint16_t value.
|
|
*/
|
|
uint16_t fx248Tou16(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to an uint8_t value.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint8_t value.
|
|
*/
|
|
uint8_t fx248Tou8(const float_t a);
|
|
|
|
|
|
|
|
/**
|
|
* Add a float_t value to another float_t value.
|
|
*
|
|
* @param a First float_t value.
|
|
* @param b Second float_t value to add to the first value.
|
|
* @return The result of the addition as a float_t value.
|
|
*/
|
|
float_t fx248Addfx248(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Add an int32_t value to a float_t value.
|
|
*
|
|
* @param a The float_t value to which the int32_t will be added.
|
|
* @param b The int32_t value to add to the float_t value.
|
|
* @return The result of the addition as a float_t value.
|
|
*/
|
|
float_t fx248Addi32(const float_t a, const int32_t b);
|
|
|
|
/**
|
|
* Add a uint32_t value to a float_t value.
|
|
*
|
|
* @param a The float_t value to which the uint32_t will be added.
|
|
* @param b The uint32_t value to add to the float_t value.
|
|
* @return The result of the addition as a float_t value.
|
|
*/
|
|
float_t fx248Addu32(const float_t a, const uint32_t b);
|
|
|
|
/**
|
|
* Add a float_t value to a float_t value.
|
|
*
|
|
* @param a Pointer to the float_t value (will be modified).
|
|
* @param b The float_t value to add to the float_t value.
|
|
* @return The result of the addition as a float_t value.
|
|
*/
|
|
float_t fx248Addf32(const float_t a, const float_t b);
|
|
|
|
|
|
|
|
/**
|
|
* Subtract a float_t value from another float_t value.
|
|
*
|
|
* @param a First float_t value.
|
|
* @param b The float_t value to subtract from the first value.
|
|
* @return The result of the subtraction as a float_t value.
|
|
*/
|
|
float_t fx248Subfx248(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Subtract an int32_t value from a float_t value.
|
|
*
|
|
* @param a The float_t value from which the int32_t will be subtracted.
|
|
* @param b The int32_t value to subtract from the float_t value.
|
|
* @return The result of the subtraction as a float_t value.
|
|
*/
|
|
float_t fx248Subi32(const float_t a, const int32_t b);
|
|
|
|
/**
|
|
* Subtract a uint32_t value from a float_t value.
|
|
*
|
|
* @param a The float_t value from which the uint32_t will be subtracted.
|
|
* @param b The uint32_t value to subtract from the float_t value.
|
|
* @return The result of the subtraction as a float_t value.
|
|
*/
|
|
float_t fx248Subu32(const float_t a, const uint32_t b);
|
|
|
|
/**
|
|
* Subtract a float_t value from a float_t value.
|
|
*
|
|
* @param a The float_t value from which the float_t will be subtracted.
|
|
* @param b The float_t value to subtract from the float_t value.
|
|
* @return The result of the subtraction as a float_t value.
|
|
*/
|
|
float_t fx248Subf32(const float_t a, const float_t b);
|
|
|
|
|
|
|
|
/**
|
|
* Multiply two float_t values.
|
|
*
|
|
* @param a First float_t value.
|
|
* @param b Second float_t value to multiply with the first value.
|
|
* @return The result of the multiplication as a float_t value.
|
|
*/
|
|
float_t fx248Mulfx248(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Multiply a float_t value by an int32_t value.
|
|
*
|
|
* @param a The float_t value to multiply.
|
|
* @param b The int32_t value to multiply with the float_t value.
|
|
* @return The result of the multiplication as a float_t value.
|
|
*/
|
|
float_t fx248Muli32(const float_t a, const int32_t b);
|
|
|
|
/**
|
|
* Multiply a float_t value by a uint32_t value.
|
|
*
|
|
* @param a The float_t value to multiply.
|
|
* @param b The uint32_t value to multiply with the float_t value.
|
|
* @return The result of the multiplication as a float_t value.
|
|
*/
|
|
float_t fx248Mulu32(const float_t a, const uint32_t b);
|
|
|
|
/**
|
|
* Multiply a float_t value by a float_t value.
|
|
*
|
|
* @param a The float_t value to multiply.
|
|
* @param b The float_t value to multiply with the float_t value.
|
|
* @return The result of the multiplication as a float_t value.
|
|
*/
|
|
float_t fx248Mulf32(const float_t a, const float_t b);
|
|
|
|
|
|
|
|
/**
|
|
* Divide two float_t values.
|
|
*
|
|
* @param a The float_t value to be divided.
|
|
* @param b The float_t value to divide by.
|
|
* @return The result of the division as a float_t value.
|
|
*/
|
|
float_t fx248Divfx248(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Divide a float_t value by an int32_t value.
|
|
*
|
|
* @param a The float_t value to be divided.
|
|
* @param b The int32_t value to divide by.
|
|
* @return The result of the division as a float_t value.
|
|
*/
|
|
float_t fx248Divi32(const float_t a, const int32_t b);
|
|
|
|
/**
|
|
* Divide a float_t value by a uint32_t value.
|
|
*
|
|
* @param a The float_t value to be divided.
|
|
* @param b The uint32_t value to divide by.
|
|
* @return The result of the division as a float_t value.
|
|
*/
|
|
float_t fx248Divu32(const float_t a, const uint32_t b);
|
|
|
|
/**
|
|
* Divide a float_t value by a float_t value.
|
|
*
|
|
* @param a The float_t value to be divided.
|
|
* @param b The float_t value to divide by.
|
|
* @return The result of the division as a float_t value.
|
|
*/
|
|
float_t fx248Divf32(const float_t a, const float_t b);
|
|
|
|
|
|
|
|
/**
|
|
* Convert a float_t value to an int32_t value, rounding towards zero.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted int32_t value.
|
|
*/
|
|
float_t fx248Floor(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to an int32_t value, rounding towards positive
|
|
* infinity.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted int32_t value.
|
|
*/
|
|
float_t fx248Ceil(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to an int32_t value, rounding to the nearest
|
|
* integer.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted int32_t value.
|
|
*/
|
|
float_t fx248Round(const float_t a);
|
|
|
|
|
|
|
|
/**
|
|
* Convert a float_t value to a uint32_t value, rounding towards zero.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint32_t value.
|
|
*/
|
|
uint32_t fx248Flooru32(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to a uint32_t value, rounding towards positive
|
|
* infinity.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint32_t value.
|
|
*/
|
|
uint32_t fx248Ceilu32(const float_t a);
|
|
|
|
/**
|
|
* Convert a float_t value to a uint32_t value, rounding to the nearest
|
|
* integer.
|
|
*
|
|
* @param a The float_t value to convert.
|
|
* @return The converted uint32_t value.
|
|
*/
|
|
uint32_t fx248Roundu32(const float_t a);
|
|
|
|
/**
|
|
* Returns the square root of a float_t value.
|
|
*
|
|
* @param a The float_t value to calculate the square root of.
|
|
*/
|
|
float_t fx248Sqrt(const float_t a);
|
|
|
|
|
|
|
|
/**
|
|
* Returns the maximum of two float_t values.
|
|
*
|
|
* @param a First float_t value.
|
|
* @param b Second float_t value.
|
|
* @return The maximum of the two values.
|
|
*/
|
|
float_t fx248Max(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Returns the minimum of two float_t values.
|
|
*
|
|
* @param a First float_t value.
|
|
* @param b Second float_t value.
|
|
* @return The minimum of the two values.
|
|
*/
|
|
float_t fx248Min(const float_t a, const float_t b);
|
|
|
|
/**
|
|
* Clamp a float_t value between a minimum and maximum value.
|
|
*
|
|
* @param a The float_t value to clamp.
|
|
* @param min The minimum value to clamp to.
|
|
* @param max The maximum value to clamp to.
|
|
* @return The clamped float_t value.
|
|
*/
|
|
float_t fx248Clamp(
|
|
const float_t a,
|
|
const float_t min,
|
|
const float_t max
|
|
);
|
|
|
|
/**
|
|
* Returns the absolute value of a float_t value.
|
|
*
|
|
* @param a The float_t value to calculate the absolute value of.
|
|
* @return The absolute value as a float_t value.
|
|
*/
|
|
float_t fx248Abs(const float_t a);
|
|
|
|
|
|
|
|
/**
|
|
* Calculate the arctangent of a float_t value.
|
|
*
|
|
* @param y Y coordinate value.
|
|
* @param x X coordinate value.
|
|
* @return The arctangent of the value as a float_t value.
|
|
*/
|
|
float_t fx248Atan2(
|
|
const float_t y,
|
|
const float_t x
|
|
); |