Skip to content

Commit f4edc9b

Browse files
committed
Support gpu
1 parent 0d52071 commit f4edc9b

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

libc/src/time/gpu/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,15 @@ add_entrypoint_object(
4444
libc.hdr.types.struct_timespec
4545
.time_utils
4646
)
47+
48+
add_entrypoint_object(
49+
timespec_get
50+
SRCS
51+
timespec_get.cpp
52+
HDRS
53+
../timespec_get.h
54+
DEPENDS
55+
libc.hdr.time_macros
56+
libc.hdr.types.struct_timespec
57+
.time_utils
58+
)

libc/src/time/gpu/timespec_get.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Implementation of timespec_get for gpu ----------------------------===//
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/timespec_get.h"
10+
#include "hdr/time_macros.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(int, timespec_get, (struct timespec * ts, int base)) {
17+
if (base != TIME_MONOTONIC || !ts)
18+
return 0;
19+
20+
uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
21+
uint64_t ticks = gpu::fixed_frequency_clock();
22+
23+
ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
24+
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
25+
26+
return base;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/time/timespec_get_test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
#include "test/UnitTest/Test.h"
1414

1515
TEST(LlvmLibcTimespecGet, Utc) {
16-
#ifndef LIBC_TARGET_ARCH_IS_GPU
1716
timespec ts;
1817
int result;
1918
result = LIBC_NAMESPACE::timespec_get(&ts, TIME_UTC);
19+
#ifdef LIBC_TARGET_ARCH_IS_GPU
20+
ASSERT_EQ(result, 0);
21+
#else
2022
ASSERT_EQ(result, TIME_UTC);
2123
ASSERT_GT(ts.tv_sec, time_t(0));
2224
#endif
2325
}
2426

2527
TEST(LlvmLibcTimespecGet, Monotonic) {
26-
#ifndef LIBC_TARGET_ARCH_IS_GPU
2728
timespec ts1, ts2;
2829
int result;
2930
result = LIBC_NAMESPACE::timespec_get(&ts1, TIME_MONOTONIC);
@@ -35,14 +36,11 @@ TEST(LlvmLibcTimespecGet, Monotonic) {
3536
if (ts2.tv_sec == ts1.tv_sec) {
3637
ASSERT_GE(ts2.tv_nsec, ts1.tv_nsec);
3738
}
38-
#endif
3939
}
4040

4141
TEST(LlvmLibcTimespecGet, Unknown) {
42-
#ifndef LIBC_TARGET_ARCH_IS_GPU
4342
timespec ts;
4443
int result;
4544
result = LIBC_NAMESPACE::timespec_get(&ts, 0);
4645
ASSERT_EQ(result, 0);
47-
#endif
4846
}

0 commit comments

Comments
 (0)