1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_TIME64_H
3 #define _LINUX_TIME64_H
5 #include <uapi/linux/time.h>
6 #include <linux/math64.h>
8 typedef __s64 time64_t
;
9 typedef __u64 timeu64_t
;
11 #if __BITS_PER_LONG == 64
12 /* this trick allows us to optimize out timespec64_to_timespec */
13 # define timespec64 timespec
14 #define itimerspec64 itimerspec
17 time64_t tv_sec
; /* seconds */
18 long tv_nsec
; /* nanoseconds */
22 struct timespec64 it_interval
;
23 struct timespec64 it_value
;
28 /* Parameters used to convert the timespec values: */
29 #define MSEC_PER_SEC 1000L
30 #define USEC_PER_MSEC 1000L
31 #define NSEC_PER_USEC 1000L
32 #define NSEC_PER_MSEC 1000000L
33 #define USEC_PER_SEC 1000000L
34 #define NSEC_PER_SEC 1000000000L
35 #define FSEC_PER_SEC 1000000000000000LL
37 /* Located here for timespec[64]_valid_strict */
38 #define TIME64_MAX ((s64)~((u64)1 << 63))
39 #define KTIME_MAX ((s64)~((u64)1 << 63))
40 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
42 static inline int timespec64_equal(const struct timespec64
*a
,
43 const struct timespec64
*b
)
45 return (a
->tv_sec
== b
->tv_sec
) && (a
->tv_nsec
== b
->tv_nsec
);
49 * lhs < rhs: return <0
50 * lhs == rhs: return 0
51 * lhs > rhs: return >0
53 static inline int timespec64_compare(const struct timespec64
*lhs
, const struct timespec64
*rhs
)
55 if (lhs
->tv_sec
< rhs
->tv_sec
)
57 if (lhs
->tv_sec
> rhs
->tv_sec
)
59 return lhs
->tv_nsec
- rhs
->tv_nsec
;
62 extern void set_normalized_timespec64(struct timespec64
*ts
, time64_t sec
, s64 nsec
);
64 static inline struct timespec64
timespec64_add(struct timespec64 lhs
,
65 struct timespec64 rhs
)
67 struct timespec64 ts_delta
;
68 set_normalized_timespec64(&ts_delta
, lhs
.tv_sec
+ rhs
.tv_sec
,
69 lhs
.tv_nsec
+ rhs
.tv_nsec
);
74 * sub = lhs - rhs, in normalized form
76 static inline struct timespec64
timespec64_sub(struct timespec64 lhs
,
77 struct timespec64 rhs
)
79 struct timespec64 ts_delta
;
80 set_normalized_timespec64(&ts_delta
, lhs
.tv_sec
- rhs
.tv_sec
,
81 lhs
.tv_nsec
- rhs
.tv_nsec
);
86 * Returns true if the timespec64 is norm, false if denorm:
88 static inline bool timespec64_valid(const struct timespec64
*ts
)
90 /* Dates before 1970 are bogus */
93 /* Can't have more nanoseconds then a second */
94 if ((unsigned long)ts
->tv_nsec
>= NSEC_PER_SEC
)
99 static inline bool timespec64_valid_strict(const struct timespec64
*ts
)
101 if (!timespec64_valid(ts
))
103 /* Disallow values that could overflow ktime_t */
104 if ((unsigned long long)ts
->tv_sec
>= KTIME_SEC_MAX
)
110 * timespec64_to_ns - Convert timespec64 to nanoseconds
111 * @ts: pointer to the timespec64 variable to be converted
113 * Returns the scalar nanosecond representation of the timespec64
116 static inline s64
timespec64_to_ns(const struct timespec64
*ts
)
118 return ((s64
) ts
->tv_sec
* NSEC_PER_SEC
) + ts
->tv_nsec
;
122 * ns_to_timespec64 - Convert nanoseconds to timespec64
123 * @nsec: the nanoseconds value to be converted
125 * Returns the timespec64 representation of the nsec parameter.
127 extern struct timespec64
ns_to_timespec64(const s64 nsec
);
130 * timespec64_add_ns - Adds nanoseconds to a timespec64
131 * @a: pointer to timespec64 to be incremented
132 * @ns: unsigned nanoseconds value to be added
134 * This must always be inlined because its used from the x86-64 vdso,
135 * which cannot call other kernel functions.
137 static __always_inline
void timespec64_add_ns(struct timespec64
*a
, u64 ns
)
139 a
->tv_sec
+= __iter_div_u64_rem(a
->tv_nsec
+ ns
, NSEC_PER_SEC
, &ns
);
144 * timespec64_add_safe assumes both values are positive and checks for
145 * overflow. It will return TIME64_MAX in case of overflow.
147 extern struct timespec64
timespec64_add_safe(const struct timespec64 lhs
,
148 const struct timespec64 rhs
);
150 #endif /* _LINUX_TIME64_H */