Subversion Repositories Kolibri OS

Rev

Rev 5270 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6082 serge 1
/*
2
 *  linux/kernel/time.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 *
6
 *  This file contains the interface functions for the various
7
 *  time related system calls: time, stime, gettimeofday, settimeofday,
8
 *			       adjtime
9
 */
10
/*
11
 * Modification history kernel/time.c
12
 *
13
 * 1993-09-02    Philip Gladstone
14
 *      Created file with time related functions from sched/core.c and adjtimex()
15
 * 1993-10-08    Torsten Duwe
16
 *      adjtime interface update and CMOS clock write code
17
 * 1995-08-13    Torsten Duwe
18
 *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
19
 * 1999-01-16    Ulrich Windl
20
 *	Introduced error checking for many cases in adjtimex().
21
 *	Updated NTP code according to technical memorandum Jan '96
22
 *	"A Kernel Model for Precision Timekeeping" by Dave Mills
23
 *	Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
24
 *	(Even though the technical memorandum forbids it)
25
 * 2004-07-14	 Christoph Lameter
26
 *	Added getnstimeofday to allow the posix timer functions to return
27
 *	with nanosecond accuracy
28
 */
29
 
5270 serge 30
#include 
6082 serge 31
#include 
32
#include 
3031 serge 33
 
34
 
35
 
36
#define HZ_TO_MSEC_MUL32 0xA0000000
37
#define HZ_TO_MSEC_ADJ32 0x0
38
#define HZ_TO_MSEC_SHR32 28
39
#define HZ_TO_MSEC_MUL64 0xA000000000000000
40
#define HZ_TO_MSEC_ADJ64 0x0
41
#define HZ_TO_MSEC_SHR64 60
42
#define MSEC_TO_HZ_MUL32 0xCCCCCCCD
43
#define MSEC_TO_HZ_ADJ32 0x733333333
44
#define MSEC_TO_HZ_SHR32 35
45
#define MSEC_TO_HZ_MUL64 0xCCCCCCCCCCCCCCCD
46
#define MSEC_TO_HZ_ADJ64 0x73333333333333333
47
#define MSEC_TO_HZ_SHR64 67
48
#define HZ_TO_MSEC_NUM 10
49
#define HZ_TO_MSEC_DEN 1
50
#define MSEC_TO_HZ_NUM 1
51
#define MSEC_TO_HZ_DEN 10
52
 
53
#define HZ_TO_USEC_MUL32 0x9C400000
54
#define HZ_TO_USEC_ADJ32 0x0
55
#define HZ_TO_USEC_SHR32 18
56
#define HZ_TO_USEC_MUL64 0x9C40000000000000
57
#define HZ_TO_USEC_ADJ64 0x0
58
#define HZ_TO_USEC_SHR64 50
59
#define USEC_TO_HZ_MUL32 0xD1B71759
60
#define USEC_TO_HZ_ADJ32 0x1FFF2E48E8A7
61
#define USEC_TO_HZ_SHR32 45
62
#define USEC_TO_HZ_MUL64 0xD1B71758E219652C
63
#define USEC_TO_HZ_ADJ64 0x1FFF2E48E8A71DE69AD4
64
#define USEC_TO_HZ_SHR64 77
65
#define HZ_TO_USEC_NUM 10000
66
#define HZ_TO_USEC_DEN 1
67
#define USEC_TO_HZ_NUM 1
68
#define USEC_TO_HZ_DEN 10000
69
 
70
 
71
#define MSEC_PER_SEC    1000L
72
#define USEC_PER_MSEC   1000L
73
#define NSEC_PER_USEC   1000L
74
#define NSEC_PER_MSEC   1000000L
75
#define USEC_PER_SEC    1000000L
76
#define NSEC_PER_SEC    1000000000L
77
#define FSEC_PER_SEC    1000000000000000LL
78
 
6082 serge 79
# define USER_HZ        100
80
/*
81
 * Convert jiffies to milliseconds and back.
82
 *
83
 * Avoid unnecessary multiplications/divisions in the
84
 * two most common HZ cases:
85
 */
3031 serge 86
unsigned int jiffies_to_msecs(const unsigned long j)
87
{
88
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
6082 serge 89
	return (MSEC_PER_SEC / HZ) * j;
3031 serge 90
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
6082 serge 91
	return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
3031 serge 92
#else
93
# if BITS_PER_LONG == 32
6082 serge 94
	return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32;
3031 serge 95
# else
6082 serge 96
	return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN;
3031 serge 97
# endif
98
#endif
99
}
6082 serge 100
EXPORT_SYMBOL(jiffies_to_msecs);
3031 serge 101
 
102
unsigned int jiffies_to_usecs(const unsigned long j)
103
{
104
#if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
105
        return (USEC_PER_SEC / HZ) * j;
106
#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
107
        return (j + (HZ / USEC_PER_SEC) - 1)/(HZ / USEC_PER_SEC);
108
#else
109
# if BITS_PER_LONG == 32
6082 serge 110
	return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
3031 serge 111
# else
6082 serge 112
	return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
3031 serge 113
# endif
114
#endif
115
}
6082 serge 116
EXPORT_SYMBOL(jiffies_to_usecs);
3031 serge 117
 
6082 serge 118
/**
119
 * timespec_trunc - Truncate timespec to a granularity
120
 * @t: Timespec
121
 * @gran: Granularity in ns.
122
 *
123
 * Truncate a timespec to a granularity. Always rounds down. gran must
124
 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
125
 */
126
struct timespec timespec_trunc(struct timespec t, unsigned gran)
127
{
128
	/* Avoid division in the common cases 1 ns and 1 s. */
129
	if (gran == 1) {
130
		/* nothing */
131
	} else if (gran == NSEC_PER_SEC) {
132
		t.tv_nsec = 0;
133
	} else if (gran > 1 && gran < NSEC_PER_SEC) {
134
		t.tv_nsec -= t.tv_nsec % gran;
135
	} else {
136
		WARN(1, "illegal file time granularity: %u", gran);
137
	}
138
	return t;
139
}
140
EXPORT_SYMBOL(timespec_trunc);
3031 serge 141
 
142
/*
6082 serge 143
 * mktime64 - Converts date to seconds.
144
 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
145
 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
146
 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
3031 serge 147
 *
6082 serge 148
 * [For the Julian calendar (which was used in Russia before 1917,
149
 * Britain & colonies before 1752, anywhere else before 1582,
150
 * and is still in use by some communities) leave out the
151
 * -year/100+year/400 terms, and add 10.]
3031 serge 152
 *
6082 serge 153
 * This algorithm was first published by Gauss (I think).
154
 */
155
time64_t mktime64(const unsigned int year0, const unsigned int mon0,
156
		const unsigned int day, const unsigned int hour,
157
		const unsigned int min, const unsigned int sec)
158
{
159
	unsigned int mon = mon0, year = year0;
160
 
161
	/* 1..12 -> 11,12,1..10 */
162
	if (0 >= (int) (mon -= 2)) {
163
		mon += 12;	/* Puts Feb last since it has leap day */
164
		year -= 1;
165
	}
166
 
167
	return ((((time64_t)
168
		  (year/4 - year/100 + year/400 + 367*mon/12 + day) +
169
		  year*365 - 719499
170
	    )*24 + hour /* now have hours */
171
	  )*60 + min /* now have minutes */
172
	)*60 + sec; /* finally seconds */
173
}
174
EXPORT_SYMBOL(mktime64);
175
 
176
/**
177
 * set_normalized_timespec - set timespec sec and nsec parts and normalize
3031 serge 178
 *
6082 serge 179
 * @ts:		pointer to timespec variable to be set
180
 * @sec:	seconds to set
181
 * @nsec:	nanoseconds to set
3031 serge 182
 *
6082 serge 183
 * Set seconds and nanoseconds field of a timespec variable and
184
 * normalize to the timespec storage format
185
 *
186
 * Note: The tv_nsec part is always in the range of
187
 *	0 <= tv_nsec < NSEC_PER_SEC
188
 * For negative values only the tv_sec field is negative !
3031 serge 189
 */
6082 serge 190
void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec)
3031 serge 191
{
6082 serge 192
	while (nsec >= NSEC_PER_SEC) {
193
		/*
194
		 * The following asm() prevents the compiler from
195
		 * optimising this loop into a modulo operation. See
196
		 * also __iter_div_u64_rem() in include/linux/time.h
197
		 */
198
		asm("" : "+rm"(nsec));
199
		nsec -= NSEC_PER_SEC;
200
		++sec;
201
	}
202
	while (nsec < 0) {
203
		asm("" : "+rm"(nsec));
204
		nsec += NSEC_PER_SEC;
205
		--sec;
206
	}
207
	ts->tv_sec = sec;
208
	ts->tv_nsec = nsec;
209
}
210
EXPORT_SYMBOL(set_normalized_timespec);
3031 serge 211
 
6082 serge 212
/**
213
 * ns_to_timespec - Convert nanoseconds to timespec
214
 * @nsec:       the nanoseconds value to be converted
215
 *
216
 * Returns the timespec representation of the nsec parameter.
217
 */
218
struct timespec ns_to_timespec(const s64 nsec)
219
{
220
	struct timespec ts;
221
	s32 rem;
3031 serge 222
 
6082 serge 223
	if (!nsec)
224
		return (struct timespec) {0, 0};
3031 serge 225
 
6082 serge 226
	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
227
	if (unlikely(rem < 0)) {
228
		ts.tv_sec--;
229
		rem += NSEC_PER_SEC;
230
	}
231
	ts.tv_nsec = rem;
232
 
233
	return ts;
3031 serge 234
}
6082 serge 235
EXPORT_SYMBOL(ns_to_timespec);
3031 serge 236
 
6082 serge 237
/**
238
 * ns_to_timeval - Convert nanoseconds to timeval
239
 * @nsec:       the nanoseconds value to be converted
240
 *
241
 * Returns the timeval representation of the nsec parameter.
242
 */
243
struct timeval ns_to_timeval(const s64 nsec)
3031 serge 244
{
6082 serge 245
	struct timespec ts = ns_to_timespec(nsec);
246
	struct timeval tv;
247
 
248
	tv.tv_sec = ts.tv_sec;
249
	tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
250
 
251
	return tv;
252
}
253
EXPORT_SYMBOL(ns_to_timeval);
254
 
255
#if BITS_PER_LONG == 32
256
/**
257
 * set_normalized_timespec - set timespec sec and nsec parts and normalize
258
 *
259
 * @ts:		pointer to timespec variable to be set
260
 * @sec:	seconds to set
261
 * @nsec:	nanoseconds to set
262
 *
263
 * Set seconds and nanoseconds field of a timespec variable and
264
 * normalize to the timespec storage format
265
 *
266
 * Note: The tv_nsec part is always in the range of
267
 *	0 <= tv_nsec < NSEC_PER_SEC
268
 * For negative values only the tv_sec field is negative !
269
 */
270
void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)
271
{
272
	while (nsec >= NSEC_PER_SEC) {
273
		/*
274
		 * The following asm() prevents the compiler from
275
		 * optimising this loop into a modulo operation. See
276
		 * also __iter_div_u64_rem() in include/linux/time.h
277
		 */
278
		asm("" : "+rm"(nsec));
279
		nsec -= NSEC_PER_SEC;
280
		++sec;
281
	}
282
	while (nsec < 0) {
283
		asm("" : "+rm"(nsec));
284
		nsec += NSEC_PER_SEC;
285
		--sec;
286
	}
287
	ts->tv_sec = sec;
288
	ts->tv_nsec = nsec;
289
}
290
EXPORT_SYMBOL(set_normalized_timespec64);
291
 
292
/**
293
 * ns_to_timespec64 - Convert nanoseconds to timespec64
294
 * @nsec:       the nanoseconds value to be converted
295
 *
296
 * Returns the timespec64 representation of the nsec parameter.
297
 */
298
struct timespec64 ns_to_timespec64(const s64 nsec)
299
{
300
	struct timespec64 ts;
301
	s32 rem;
302
 
303
	if (!nsec)
304
		return (struct timespec64) {0, 0};
305
 
306
	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
307
	if (unlikely(rem < 0)) {
308
		ts.tv_sec--;
309
		rem += NSEC_PER_SEC;
310
	}
311
	ts.tv_nsec = rem;
312
 
313
	return ts;
314
}
315
EXPORT_SYMBOL(ns_to_timespec64);
3031 serge 316
#endif
6082 serge 317
/**
318
 * msecs_to_jiffies: - convert milliseconds to jiffies
319
 * @m:	time in milliseconds
320
 *
321
 * conversion is done as follows:
322
 *
323
 * - negative values mean 'infinite timeout' (MAX_JIFFY_OFFSET)
324
 *
325
 * - 'too large' values [that would result in larger than
326
 *   MAX_JIFFY_OFFSET values] mean 'infinite timeout' too.
327
 *
328
 * - all other values are converted to jiffies by either multiplying
329
 *   the input value by a factor or dividing it with a factor and
330
 *   handling any 32-bit overflows.
331
 *   for the details see __msecs_to_jiffies()
332
 *
333
 * msecs_to_jiffies() checks for the passed in value being a constant
334
 * via __builtin_constant_p() allowing gcc to eliminate most of the
335
 * code, __msecs_to_jiffies() is called if the value passed does not
336
 * allow constant folding and the actual conversion must be done at
337
 * runtime.
338
 * the _msecs_to_jiffies helpers are the HZ dependent conversion
339
 * routines found in include/linux/jiffies.h
340
 */
341
unsigned long __msecs_to_jiffies(const unsigned int m)
342
{
343
	/*
344
	 * Negative value, means infinite timeout:
345
	 */
346
	if ((int)m < 0)
347
		return MAX_JIFFY_OFFSET;
348
	return _msecs_to_jiffies(m);
3031 serge 349
}
6082 serge 350
EXPORT_SYMBOL(__msecs_to_jiffies);
3031 serge 351
 
6082 serge 352
unsigned long __usecs_to_jiffies(const unsigned int u)
353
{
354
	if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
355
		return MAX_JIFFY_OFFSET;
356
	return _usecs_to_jiffies(u);
357
}
358
EXPORT_SYMBOL(__usecs_to_jiffies);
359
 
5270 serge 360
/*
361
 * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
362
 * that a remainder subtract here would not do the right thing as the
363
 * resolution values don't fall on second boundries.  I.e. the line:
364
 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
365
 * Note that due to the small error in the multiplier here, this
366
 * rounding is incorrect for sufficiently large values of tv_nsec, but
367
 * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
368
 * OK.
369
 *
370
 * Rather, we just shift the bits off the right.
371
 *
372
 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
373
 * value to a scaled second value.
374
 */
375
static unsigned long
6082 serge 376
__timespec64_to_jiffies(u64 sec, long nsec)
3297 Serge 377
{
5270 serge 378
	nsec = nsec + TICK_NSEC - 1;
3297 Serge 379
 
6082 serge 380
	if (sec >= MAX_SEC_IN_JIFFIES){
381
		sec = MAX_SEC_IN_JIFFIES;
382
		nsec = 0;
383
	}
384
	return ((sec * SEC_CONVERSION) +
385
		(((u64)nsec * NSEC_CONVERSION) >>
386
		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
3297 Serge 387
 
388
}
389
 
6082 serge 390
static unsigned long
391
__timespec_to_jiffies(unsigned long sec, long nsec)
392
{
393
	return __timespec64_to_jiffies((u64)sec, nsec);
394
}
395
 
5270 serge 396
unsigned long
6082 serge 397
timespec64_to_jiffies(const struct timespec64 *value)
5270 serge 398
{
6082 serge 399
	return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec);
5270 serge 400
}
6082 serge 401
EXPORT_SYMBOL(timespec64_to_jiffies);
5270 serge 402
 
403
void
6082 serge 404
jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value)
5270 serge 405
{
406
	/*
407
	 * Convert jiffies to nanoseconds and separate with
408
	 * one divide.
409
	 */
410
	u32 rem;
411
	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
412
				    NSEC_PER_SEC, &rem);
413
	value->tv_nsec = rem;
414
}
6082 serge 415
EXPORT_SYMBOL(jiffies_to_timespec64);
5270 serge 416
 
6082 serge 417
/*
418
 * We could use a similar algorithm to timespec_to_jiffies (with a
419
 * different multiplier for usec instead of nsec). But this has a
420
 * problem with rounding: we can't exactly add TICK_NSEC - 1 to the
421
 * usec value, since it's not necessarily integral.
422
 *
423
 * We could instead round in the intermediate scaled representation
424
 * (i.e. in units of 1/2^(large scale) jiffies) but that's also
425
 * perilous: the scaling introduces a small positive error, which
426
 * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1
427
 * units to the intermediate before shifting) leads to accidental
428
 * overflow and overestimates.
429
 *
430
 * At the cost of one additional multiplication by a constant, just
431
 * use the timespec implementation.
432
 */
433
unsigned long
434
timeval_to_jiffies(const struct timeval *value)
435
{
436
	return __timespec_to_jiffies(value->tv_sec,
437
				     value->tv_usec * NSEC_PER_USEC);
438
}
439
EXPORT_SYMBOL(timeval_to_jiffies);
440
 
441
void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
442
{
443
	/*
444
	 * Convert jiffies to nanoseconds and separate with
445
	 * one divide.
446
	 */
447
	u32 rem;
448
 
449
	value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
450
				    NSEC_PER_SEC, &rem);
451
	value->tv_usec = rem / NSEC_PER_USEC;
452
}
453
EXPORT_SYMBOL(jiffies_to_timeval);
454
 
455
/*
456
 * Convert jiffies/jiffies_64 to clock_t and back.
457
 */
458
clock_t jiffies_to_clock_t(unsigned long x)
459
{
460
#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
461
# if HZ < USER_HZ
462
	return x * (USER_HZ / HZ);
463
# else
464
	return x / (HZ / USER_HZ);
465
# endif
466
#else
467
	return div_u64((u64)x * TICK_NSEC, NSEC_PER_SEC / USER_HZ);
468
#endif
469
}
470
EXPORT_SYMBOL(jiffies_to_clock_t);
471
 
472
unsigned long clock_t_to_jiffies(unsigned long x)
473
{
474
#if (HZ % USER_HZ)==0
475
	if (x >= ~0UL / (HZ / USER_HZ))
476
		return ~0UL;
477
	return x * (HZ / USER_HZ);
478
#else
479
	/* Don't worry about loss of precision here .. */
480
	if (x >= ~0UL / HZ * USER_HZ)
481
		return ~0UL;
482
 
483
	/* .. but do try to contain it here */
484
	return div_u64((u64)x * HZ, USER_HZ);
485
#endif
486
}
487
EXPORT_SYMBOL(clock_t_to_jiffies);
488
 
489
u64 jiffies_64_to_clock_t(u64 x)
490
{
491
#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0
492
# if HZ < USER_HZ
493
	x = div_u64(x * USER_HZ, HZ);
494
# elif HZ > USER_HZ
495
	x = div_u64(x, HZ / USER_HZ);
496
# else
497
	/* Nothing to do */
498
# endif
499
#else
500
	/*
501
	 * There are better ways that don't overflow early,
502
	 * but even this doesn't overflow in hundreds of years
503
	 * in 64 bits, so..
504
	 */
505
	x = div_u64(x * TICK_NSEC, (NSEC_PER_SEC / USER_HZ));
506
#endif
507
	return x;
508
}
509
EXPORT_SYMBOL(jiffies_64_to_clock_t);
510
 
511
u64 nsec_to_clock_t(u64 x)
512
{
513
#if (NSEC_PER_SEC % USER_HZ) == 0
514
	return div_u64(x, NSEC_PER_SEC / USER_HZ);
515
#elif (USER_HZ % 512) == 0
516
	return div_u64(x * USER_HZ / 512, NSEC_PER_SEC / 512);
517
#else
518
	/*
519
         * max relative error 5.7e-8 (1.8s per year) for USER_HZ <= 1024,
520
         * overflow after 64.99 years.
521
         * exact for HZ=60, 72, 90, 120, 144, 180, 300, 600, 900, ...
522
         */
523
	return div_u64(x * 9, (9ull * NSEC_PER_SEC + (USER_HZ / 2)) / USER_HZ);
524
#endif
525
}
526
 
527
/**
528
 * nsecs_to_jiffies64 - Convert nsecs in u64 to jiffies64
529
 *
530
 * @n:	nsecs in u64
531
 *
532
 * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
533
 * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
534
 * for scheduler, not for use in device drivers to calculate timeout value.
535
 *
536
 * note:
537
 *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
538
 *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
539
 */
540
u64 nsecs_to_jiffies64(u64 n)
541
{
542
#if (NSEC_PER_SEC % HZ) == 0
543
	/* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
544
	return div_u64(n, NSEC_PER_SEC / HZ);
545
#elif (HZ % 512) == 0
546
	/* overflow after 292 years if HZ = 1024 */
547
	return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
548
#else
549
	/*
550
	 * Generic case - optimized for cases where HZ is a multiple of 3.
551
	 * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
552
	 */
553
	return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
554
#endif
555
}
556
EXPORT_SYMBOL(nsecs_to_jiffies64);
557
 
558
/**
559
 * nsecs_to_jiffies - Convert nsecs in u64 to jiffies
560
 *
561
 * @n:	nsecs in u64
562
 *
563
 * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64.
564
 * And this doesn't return MAX_JIFFY_OFFSET since this function is designed
565
 * for scheduler, not for use in device drivers to calculate timeout value.
566
 *
567
 * note:
568
 *   NSEC_PER_SEC = 10^9 = (5^9 * 2^9) = (1953125 * 512)
569
 *   ULLONG_MAX ns = 18446744073.709551615 secs = about 584 years
570
 */
571
unsigned long nsecs_to_jiffies(u64 n)
572
{
573
	return (unsigned long)nsecs_to_jiffies64(n);
574
}
575
EXPORT_SYMBOL_GPL(nsecs_to_jiffies);
576
 
577
/*
578
 * Add two timespec values and do a safety check for overflow.
579
 * It's assumed that both values are valid (>= 0)
580
 */
581
struct timespec timespec_add_safe(const struct timespec lhs,
582
				  const struct timespec rhs)
583
{
584
	struct timespec res;
585
 
586
	set_normalized_timespec(&res, lhs.tv_sec + rhs.tv_sec,
587
				lhs.tv_nsec + rhs.tv_nsec);
588
 
589
	if (res.tv_sec < lhs.tv_sec || res.tv_sec < rhs.tv_sec)
590
		res.tv_sec = TIME_T_MAX;
591
 
592
	return res;
593
}
594
 
4244 Serge 595
s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
596
{
597
        u64 quotient;
598
 
599
        if (dividend < 0) {
600
                quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
601
                *remainder = -*remainder;
602
                if (divisor > 0)
603
                        quotient = -quotient;
604
        } else {
605
                quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
606
                if (divisor < 0)
607
                        quotient = -quotient;
608
        }
609
        return quotient;
610
}
611