feat(libc): add floating-point string conversions

This commit is contained in:
SamG-Coder
2026-09-25 06:55:31 +10:00
parent c7d26dda5e
commit 7d07187ac8
3 changed files with 41 additions and 0 deletions
+7
View File
@@ -97,6 +97,13 @@ double APS5_VABI strtod_nid_postfix(const char* str, char** endptr) {
return std::strtod(str, endptr);
}
double APS5_VABI atof_nid_postfix(const char* str) { return std::atof(str); }
float APS5_VABI strtof_nid_postfix(const char* str, char** endptr) { return std::strtof(str, endptr); }
long double APS5_VABI strtold_nid_postfix(const char* str, char** endptr) {
static_assert(sizeof(long double) == 16, "Guest long double requires x87 extended precision storage");
return std::strtold(str, endptr);
}
int APS5_VABI atoi_nid_postfix(const char* str) {
return std::atoi(str);
}
+14
View File
@@ -4,6 +4,10 @@
#include <cstdlib>
#include <limits>
extern "C" {
double APS5_VABI atof_nid_postfix(const char*);
float APS5_VABI strtof_nid_postfix(const char*, char**);
long double APS5_VABI strtold_nid_postfix(const char*, char**);
int* APS5_VABI __error_nid_postfix();
float APS5_VABI fmodf_nid_postfix(float, float);
float APS5_VABI asinf_nid_postfix(float);
float APS5_VABI acosf_nid_postfix(float);
@@ -26,6 +30,16 @@ int APS5_VABI __isinff_nid_postfix(float);
}
static void Require(bool value) { if (!value) std::abort(); }
int main() {
Require(atof_nid_postfix(" -12.5tail") == -12.5);
char* end = nullptr;
const char input[] = "0x1.8p+2 remainder";
Require(strtof_nid_postfix(input, &end) == 6.f && end == input + 8);
const char invalid[] = "invalid";
Require(strtof_nid_postfix(invalid, &end) == 0.f && end == invalid);
*__error_nid_postfix() = 0;
Require(std::isinf(strtof_nid_postfix("1e1000", nullptr)));
Require(*__error_nid_postfix() == 34);
Require(strtold_nid_postfix("1.0000000000000000001!", &end) > 1.L && *end == '!');
Require(fmodf_nid_postfix(5.5f, 2.f) == 1.5f);
Require(fmodf_nid_postfix(-5.5f, 2.f) == -1.5f);
Require(std::signbit(fmodf_nid_postfix(-4.f, 2.f)));
+20
View File
@@ -336,3 +336,23 @@ delimiter sets, repeated end-of-input calls, and independence from host strtok.
All nineteen suites pass after rebuilding the libraries. Latest unchanged-game
startup resolves strtok and stops at `88Vv-AzHVj8` (`fmodf`). The game has still
not reached its entry point or produced a rendered frame.
## Follow-up: math and floating-point parsing
Added nineteen math exports covering single-precision remainder/trigonometry,
binary scaling and decomposition, integer rounding, and value classification.
Guest lround/lroundf return explicit 64-bit integers through host llround, avoiding
Windows' 32-bit long truncation. __isinff returns one for either sign of infinity,
matching the [FreeBSD 11 implementation](https://raw.githubusercontent.com/freebsd/freebsd-src/releng/11.4/lib/libc/gen/isinf.c).
Added atof, strtof, and strtold using the host CRT through the guest calling
convention. Tests verify extended-precision results through the long-double ABI.
Tests cover negative remainders and signed zero, NaN/infinity/subnormal classes,
integer results above 32 bits, exponent output pointers, numeric parsing end
pointers, invalid input, and overflow errno. The math suite passes and the full
suite contains twenty passing tests. Exhaustive numerical accuracy, alternate
rounding modes, locale-dependent parsing, and all floating-point exception
semantics have not been validated.
The unchanged game resolves fmodf and atof. Startup now stops at `TYE4irxSmko`
(`toupper`), still before guest entry-point execution or rendered output.