vserver 1.9.3
[linux-2.6.git] / include / linux / times.h
1 #ifndef _LINUX_TIMES_H
2 #define _LINUX_TIMES_H
3
4 #ifdef __KERNEL__
5 #include <linux/timex.h>
6 #include <asm/div64.h>
7 #include <asm/types.h>
8 #include <asm/param.h>
9
10 static inline clock_t jiffies_to_clock_t(long x)
11 {
12 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
13         return x / (HZ / USER_HZ);
14 #else
15         u64 tmp = (u64)x * TICK_NSEC;
16         do_div(tmp, (NSEC_PER_SEC / USER_HZ));
17         return (long)tmp;
18 #endif
19 }
20
21 static inline unsigned long clock_t_to_jiffies(unsigned long x)
22 {
23 #if (HZ % USER_HZ)==0
24         if (x >= ~0UL / (HZ / USER_HZ))
25                 return ~0UL;
26         return x * (HZ / USER_HZ);
27 #else
28         u64 jif;
29
30         /* Don't worry about loss of precision here .. */
31         if (x >= ~0UL / HZ * USER_HZ)
32                 return ~0UL;
33
34         /* .. but do try to contain it here */
35         jif = x * (u64) HZ;
36         do_div(jif, USER_HZ);
37         return jif;
38 #endif
39 }
40
41 static inline u64 jiffies_64_to_clock_t(u64 x)
42 {
43 #if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
44         do_div(x, HZ / USER_HZ);
45 #else
46         /*
47          * There are better ways that don't overflow early,
48          * but even this doesn't overflow in hundreds of years
49          * in 64 bits, so..
50          */
51         x *= TICK_NSEC;
52         do_div(x, (NSEC_PER_SEC / USER_HZ));
53 #endif
54         return x;
55 }
56 #endif
57
58 static inline u64 nsec_to_clock_t(u64 x)
59 {
60 #if (NSEC_PER_SEC % USER_HZ) == 0
61         do_div(x, (NSEC_PER_SEC / USER_HZ));
62 #elif (USER_HZ % 512) == 0
63         x *= USER_HZ/512;
64         do_div(x, (NSEC_PER_SEC / 512));
65 #else
66         /*
67          * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
68          * overflow after 64.99 years.
69          * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
70          */
71         x *= 9;
72         do_div(x, (unsigned long)((9ull * NSEC_PER_SEC + (USER_HZ/2))
73                                   / USER_HZ));
74 #endif
75         return x;
76 }
77
78 struct tms {
79         clock_t tms_utime;
80         clock_t tms_stime;
81         clock_t tms_cutime;
82         clock_t tms_cstime;
83 };
84
85 #endif