[libc] Add str_to_* and rpc_* shared tests. (#190351)

Also fix several things for LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE to
make them build.
This commit is contained in:
lntue
2026-04-03 16:34:23 -04:00
committed by GitHub
parent 1deab59f6f
commit c34c0442ee
12 changed files with 215 additions and 10 deletions

View File

@@ -798,7 +798,9 @@ template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
return FPType::IEEE754_Binary64;
else if constexpr (LDBL_MANT_DIG == 64)
return FPType::X86_Binary80;
else if constexpr (LDBL_MANT_DIG == 113)
// TODO: properly treat double-double type.
// else if constexpr (LDBL_MANT_DIG == 113)
else
return FPType::IEEE754_Binary128;
}
#if defined(LIBC_TYPES_HAS_FLOAT16)

View File

@@ -43,8 +43,10 @@
#define LIBC_COPT_PRINTF_DISABLE_WIDE
#endif
// TODO: Make printf and our internal tools able to force the long double types
// properly.
// The 'long double' type is 8 bytes.
#define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
// #define LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
#include "shared/rpc.h"
#include "shared/rpc_opcodes.h"
@@ -226,7 +228,7 @@ LIBC_INLINE static void handle_printf(rpc::Server::Port &port,
writer.write(cur_section.raw_string);
}
}
buffer_size[lane] = writer.get_chars_written();
buffer_size[lane] = static_cast<int>(writer.get_chars_written());
}
// Receive any strings from the client and push them into a buffer.

View File

@@ -606,6 +606,7 @@ public:
};
#if !defined(LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64) && \
!defined(LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE) && \
!defined(LIBC_COPT_FLOAT_TO_STR_NO_SPECIALIZE_LD)
// --------------------------- LONG DOUBLE FUNCTIONS ---------------------------

View File

@@ -29,7 +29,11 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using StorageType = UInt128;
#else
using StorageType = fputil::FPBits<long double>::StorageType;
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using DecimalString = IntegerToString<intmax_t>;
using ExponentString =
IntegerToString<intmax_t, radix::Dec::WithWidth<2>::WithSign>;
@@ -243,7 +247,9 @@ template <WriteMode write_mode> class FloatWriter {
// -exponent will never overflow because all long double types we support
// have at most 15 bits of mantissa and the C standard defines an int as
// being at least 16 bits.
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
static_assert(fputil::FPBits<long double>::EXP_LEN < (sizeof(int) * 8));
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
public:
LIBC_INLINE FloatWriter(Writer<write_mode> *init_writer,
@@ -1127,6 +1133,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer<write_mode> *writer,
template <WriteMode write_mode>
LIBC_INLINE int convert_float_decimal(Writer<write_mode> *writer,
const FormatSection &to_conv) {
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
@@ -1134,7 +1141,9 @@ LIBC_INLINE int convert_float_decimal(Writer<write_mode> *writer,
return convert_float_decimal_typed<long double>(writer, to_conv,
float_bits);
}
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
@@ -1149,6 +1158,7 @@ LIBC_INLINE int convert_float_decimal(Writer<write_mode> *writer,
template <WriteMode write_mode>
LIBC_INLINE int convert_float_dec_exp(Writer<write_mode> *writer,
const FormatSection &to_conv) {
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
@@ -1156,7 +1166,9 @@ LIBC_INLINE int convert_float_dec_exp(Writer<write_mode> *writer,
return convert_float_dec_exp_typed<long double>(writer, to_conv,
float_bits);
}
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);
@@ -1171,6 +1183,7 @@ LIBC_INLINE int convert_float_dec_exp(Writer<write_mode> *writer,
template <WriteMode write_mode>
LIBC_INLINE int convert_float_dec_auto(Writer<write_mode> *writer,
const FormatSection &to_conv) {
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
@@ -1178,7 +1191,9 @@ LIBC_INLINE int convert_float_dec_auto(Writer<write_mode> *writer,
return convert_float_dec_auto_typed<long double>(writer, to_conv,
float_bits);
}
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);

View File

@@ -59,7 +59,11 @@ namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
enum class ConversionType { E, F, G };
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using StorageType = UInt128;
#else
using StorageType = fputil::FPBits<long double>::StorageType;
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
constexpr unsigned MAX_DIGITS = 39;
constexpr size_t DF_BITS = 320;
@@ -633,14 +637,17 @@ template <WriteMode write_mode>
LIBC_INLINE int convert_float_outer(Writer<write_mode> *writer,
const FormatSection &to_conv,
ConversionType ctype) {
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
if (!float_bits.is_inf_or_nan()) {
return convert_float_typed<long double>(writer, to_conv, float_bits,
ctype);
}
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);

View File

@@ -28,14 +28,21 @@ namespace printf_core {
template <WriteMode write_mode>
LIBC_INLINE int convert_float_hex_exp(Writer<write_mode> *writer,
const FormatSection &to_conv) {
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using LDBits = fputil::FPBits<double>;
using StorageType = LDBits::StorageType;
#else
using LDBits = fputil::FPBits<long double>;
using StorageType = LDBits::StorageType;
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
bool is_negative;
int exponent;
StorageType mantissa;
bool is_inf_or_nan;
uint32_t fraction_bits;
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fraction_bits = LDBits::FRACTION_LEN;
LDBits::StorageType float_raw = to_conv.conv_val_raw;
@@ -44,7 +51,9 @@ LIBC_INLINE int convert_float_hex_exp(Writer<write_mode> *writer,
exponent = float_bits.get_explicit_exponent();
mantissa = float_bits.get_explicit_mantissa();
is_inf_or_nan = float_bits.is_inf_or_nan();
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
using LBits = fputil::FPBits<double>;
fraction_bits = LBits::FRACTION_LEN;
LBits::StorageType float_raw =

View File

@@ -22,7 +22,11 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
using StorageType = UInt128;
#else
using StorageType = fputil::FPBits<long double>::StorageType;
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
template <WriteMode write_mode>
LIBC_INLINE int convert_inf_nan(Writer<write_mode> *writer,
@@ -31,12 +35,15 @@ LIBC_INLINE int convert_inf_nan(Writer<write_mode> *writer,
// the appropriate case based on the case of the conversion.
bool is_negative;
StorageType mantissa;
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
if (to_conv.length_modifier == LengthModifier::L) {
fputil::FPBits<long double>::StorageType float_raw = to_conv.conv_val_raw;
fputil::FPBits<long double> float_bits(float_raw);
is_negative = float_bits.is_neg();
mantissa = float_bits.get_mantissa();
} else {
} else
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
{
fputil::FPBits<double>::StorageType float_raw =
static_cast<fputil::FPBits<double>::StorageType>(to_conv.conv_val_raw);
fputil::FPBits<double> float_bits(float_raw);

View File

@@ -15,6 +15,7 @@
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/types.h"
#include "src/__support/str_to_integer.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/printf_config.h"
@@ -40,9 +41,11 @@ template <typename T> struct int_type_of {
template <> struct int_type_of<double> {
using type = fputil::FPBits<double>::StorageType;
};
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
template <> struct int_type_of<long double> {
using type = fputil::FPBits<long double>::StorageType;
};
#endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
#ifdef LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT
template <typename T>
@@ -254,7 +257,9 @@ public:
if (lm != LengthModifier::L) {
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, double, conv_index);
} else {
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, long double, conv_index);
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
}
break;
#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
@@ -493,8 +498,10 @@ private:
// Floating point numbers are stored separately from the other arguments.
else if (cur_type_desc == type_desc_from_type<double>())
args_cur.template next_var<double>();
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
else if (cur_type_desc == type_desc_from_type<long double>())
args_cur.template next_var<long double>();
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
#ifdef LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT
// Floating point numbers may be stored separately from the other
@@ -660,8 +667,10 @@ private:
case ('G'):
if (lm != LengthModifier::L)
conv_size = type_desc_from_type<double>();
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
else
conv_size = type_desc_from_type<long double>();
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
break;
#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
#ifdef LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT

View File

@@ -83,11 +83,13 @@ LIBC_INLINE void write_float_with_length(char *str,
*reinterpret_cast<double *>(output_ptr) = value;
break;
}
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
case (LengthModifier::L): {
auto value = internal::strtofloatingpoint<long double>(str);
*reinterpret_cast<long double *>(output_ptr) = value;
break;
}
#endif // !LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
default: {
auto value = internal::strtofloatingpoint<float>(str);
*reinterpret_cast<float *>(output_ptr) = value;

View File

@@ -276,3 +276,29 @@ add_fp_unittest(
libc.src.__support.math.ceill
libc.src.__support.math.log
)
add_fp_unittest(
shared_str_to_num_test
SUITE
libc-shared-tests
SRCS
shared_str_to_num_test.cpp
DEPENDS
libc.src.__support.str_to_float
libc.src.__support.str_to_integer
)
if(NOT MSVC AND NOT LLVM_LIBC_FULL_BUILD)
add_fp_unittest(
shared_rpc_test
SUITE
libc-shared-tests
SRCS
shared_rpc_test.cpp
DEPENDS
libc.src.stdio.printf_core.converter
libc.src.stdio.printf_core.parser
libc.src.stdio.printf_core.writer
libc.src.__support.arg_list
)
endif()

View File

@@ -0,0 +1,69 @@
//===-- Unittests for shared RPC server -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "shared/rpc.h"
#include "shared/rpc_server.h"
#include "test/UnitTest/Test.h"
TEST(LlvmLibcSharedRpcTest, TestIncrement) {
constexpr uint32_t port_count = 8;
constexpr uint32_t lane_size = 4;
alignas(64)
uint8_t buffer[::rpc::Server::allocation_size(lane_size, port_count)] = {
0};
using ProcAType = ::rpc::Process<false>;
using ProcBType = ::rpc::Process<true>;
ProcAType ProcA(port_count, buffer);
ProcBType ProcB(port_count, buffer);
uint32_t index = 0; // any < port_count
uint64_t lane_mask = 1;
// Each process has its own local lock for index
EXPECT_TRUE(ProcA.try_lock(lane_mask, index));
EXPECT_TRUE(ProcB.try_lock(lane_mask, index));
// All zero to begin with
EXPECT_EQ(ProcA.load_inbox(lane_mask, index), 0u);
EXPECT_EQ(ProcB.load_inbox(lane_mask, index), 0u);
EXPECT_EQ(ProcA.load_outbox(lane_mask, index), 0u);
EXPECT_EQ(ProcB.load_outbox(lane_mask, index), 0u);
// Available for ProcA and not for ProcB
EXPECT_FALSE(ProcA.buffer_unavailable(ProcA.load_inbox(lane_mask, index),
ProcA.load_outbox(lane_mask, index)));
EXPECT_TRUE(ProcB.buffer_unavailable(ProcB.load_inbox(lane_mask, index),
ProcB.load_outbox(lane_mask, index)));
// ProcA write to outbox
uint32_t ProcAOutbox = ProcA.load_outbox(lane_mask, index);
EXPECT_EQ(ProcAOutbox, 0u);
ProcAOutbox = ProcA.invert_outbox(lane_mask, index, ProcAOutbox);
EXPECT_EQ(ProcAOutbox, 1u);
// No longer available for ProcA
EXPECT_TRUE(ProcA.buffer_unavailable(ProcA.load_inbox(lane_mask, index),
ProcAOutbox));
// Outbox is still zero, hasn't been written to
EXPECT_EQ(ProcB.load_outbox(lane_mask, index), 0u);
// Wait for ownership will terminate because load_inbox returns 1
EXPECT_EQ(ProcB.load_inbox(lane_mask, index), 1u);
ProcB.wait_for_ownership(lane_mask, index, 0u, 0u);
// and B now has the buffer available
EXPECT_FALSE(ProcB.buffer_unavailable(ProcB.load_inbox(lane_mask, index),
ProcB.load_outbox(lane_mask, index)));
// Enough checks for one test, close the locks
ProcA.unlock(lane_mask, index);
ProcB.unlock(lane_mask, index);
}

View File

@@ -0,0 +1,56 @@
//===-- Unittests for shared string to number functions -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "shared/str_to_float.h"
#include "shared/str_to_integer.h"
#include "test/UnitTest/Test.h"
namespace LIBC_NAMESPACE_DECL {
TEST(LlvmLibcSharedStrToNumTest, IntegerTests) {
{
auto result = shared::strtointeger<int>("123", 10);
EXPECT_EQ(result.value, 123);
EXPECT_EQ(result.parsed_len, ptrdiff_t(3));
EXPECT_EQ(result.error, 0);
}
{
auto result = shared::strtointeger<int>(" -0x123", 0);
EXPECT_EQ(result.value, -0x123);
EXPECT_EQ(result.parsed_len, ptrdiff_t(8));
EXPECT_EQ(result.error, 0);
}
}
TEST(LlvmLibcSharedStrToNumTest, FloatTests) {
{
// 1.25 = 1.01b = 5 * 2^-2
shared::ExpandedFloat<double> input;
input.mantissa = 5;
input.exponent = -2;
auto result = shared::binary_exp_to_float<double>(
input, false, shared::RoundDirection::Nearest);
EXPECT_EQ(result.num.mantissa, uint64_t(0x4000000000000));
EXPECT_EQ(result.num.exponent, 1023);
EXPECT_EQ(result.error, 0);
}
{
// 1.25 = 125 * 10^-2
shared::ExpandedFloat<double> input;
input.mantissa = 125;
input.exponent = -2;
const char *str = "1.25";
auto result = shared::decimal_exp_to_float<double>(
input, false, shared::RoundDirection::Nearest, str);
EXPECT_EQ(result.num.mantissa, uint64_t(0x14000000000000));
EXPECT_EQ(result.num.exponent, 1023);
EXPECT_EQ(result.error, 0);
}
}
} // namespace LIBC_NAMESPACE_DECL