xref: /llvm-project/libc/src/__support/time/gpu/clock_gettime.cpp (revision e6cf5d2863b77895ae7183952514bedd9e8dde16)
1*e6cf5d28SSchrodinger ZHU Yifan //===---------- GPU implementation of the clock_gettime function ----------===//
2*e6cf5d28SSchrodinger ZHU Yifan //
3*e6cf5d28SSchrodinger ZHU Yifan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e6cf5d28SSchrodinger ZHU Yifan // See https://llvm.org/LICENSE.txt for license information.
5*e6cf5d28SSchrodinger ZHU Yifan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e6cf5d28SSchrodinger ZHU Yifan //
7*e6cf5d28SSchrodinger ZHU Yifan //===----------------------------------------------------------------------===//
8*e6cf5d28SSchrodinger ZHU Yifan 
9*e6cf5d28SSchrodinger ZHU Yifan #include "src/time/clock_gettime.h"
10*e6cf5d28SSchrodinger ZHU Yifan 
11*e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/common.h"
12*e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/macros/config.h"
13*e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/time/clock_gettime.h"
14*e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/time/gpu/time_utils.h"
15*e6cf5d28SSchrodinger ZHU Yifan 
16*e6cf5d28SSchrodinger ZHU Yifan namespace LIBC_NAMESPACE_DECL {
17*e6cf5d28SSchrodinger ZHU Yifan namespace internal {
18*e6cf5d28SSchrodinger ZHU Yifan constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
19*e6cf5d28SSchrodinger ZHU Yifan 
20*e6cf5d28SSchrodinger ZHU Yifan ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
21*e6cf5d28SSchrodinger ZHU Yifan   if (clockid != CLOCK_MONOTONIC || !ts)
22*e6cf5d28SSchrodinger ZHU Yifan     return cpp::unexpected(-1);
23*e6cf5d28SSchrodinger ZHU Yifan 
24*e6cf5d28SSchrodinger ZHU Yifan   uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
25*e6cf5d28SSchrodinger ZHU Yifan   uint64_t ticks = gpu::fixed_frequency_clock();
26*e6cf5d28SSchrodinger ZHU Yifan 
27*e6cf5d28SSchrodinger ZHU Yifan   ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
28*e6cf5d28SSchrodinger ZHU Yifan   ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
29*e6cf5d28SSchrodinger ZHU Yifan 
30*e6cf5d28SSchrodinger ZHU Yifan   return 0;
31*e6cf5d28SSchrodinger ZHU Yifan }
32*e6cf5d28SSchrodinger ZHU Yifan } // namespace internal
33*e6cf5d28SSchrodinger ZHU Yifan } // namespace LIBC_NAMESPACE_DECL
34