summaryrefslogtreecommitdiff
path: root/libc/test/src/stdio/sprintf_test.cpp
AgeCommit message (Collapse)Author
2025-06-11[libc] Move libc_errno.h to libc/src/__support and make ↵lntue
LIBC_ERRNO_MODE_SYSTEM to be header-only. (#143187) This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450
2025-02-04[libc] Alternative algorithm for decimal FP printf (#123643)Simon Tatham
The existing options for bin→dec float conversion are all based on the Ryū algorithm, which generates 9 output digits at a time using a table lookup. For users who can't afford the space cost of the table, the table-lookup subroutine is replaced with one that computes the needed table entry on demand, but the algorithm is otherwise unmodified. The performance problem with computing table entries on demand is that now you need to calculate a power of 10 for each 9 digits you output. But if you're calculating a custom power of 10 anyway, it's easier to just compute one, and multiply the _whole_ mantissa by it. This patch adds a header file alongside `float_dec_converter.h`, which replaces the whole Ryū system instead of just the table-lookup routine, implementing this alternative simpler algorithm. The result is accurate enough to satisfy (minimally) the accuracy demands of IEEE 754-2019 even in 128-bit long double. The new float128 test cases demonstrate this by testing the cases closest to the 39-digit rounding boundary. In my tests of generating 39 output digits (the maximum number supported by this algorithm) this code is also both faster and smaller than the USE_DYADIC_FLOAT version of the existing Ryū code.
2024-10-01[libc] clean up sprintf macros and float tests (#110759)Michael Jones
The sprintf tests have a macro named "ASSERT_STREQ_LEN" which was used in about half of the tests. This patch moves all of the tests which can to using that macro. This patch also enables long double tests for %e and %g, since those never got finished. There's still some work to do enabling long double testing for long doubles other than the intel 80 bit format, but that can land in a followup. The `#ifdef LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION` lines are for a followup patch.
2024-10-01[libc] Fix race conditions in sprintf_test. (#110624)lntue
2024-09-19[libc] Add printf strerror conversion (%m) (#105891)Michael Jones
This patch adds the %m conversion to printf, which prints the strerror(errno). Explanation of why is below, this patch also updates the docs, tests, and build system to accomodate this. The standard for syslog in posix specifies it uses the same format as printf, but adds %m which prints the error message string for the current value of errno. For ease of implementation, it's standard practice for libc implementers to just add %m to printf instead of creating a separate parser for syslog.
2024-07-15[libc] Fix warnings emitted by GCC (#98751)OverMighty
Fixes #98709.
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek
This is a part of #97655.
2024-07-09[libc] Fix sprintf FixedConv test (#98204)Mikhail R. Gadelha
This patch enlarges the number to ensure that a 64-bit number is being read.
2024-07-09[libc] adjust printf macro test to use all 64 bits (#98195)Michael Jones
The previous printf macro test for 64 bit octal used the number 0123, which is not large enough to ensure that the macro is actually reading a 64 bit number. This patch enlarges the number, and also makes sure the return value of sprintf is correct for the macro tests.
2024-03-29[libc] Fix missing UINTMAX_WIDTH (#87092)Michael Jones
In patch #82461 the sprintf tests were made to use UINTMAX_WIDTH which isn't defined on all systems. This patch changes it to sizeof(uintmax_t)*CHAR_BIT which is more portable.
2024-03-29Add bit width length modifier to printf (#82461)Om Prakaash
Resolves #81685. This adds support for wN and wfN length modifiers in fprintf.
2024-03-19[libc] Make 'printf' converter output "(null)" instead of "null" (#85845)Joseph Huber
Summary: Currently we print `null` for the null pointer in a `%s` expression. Although it's not defined by the standard, other implementations choose to use `(null)` to indicate this. We also currently print `(nullptr)` so I think it's more consistent to use parens in both cases.
2024-03-04[libc][NFC] Rename `LIBC_LONG_DOUBLE_IS_` macros (#83399)Guillaume Chatelet
Umbrella bug #83182
2024-02-27[libc] Add fixed point support to printf (#82707)Michael Jones
This patch adds the r, R, k, and K conversion specifiers to printf, with accompanying tests. They are guarded behind the LIBC_COPT_PRINTF_DISABLE_FIXED_POINT flag as well as automatic fixed point support detection.
2024-02-07[libc] Support C23 'b' (binary) modifier in printf (#80851)Artem Tyurin
Reference: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2612.pdf. Fixes https://github.com/llvm/llvm-project/issues/80727.
2024-02-06[libc] add inttypes macros (#80726)Schrodinger ZHU Yifan
Standard file: https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/ Notice that we are not quite the same as other implementations: 1. MUSL: https://github.com/bminor/musl/blob/master/include/inttypes.h 2. GLIBC: https://github.com/bminor/glibc/blob/bbd248ac0d75efdef8fe61ea69b1fb25fb95b6e7/stdlib/inttypes.h#L57 3. CheriBSD: https://github.com/CTSRD-CHERI/cheribsd/blob/698d1636dd1fe2322e5bc7029e415928c80b76b1/sys/arm64/include/_inttypes.h fixes #80186
2024-01-30[libc][NFC] Rename `FPBits` nan functions (#79998)Guillaume Chatelet
- [libc][NFC] Rename FPBits nan functions - rename build_signaling_nan in signaling_nan - rename build_quiet_nan to quiet_nan
2024-01-29[libc] Remove specific nan payload in math functions (#79165)Guillaume Chatelet
2024-01-25[libc] Move printf long double to simple calc (#75414)michaelrj-google
The Ryu algorithm is very fast with its table, but that table grows too large for long doubles. This patch adds a method of calculating the digits of long doubles using just wide integers and fast modulo operations. This results in significant performance improvements vs the previous int calc mode, while taking up a similar amound of peak memory. It will be slow in some %e/%g cases, but reasonable fast for %f with no loss of accuracy.
2024-01-23[reland][libc] Remove unnecessary `FPBits` functions and properties (#79128)Guillaume Chatelet
- reland #79113 - Fix aarch64 RISC-V build
2024-01-23Revert "[libc] Remove unnecessary `FPBits` functions and properties" (#79118)Guillaume Chatelet
Reverts llvm/llvm-project#79113 It broke aarch64 build bot machines.
2024-01-23[libc] Remove unnecessary `FPBits` functions and properties (#79113)Guillaume Chatelet
This patch reduces the surface of `FPBits`.
2023-12-01[libc][NFC] Rename SPECIAL_X86_LONG_DOUBLE in ↵Guillaume Chatelet
LIBC_LONG_DOUBLE_IS_X86_FLOAT80 (#73950)
2023-12-01[libc][NFC] rename LONG_DOUBLE_IS_DOUBLE into LIBC_LONG_DOUBLE_IS_FLOAT64 ↵Guillaume Chatelet
(#73948)
2023-11-23[libc][NFC] Sink "PlatformDefs.h" into "FloatProperties.h" (#73226)Guillaume Chatelet
`PlatformDefs.h` does not bring a lot of value as a separate file. It is transitively included in `FloatProperties.h` and `FPBits.h`. This patch sinks it into `FloatProperties.h` and removes the associated build targets.
2023-11-06[libc][math] Add min/max/min_denorm/max_denorm constants to FPBits and clean ↵lntue
up its constants return types. (#71298)
2023-10-30[libc] Fix printf long double truncation bound (#70705)michaelrj-google
The calculation for if a number being printed is truncated and should be rounded up assumed a double for one of its constants, causing occassional misrounding. This fixes that by making the constant based on the mantissa width.
2023-10-27[libc] Fix incorrect printing for alt mode ints (#70252)michaelrj-google
Previously, our printf would incorrectly handle conversions like ("%#x",0) and ("%#o",0). This patch corrects the behavior to match what is described in the standard.
2023-10-24[libc] Disable -NaN test on float128 systems (#70146)michaelrj-google
Some float128 systems (specifically the ones used for aarch64 buildbots) don't respect signs for long double NaNs. This patch disables the printf test that was failing due to this.
2023-10-24[libc] Fix printf long double inf, bitcast in msan (#70067)michaelrj-google
These bugs were found with the new printf long double fuzzing. The long double inf vs nan bug was introduced when we changed to get_explicit_exponent. The bitcast msan issue hadn't come up previously, but isn't a real bug, just a poisoning confusion.
2023-10-16[libc] Add simple long double to printf float fuzz (#68449)michaelrj-google
Recent testing has uncovered some hard-to-find bugs in printf's long double support. This patch adds an extra long double path to the fuzzer with minimal extra effort. While a more thorough long double fuzzer would be useful, it would need to handle the non-standard cases of 80 bit long doubles such as unnormal and pseudo-denormal numbers. For that reason, a standalone long double fuzzer is left for future development.
2023-10-04[libc] Fix typo in long double negative block (#68243)michaelrj-google
The long double version of float to string's get_negative_block had a bug in table mode. In table mode, one of the tables is named "MIN_BLOCK_2" and it stores the number of blocks that are all zeroes before the digits start for a given index. The check for long doubles was incorrectly "block_index <= MIN_BLOCK_2[idx]" when it should be "block_index < MIN_BLOCK_2[idx]" (without the equal sign). This bug caused an off-by-one error for some long double values. This patch fixes the bug and adds tests to ensure it doesn't regress.
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet
This is step 4 of https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-09-21[libc] Fix Off By One Errors In Printf Long Double (#66957)michaelrj-google
Two major off-by-one errors are fixed in this patch. The first is in float_to_string.h with length_for_num, which wasn't accounting for the implicit leading bit when calculating the length of a number, causing a missing digit on 80 bit float max. The other off-by-one is the ryu_long_double_constants.h (a.k.a the Mega Table) not having any entries for the last POW10_OFFSET in POW10_SPLIT. This was also found on 80 bit float max. Finally, the integer calculation mode was using a slightly too short integer, again on 80 bit float max, not accounting for the mantissa width. All of these are fixed in this patch.
2023-09-07[libc] Fix printf %p formatMichael Jones
The %p format wasn't correctly passing along flags and modifiers to the integer conversion behind the scenes. This patch fixes that behavior, as well as changing the nullptr behavior to be a string conversion behind the scenes. Reviewed By: lntue, jhuber6 Differential Revision: https://reviews.llvm.org/D159458
2023-08-15[libc] Fix printf %e and %g bugsMichael Jones
Fuzzing revealed bugs in the %e and %g conversions. Since these are very similar, they are grouped together. Again, most of the bugs were related to rounding. As an example, previously the code to check if the number was truncated only worked for digits below the decimal point, due to it being originally designed for %f. This patch adds a mechanism to check the digits above the decimal point for both %e and %g. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D157536
2023-08-15[libc] Fix printf %f bugsMichael Jones
Fuzzing revealed several bugs in the %f float conversion. This patch fixes them. Most of these bugs are related to rounding, such as 1.999...999 being rounded to 2.999...999 instead of 2.000...000 due to rounding up not properly changing the nines to zeros. Additionally, much of the rounding infrastructure has been refactored out so it can be shared with the other conversions. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D157535
2023-08-15[libc] Fix printf %a padding issueMichael Jones
The trailing zeroes were previously not counted when calculating the padding, which caused a high-precision number to get too much padding. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D157534
2023-08-07[libc] Add nullptr check option to printf %sMichael Jones
Some printf implementations perform a null check on pointers passed to %s. While that's not in the standard, this patch adds it as an option for compatibility. It also puts a similar check in %n behind the same flag. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D156923
2023-07-28[libc] Fix printf g conversion with high precisionMichael Jones
The number of trailing zeroes was being calculated incorrectly. It was assuming that it could add all of the implicit leading zeroes in the final block, not accounting for the number of digits actually reqested by the precision. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D156489
2023-07-26[libc][NFC] fix sprintf test on 32 bit systemsMichael Jones
The expected number for the max ptrdiff value was expected to be exactly 4294967296 (2**32) for 32 bit systems, when it should be 4294967295 (2**32 - 1). This also adds a second test to check for this case on non-32 bit systems. Reviewed By: lntue, mikhail.ramalho Differential Revision: https://reviews.llvm.org/D156257
2023-06-14[libc] Enable hermetic floating point tests again.Tue Ly
Fixing an issue with LLVM libc's fenv.h defined rounding mode macros differently from system libc, making get_round() return different values from fegetround(). Also letting math tests to skip rounding modes that cannot be set. This should allow math tests to be run on platforms in which fenv.h is not implemented yet. This allows us to re-enable hermatic floating point tests in https://reviews.llvm.org/D151123 and reverting https://reviews.llvm.org/D152742. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D152873
2023-06-08[libc] disable printf Lf tests for float128 platsMichael Jones
The results for the %Lf tests were calculated on 80 bit long double systems, meaning the results are not necessarily accurate for float128 systems. In future these will be fixed, but for the moment I'm just turning them off. Differential Revision: https://reviews.llvm.org/D152471
2023-06-08[libc] add options to printf decimal floatsMichael Jones
This patch adds three options for printf decimal long doubles, and these can also apply to doubles. 1. Use a giant table which is fast and accurate, but takes up ~5MB). 2. Use dyadic floats for approximations, which only gives ~50 digits of accuracy but is very fast. 3. Use large integers for approximations, which is accurate but very slow. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D150399
2023-04-18[libc][NFC] Move RoundingModeUtils to LibcFPTestHelpers.Siva Chandra Reddy
Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D148602
2023-03-01[libc] Fix printf %f rounding conditionMichael Jones
When running the tbin2dec tests I found a rounding error in my code. Upon inspection I realized it was due to a lack of parenthesis when calculating the number of trailing digits. This patch fixes that mistake and adds unit tests to catch regressions in future. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D145011
2023-02-15[libc][NFC] Make tuning macros start with LIBC_COPT_Guillaume Chatelet
Rename preprocessor definitions that control tuning of llvm libc. Differential Revision: https://reviews.llvm.org/D143913
2023-02-07[libc][NFC] Move UnitTest and IntegrationTest to the 'test' directory.Siva Chandra Reddy
This part of the effort to make all test related pieces into the `test` directory. This helps is excluding test related pieces in a straight forward manner if LLVM_INCLUDE_TESTS is OFF. Future patches will also move the MPFR wrapper and testutils into the 'test' directory.