diff --git a/libc/src/__support/time/CMakeLists.txt b/libc/src/__support/time/CMakeLists.txt index e73f2744b15a0..8247e792e8410 100644 --- a/libc/src/__support/time/CMakeLists.txt +++ b/libc/src/__support/time/CMakeLists.txt @@ -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( @@ -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 -) diff --git a/libc/src/__support/time/gpu/CMakeLists.txt b/libc/src/__support/time/gpu/CMakeLists.txt new file mode 100644 index 0000000000000..efa6cd32454cc --- /dev/null +++ b/libc/src/__support/time/gpu/CMakeLists.txt @@ -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 +) diff --git a/libc/src/__support/time/gpu/clock_gettime.cpp b/libc/src/__support/time/gpu/clock_gettime.cpp new file mode 100644 index 0000000000000..cede72a1f35da --- /dev/null +++ b/libc/src/__support/time/gpu/clock_gettime.cpp @@ -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 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 diff --git a/libc/src/time/gpu/time_utils.cpp b/libc/src/__support/time/gpu/time_utils.cpp similarity index 100% rename from libc/src/time/gpu/time_utils.cpp rename to libc/src/__support/time/gpu/time_utils.cpp diff --git a/libc/src/time/gpu/time_utils.h b/libc/src/__support/time/gpu/time_utils.h similarity index 100% rename from libc/src/time/gpu/time_utils.h rename to libc/src/__support/time/gpu/time_utils.h diff --git a/libc/src/time/gpu/CMakeLists.txt b/libc/src/time/gpu/CMakeLists.txt index 8da5d3a22f5a0..8ad1f93c53e19 100644 --- a/libc/src/time/gpu/CMakeLists.txt +++ b/libc/src/time/gpu/CMakeLists.txt @@ -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 @@ -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( @@ -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( @@ -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 ) diff --git a/libc/src/time/gpu/clock.cpp b/libc/src/time/gpu/clock.cpp index 4cdb1d505aed2..add5b2725ef8f 100644 --- a/libc/src/time/gpu/clock.cpp +++ b/libc/src/time/gpu/clock.cpp @@ -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 { diff --git a/libc/src/time/gpu/clock_gettime.cpp b/libc/src/time/gpu/clock_gettime.cpp index de7899a2a17cc..8feac294c68f0 100644 --- a/libc/src/time/gpu/clock_gettime.cpp +++ b/libc/src/time/gpu/clock_gettime.cpp @@ -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 result = internal::clock_gettime(clockid, ts); + if (result) + return result.value(); + return result.error(); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/gpu/nanosleep.cpp b/libc/src/time/gpu/nanosleep.cpp index 3f4a609dd40eb..25a22d5703fa7 100644 --- a/libc/src/time/gpu/nanosleep.cpp +++ b/libc/src/time/gpu/nanosleep.cpp @@ -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 {