xref: /aosp_15_r20/external/llvm-libc/src/time/gpu/clock_gettime.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===---------- GPU implementation of the POSIX clock_gettime 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_gettime.h"
10 
11 #include "src/__support/common.h"
12 #include "src/__support/macros/config.h"
13 #include "time_utils.h"
14 
15 namespace LIBC_NAMESPACE_DECL {
16 
17 constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
18 
19 LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, timespec *ts)) {
20   if (clockid != CLOCK_MONOTONIC || !ts)
21     return -1;
22 
23   uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
24   uint64_t ticks = gpu::fixed_frequency_clock();
25 
26   ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
27   ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
28 
29   return 0;
30 }
31 
32 } // namespace LIBC_NAMESPACE_DECL
33