diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2014-08-08 14:42:07 +0300 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2014-08-08 14:42:07 +0300 |
| commit | a6163888f3c56123b1db313743c6147ba498732c (patch) | |
| tree | ff7d15d991d1d09ba6cbc0cec80924f57445bfdd /third_party/ptpd-1.1.0/src/arith.c | |
| parent | c3e4c9a25c2910d2d66d52215b3406b13d5b23d5 (diff) | |
Add third_party libs
Diffstat (limited to 'third_party/ptpd-1.1.0/src/arith.c')
| -rw-r--r-- | third_party/ptpd-1.1.0/src/arith.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/third_party/ptpd-1.1.0/src/arith.c b/third_party/ptpd-1.1.0/src/arith.c new file mode 100644 index 0000000..a258a9e --- /dev/null +++ b/third_party/ptpd-1.1.0/src/arith.c @@ -0,0 +1,113 @@ +/**
+ * @file arith.c
+ * @date Tue Jul 20 16:12:51 2010
+ *
+ * @brief Time format conversion routines and additional math functions.
+ *
+ *
+ */
+
+#include "ptpd.h"
+
+/* from annex C of the spec */
+UInteger32
+crc_algorithm(Octet * buf, Integer16 length)
+{
+ Integer16 i;
+ UInteger8 data;
+ UInteger32 polynomial = 0xedb88320, crc = 0xffffffff;
+
+ while (length-- > 0) {
+ data = *(UInteger8 *) (buf++);
+
+ for (i = 0; i < 8; i++) {
+ if ((crc ^ data) & 1) {
+ crc = (crc >> 1);
+ crc ^= polynomial;
+ } else {
+ crc = (crc >> 1);
+ }
+ data >>= 1;
+ }
+ }
+
+ return crc ^ 0xffffffff;
+}
+
+UInteger32
+sum(Octet * buf, Integer16 length)
+{
+ UInteger32 sum = 0;
+
+ while (length-- > 0)
+ sum += *(UInteger8 *) (buf++);
+
+ return sum;
+}
+
+void
+fromInternalTime(TimeInternal * internal, TimeRepresentation * external, Boolean halfEpoch)
+{
+ external->seconds = labs(internal->seconds) + halfEpoch * INT_MAX;
+
+ if (internal->seconds < 0 || internal->nanoseconds < 0) {
+ external->nanoseconds = labs(internal->nanoseconds) | ~INT_MAX;
+ } else {
+ external->nanoseconds = labs(internal->nanoseconds);
+ }
+
+ DBGV("fromInternalTime: %10ds %11dns -> %10us %11dns\n",
+ internal->seconds, internal->nanoseconds,
+ external->seconds, external->nanoseconds);
+}
+
+void
+toInternalTime(TimeInternal * internal, TimeRepresentation * external, Boolean * halfEpoch)
+{
+ *halfEpoch = external->seconds / INT_MAX;
+
+ if (external->nanoseconds & ~INT_MAX) {
+ internal->seconds = -(external->seconds % INT_MAX);
+ internal->nanoseconds = -(external->nanoseconds & INT_MAX);
+ } else {
+ internal->seconds = external->seconds % INT_MAX;
+ internal->nanoseconds = external->nanoseconds;
+ }
+
+ DBGV("toInternalTime: %10ds %11dns <- %10us %11dns\n",
+ internal->seconds, internal->nanoseconds,
+ external->seconds, external->nanoseconds);
+}
+
+void
+normalizeTime(TimeInternal * r)
+{
+ r->seconds += r->nanoseconds / 1000000000;
+ r->nanoseconds -= r->nanoseconds / 1000000000 * 1000000000;
+
+ if (r->seconds > 0 && r->nanoseconds < 0) {
+ r->seconds -= 1;
+ r->nanoseconds += 1000000000;
+ } else if (r->seconds < 0 && r->nanoseconds > 0) {
+ r->seconds += 1;
+ r->nanoseconds -= 1000000000;
+ }
+}
+
+void
+addTime(TimeInternal * r, TimeInternal * x, TimeInternal * y)
+{
+ r->seconds = x->seconds + y->seconds;
+ r->nanoseconds = x->nanoseconds + y->nanoseconds;
+
+ normalizeTime(r);
+}
+
+void
+subTime(TimeInternal * r, TimeInternal * x, TimeInternal * y)
+{
+ r->seconds = x->seconds - y->seconds;
+ r->nanoseconds = x->nanoseconds - y->nanoseconds;
+
+ normalizeTime(r);
+}
|
