[POWERPC] Move RTAS exports next to their declarations
[linux-2.6/x86.git] / include / linux / ktime.h
blobed3396dcc4f753d0d7cdfe8780c679f7315bc65d
1 /*
2 * include/linux/ktime.h
4 * ktime_t - nanosecond-resolution time format.
6 * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
9 * data type definitions, declarations, prototypes and macros.
11 * Started by: Thomas Gleixner and Ingo Molnar
13 * Credits:
15 * Roman Zippel provided the ideas and primary code snippets of
16 * the ktime_t union and further simplifications of the original
17 * code.
19 * For licencing details see kernel-base/COPYING
21 #ifndef _LINUX_KTIME_H
22 #define _LINUX_KTIME_H
24 #include <linux/time.h>
25 #include <linux/jiffies.h>
28 * ktime_t:
30 * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers
31 * internal representation of time values in scalar nanoseconds. The
32 * design plays out best on 64-bit CPUs, where most conversions are
33 * NOPs and most arithmetic ktime_t operations are plain arithmetic
34 * operations.
36 * On 32-bit CPUs an optimized representation of the timespec structure
37 * is used to avoid expensive conversions from and to timespecs. The
38 * endian-aware order of the tv struct members is choosen to allow
39 * mathematical operations on the tv64 member of the union too, which
40 * for certain operations produces better code.
42 * For architectures with efficient support for 64/32-bit conversions the
43 * plain scalar nanosecond based representation can be selected by the
44 * config switch CONFIG_KTIME_SCALAR.
46 typedef union {
47 s64 tv64;
48 #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
49 struct {
50 # ifdef __BIG_ENDIAN
51 s32 sec, nsec;
52 # else
53 s32 nsec, sec;
54 # endif
55 } tv;
56 #endif
57 } ktime_t;
59 #define KTIME_MAX (~((u64)1 << 63))
62 * ktime_t definitions when using the 64-bit scalar representation:
65 #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)
67 /**
68 * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
69 * @secs: seconds to set
70 * @nsecs: nanoseconds to set
72 * Return the ktime_t representation of the value
74 static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
76 return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
79 /* Subtract two ktime_t variables. rem = lhs -rhs: */
80 #define ktime_sub(lhs, rhs) \
81 ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; })
83 /* Add two ktime_t variables. res = lhs + rhs: */
84 #define ktime_add(lhs, rhs) \
85 ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
88 * Add a ktime_t variable and a scalar nanosecond value.
89 * res = kt + nsval:
91 #define ktime_add_ns(kt, nsval) \
92 ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
94 /* convert a timespec to ktime_t format: */
95 static inline ktime_t timespec_to_ktime(struct timespec ts)
97 return ktime_set(ts.tv_sec, ts.tv_nsec);
100 /* convert a timeval to ktime_t format: */
101 static inline ktime_t timeval_to_ktime(struct timeval tv)
103 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
106 /* Map the ktime_t to timespec conversion to ns_to_timespec function */
107 #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)
109 /* Map the ktime_t to timeval conversion to ns_to_timeval function */
110 #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
112 /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
113 #define ktime_to_ns(kt) ((kt).tv64)
115 #else
118 * Helper macros/inlines to get the ktime_t math right in the timespec
119 * representation. The macros are sometimes ugly - their actual use is
120 * pretty okay-ish, given the circumstances. We do all this for
121 * performance reasons. The pure scalar nsec_t based code was nice and
122 * simple, but created too many 64-bit / 32-bit conversions and divisions.
124 * Be especially aware that negative values are represented in a way
125 * that the tv.sec field is negative and the tv.nsec field is greater
126 * or equal to zero but less than nanoseconds per second. This is the
127 * same representation which is used by timespecs.
129 * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC
132 /* Set a ktime_t variable to a value in sec/nsec representation: */
133 static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
135 return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
139 * ktime_sub - subtract two ktime_t variables
140 * @lhs: minuend
141 * @rhs: subtrahend
143 * Returns the remainder of the substraction
145 static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs)
147 ktime_t res;
149 res.tv64 = lhs.tv64 - rhs.tv64;
150 if (res.tv.nsec < 0)
151 res.tv.nsec += NSEC_PER_SEC;
153 return res;
157 * ktime_add - add two ktime_t variables
158 * @add1: addend1
159 * @add2: addend2
161 * Returns the sum of addend1 and addend2
163 static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
165 ktime_t res;
167 res.tv64 = add1.tv64 + add2.tv64;
169 * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
170 * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
172 * it's equivalent to:
173 * tv.nsec -= NSEC_PER_SEC
174 * tv.sec ++;
176 if (res.tv.nsec >= NSEC_PER_SEC)
177 res.tv64 += (u32)-NSEC_PER_SEC;
179 return res;
183 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
184 * @kt: addend
185 * @nsec: the scalar nsec value to add
187 * Returns the sum of kt and nsec in ktime_t format
189 extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);
192 * timespec_to_ktime - convert a timespec to ktime_t format
193 * @ts: the timespec variable to convert
195 * Returns a ktime_t variable with the converted timespec value
197 static inline ktime_t timespec_to_ktime(const struct timespec ts)
199 return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec,
200 .nsec = (s32)ts.tv_nsec } };
204 * timeval_to_ktime - convert a timeval to ktime_t format
205 * @tv: the timeval variable to convert
207 * Returns a ktime_t variable with the converted timeval value
209 static inline ktime_t timeval_to_ktime(const struct timeval tv)
211 return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec,
212 .nsec = (s32)tv.tv_usec * 1000 } };
216 * ktime_to_timespec - convert a ktime_t variable to timespec format
217 * @kt: the ktime_t variable to convert
219 * Returns the timespec representation of the ktime value
221 static inline struct timespec ktime_to_timespec(const ktime_t kt)
223 return (struct timespec) { .tv_sec = (time_t) kt.tv.sec,
224 .tv_nsec = (long) kt.tv.nsec };
228 * ktime_to_timeval - convert a ktime_t variable to timeval format
229 * @kt: the ktime_t variable to convert
231 * Returns the timeval representation of the ktime value
233 static inline struct timeval ktime_to_timeval(const ktime_t kt)
235 return (struct timeval) {
236 .tv_sec = (time_t) kt.tv.sec,
237 .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) };
241 * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds
242 * @kt: the ktime_t variable to convert
244 * Returns the scalar nanoseconds representation of kt
246 static inline u64 ktime_to_ns(const ktime_t kt)
248 return (u64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec;
251 #endif
254 * The resolution of the clocks. The resolution value is returned in
255 * the clock_getres() system call to give application programmers an
256 * idea of the (in)accuracy of timers. Timer values are rounded up to
257 * this resolution values.
259 #define KTIME_REALTIME_RES (ktime_t){ .tv64 = TICK_NSEC }
260 #define KTIME_MONOTONIC_RES (ktime_t){ .tv64 = TICK_NSEC }
262 /* Get the monotonic time in timespec format: */
263 extern void ktime_get_ts(struct timespec *ts);
265 /* Get the real (wall-) time in timespec format: */
266 #define ktime_get_real_ts(ts) getnstimeofday(ts)
268 #endif