Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions libc/src/__support/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
add_header_library(
units
HDRS
units.h
DEPENDS
libc.src.__support.common
libc.hdr.types.time_t
)

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${LIBC_TARGET_OS})
else()
return()
endif()

add_object_library(
Expand All @@ -8,12 +19,3 @@ add_object_library(
DEPENDS
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
)

add_header_library(
units
HDRS
units.h
DEPENDS
libc.src.__support.common
libc.hdr.types.time_t
)
22 changes: 22 additions & 0 deletions libc/src/__support/time/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
add_object_library(
time_utils
SRCS
time_utils.cpp
HDRS
time_utils.h
DEPENDS
libc.hdr.types.clock_t
libc.hdr.time_macros
)

add_entrypoint_object(
clock_gettime
SRCS
clock_gettime.cpp
HDRS
../clock_gettime.h
DEPENDS
libc.hdr.types.clockid_t
libc.hdr.types.struct_timespec
.time_utils
)
33 changes: 33 additions & 0 deletions libc/src/__support/time/gpu/clock_gettime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===---------- GPU implementation of the clock_gettime function ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/time/clock_gettime.h"

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/time/clock_gettime.h"
#include "src/__support/time/gpu/time_utils.h"

namespace LIBC_NAMESPACE_DECL {
namespace internal {
constexpr uint64_t TICKS_PER_SEC = 1000000000UL;

ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
if (clockid != CLOCK_MONOTONIC || !ts)
return cpp::unexpected(-1);

uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
uint64_t ticks = gpu::fixed_frequency_clock();

ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;

return 0;
}
} // namespace internal
} // namespace LIBC_NAMESPACE_DECL
30 changes: 4 additions & 26 deletions libc/src/time/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
add_object_library(
time_utils
SRCS
time_utils.cpp
HDRS
time_utils.h
DEPENDS
libc.hdr.types.clock_t
libc.hdr.time_macros
)

add_entrypoint_object(
clock
SRCS
Expand All @@ -18,7 +7,8 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
.time_utils
libc.src.__support.time.clock_gettime
libc.src.__support.time.gpu.time_utils
)

add_entrypoint_object(
Expand All @@ -30,19 +20,7 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
.time_utils
)

add_entrypoint_object(
clock_gettime
SRCS
clock_gettime.cpp
HDRS
../clock_gettime.h
DEPENDS
libc.hdr.types.clockid_t
libc.hdr.types.struct_timespec
.time_utils
libc.src.__support.time.gpu.time_utils
)

add_entrypoint_object(
Expand All @@ -54,5 +32,5 @@ add_entrypoint_object(
DEPENDS
libc.hdr.time_macros
libc.hdr.types.struct_timespec
.time_utils
libc.src.__support.time.gpu.time_utils
)
2 changes: 1 addition & 1 deletion libc/src/time/gpu/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "src/time/clock.h"
#include "src/__support/macros/config.h"
#include "src/time/gpu/time_utils.h"
#include "src/__support/time/gpu/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

Expand Down
17 changes: 6 additions & 11 deletions libc/src/time/gpu/clock_gettime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,18 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "time_utils.h"
#include "src/__support/time/clock_gettime.h"
#include "src/__support/time/gpu/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

constexpr uint64_t TICKS_PER_SEC = 1000000000UL;

LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, timespec *ts)) {
if (clockid != CLOCK_MONOTONIC || !ts)
return -1;

uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
uint64_t ticks = gpu::fixed_frequency_clock();

ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;

return 0;
ErrorOr<int> result = internal::clock_gettime(clockid, ts);
if (result)
return result.value();
return result.error();
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/time/gpu/nanosleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "src/time/nanosleep.h"

#include "src/__support/macros/config.h"
#include "time_utils.h"
#include "src/__support/time/gpu/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

Expand Down
Loading