Linux 4.13.16
[linux/fpc-iii.git] / include / linux / time64.h
blob980c71b3001a535fa8e304030a6fb8002726cebf
1 #ifndef _LINUX_TIME64_H
2 #define _LINUX_TIME64_H
4 #include <uapi/linux/time.h>
5 #include <linux/math64.h>
7 typedef __s64 time64_t;
8 typedef __u64 timeu64_t;
11 * This wants to go into uapi/linux/time.h once we agreed about the
12 * userspace interfaces.
14 #if __BITS_PER_LONG == 64
15 # define timespec64 timespec
16 #define itimerspec64 itimerspec
17 #else
18 struct timespec64 {
19 time64_t tv_sec; /* seconds */
20 long tv_nsec; /* nanoseconds */
23 struct itimerspec64 {
24 struct timespec64 it_interval;
25 struct timespec64 it_value;
28 #endif
30 /* Parameters used to convert the timespec values: */
31 #define MSEC_PER_SEC 1000L
32 #define USEC_PER_MSEC 1000L
33 #define NSEC_PER_USEC 1000L
34 #define NSEC_PER_MSEC 1000000L
35 #define USEC_PER_SEC 1000000L
36 #define NSEC_PER_SEC 1000000000L
37 #define FSEC_PER_SEC 1000000000000000LL
39 /* Located here for timespec[64]_valid_strict */
40 #define TIME64_MAX ((s64)~((u64)1 << 63))
41 #define KTIME_MAX ((s64)~((u64)1 << 63))
42 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
44 #if __BITS_PER_LONG == 64
46 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
48 return ts64;
51 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
53 return ts;
56 static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
58 return *its64;
61 static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
63 return *its;
66 # define timespec64_equal timespec_equal
67 # define timespec64_compare timespec_compare
68 # define set_normalized_timespec64 set_normalized_timespec
69 # define timespec64_add timespec_add
70 # define timespec64_sub timespec_sub
71 # define timespec64_valid timespec_valid
72 # define timespec64_valid_strict timespec_valid_strict
73 # define timespec64_to_ns timespec_to_ns
74 # define ns_to_timespec64 ns_to_timespec
75 # define timespec64_add_ns timespec_add_ns
77 #else
79 static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64)
81 struct timespec ret;
83 ret.tv_sec = (time_t)ts64.tv_sec;
84 ret.tv_nsec = ts64.tv_nsec;
85 return ret;
88 static inline struct timespec64 timespec_to_timespec64(const struct timespec ts)
90 struct timespec64 ret;
92 ret.tv_sec = ts.tv_sec;
93 ret.tv_nsec = ts.tv_nsec;
94 return ret;
97 static inline struct itimerspec itimerspec64_to_itimerspec(struct itimerspec64 *its64)
99 struct itimerspec ret;
101 ret.it_interval = timespec64_to_timespec(its64->it_interval);
102 ret.it_value = timespec64_to_timespec(its64->it_value);
103 return ret;
106 static inline struct itimerspec64 itimerspec_to_itimerspec64(struct itimerspec *its)
108 struct itimerspec64 ret;
110 ret.it_interval = timespec_to_timespec64(its->it_interval);
111 ret.it_value = timespec_to_timespec64(its->it_value);
112 return ret;
115 static inline int timespec64_equal(const struct timespec64 *a,
116 const struct timespec64 *b)
118 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec);
122 * lhs < rhs: return <0
123 * lhs == rhs: return 0
124 * lhs > rhs: return >0
126 static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs)
128 if (lhs->tv_sec < rhs->tv_sec)
129 return -1;
130 if (lhs->tv_sec > rhs->tv_sec)
131 return 1;
132 return lhs->tv_nsec - rhs->tv_nsec;
135 extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec);
137 static inline struct timespec64 timespec64_add(struct timespec64 lhs,
138 struct timespec64 rhs)
140 struct timespec64 ts_delta;
141 set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec,
142 lhs.tv_nsec + rhs.tv_nsec);
143 return ts_delta;
147 * sub = lhs - rhs, in normalized form
149 static inline struct timespec64 timespec64_sub(struct timespec64 lhs,
150 struct timespec64 rhs)
152 struct timespec64 ts_delta;
153 set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec,
154 lhs.tv_nsec - rhs.tv_nsec);
155 return ts_delta;
159 * Returns true if the timespec64 is norm, false if denorm:
161 static inline bool timespec64_valid(const struct timespec64 *ts)
163 /* Dates before 1970 are bogus */
164 if (ts->tv_sec < 0)
165 return false;
166 /* Can't have more nanoseconds then a second */
167 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
168 return false;
169 return true;
172 static inline bool timespec64_valid_strict(const struct timespec64 *ts)
174 if (!timespec64_valid(ts))
175 return false;
176 /* Disallow values that could overflow ktime_t */
177 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
178 return false;
179 return true;
183 * timespec64_to_ns - Convert timespec64 to nanoseconds
184 * @ts: pointer to the timespec64 variable to be converted
186 * Returns the scalar nanosecond representation of the timespec64
187 * parameter.
189 static inline s64 timespec64_to_ns(const struct timespec64 *ts)
191 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
195 * ns_to_timespec64 - Convert nanoseconds to timespec64
196 * @nsec: the nanoseconds value to be converted
198 * Returns the timespec64 representation of the nsec parameter.
200 extern struct timespec64 ns_to_timespec64(const s64 nsec);
203 * timespec64_add_ns - Adds nanoseconds to a timespec64
204 * @a: pointer to timespec64 to be incremented
205 * @ns: unsigned nanoseconds value to be added
207 * This must always be inlined because its used from the x86-64 vdso,
208 * which cannot call other kernel functions.
210 static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns)
212 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns);
213 a->tv_nsec = ns;
216 #endif
219 * timespec64_add_safe assumes both values are positive and checks for
220 * overflow. It will return TIME64_MAX in case of overflow.
222 extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs,
223 const struct timespec64 rhs);
225 #endif /* _LINUX_TIME64_H */