mlibc/frigg: better float printf handling

This commit is contained in:
Matheus
2026-08-13 23:13:41 -03:00
parent 52a8066610
commit 83610d9493
4 changed files with 159 additions and 23 deletions
+123
View File
@@ -0,0 +1,123 @@
diff --git frigg-clean/include/frg/formatting.hpp frigg-workdir/include/frg/formatting.hpp
index a5de22c6..04c44d9c 100644
--- frigg-clean/include/frg/formatting.hpp
+++ frigg-workdir/include/frg/formatting.hpp
@@ -341,7 +341,9 @@ namespace _fmt_basics {
void print_float(S &sink, T number, int width = 0, optional<int> precision = 6,
Char padding = ' ', bool left_justify = false, bool alt_conversion = false,
bool use_capitals = false, bool group_thousands = false, bool use_compact = false,
- bool exponential_form = false, bool print_hexfloat = false, locale_options<Char> locale_opts = {}) {
+ bool exponential_form = false, bool print_hexfloat = false,
+ bool always_sign = false, bool plus_becomes_space = false,
+ locale_options<Char> locale_opts = {}) {
using P = frg::FormatterPolicy<Char>;
auto textLength = [](int i, unsigned base = 10, bool ignoreSign = false) {
@@ -353,14 +355,20 @@ namespace _fmt_basics {
return length;
};
- bool has_sign = false;
- if (__builtin_signbit(number)) {
- has_sign = true;
- }
+ bool has_sign = __builtin_signbit(number);
+ bool show_sign = has_sign || always_sign || plus_becomes_space;
+ auto emit_sign = [&] {
+ if (has_sign)
+ sink.append('-');
+ else if (always_sign)
+ sink.append('+');
+ else if (plus_becomes_space)
+ sink.append(' ');
+ };
bool inf = __builtin_isinf(number), nan = __builtin_isnan(number);
if (inf || nan) {
- auto total_length = 3 + has_sign;
+ auto total_length = 3 + show_sign;
auto pad_length = width > total_length ? width - total_length : 0;
if (!left_justify) {
while (pad_length > 0) {
@@ -369,8 +377,7 @@ namespace _fmt_basics {
}
}
- if (has_sign)
- sink.append('-');
+ emit_sign();
if (inf)
sink.append(use_capitals ? P::infUpper : P::inf);
@@ -427,7 +434,7 @@ namespace _fmt_basics {
}
}
- auto int_length = has_sign + 3;
+ auto int_length = show_sign + 3;
int frac_length = (shift_by >> 2) - trailingZeroes;
auto exp_length = 2 + textLength(exp, 10, true);
@@ -454,8 +461,7 @@ namespace _fmt_basics {
}
}
- if (has_sign)
- sink.append('-');
+ emit_sign();
if (use_capitals)
sink.append(number == 0.0 ? (P::hexPrefixUpperZero) : (P::hexPrefixUpperOne));
@@ -580,7 +586,7 @@ namespace _fmt_basics {
auto decimal_point_length = print_decimal_point ? generic_strlen(locale_opts.decimal_point) : 0;
// Plus one for the decimal point
- int total_length = has_sign + int_length + group_sep_length + decimal_point_length + *precision;
+ int total_length = show_sign + int_length + group_sep_length + decimal_point_length + *precision;
// Handle the exponent in the style of `e+09`
if (exponential_form)
@@ -595,8 +601,7 @@ namespace _fmt_basics {
}
}
- if (has_sign)
- sink.append('-');
+ emit_sign();
print_int<S, decltype(integralDigits), Char>(sink, integralDigits, 10, 0, 1, {}, false, group_thousands, false, false, false, locale_opts);
@@ -624,7 +629,8 @@ namespace _fmt_basics {
int precision_or_default = fo.precision.has_value() ? *fo.precision : 6;
print_float(sink, object, fo.minimum_width, precision_or_default,
fo.fill_zeros ? '0' : ' ', fo.left_justify, fo.alt_conversion,
- fo.use_capitals, fo.group_thousands, fo.use_compact, fo.use_exponential);
+ fo.use_capitals, fo.group_thousands, fo.use_compact, fo.use_exponential,
+ false, fo.always_sign, fo.plus_becomes_space);
}
#endif /* __STDC_HOSTED__ */
};
diff --git frigg-clean/include/frg/printf.hpp frigg-workdir/include/frg/printf.hpp
index 259c3b61..f0263130 100644
--- frigg-clean/include/frg/printf.hpp
+++ frigg-workdir/include/frg/printf.hpp
@@ -983,7 +983,8 @@ void do_printf_floats(S &sink, Char t, format_options opts,
sink, pop_arg<long double>(vsp, &opts),
opts.minimum_width, opts.precision,
Char(opts.fill_zeros ? '0' : ' '), opts.left_justify, opts.alt_conversion,
- use_capitals, opts.group_thousands, use_compact, exponent_form, print_hexfloat, locale_opts);
+ use_capitals, opts.group_thousands, use_compact, exponent_form, print_hexfloat,
+ opts.always_sign, opts.plus_becomes_space, locale_opts);
break;
}
#endif
@@ -992,7 +993,8 @@ void do_printf_floats(S &sink, Char t, format_options opts,
sink, pop_arg<double>(vsp, &opts),
opts.minimum_width, opts.precision,
Char(opts.fill_zeros ? '0' : ' '), opts.left_justify, opts.alt_conversion, use_capitals,
- opts.group_thousands, use_compact, exponent_form, print_hexfloat, locale_opts);
+ opts.group_thousands, use_compact, exponent_form, print_hexfloat,
+ opts.always_sign, opts.plus_becomes_space, locale_opts);
break;
default:
FRG_ASSERT(!"Unexpected printf terminal");
+34 -21
View File
@@ -1,5 +1,5 @@
diff --git mlibc-clean/options/glibc/generic/stdio_ext.cpp mlibc-workdir/options/glibc/generic/stdio_ext.cpp
index ccb3725..3dfa27a 100644
index ccb37257..3dfa27af 100644
--- mlibc-clean/options/glibc/generic/stdio_ext.cpp
+++ mlibc-workdir/options/glibc/generic/stdio_ext.cpp
@@ -71,14 +71,38 @@ size_t __freadahead(FILE *file_base) {
@@ -53,7 +53,7 @@ index ccb3725..3dfa27a 100644
}
-
diff --git mlibc-clean/options/internal/generic/global-config.cpp mlibc-workdir/options/internal/generic/global-config.cpp
index 3b7b862..3d86e7a 100644
index 3b7b8627..3d86e7a4 100644
--- mlibc-clean/options/internal/generic/global-config.cpp
+++ mlibc-workdir/options/internal/generic/global-config.cpp
@@ -1,9 +1,15 @@
@@ -92,7 +92,7 @@ index 3b7b862..3d86e7a 100644
+
} // namespace mlibc
diff --git mlibc-clean/options/internal/include/mlibc/allocator.hpp mlibc-workdir/options/internal/include/mlibc/allocator.hpp
index 852620a..7f1aa89 100644
index 852620a5..7f1aa89f 100644
--- mlibc-clean/options/internal/include/mlibc/allocator.hpp
+++ mlibc-workdir/options/internal/include/mlibc/allocator.hpp
@@ -3,6 +3,7 @@
@@ -148,7 +148,7 @@ index 852620a..7f1aa89 100644
+
#endif // MLIBC_FRIGG_ALLOC
diff --git mlibc-clean/options/internal/include/mlibc/global-config.hpp mlibc-workdir/options/internal/include/mlibc/global-config.hpp
index 0235c54..3fad411 100644
index 0235c541..3fad411a 100644
--- mlibc-clean/options/internal/include/mlibc/global-config.hpp
+++ mlibc-workdir/options/internal/include/mlibc/global-config.hpp
@@ -14,11 +14,8 @@ struct GlobalConfig {
@@ -166,7 +166,7 @@ index 0235c54..3fad411 100644
#endif // MLIBC_GLOBAL_CONFIG
diff --git mlibc-clean/options/internal/include/mlibc/lock.hpp mlibc-workdir/options/internal/include/mlibc/lock.hpp
index f447741..ab44667 100644
index f4477411..ab446678 100644
--- mlibc-clean/options/internal/include/mlibc/lock.hpp
+++ mlibc-workdir/options/internal/include/mlibc/lock.hpp
@@ -14,7 +14,7 @@
@@ -179,7 +179,7 @@ index f447741..ab44667 100644
FutexLockImpl(const FutexLockImpl &) = delete;
diff --git mlibc-clean/options/internal/include/mlibc/sysdep-signatures.hpp mlibc-workdir/options/internal/include/mlibc/sysdep-signatures.hpp
index d639e43..694021a 100644
index d639e43c..694021ac 100644
--- mlibc-clean/options/internal/include/mlibc/sysdep-signatures.hpp
+++ mlibc-workdir/options/internal/include/mlibc/sysdep-signatures.hpp
@@ -23,6 +23,10 @@
@@ -210,7 +210,7 @@ index d639e43..694021a 100644
SYSDEP_FUNC(Unlockpt, int fd);
SYSDEP_FUNC(ThreadSetname, void *tcb, const char *name);
diff --git mlibc-clean/options/internal/include/mlibc/sysdep-tags.hpp mlibc-workdir/options/internal/include/mlibc/sysdep-tags.hpp
index 17cf422..01941c1 100644
index 17cf4222..01941c14 100644
--- mlibc-clean/options/internal/include/mlibc/sysdep-tags.hpp
+++ mlibc-workdir/options/internal/include/mlibc/sysdep-tags.hpp
@@ -316,6 +316,8 @@ struct Symlink {};
@@ -232,7 +232,7 @@ index 17cf422..01941c1 100644
struct Ptsname {};
// int sys_unlockpt(int fd);
diff --git mlibc-clean/options/linux/generic/malloc.cpp mlibc-workdir/options/linux/generic/malloc.cpp
index 065de6c..10c7395 100644
index 065de6c1..10c73955 100644
--- mlibc-clean/options/linux/generic/malloc.cpp
+++ mlibc-workdir/options/linux/generic/malloc.cpp
@@ -1,7 +1,13 @@
@@ -253,8 +253,21 @@ index 065de6c..10c7395 100644
+ }
+ return ptr;
}
diff --git mlibc-clean/options/posix/generic/monetary.cpp mlibc-workdir/options/posix/generic/monetary.cpp
index 55a567aa..d1d77873 100644
--- mlibc-clean/options/posix/generic/monetary.cpp
+++ mlibc-workdir/options/posix/generic/monetary.cpp
@@ -389,6 +389,8 @@ ssize_t strfmon_internal(char *__restrict s, size_t maxsize, mlibc::localeinfo *
false,
false,
false,
+ false,
+ false,
opts
);
diff --git mlibc-clean/options/posix/generic/net-if.cpp mlibc-workdir/options/posix/generic/net-if.cpp
index ed0c463..8df2580 100644
index ed0c463c..8df25802 100644
--- mlibc-clean/options/posix/generic/net-if.cpp
+++ mlibc-workdir/options/posix/generic/net-if.cpp
@@ -6,8 +6,13 @@
@@ -291,7 +304,7 @@ index ed0c463..8df2580 100644
unsigned int if_nametoindex(const char *name) {
diff --git mlibc-clean/options/posix/generic/posix_stdio.cpp mlibc-workdir/options/posix/generic/posix_stdio.cpp
index ab4f784..ba663bb 100644
index ab4f784e..ba663bb2 100644
--- mlibc-clean/options/posix/generic/posix_stdio.cpp
+++ mlibc-workdir/options/posix/generic/posix_stdio.cpp
@@ -233,7 +233,10 @@ int renameat(int olddirfd, const char *old_path, int newdirfd, const char *new_p
@@ -309,7 +322,7 @@ index ab4f784..ba663bb 100644
+ return 0;
}
diff --git mlibc-clean/options/posix/include/bits/posix/posix_stdio.h mlibc-workdir/options/posix/include/bits/posix/posix_stdio.h
index f0d6edd..ab57b6b 100644
index f0d6edd1..ab57b6b1 100644
--- mlibc-clean/options/posix/include/bits/posix/posix_stdio.h
+++ mlibc-workdir/options/posix/include/bits/posix/posix_stdio.h
@@ -63,6 +63,7 @@ char *fgetln(FILE *__stream, size_t *__size);
@@ -326,7 +339,7 @@ index f0d6edd..ab57b6b 100644
-
diff --git mlibc-clean/options/rtld/x86_64/runtime.S mlibc-workdir/options/rtld/x86_64/runtime.S
index ba9a520..b958ef3 100644
index ba9a5208..b958ef33 100644
--- mlibc-clean/options/rtld/x86_64/runtime.S
+++ mlibc-workdir/options/rtld/x86_64/runtime.S
@@ -60,4 +60,3 @@ pltRelocateStub:
@@ -335,7 +348,7 @@ index ba9a520..b958ef3 100644
.section .note.GNU-stack,"",%progbits
-
diff --git mlibc-clean/sysdeps/astral/generic/astral.cpp mlibc-workdir/sysdeps/astral/generic/astral.cpp
index a86353f..3e1a18c 100644
index a86353f3..3e1a18c2 100644
--- mlibc-clean/sysdeps/astral/generic/astral.cpp
+++ mlibc-workdir/sysdeps/astral/generic/astral.cpp
@@ -1,13 +1,25 @@
@@ -366,7 +379,7 @@ index a86353f..3e1a18c 100644
return error ? -1 : ret;
}
diff --git mlibc-clean/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
index 3b04372..ec94347 100644
index 3b043726..ec94347e 100644
--- mlibc-clean/sysdeps/astral/generic/generic.cpp
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
@@ -1,9 +1,12 @@
@@ -907,7 +920,7 @@ index 3b04372..ec94347 100644
+ }
} // namespace mlibc
diff --git mlibc-clean/sysdeps/astral/include/astral/archctl.h mlibc-workdir/sysdeps/astral/include/astral/archctl.h
index 7e4f2a1..ecc2677 100644
index 7e4f2a18..ecc26777 100644
--- mlibc-clean/sysdeps/astral/include/astral/archctl.h
+++ mlibc-workdir/sysdeps/astral/include/astral/archctl.h
@@ -5,6 +5,17 @@
@@ -929,7 +942,7 @@ index 7e4f2a1..ecc2677 100644
#ifdef __cplusplus
extern "C" {
diff --git mlibc-clean/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
index a6c4067..9040d45 100644
index a6c40676..9040d450 100644
--- mlibc-clean/sysdeps/astral/include/astral/syscall.h
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
@@ -103,6 +103,14 @@
@@ -949,7 +962,7 @@ index a6c4067..9040d45 100644
diff --git mlibc-workdir/sysdeps/astral/include/astral/sysctl.h mlibc-workdir/sysdeps/astral/include/astral/sysctl.h
new file mode 100644
index 0000000..0c70df3
index 00000000..0c70df32
--- /dev/null
+++ mlibc-workdir/sysdeps/astral/include/astral/sysctl.h
@@ -0,0 +1,59 @@
@@ -1013,7 +1026,7 @@ index 0000000..0c70df3
+
+#endif /* _ASTRAL_SYSCTL_H */
diff --git mlibc-clean/sysdeps/astral/include/mlibc/sysdeps.hpp mlibc-workdir/sysdeps/astral/include/mlibc/sysdeps.hpp
index ce91084..3e78eee 100644
index ce910847..3e78eee2 100644
--- mlibc-clean/sysdeps/astral/include/mlibc/sysdeps.hpp
+++ mlibc-workdir/sysdeps/astral/include/mlibc/sysdeps.hpp
@@ -5,10 +5,33 @@
@@ -1089,7 +1102,7 @@ index ce91084..3e78eee 100644
template<typename Tag>
diff --git mlibc-clean/sysdeps/astral/meson.build mlibc-workdir/sysdeps/astral/meson.build
index 43dd6b0..3e01e87 100644
index 43dd6b0d..3e01e87d 100644
--- mlibc-clean/sysdeps/astral/meson.build
+++ mlibc-workdir/sysdeps/astral/meson.build
@@ -86,6 +86,7 @@ if not no_headers
@@ -1102,7 +1115,7 @@ index 43dd6b0..3e01e87 100644
)
diff --git mlibc-workdir/tests/glibc/memalign.c mlibc-workdir/tests/glibc/memalign.c
new file mode 100644
index 0000000..0b3a158
index 00000000..0b3a1581
--- /dev/null
+++ mlibc-workdir/tests/glibc/memalign.c
@@ -0,0 +1,22 @@
@@ -1129,7 +1142,7 @@ index 0000000..0b3a158
+ return 0;
+}
diff --git mlibc-clean/tests/meson.build mlibc-workdir/tests/meson.build
index 9c5bcd7..8b2b95e 100644
index 9c5bcd72..8b2b95e1 100644
--- mlibc-clean/tests/meson.build
+++ mlibc-workdir/tests/meson.build
@@ -170,6 +170,7 @@ all_test_cases = [
+1 -1
View File
@@ -1,6 +1,6 @@
name=frigg
from_source=frigg
revision=2
revision=3
hostdeps="xgcc xbinutils pkgconfig"
imagedeps="meson ninja-build build-essential"
+1 -1
View File
@@ -1,6 +1,6 @@
name=mlibc
from_source=mlibc
revision=11
revision=12
hostdeps="xgcc xbinutils pkgconfig"
builddeps="freestnd-c-hdrs freestnd-cxx-hdrs frigg libsmarter"
imagedeps="meson ninja-build git build-essential binutils"