Skip to content

Commit 92c8c87

Browse files
authored
[libc] Implement wcstod and wcstold. (#168020)
These are simply implemented as specializations of strtofloatingpoint for double / long double and for wchar_t. The unit tests are copied from the strtod / strtold ones.
1 parent 3cba379 commit 92c8c87

File tree

10 files changed

+1015
-1
lines changed

10 files changed

+1015
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,11 @@ set(TARGET_LIBC_ENTRYPOINTS
398398
libc.src.wchar.wmemchr
399399
libc.src.wchar.wcpcpy
400400
libc.src.wchar.wcpncpy
401+
libc.src.wchar.wcstod
401402
libc.src.wchar.wcstof
402403
libc.src.wchar.wcstok
403404
libc.src.wchar.wcstol
405+
libc.src.wchar.wcstold
404406
libc.src.wchar.wcstoll
405407
libc.src.wchar.wcstoul
406408
libc.src.wchar.wcstoull

libc/include/wchar.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,17 @@ functions:
367367
arguments:
368368
- type: const wchar_t *__restrict
369369
- type: wchar_t **__restrict
370+
- name: wcstod
371+
standards:
372+
- stdc
373+
return_type: double
374+
arguments:
375+
- type: const wchar_t *__restrict
376+
- type: wchar_t **__restrict
377+
- name: wcstold
378+
standards:
379+
- stdc
380+
return_type: long double
381+
arguments:
382+
- type: const wchar_t *__restrict
383+
- type: wchar_t **__restrict

libc/src/wchar/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ add_entrypoint_object(
110110
libc.src.errno.errno
111111
)
112112

113+
add_entrypoint_object(
114+
wcstod
115+
SRCS
116+
wcstod.cpp
117+
HDRS
118+
wcstod.h
119+
DEPENDS
120+
libc.src.__support.str_to_float
121+
libc.src.errno.errno
122+
)
123+
124+
add_entrypoint_object(
125+
wcstold
126+
SRCS
127+
wcstold.cpp
128+
HDRS
129+
wcstold.h
130+
DEPENDS
131+
libc.src.__support.str_to_float
132+
libc.src.errno.errno
133+
)
134+
113135
add_entrypoint_object(
114136
wcstok
115137
SRCS

libc/src/wchar/wcstod.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of wcstod ------------------------------------------===//
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/wchar/wcstod.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/libc_errno.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/str_to_float.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, wcstod,
18+
(const wchar_t *__restrict str,
19+
wchar_t **__restrict str_end)) {
20+
auto result = internal::strtofloatingpoint<double>(str);
21+
if (result.has_error())
22+
libc_errno = result.error;
23+
24+
if (str_end != nullptr)
25+
*str_end = const_cast<wchar_t *>(str + result.parsed_len);
26+
27+
return result.value;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcstod.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for wcstod ------------------------*- 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_WCHAR_WCSTOD_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSTOD_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
double wcstod(const wchar_t *__restrict str, wchar_t **__restrict str_end);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_WCHAR_WCSTOD_H

libc/src/wchar/wcstold.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of wcstold -----------------------------------------===//
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/wchar/wcstold.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/libc_errno.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/str_to_float.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(long double, wcstold,
18+
(const wchar_t *__restrict str,
19+
wchar_t **__restrict str_end)) {
20+
auto result = internal::strtofloatingpoint<long double>(str);
21+
if (result.has_error())
22+
libc_errno = result.error;
23+
24+
if (str_end != nullptr)
25+
*str_end = const_cast<wchar_t *>(str + result.parsed_len);
26+
27+
return result.value;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcstold.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcstold -----------------------*- 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_WCHAR_WCSTOLD_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSTOLD_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
long double wcstold(const wchar_t *__restrict str,
17+
wchar_t **__restrict str_end);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSTOLD_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,5 +538,32 @@ add_libc_test(
538538
DEPENDS
539539
libc.src.wchar.wcstof
540540
libc.test.UnitTest.ErrnoCheckingTest
541-
libc.test.UnitTest.LibcFPTestHelpers
541+
LINK_LIBRARIES
542+
LibcFPTestHelpers
543+
)
544+
545+
add_libc_test(
546+
wcstod_test
547+
SUITE
548+
libc_wchar_unittests
549+
SRCS
550+
wcstod_test.cpp
551+
DEPENDS
552+
libc.src.wchar.wcstod
553+
libc.test.UnitTest.ErrnoCheckingTest
554+
LINK_LIBRARIES
555+
LibcFPTestHelpers
556+
)
557+
558+
add_libc_test(
559+
wcstold_test
560+
SUITE
561+
libc_wchar_unittests
562+
SRCS
563+
wcstold_test.cpp
564+
DEPENDS
565+
libc.src.__support.FPUtil.fp_bits
566+
libc.src.__support.uint128
567+
libc.src.wchar.wcstold
568+
libc.test.UnitTest.ErrnoCheckingTest
542569
)

0 commit comments

Comments
 (0)