From a75fc765d3a5e40d02a69f21b4cf29e25cb041dc Mon Sep 17 00:00:00 2001 From: William <113542065+saturn691@users.noreply.github.com> Date: Fri, 6 Jun 2025 21:05:14 +0100 Subject: [PATCH] [libc] Add definitions to embedded AArch32/AArch64 (#142597) Add `CLOCKS_PER_SEC` and the older `CLK_TCK`. Allows the user to define a `__CLK_TCK` to override if necessary. Also add an extra column for embedded AArch64 in `time.rst` --- libc/docs/headers/time.rst | 132 +++++++++--------- .../llvm-libc-macros/baremetal/CMakeLists.txt | 5 + .../llvm-libc-macros/baremetal/time-macros.h | 26 ++++ libc/include/llvm-libc-macros/time-macros.h | 6 + 4 files changed, 104 insertions(+), 65 deletions(-) create mode 100644 libc/include/llvm-libc-macros/baremetal/CMakeLists.txt create mode 100644 libc/include/llvm-libc-macros/baremetal/time-macros.h diff --git a/libc/docs/headers/time.rst b/libc/docs/headers/time.rst index de82d80a2bec..9733a176fbb2 100644 --- a/libc/docs/headers/time.rst +++ b/libc/docs/headers/time.rst @@ -42,72 +42,74 @@ Implementation Status * To check date and time functions enabled for embedded system: + - `barebone-aarch64 `_ + - `barebone-aarch32 `_ - barebone-riscv32 - to be added -+---------------------+-----------------------------------------------+-------------------+-------------------+-------------------+-------------------+ -| | Linux | Windows | MacOS | Embedded | GPU | -| +---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| | x86_64 | aarch64 | aarch32 | riscv64/riscv32 | x86_64 | aarch64 | x86_64 | aarch64 | aarch32 | riscv32 | AMD | nVidia | -+=====================+=========+=========+=========+=================+=========+=========+=========+=========+=========+=========+=========+=========+ -| asctime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| asctime_r | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| ctime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| ctime_r | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock_getcpuclockid | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock_getres | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock_gettime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock_nanosleep | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| clock_settime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| ctime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| ctime_r | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| difftime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| getdate | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| gettimeofday | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| gmtime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| gmtime_r | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| localtime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| localtime_r | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| mktime | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| nanosleep | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| strftime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| strptime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| time | |check| | |check| | | |check| | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| timer_create | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| timer_delete | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| timer_gettime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| timer_getoverrun | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| timer_settime | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ -| tzset | | | | | | | | | | | | | -+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+ ++---------------------+-----------------------------------------------+-------------------+-------------------+-----------------------------+---------+---------+ +| | Linux | Windows | MacOS | Embedded | GPU | +| +---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| | x86_64 | aarch64 | aarch32 | riscv64/riscv32 | x86_64 | aarch64 | x86_64 | aarch64 | aarch64 | aarch32 | riscv32 | AMD | nVidia | ++=====================+=========+=========+=========+=================+=========+=========+=========+=========+=========+=========+=========+=========+=========+ +| asctime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| asctime_r | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| ctime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| ctime_r | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock_getcpuclockid | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock_getres | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock_gettime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock_nanosleep | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| clock_settime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| ctime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| ctime_r | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| difftime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| getdate | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| gettimeofday | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| gmtime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| gmtime_r | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| localtime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| localtime_r | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| mktime | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| nanosleep | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| strftime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| strptime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| time | |check| | |check| | | |check| | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| timer_create | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| timer_delete | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| timer_gettime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| timer_getoverrun | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| timer_settime | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ +| tzset | | | | | | | | | | | | | | ++---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+ \ No newline at end of file diff --git a/libc/include/llvm-libc-macros/baremetal/CMakeLists.txt b/libc/include/llvm-libc-macros/baremetal/CMakeLists.txt new file mode 100644 index 000000000000..ea08c63c0030 --- /dev/null +++ b/libc/include/llvm-libc-macros/baremetal/CMakeLists.txt @@ -0,0 +1,5 @@ +add_header( + time_macros + HDR + time-macros.h +) diff --git a/libc/include/llvm-libc-macros/baremetal/time-macros.h b/libc/include/llvm-libc-macros/baremetal/time-macros.h new file mode 100644 index 000000000000..3537376c4bca --- /dev/null +++ b/libc/include/llvm-libc-macros/baremetal/time-macros.h @@ -0,0 +1,26 @@ +//===-- Definition of macros from time.h ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_MACROS_BAREMETAL_TIME_MACROS_H +#define LLVM_LIBC_MACROS_BAREMETAL_TIME_MACROS_H + +#ifdef __CLK_TCK +#define CLOCKS_PER_SEC __CLK_TCK +#else +#if defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \ + defined(__arm64__) || defined(_M_ARM64) +// This default implementation of this function shall use semihosting +// Semihosting measures time in centiseconds +// https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst#sys-clock-0x10 +#define CLOCKS_PER_SEC 100 +#else +#define CLOCKS_PER_SEC 1000000 +#endif +#endif + +#endif // LLVM_LIBC_MACROS_BAREMETAL_TIME_MACROS_H diff --git a/libc/include/llvm-libc-macros/time-macros.h b/libc/include/llvm-libc-macros/time-macros.h index 445d8b3e837e..30e0a310a548 100644 --- a/libc/include/llvm-libc-macros/time-macros.h +++ b/libc/include/llvm-libc-macros/time-macros.h @@ -5,8 +5,14 @@ #include "gpu/time-macros.h" #elif defined(__linux__) #include "linux/time-macros.h" +#elif defined(__ELF__) +#include "baremetal/time-macros.h" +#else +#define CLOCKS_PER_SEC 1000000 #endif +#define CLK_TCK CLOCKS_PER_SEC + #define TIME_UTC 1 #define TIME_MONOTONIC 2 #define TIME_ACTIVE 3