brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 26b75d7 Raw
46 lines · cpp
1//===-- Baremetal 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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/time/clock.h"10#include "hdr/time_macros.h"11#include "hdr/types/struct_timespec.h"12#include "src/__support/CPP/limits.h"13#include "src/__support/common.h"14#include "src/__support/macros/config.h"15#include "src/__support/time/units.h"16 17namespace LIBC_NAMESPACE_DECL {18 19extern "C" bool __llvm_libc_timespec_get_active(struct timespec *ts);20 21LLVM_LIBC_FUNCTION(clock_t, clock, ()) {22  using namespace time_units;23  struct timespec ts;24  if (!__llvm_libc_timespec_get_active(&ts))25    return clock_t(-1);26 27  // The above call gets the CPU time in seconds plus nanoseconds.28  // The standard requires that we return clock_t(-1) if we cannot represent29  // clocks as a clock_t value.30  constexpr clock_t CLOCK_SECS_MAX =31      cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;32  if (ts.tv_sec > CLOCK_SECS_MAX)33    return clock_t(-1);34  if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)35    return clock_t(-1);36 37  // For the integer computation converting tv_nsec to clocks to work38  // correctly, we want CLOCKS_PER_SEC to be less than 1000000000.39  static_assert(1_s_ns > CLOCKS_PER_SEC,40                "Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");41  return clock_t(ts.tv_sec * CLOCKS_PER_SEC +42                 ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));43}44 45} // namespace LIBC_NAMESPACE_DECL46