Skip to content

Commit bce3cbc

Browse files
authored
[libc] Baremetal version of clock (#146417)
This is analogous to the baremetal version of timespec_get using the __llvm_libc_timespec_get_active embedding interface.
1 parent cf10a09 commit bce3cbc

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ set(TARGET_LIBC_ENTRYPOINTS
262262
# time.h entrypoints
263263
libc.src.time.asctime
264264
libc.src.time.asctime_r
265+
libc.src.time.clock
265266
libc.src.time.ctime
266267
libc.src.time.ctime_r
267268
libc.src.time.difftime

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ set(TARGET_LIBC_ENTRYPOINTS
262262
# time.h entrypoints
263263
libc.src.time.asctime
264264
libc.src.time.asctime_r
265+
libc.src.time.clock
265266
libc.src.time.ctime
266267
libc.src.time.ctime_r
267268
libc.src.time.difftime

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ set(TARGET_LIBC_ENTRYPOINTS
262262
# time.h entrypoints
263263
libc.src.time.asctime
264264
libc.src.time.asctime_r
265+
libc.src.time.clock
265266
libc.src.time.ctime
266267
libc.src.time.ctime_r
267268
libc.src.time.difftime

libc/src/time/baremetal/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
add_entrypoint_object(
2+
clock
3+
SRCS
4+
clock.cpp
5+
HDRS
6+
../clock.h
7+
DEPENDS
8+
libc.hdr.time_macros
9+
libc.hdr.types.struct_timespec
10+
)
11+
112
add_entrypoint_object(
213
timespec_get
314
SRCS

libc/src/time/baremetal/clock.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- Baremetal implementation of the clock function --------------------===//
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/time/clock.h"
10+
#include "hdr/time_macros.h"
11+
#include "hdr/types/struct_timespec.h"
12+
#include "src/__support/CPP/limits.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/macros/config.h"
15+
#include "src/__support/time/units.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
extern "C" bool __llvm_libc_timespec_get_active(struct timespec *ts);
20+
21+
LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
22+
using namespace time_units;
23+
struct timespec ts;
24+
if (!__llvm_libc_timespec_get_active(&ts))
25+
return clock_t(-1);
26+
27+
// The above call gets the CPU time in seconds plus nanoseconds.
28+
// The standard requires that we return clock_t(-1) if we cannot represent
29+
// clocks as a clock_t value.
30+
constexpr clock_t CLOCK_SECS_MAX =
31+
cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;
32+
if (ts.tv_sec > CLOCK_SECS_MAX)
33+
return clock_t(-1);
34+
if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)
35+
return clock_t(-1);
36+
37+
// For the integer computation converting tv_nsec to clocks to work
38+
// correctly, we want CLOCKS_PER_SEC to be less than 1000000000.
39+
static_assert(1_s_ns > CLOCKS_PER_SEC,
40+
"Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");
41+
return clock_t(ts.tv_sec * CLOCKS_PER_SEC +
42+
ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));
43+
}
44+
45+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)