xref: /llvm-project/libc/src/time/gpu/clock.cpp (revision ac604b2fa6ff0344a555954069721c0db7b874f9)
1 //===-- GPU implementation of the clock 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.h"
10 
11 #include "src/__support/common.h"
12 #include "src/__support/macros/config.h"
13 #include "src/__support/time/gpu/time_utils.h"
14 
15 namespace LIBC_NAMESPACE_DECL {
16 
17 LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
18   if (!GPU_CLOCKS_PER_SEC)
19     return clock_t(0);
20 
21   uint64_t ticks = gpu::fixed_frequency_clock();
22 
23   // We need to convert between the GPU's fixed frequency and whatever `time.h`
24   // declares it to be. This is done so that dividing the result of this
25   // function by 'CLOCKS_PER_SEC' yields the elapsed time.
26   if (GPU_CLOCKS_PER_SEC > CLOCKS_PER_SEC)
27     return clock_t(ticks / (GPU_CLOCKS_PER_SEC / CLOCKS_PER_SEC));
28   return clock_t(ticks * (CLOCKS_PER_SEC / GPU_CLOCKS_PER_SEC));
29 }
30 
31 } // namespace LIBC_NAMESPACE_DECL
32