Commit fdd9844d authored by Aurelio Colosimo's avatar Aurelio Colosimo

Integration of arith.c file, containing functions for time handling

parent 67823708
#ifndef __PTP_CONSTANTS_H__
#define __PTP_CONSTANTS_H__
/* general purpose constants */
#define PP_NSEC_PER_SEC 1000000000
/* implementation specific constants */
#define PP_DEFAULT_INBOUND_LATENCY 0 /* in nsec */
#define PP_DEFAULT_OUTBOUND_LATENCY 0 /* in nsec */
......
/**
* @file arith.c
* @date Tue Jul 20 16:12:51 2010
*
* @brief Time format conversion routines and additional math functions.
*
*
/*
* FIXME header
*/
#include "ptpd.h"
#include <pproto/pproto.h>
void
integer64_to_internalTime(Integer64 bigint, TimeInternal * internal)
void int64_to_TimeInternal(Integer64 bigint, TimeInternal *internal)
{
int s_msb;
double ns_msb;
int entire;
char *p_lsb, *p_msb;
Boolean negative = FALSE;
p_lsb = (char *)&bigint.lsb;
/* lsb type is unsigned int */
p_msb = (char *)&bigint.msb;
/* msb type is int */
/* Test if bigint is a negative number */
negative = ((bigint.msb & 0x80000000) == 0x80000000);
if (!negative) {
/* Positive case */
/* fractional nanoseconds are excluded (see 5.3.2) */
bigint.lsb = bigint.lsb >> 16;
/*
* copy two least significant octet of msb to most
* significant octet of lsb
*/
*(p_lsb + 2) = *p_msb;
*(p_lsb + 3) = *(p_msb + 1);
bigint.msb = bigint.msb >> 16;
internal->nanoseconds = bigint.lsb % 1000000000;
internal->seconds = bigint.lsb / 1000000000;
/* (2^32 / 10^9) = 4,294967296 */
s_msb = 4 * bigint.msb;
ns_msb = 0.294967296 * (double)bigint.msb;
entire = (int)ns_msb;
s_msb += entire;
ns_msb -= entire;
ns_msb *= 1000000000;
internal->nanoseconds = (float)internal->nanoseconds +
(float)ns_msb;
internal->seconds += s_msb;
normalizeTime(internal);
}
/* End of positive Case */
else { /* Negative case */
/* Take the two complement */
bigint.lsb = ~bigint.lsb;
bigint.msb = ~bigint.msb;
int64_t bigint_val;
if (bigint.lsb == 0xffffffff) {
bigint.lsb = 0;
bigint.msb++;
} else {
bigint.lsb++;
}
bigint_val = bigint.lsb;
bigint_val+= ((int64_t)bigint.msb) << 32;
/* fractional nanoseconds are excluded (see 5.3.2) */
bigint.lsb = bigint.lsb >> 16;
/*
* copy two least significant octet of msb to most
* significant octet of lsb
*/
*(p_lsb + 2) = *p_msb;
*(p_lsb + 3) = *(p_msb + 1);
bigint.msb = bigint.msb >> 16;
internal->nanoseconds = bigint.lsb % 1000000000;
internal->seconds = bigint.lsb / 1000000000;
/* (2^32 / 10^9) = 4,294967296 */
s_msb = 4 * bigint.msb;
ns_msb = 0.294967296 * (double)bigint.msb;
entire = (int)ns_msb;
s_msb += entire;
ns_msb -= entire;
ns_msb *= 1000000000;
internal->nanoseconds = (float)internal->nanoseconds +
(float)ns_msb;
internal->seconds += s_msb;
normalizeTime(internal);
internal->nanoseconds = -internal->nanoseconds;
internal->seconds = -internal->seconds;
}
/* End of negative Case */
internal->nanoseconds = bigint_val % PP_NSEC_PER_SEC;
internal->seconds = bigint_val / PP_NSEC_PER_SEC;
}
void
fromInternalTime(TimeInternal * internal, Timestamp * external)
void from_TimeInternal(TimeInternal *internal, Timestamp *external)
{
/*
* fromInternalTime is only used to convert time given by the system
* to a timestamp As a consequence, no negative value can normally
......@@ -117,62 +27,60 @@ fromInternalTime(TimeInternal * internal, Timestamp * external)
* so there is no problem here.
*/
if ((internal->seconds & ~INT_MAX) ||
if ((internal->seconds & ~INT_MAX) ||
(internal->nanoseconds & ~INT_MAX)) {
DBG("Negative value canno't be converted into timestamp \n");
/* FIXME diag
* DBG("Negative value cannot be converted into timestamp \n");
*/
return;
} else {
external->secondsField.lsb = internal->seconds;
external->nanosecondsField = internal->nanoseconds;
external->secondsField.msb = 0;
}
}
void
toInternalTime(TimeInternal * internal, Timestamp * external)
void to_TimeInternal(TimeInternal *internal, Timestamp *external)
{
/* Program will not run after 2038... */
if (external->secondsField.lsb < INT_MAX) {
internal->seconds = external->secondsField.lsb;
internal->nanoseconds = external->nanosecondsField;
} else {
/* FIXME diag
DBG("Clock servo canno't be executed : "
"seconds field is higher than signed integer (32bits) \n");
*/
return;
}
}
void
normalizeTime(TimeInternal * r)
void normalize_TimeInternal(TimeInternal *r)
{
r->seconds += r->nanoseconds / 1000000000;
r->nanoseconds -= r->nanoseconds / 1000000000 * 1000000000;
r->seconds+= r->nanoseconds / PP_NSEC_PER_SEC;
r->nanoseconds-= r->nanoseconds / PP_NSEC_PER_SEC * PP_NSEC_PER_SEC;
if (r->seconds > 0 && r->nanoseconds < 0) {
r->seconds -= 1;
r->nanoseconds += 1000000000;
r->seconds-= 1;
r->nanoseconds+= PP_NSEC_PER_SEC;
} else if (r->seconds < 0 && r->nanoseconds > 0) {
r->seconds += 1;
r->nanoseconds -= 1000000000;
r->seconds+= 1;
r->nanoseconds-= PP_NSEC_PER_SEC;
}
}
void
addTime(TimeInternal * r, TimeInternal * x, TimeInternal * y)
void add_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
{
r->seconds = x->seconds + y->seconds;
r->nanoseconds = x->nanoseconds + y->nanoseconds;
normalizeTime(r);
normalize_TimeInternal(r);
}
void
subTime(TimeInternal * r, TimeInternal * x, TimeInternal * y)
void sub_TimeInternal(TimeInternal *r, TimeInternal *x, TimeInternal *y)
{
r->seconds = x->seconds - y->seconds;
r->nanoseconds = x->nanoseconds - y->nanoseconds;
normalizeTime(r);
normalize_TimeInternal(r);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment