summaryrefslogtreecommitdiff
path: root/cpu/sysdeps/unix/sysv/linux/i386/cpu-time-linux.h
blob: 2bf31d955583a2b5aca92bc09e555f3fc9739c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/***************************************************************
  CPU-TIME-LINUX.H

       Linux/i386/ILP32 conversion helpers for the MCPU time ABI.

       PART OF : CPU - library .

       USAGE   : Internal only .

       Copyright (C) 2026  by Andrew V.Kosteltsev.
       All Rights Reserved.
 ***************************************************************/

#ifndef   __CPU_TIME_LINUX_H
#define   __CPU_TIME_LINUX_H

#include <errno.h>
#include <time.h>


static int
__cpu_clockid_to_host( __mpu_uint64_t clockid, clockid_t *host_clockid )
{
  if( !host_clockid ) return( __MCPU_EFAULT );

  switch( clockid )
  {
  case __CPU_CLOCK_REALTIME:
    *host_clockid = CLOCK_REALTIME;
    return( 0 );
  case __CPU_CLOCK_MONOTONIC:
    *host_clockid = CLOCK_MONOTONIC;
    return( 0 );
  default:
    return( __MCPU_EINVAL );
  }

} /* End of __cpu_clockid_to_host() */


static int
__cpu_timespec_from_host( struct __cpu_timespec *dst,
                          const struct timespec *src )
{
  __mpu_int64_t sec;
  __mpu_int64_t nsec;

  if( !dst || !src ) return( __MCPU_EFAULT );

  sec = (__mpu_int64_t)src->tv_sec;
  if( (time_t)sec != src->tv_sec ) return( __MCPU_EOVERFLOW );

  nsec = (__mpu_int64_t)src->tv_nsec;
  if( (long)nsec != src->tv_nsec ) return( __MCPU_EOVERFLOW );
  if( nsec < 0 || nsec > 999999999 ) return( __MCPU_EOVERFLOW );

  dst->tv_sec = sec;
  dst->tv_nsec = nsec;

  return( 0 );

} /* End of __cpu_timespec_from_host() */

#endif /* __CPU_TIME_LINUX_H */