Files
linux/tools/include/nolibc/sys/time.h
Thomas Weißschuh 7efd15d22a tools/nolibc/gettimeofday: avoid libgcc 64-bit divisions
timespec::tv_nsec is going to be 64-bit wide even on 32-bit
architectures. As not all architectures support 64-bit division
instructions, calls to libgcc (__divdi3()) may be emitted by the
compiler which are not provided by nolibc.

As tv_nsec is guaranteed to always fit into an uint32_t, perform a
32-bit division instead.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20251220-nolibc-uapi-types-v3-6-c662992f75d7@weissschuh.net
2026-01-04 10:29:01 +01:00

46 lines
954 B
C

/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
/*
* time definitions for NOLIBC
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
*/
/* make sure to include all global symbols */
#include "../nolibc.h"
#ifndef _NOLIBC_SYS_TIME_H
#define _NOLIBC_SYS_TIME_H
#include "../arch.h"
#include "../sys.h"
static int sys_clock_gettime(clockid_t clockid, struct timespec *tp);
/*
* int gettimeofday(struct timeval *tv, struct timezone *tz);
*/
static __attribute__((unused))
int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
{
(void) tz; /* Non-NULL tz is undefined behaviour */
struct timespec tp;
int ret;
ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
if (!ret && tv) {
tv->tv_sec = tp.tv_sec;
tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
}
return ret;
}
static __attribute__((unused))
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
return __sysret(sys_gettimeofday(tv, tz));
}
#endif /* _NOLIBC_SYS_TIME_H */