Bit more cleanup of floats

This commit is contained in:
2026-06-18 13:03:57 -05:00
parent c88b672f42
commit 4c2a883038
4 changed files with 206 additions and 24 deletions
+6 -6
View File
@@ -97,15 +97,15 @@ chunkindex_t chunkPosToIndex(const chunkpos_t *pos) {
}
void worldPosToFixed(const worldpos_t *pos, fixed_t *out) {
out[0] = fixedFromInt(pos->x);
out[1] = fixedFromInt(pos->y);
out[2] = fixedFromInt(pos->z);
out[0] = fixedFromI16(pos->x);
out[1] = fixedFromI16(pos->y);
out[2] = fixedFromI16(pos->z);
}
worldpos_t fixedToWorldPos(const fixed_t *position) {
return (worldpos_t){
(worldunit_t)fixedToInt(position[0]),
(worldunit_t)fixedToInt(position[1]),
(worldunit_t)fixedToInt(position[2])
fixedToI16(position[0]),
fixedToI16(position[1]),
fixedToI16(position[2])
};
}
+18 -11
View File
@@ -7,7 +7,6 @@
#include "uifullbox.h"
#include "assert/assert.h"
#include "util/fixed.h"
#include "util/memory.h"
#include "display/screen/screen.h"
#include "display/texture/texture.h"
@@ -43,19 +42,27 @@ static color_t uiFullboxGetColor(const uifullbox_t *fullbox) {
if(fullbox->duration <= 0 || fullbox->time >= fullbox->duration) {
return fullbox->toColor;
}
float_t t = fixedToFloat(easingApply(
fixed_t t = easingApply(
fullbox->easing,
fixedDiv(fullbox->time, fullbox->duration)
));
);
return color4b(
(uint8_t)((float_t)fullbox->fromColor.r +
((float_t)fullbox->toColor.r - (float_t)fullbox->fromColor.r) * t),
(uint8_t)((float_t)fullbox->fromColor.g +
((float_t)fullbox->toColor.g - (float_t)fullbox->fromColor.g) * t),
(uint8_t)((float_t)fullbox->fromColor.b +
((float_t)fullbox->toColor.b - (float_t)fullbox->fromColor.b) * t),
(uint8_t)((float_t)fullbox->fromColor.a +
((float_t)fullbox->toColor.a - (float_t)fullbox->fromColor.a) * t)
fixedToU8(fixedLerp(
fixedFromU8(fullbox->fromColor.r),
fixedFromU8(fullbox->toColor.r), t
)),
fixedToU8(fixedLerp(
fixedFromU8(fullbox->fromColor.g),
fixedFromU8(fullbox->toColor.g), t
)),
fixedToU8(fixedLerp(
fixedFromU8(fullbox->fromColor.b),
fixedFromU8(fullbox->toColor.b), t
)),
fixedToU8(fixedLerp(
fixedFromU8(fullbox->fromColor.a),
fixedFromU8(fullbox->toColor.a), t
))
);
}
+58 -2
View File
@@ -7,18 +7,74 @@
#include "fixed.h"
fixed_t fixedFromInt(int32_t n) {
fixed_t fixedFromI8(int8_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromU8(uint8_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromI16(int16_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromU16(uint16_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromI32(int32_t n) {
return (fixed_t)(n << FIXED_FRAC_BITS);
}
fixed_t fixedFromU32(uint32_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromI64(int64_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromU64(uint64_t n) {
return (fixed_t)((int32_t)n << FIXED_FRAC_BITS);
}
fixed_t fixedFromFloat(float_t f) {
return (fixed_t)(f * (float_t)FIXED_SCALE);
}
int32_t fixedToInt(fixed_t f) {
int8_t fixedToI8(fixed_t f) {
return (int8_t)(f >> FIXED_FRAC_BITS);
}
uint8_t fixedToU8(fixed_t f) {
return (uint8_t)(f >> FIXED_FRAC_BITS);
}
int16_t fixedToI16(fixed_t f) {
return (int16_t)(f >> FIXED_FRAC_BITS);
}
uint16_t fixedToU16(fixed_t f) {
return (uint16_t)(f >> FIXED_FRAC_BITS);
}
int32_t fixedToI32(fixed_t f) {
return (int32_t)(f >> FIXED_FRAC_BITS);
}
uint32_t fixedToU32(fixed_t f) {
return (uint32_t)(f >> FIXED_FRAC_BITS);
}
int64_t fixedToI64(fixed_t f) {
return (int64_t)(f >> FIXED_FRAC_BITS);
}
uint64_t fixedToU64(fixed_t f) {
return (uint64_t)(f >> FIXED_FRAC_BITS);
}
float_t fixedToFloat(fixed_t f) {
return (float_t)f / (float_t)FIXED_SCALE;
}
+124 -5
View File
@@ -27,12 +27,68 @@ typedef int32_t fixed_t;
#define FIXED(x) ((fixed_t)((x) * FIXED_SCALE))
/**
* Creates a fixed-point value from an integer.
* Creates a fixed-point value from a signed 8-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromInt(int32_t n);
fixed_t fixedFromI8(int8_t n);
/**
* Creates a fixed-point value from an unsigned 8-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromU8(uint8_t n);
/**
* Creates a fixed-point value from a signed 16-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromI16(int16_t n);
/**
* Creates a fixed-point value from an unsigned 16-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromU16(uint16_t n);
/**
* Creates a fixed-point value from a signed 32-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromI32(int32_t n);
/**
* Creates a fixed-point value from an unsigned 32-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromU32(uint32_t n);
/**
* Creates a fixed-point value from a signed 64-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromI64(int64_t n);
/**
* Creates a fixed-point value from an unsigned 64-bit integer.
*
* @param n Integer to convert.
* @returns The fixed-point representation.
*/
fixed_t fixedFromU64(uint64_t n);
/**
* Creates a fixed-point value from a float.
@@ -43,13 +99,76 @@ fixed_t fixedFromInt(int32_t n);
fixed_t fixedFromFloat(float_t f);
/**
* Converts a fixed-point value to an integer, rounding toward negative
* infinity.
* Converts a fixed-point value to a signed 8-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part clamped to int8_t range.
*/
int8_t fixedToI8(fixed_t f);
/**
* Converts a fixed-point value to an unsigned 8-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part clamped to uint8_t range.
*/
uint8_t fixedToU8(fixed_t f);
/**
* Converts a fixed-point value to a signed 16-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part clamped to int16_t range.
*/
int16_t fixedToI16(fixed_t f);
/**
* Converts a fixed-point value to an unsigned 16-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part clamped to uint16_t range.
*/
uint16_t fixedToU16(fixed_t f);
/**
* Converts a fixed-point value to a signed 32-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part.
*/
int32_t fixedToInt(fixed_t f);
int32_t fixedToI32(fixed_t f);
/**
* Converts a fixed-point value to an unsigned 32-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part (negative values wrap to 0).
*/
uint32_t fixedToU32(fixed_t f);
/**
* Converts a fixed-point value to a signed 64-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part.
*/
int64_t fixedToI64(fixed_t f);
/**
* Converts a fixed-point value to an unsigned 64-bit integer, truncating the
* fractional part.
*
* @param f Fixed-point value.
* @returns Integer part (negative values wrap to 0).
*/
uint64_t fixedToU64(fixed_t f);
/**
* Converts a fixed-point value to a float.