Skip to content

Commit 4081088

Browse files
committed
[libc]: Implement strfrom* functions and utils
1 parent ddff198 commit 4081088

File tree

8 files changed

+323
-1
lines changed

8 files changed

+323
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ set(TARGET_LIBC_ENTRYPOINTS
180180
libc.src.stdlib.qsort_r
181181
libc.src.stdlib.rand
182182
libc.src.stdlib.srand
183+
libc.src.stdlib.strfromf
183184
libc.src.stdlib.strtod
184185
libc.src.stdlib.strtof
185186
libc.src.stdlib.strtol

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ def StdC : StandardSpec<"stdc"> {
954954
FunctionSpec<"rand", RetValSpec<IntType>, [ArgSpec<VoidType>]>,
955955
FunctionSpec<"srand", RetValSpec<VoidType>, [ArgSpec<UnsignedIntType>]>,
956956

957+
FunctionSpec<"strfromf", RetValSpec<IntType>, [ArgSpec<CharRestrictedPtr>, ArgSpec<SizeTType>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<FloatType>]>,
958+
957959
FunctionSpec<"strtof", RetValSpec<FloatType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,
958960
FunctionSpec<"strtod", RetValSpec<DoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,
959961
FunctionSpec<"strtold", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>]>,

libc/src/stdlib/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ add_entrypoint_object(
5252
libc.config.linux.app_h
5353
)
5454

55+
add_entrypoint_object(
56+
strfromf
57+
SRCS
58+
strfromf.cpp
59+
HDRS
60+
strfromf.h
61+
DEPENDS
62+
.str_from_util
63+
)
64+
65+
add_header_library(
66+
str_from_util
67+
HDRS
68+
str_from_util.h
69+
DEPENDS
70+
libc.src.stdio.printf_core.converter_atlas
71+
libc.src.stdio.printf_core.core_structs
72+
libc.src.stdio.printf_core.writer
73+
libc.src.__support.str_to_integer
74+
libc.src.__support.CPP.type_traits
75+
)
76+
5577
add_entrypoint_object(
5678
strtof
5779
SRCS

libc/src/stdlib/str_from_util.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===-- Implementation header for strfromx() utilitites -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// According to the C23 standard, any input character sequences except a
10+
// precision specifier and the usual floating point formats, namely
11+
// %{a,A,e,E,f,F,g,G}, are not allowed and any code that does otherwise results
12+
// in undefined behaviour(including use of a '%%' conversion specifier); which
13+
// in this case is that the buffer string is simply populated with the format
14+
// string. The case of the value of buffer size being 0 or the input being NULL
15+
// should be handled in the calling function (strfromf, strfromd, strfroml)
16+
// itself.
17+
18+
#ifndef LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H
19+
#define LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H
20+
21+
#include "src/__support/CPP/type_traits.h"
22+
#include "src/__support/str_to_integer.h"
23+
#include "src/stdio/printf_core/converter_atlas.h"
24+
#include "src/stdio/printf_core/core_structs.h"
25+
#include "src/stdio/printf_core/writer.h"
26+
27+
#include <stddef.h>
28+
29+
namespace LIBC_NAMESPACE::internal {
30+
31+
template <typename T>
32+
using storage_type = typename fputil::FPBits<T>::StorageType;
33+
34+
template <typename T>
35+
printf_core::FormatSection parse_format_string(const char *__restrict format,
36+
T fp) {
37+
printf_core::FormatSection section;
38+
size_t cur_pos = 0;
39+
[[maybe_unused]] double new_fp;
40+
41+
bool t_is_single_prec_type = cpp::is_same<T, float>::value;
42+
if (t_is_single_prec_type)
43+
new_fp = (double)fp;
44+
45+
if (format[cur_pos] == '%') {
46+
section.has_conv = true;
47+
++cur_pos;
48+
49+
// handle precision
50+
section.precision = -1;
51+
if (format[cur_pos] == '.') {
52+
++cur_pos;
53+
section.precision = 0;
54+
55+
// The standard does not allow the '*' (asterisk) operator for strfromx()
56+
// functions
57+
if (internal::isdigit(format[cur_pos])) {
58+
auto result = internal::strtointeger<int>(format + cur_pos, 10);
59+
section.precision += result.value;
60+
cur_pos += result.parsed_len;
61+
}
62+
}
63+
64+
section.conv_name = format[cur_pos];
65+
switch (format[cur_pos]) {
66+
case 'a':
67+
case 'A':
68+
if (t_is_single_prec_type)
69+
section.conv_val_raw = cpp::bit_cast<storage_type<double>>(new_fp);
70+
else
71+
section.conv_val_raw = cpp::bit_cast<storage_type<T>>(fp);
72+
break;
73+
case 'e':
74+
case 'E':
75+
case 'f':
76+
case 'F':
77+
case 'g':
78+
case 'G':
79+
section.conv_val_raw = cpp::bit_cast<storage_type<T>>(fp);
80+
break;
81+
default:
82+
section.has_conv = false;
83+
while (format[cur_pos] != '\0')
84+
++cur_pos;
85+
break;
86+
}
87+
88+
if (format[cur_pos] != '\0')
89+
++cur_pos;
90+
} else {
91+
section.has_conv = false;
92+
// We are looking for exactly one section, so no more '%'
93+
while (format[cur_pos] != '\0')
94+
++cur_pos;
95+
}
96+
97+
section.raw_string = {format, cur_pos};
98+
return section;
99+
}
100+
101+
template <typename T>
102+
int strfromfloat_convert(printf_core::Writer *writer,
103+
const printf_core::FormatSection &section) {
104+
if (!section.has_conv)
105+
return writer->write(section.raw_string);
106+
107+
auto res = static_cast<storage_type<T>>(section.conv_val_raw);
108+
109+
fputil::FPBits<T> strfromfloat_bits(res);
110+
if (strfromfloat_bits.is_inf_or_nan())
111+
return convert_inf_nan(writer, section);
112+
113+
switch (section.conv_name) {
114+
case 'f':
115+
case 'F':
116+
return convert_float_decimal_typed(writer, section, strfromfloat_bits);
117+
case 'e':
118+
case 'E':
119+
return convert_float_dec_exp_typed(writer, section, strfromfloat_bits);
120+
case 'a':
121+
case 'A':
122+
return convert_float_hex_exp(writer, section);
123+
case 'g':
124+
case 'G':
125+
return convert_float_dec_auto_typed(writer, section, strfromfloat_bits);
126+
default:
127+
return writer->write(section.raw_string);
128+
}
129+
return -1;
130+
}
131+
132+
} // namespace LIBC_NAMESPACE::internal
133+
134+
#endif // LLVM_LIBC_SRC_STDLIB_STRFROM_UTIL_H

libc/src/stdlib/strfromf.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===-- Implementation of strfromf ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdlib/strfromf.h"
10+
#include "src/stdlib/str_from_util.h"
11+
12+
#include <stdarg.h>
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
LLVM_LIBC_FUNCTION(int, strfromf,
18+
(char *__restrict s, size_t n, const char *__restrict format,
19+
float fp)) {
20+
LIBC_ASSERT(s != nullptr);
21+
22+
printf_core::FormatSection section =
23+
internal::parse_format_string(format, fp);
24+
printf_core::WriteBuffer wb(s, (n > 0 ? n - 1 : 0));
25+
printf_core::Writer writer(&wb);
26+
27+
int result = 0;
28+
if (section.has_conv)
29+
result = internal::strfromfloat_convert<float>(&writer, section);
30+
else
31+
result = writer.write(section.raw_string);
32+
33+
if (result < 0)
34+
return result;
35+
36+
if (n > 0)
37+
wb.buff[wb.buff_cur] = '\0';
38+
39+
return writer.get_chars_written();
40+
}
41+
42+
} // namespace LIBC_NAMESPACE

libc/src/stdlib/strfromf.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for strfromf ------------------------*- C++--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDLIB_STRFROMF_H
10+
#define LLVM_LIBC_SRC_STDLIB_STRFROMF_H
11+
12+
#include <stddef.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
int strfromf(char *__restrict s, size_t n, const char *__restrict format,
17+
float fp);
18+
19+
} // namespace LIBC_NAMESPACE
20+
21+
#endif // LLVM_LIBC_SRC_STDLIB_STRTOF_H

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ add_libc_test(
168168
.strtol_test_support
169169
)
170170

171+
add_libc_test(
172+
strfromf_test
173+
SUITE
174+
libc-stdlib-tests
175+
SRCS
176+
strfromf_test.cpp
177+
DEPENDS
178+
libc.src.stdlib.strfromf
179+
)
180+
171181
add_libc_test(
172182
abs_test
173183
SUITE
@@ -259,7 +269,6 @@ add_libc_test(
259269
libc.src.stdlib.qsort
260270
)
261271

262-
263272
add_libc_test(
264273
qsort_r_test
265274
SUITE
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//===-- Unittests for strfromf --------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdlib/strfromf.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
TEST(LlvmLibcStrfromfTest, DecimalFloatFormat) {
13+
char buff[100];
14+
int written;
15+
16+
written = LIBC_NAMESPACE::strfromf(buff, 16, "%f", 1.0);
17+
EXPECT_EQ(written, 8);
18+
ASSERT_STREQ(buff, "1.000000");
19+
20+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%f", 1234567890);
21+
EXPECT_EQ(written, 17);
22+
ASSERT_STREQ(buff, "1234567936.000000");
23+
24+
written = LIBC_NAMESPACE::strfromf(buff, 5, "%f", "1234567890.0");
25+
EXPECT_EQ(written, 17);
26+
ASSERT_STREQ(buff, "1234");
27+
28+
written = LIBC_NAMESPACE::strfromf(buff, 67, "%.3f", 1.0);
29+
EXPECT_EQ(written, 5);
30+
ASSERT_STREQ(buff, "1.000");
31+
32+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%1f", 1234567890.0);
33+
EXPECT_EQ(written, 3);
34+
ASSERT_STREQ(buff, "%1f");
35+
}
36+
37+
TEST(LlvmLibcStrfromfTest, HexExpFloatFormat) {
38+
char buff[100];
39+
int written;
40+
41+
written = LIBC_NAMESPACE::strfromf(buff, 0, "%a", 1234567890.0);
42+
EXPECT_EQ(written, 0);
43+
44+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%a", 1234567890.0);
45+
EXPECT_EQ(written, 14);
46+
ASSERT_STREQ(buff, "0x1.26580cp+30");
47+
48+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%A", 1234567890.0);
49+
EXPECT_EQ(written, 14);
50+
ASSERT_STREQ(buff, "0X1.26580CP+30");
51+
}
52+
53+
TEST(LlvmLibcStrfromfTest, DecimalExpFloatFormat) {
54+
char buff[100];
55+
int written;
56+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9e", 1234567890.0);
57+
EXPECT_EQ(written, 15);
58+
ASSERT_STREQ(buff, "1.234567936e+09");
59+
60+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9E", 1234567890.0);
61+
EXPECT_EQ(written, 15);
62+
ASSERT_STREQ(buff, "1.234567936E+09");
63+
}
64+
65+
TEST(LlvmLibcStrfromfTest, AutoDecimalFloatFormat) {
66+
char buff[100];
67+
int written;
68+
69+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9g", 1234567890.0);
70+
EXPECT_EQ(written, 15);
71+
ASSERT_STREQ(buff, "1.23456794e+09");
72+
73+
written = LIBC_NAMESPACE::strfromf(buff, 20, "%.9G", 1234567890.0);
74+
EXPECT_EQ(written, 15);
75+
ASSERT_STREQ(buff, "1.23456794E+09");
76+
}
77+
78+
TEST(LlvmLibcStrfromfTest, ImproperFormatString) {
79+
80+
char buff[100];
81+
int retval;
82+
retval = LIBC_NAMESPACE::strfromf(
83+
buff, 37, "A simple string with no conversions.", 1.0);
84+
EXPECT_EQ(retval, 36);
85+
ASSERT_STREQ(buff, "A simple string with no conversions.");
86+
87+
retval = LIBC_NAMESPACE::strfromf(
88+
buff, 37, "%A simple string with one conversion, should overwrite.", 1.0);
89+
EXPECT_EQ(retval, 6);
90+
ASSERT_STREQ(buff, "0X1P+0");
91+
}

0 commit comments

Comments
 (0)