Skip to content

Commit 8beec9f

Browse files
[libc] implement a64l (#128758)
Implement the posix function a64l. Standard: https://pubs.opengroup.org/onlinepubs/9799919799/functions/a64l.html
1 parent f4a8018 commit 8beec9f

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ set(TARGET_LIBC_ENTRYPOINTS
177177
libc.src.stdbit.stdc_trailing_zeros_us
178178

179179
# stdlib.h entrypoints
180+
libc.src.stdlib.a64l
180181
libc.src.stdlib.abs
181182
libc.src.stdlib.atof
182183
libc.src.stdlib.atoi

libc/include/stdlib.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ functions:
2424
return_type: _Noreturn void
2525
arguments:
2626
- type: int
27+
- name: a64l
28+
standards:
29+
- posix
30+
return_type: long
31+
arguments:
32+
- type: const char *
2733
- name: abort
2834
standards:
2935
- stdc

libc/src/stdlib/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ add_entrypoint_object(
184184
libc.src.__support.str_to_integer
185185
)
186186

187+
add_entrypoint_object(
188+
a64l
189+
SRCS
190+
a64l.cpp
191+
HDRS
192+
a64l.h
193+
DEPENDS
194+
libc.src.__support.ctype_utils
195+
libc.hdr.types.size_t
196+
)
197+
187198
add_entrypoint_object(
188199
abs
189200
SRCS

libc/src/stdlib/a64l.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===-- Implementation of a64l --------------------------------------------===//
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/a64l.h"
10+
#include "hdr/types/size_t.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/ctype_utils.h"
13+
#include "src/__support/macros/config.h"
14+
15+
#include <stdint.h>
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
// I'm not sure this should go in ctype_utils since the specific ordering of
20+
// base64 is so very implementation specific, and also this set is unusual.
21+
// Returns -1 on any char without a specified value.
22+
constexpr static int32_t b64_char_to_int(char ch) {
23+
// from the standard: "The characters used to represent digits are '.' (dot)
24+
// for 0, '/' for 1, '0' through '9' for [2,11], 'A' through 'Z' for [12,37],
25+
// and 'a' through 'z' for [38,63]."
26+
if (ch == '.')
27+
return 0;
28+
if (ch == '/')
29+
return 1;
30+
31+
// handle the case of an unspecified char.
32+
if (!internal::isalnum(ch))
33+
return -1;
34+
35+
bool is_lower = internal::islower(ch);
36+
// add 2 to account for '.' and '/', then b36_char_to_int is case insensitive
37+
// so add case sensitivity back.
38+
return internal::b36_char_to_int(ch) + 2 + (is_lower ? 26 : 0);
39+
}
40+
41+
// This function takes a base 64 string and writes it to the low 32 bits of a
42+
// long.
43+
LLVM_LIBC_FUNCTION(long, a64l, (const char *s)) {
44+
// the standard says to only use up to 6 characters.
45+
constexpr size_t MAX_LENGTH = 6;
46+
int32_t result = 0;
47+
48+
for (size_t i = 0; i < MAX_LENGTH && s[i] != '\0'; ++i) {
49+
int32_t cur_val = b64_char_to_int(s[i]);
50+
// The standard says what happens on an unspecified character is undefined,
51+
// here we treat it as the end of the string.
52+
if (cur_val == -1)
53+
break;
54+
55+
// the first digit is the least significant, so for each subsequent digit we
56+
// shift it more. 6 bits since 2^6 = 64
57+
result += (cur_val << (6 * i));
58+
}
59+
60+
// standard says to sign extend from 32 bits.
61+
return static_cast<long>(result);
62+
}
63+
64+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/a64l.h

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

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ add_libc_test(
221221
${strfrom_test_copts}
222222
)
223223

224+
add_libc_test(
225+
a64l_test
226+
SUITE
227+
libc-stdlib-tests
228+
SRCS
229+
a64l_test.cpp
230+
DEPENDS
231+
libc.src.stdlib.a64l
232+
)
233+
224234
add_libc_test(
225235
abs_test
226236
SUITE

libc/test/src/stdlib/a64l_test.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===-- Unittests for a64l ------------------------------------------------===//
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/a64l.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
TEST(LlvmLibcA64lTest, EmptyString) { ASSERT_EQ(LIBC_NAMESPACE::a64l(""), 0l); }
13+
TEST(LlvmLibcA64lTest, FullString) {
14+
ASSERT_EQ(LIBC_NAMESPACE::a64l("AbC12/"), 1141696972l);
15+
}
16+
17+
constexpr char B64_CHARS[64] = {
18+
'.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
19+
'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
20+
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
21+
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
22+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
23+
};
24+
25+
TEST(LlvmLibcA64lTest, OneCharacter) {
26+
char example_str[2] = {'\0', '\0'};
27+
28+
for (size_t i = 0; i < 64; ++i) {
29+
example_str[0] = B64_CHARS[i];
30+
ASSERT_EQ(LIBC_NAMESPACE::a64l(example_str), static_cast<long>(i));
31+
}
32+
}
33+
34+
TEST(LlvmLibcA64lTest, TwoCharacters) {
35+
char example_str[3] = {'\0', '\0', '\0'};
36+
37+
for (size_t first = 0; first < 64; ++first) {
38+
example_str[0] = B64_CHARS[first];
39+
for (size_t second = 0; second < 64; ++second) {
40+
example_str[1] = B64_CHARS[second];
41+
42+
ASSERT_EQ(LIBC_NAMESPACE::a64l(example_str),
43+
static_cast<long>(first + (second * 64)));
44+
}
45+
}
46+
}
47+
48+
TEST(LlvmLibcA64lTest, FiveSameCharacters) {
49+
// Technically the last digit can be parsed to give the last two bits. Not
50+
// handling that here.
51+
char example_str[6] = {
52+
'\0', '\0', '\0', '\0', '\0', '\0',
53+
};
54+
55+
// set every 6th bit
56+
const long BASE_NUM = 0b1000001000001000001000001;
57+
58+
for (size_t char_val = 0; char_val < 64; ++char_val) {
59+
for (size_t i = 0; i < 5; ++i)
60+
example_str[i] = B64_CHARS[char_val];
61+
62+
const long expected_result = BASE_NUM * char_val;
63+
64+
ASSERT_EQ(LIBC_NAMESPACE::a64l(example_str), expected_result);
65+
}
66+
}
67+
68+
TEST(LlvmLibcA64lTest, OneOfSixCharacters) {
69+
char example_str[7] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'};
70+
71+
for (size_t cur_char = 0; cur_char < 6; ++cur_char) {
72+
// clear the string, set all the chars to b64(0)
73+
for (size_t i = 0; i < 6; ++i)
74+
example_str[i] = B64_CHARS[0];
75+
76+
for (size_t char_val = 0; char_val < 64; ++char_val) {
77+
example_str[cur_char] = B64_CHARS[char_val];
78+
79+
// Need to limit to 32 bits, since that's what the standard says the
80+
// function does.
81+
const long expected_result =
82+
static_cast<int32_t>(char_val << (6 * cur_char));
83+
84+
ASSERT_EQ(LIBC_NAMESPACE::a64l(example_str), expected_result);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)