Suspiciously everything worked when changing to floats

This commit is contained in:
2025-08-10 09:00:00 -05:00
parent 24fcf87fb4
commit 26ecf67472
18 changed files with 817 additions and 863 deletions

257
archive/fixed.c Normal file
View File

@@ -0,0 +1,257 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "fixed.h"
#include "assert/assert.h"
float_t fx248Fromi32(const int32_t b) {
return (float_t)b << FIXED248_FRACTION_BITS;
}
float_t fx248Fromu32(const uint32_t b) {
return (float_t)((int32_t)b << FIXED248_FRACTION_BITS);
}
float_t fx248Fromf32(const float_t b) {
return (float_t)(b * (1 << FIXED248_FRACTION_BITS));
}
float_t fx248Fromu16(const uint16_t b) {
return (float_t)((int32_t)b << FIXED248_FRACTION_BITS);
}
float_t fx248Fromu8(const uint8_t b) {
return (float_t)((int32_t)b << FIXED248_FRACTION_BITS);
}
int32_t fx248Toi32(const float_t a) {
return a >> FIXED248_FRACTION_BITS;
}
uint32_t fx248Tou32(const float_t a) {
return (uint32_t)(a >> FIXED248_FRACTION_BITS);
}
float_t fx248Tof32(const float_t a) {
return (float_t)a / (1 << FIXED248_FRACTION_BITS);
}
uint16_t fx248Tou16(const float_t a) {
return (uint16_t)(a >> FIXED248_FRACTION_BITS);
}
uint8_t fx248Tou8(const float_t a) {
return (uint8_t)(a >> FIXED248_FRACTION_BITS);
}
float_t fx248Addfx248(const float_t a, const float_t b) {
return a + b;
}
float_t fx248Addi32(const float_t a, const int32_t b) {
return fx248Addfx248(a, fx248Fromi32(b));
}
float_t fx248Addu32(const float_t a, const uint32_t b) {
return fx248Addfx248(a, fx248Fromu32(b));
}
float_t fx248Addf32(const float_t a, const float_t b) {
return fx248Addfx248(a, fx248Fromf32(b));
}
float_t fx248Subfx248(const float_t a, const float_t b) {
return a - b;
}
float_t fx248Subi32(const float_t a, const int32_t b) {
return fx248Subfx248(a, fx248Fromi32(b));
}
float_t fx248Subu32(const float_t a, const uint32_t b) {
return fx248Subfx248(a, fx248Fromu32(b));
}
float_t fx248Subf32(const float_t a, const float_t b) {
return fx248Subfx248(a, fx248Fromf32(b));
}
float_t fx248Mulfx248(const float_t a, const float_t b) {
return (float_t)(((int64_t)a * (int64_t)b) >> FIXED248_FRACTION_BITS);
}
float_t fx248Muli32(const float_t a, const int32_t b) {
return (float_t)(((int64_t)a * (int64_t)b) >> FIXED248_FRACTION_BITS);
}
float_t fx248Mulu32(const float_t a, const uint32_t b) {
return (float_t)(
((int64_t)a * (int64_t)(int32_t)b
) >> FIXED248_FRACTION_BITS);
}
float_t fx248Mulf32(const float_t a, const float_t b) {
return (float_t)((
(int64_t)a * (int64_t)(b * (1 << FIXED248_FRACTION_BITS))
) >> FIXED248_FRACTION_BITS);
}
float_t fx248Divfx248(const float_t a, const float_t b) {
assertFalse(b == 0, "Division by zero in fx248Divfx248");
return (float_t)(((int64_t)a << FIXED248_FRACTION_BITS) / (int64_t)b);
}
float_t fx248Divi32(const float_t a, const int32_t b) {
assertFalse(b == 0, "Division by zero in fx248Divi32");
return (float_t)(((int64_t)a << FIXED248_FRACTION_BITS) / (int64_t)b);
}
float_t fx248Divu32(const float_t a, const uint32_t b) {
assertFalse(b == 0, "Division by zero in fx248Divu32");
return (float_t)(
((int64_t)a << FIXED248_FRACTION_BITS
) / (int64_t)(int32_t)b);
}
float_t fx248Divf32(const float_t a, const float_t b) {
assertFalse(b == 0, "Division by zero in fx248Divf32");
return (float_t)((
(int64_t)a << FIXED248_FRACTION_BITS
) / (int64_t)(b * (1 << FIXED248_FRACTION_BITS)));
}
float_t fx248Floor(const float_t a) {
return a & ~((1 << FIXED248_FRACTION_BITS) - 1);
}
float_t fx248Ceil(const float_t a) {
if(a & ((1 << FIXED248_FRACTION_BITS) - 1)) {
return (a & ~((1 << FIXED248_FRACTION_BITS) - 1)) + (1 << FIXED248_FRACTION_BITS);
}
return a;
}
float_t fx248Round(const float_t a) {
if(a & ((1 << (FIXED248_FRACTION_BITS - 1)) - 1)) {
return (a & ~((1 << FIXED248_FRACTION_BITS) - 1)) + (1 << FIXED248_FRACTION_BITS);
}
return a & ~((1 << FIXED248_FRACTION_BITS) - 1);
}
uint32_t fx248Flooru32(const float_t a) {
return (uint32_t)((a >> FIXED248_FRACTION_BITS) & 0xFFFFFFFF);
}
uint32_t fx248Ceilu32(const float_t a) {
return (uint32_t)(((a + ((1 << FIXED248_FRACTION_BITS) - 1)) >> FIXED248_FRACTION_BITS) & 0xFFFFFFFF);
}
uint32_t fx248Roundu32(const float_t a) {
return (uint32_t)(((a + (1 << (FIXED248_FRACTION_BITS - 1))) >> FIXED248_FRACTION_BITS) & 0xFFFFFFFF);
}
float_t fx248Sqrt(const float_t a) {
if(a == 0) return 0;
float_t y = a > FIXED248(1, 0) ? a : FIXED248(1, 0);
float_t last = 0;
int max_iter = 16;
while(y != last && max_iter-- > 0) {
last = y;
int32_t div = (int32_t)(((int64_t)a << FIXED248_FRACTION_BITS) / y);
y = (y + div) >> 1;
}
return y;
}
float_t fx248Max(const float_t a, const float_t b) {
return (a > b) ? a : b;
}
float_t fx248Min(const float_t a, const float_t b) {
return (a < b) ? a : b;
}
float_t fx248Clamp(
const float_t a,
const float_t min,
const float_t max
) {
return (a < min) ? min : (a > max) ? max : a;
}
float_t fx248Abs(const float_t a) {
return (a < 0) ? -a : a;
}
float_t fx248Atan2(
const float_t y,
const float_t x
) {
// Handle special cases
if (x == 0) {
if (y > 0) return FX248_HALF_PI;
if (y < 0) return -FX248_HALF_PI;
return 0;
}
// Use absolute values for quadrant correction
float_t abs_y = y;
if (abs_y < 0) abs_y = -abs_y;
float_t angle;
if (abs_y < fx248Abs(x)) {
float_t z = fx248Divfx248(y, x);
float_t z2 = fx248Mulfx248(z, z);
float_t z3 = fx248Mulfx248(z2, z);
float_t z5 = fx248Mulfx248(z3, z2);
angle = fx248Subfx248(
fx248Addfx248(z, fx248Divfx248(z5, fx248Fromi32(5))),
fx248Divfx248(z3, fx248Fromi32(3))
);
if (x < 0) {
if (y < 0) {
angle -= FX248_PI;
} else {
angle += FX248_PI;
}
}
} else {
float_t z = fx248Divfx248(x, y);
float_t z2 = fx248Mulfx248(z, z);
float_t z3 = fx248Mulfx248(z2, z);
float_t z5 = fx248Mulfx248(z3, z2);
angle = fx248Subfx248(
fx248Addfx248(z, fx248Divfx248(z5, fx248Fromi32(5))),
fx248Divfx248(z3, fx248Fromi32(3))
);
if (y > 0) {
angle = FX248_HALF_PI - angle;
} else {
angle = -FX248_HALF_PI - angle;
}
}
return angle;
}

379
archive/fixed.h Normal file
View File

@@ -0,0 +1,379 @@
/**
* 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
);