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