arith.c 2.31 KB
Newer Older
1 2
/*
 * FIXME header
3 4
 */

5
#include <pproto/pproto.h>
6

7 8 9
/* FIXME: This is a temp workaround. How to define it? */
#define PP_INT_MAX 2147483647

10
void int64_to_TimeInternal(Integer64 bigint, TimeInternal *internal)
11
{
12
	int64_t bigint_val;
13

14 15
	bigint_val = bigint.lsb;
	bigint_val+= ((int64_t)bigint.msb) << 32;
16

17 18
	internal->nanoseconds = bigint_val % PP_NSEC_PER_SEC;
	internal->seconds = bigint_val / PP_NSEC_PER_SEC;
19 20
}

21
void from_TimeInternal(TimeInternal *internal, Timestamp *external)
22 23 24 25 26 27 28 29 30 31 32
{
	/*
	 * fromInternalTime is only used to convert time given by the system
	 * to a timestamp As a consequence, no negative value can normally
	 * be found in (internal)
	 * 
	 * Note that offsets are also represented with TimeInternal structure,
	 * and can be negative, but offset are never convert into Timestamp
	 * so there is no problem here.
	 */

33 34
	if ((internal->seconds & ~PP_INT_MAX) ||
	    (internal->nanoseconds & ~PP_INT_MAX)) {
35 36 37
		/* FIXME diag
		 * DBG("Negative value cannot be converted into timestamp \n");
		 */
38 39 40 41 42 43 44 45
		return;
	} else {
		external->secondsField.lsb = internal->seconds;
		external->nanosecondsField = internal->nanoseconds;
		external->secondsField.msb = 0;
	}
}

46
void to_TimeInternal(TimeInternal *internal, Timestamp *external)
47 48
{
	/* Program will not run after 2038... */
49
	if (external->secondsField.lsb < PP_INT_MAX) {
50 51 52
		internal->seconds = external->secondsField.lsb;
		internal->nanoseconds = external->nanosecondsField;
	} else {
53
		/* FIXME diag
54 55
		DBG("Clock servo canno't be executed : "
		    "seconds field is higher than signed integer (32bits) \n");
56
		*/
57 58 59 60
		return;
	}
}

61
void normalize_TimeInternal(TimeInternal *r)
62
{
63 64
	r->seconds+= r->nanoseconds / PP_NSEC_PER_SEC;
	r->nanoseconds-= r->nanoseconds / PP_NSEC_PER_SEC * PP_NSEC_PER_SEC;
65 66

	if (r->seconds > 0 && r->nanoseconds < 0) {
67 68
		r->seconds-= 1;
		r->nanoseconds+= PP_NSEC_PER_SEC;
69
	} else if (r->seconds < 0 && r->nanoseconds > 0) {
70 71
		r->seconds+= 1;
		r->nanoseconds-= PP_NSEC_PER_SEC;
72 73 74
	}
}

75
void add_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
76 77 78 79
{
	r->seconds = x->seconds + y->seconds;
	r->nanoseconds = x->nanoseconds + y->nanoseconds;

80
	normalize_TimeInternal(r);
81 82
}

83
void sub_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
84 85 86 87
{
	r->seconds = x->seconds - y->seconds;
	r->nanoseconds = x->nanoseconds - y->nanoseconds;

88
	normalize_TimeInternal(r);
89
}