summaryrefslogtreecommitdiff
path: root/src/uint128p.c
blob: e92b5a76a05c9e57ddf600a0b253ca09c9e97649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "uint_t.h"

/**
  @file uint128p.c
  @author feh, jannis
  @source djbdns6
  @brief packing/unpacking 128 bit integer to/from char string
*/

void uint128_pack(char s[16], uint128 u)
{
  // clang-format off
   s[0] = u.lo & 255; u.lo >>= 8;
   s[1] = u.lo & 255; u.lo >>= 8;
   s[2] = u.lo & 255; u.lo >>= 8;
   s[3] = u.lo & 255; u.lo >>= 8;
   s[4] = u.lo & 255; u.lo >>= 8;
   s[5] = u.lo & 255; u.lo >>= 8;
   s[6] = u.lo & 255; u.lo >>= 8;
   s[7] = u.lo & 255; u.lo >>= 8;

   s[8] = u.hi & 255; u.hi >>= 8;
   s[9] = u.hi & 255; u.hi >>= 8;
  s[10] = u.hi & 255; u.hi >>= 8;
  s[11] = u.hi & 255; u.hi >>= 8;
  s[12] = u.hi & 255; u.hi >>= 8;
  s[13] = u.hi & 255; u.hi >>= 8;
  s[14] = u.hi & 255; u.hi >>= 8;
  s[15] = u.hi & 255;
  // clang-format on
}

void uint128_pack_big(char s[16], uint128 u)
{
  // clang-format off
  s[15] = u.lo & 255; u.lo >>= 8;
  s[14] = u.lo & 255; u.lo >>= 8;
  s[13] = u.lo & 255; u.lo >>= 8;
  s[12] = u.lo & 255; u.lo >>= 8;
  s[11] = u.lo & 255; u.lo >>= 8;
  s[10] = u.lo & 255; u.lo >>= 8;
   s[9] = u.lo & 255; u.lo >>= 8;
   s[8] = u.lo & 255; u.lo >>= 8;

   s[7] = u.hi & 255; u.hi >>= 8;
   s[6] = u.hi & 255; u.hi >>= 8;
   s[5] = u.hi & 255; u.hi >>= 8;
   s[4] = u.hi & 255; u.hi >>= 8;
   s[3] = u.hi & 255; u.hi >>= 8;
   s[2] = u.hi & 255; u.hi >>= 8;
   s[1] = u.hi & 255; u.hi >>= 8;
   s[0] = u.hi & 255;
  // clang-format on
}

void uint128_unpack(char s[16], uint128 *u)
{
  uint128 result;
  result.hi = result.lo = 0ULL;

  // clang-format off
  result.hi =  (unsigned char) s[15]; result.hi <<= 8;
  result.hi += (unsigned char) s[14]; result.hi <<= 8;
  result.hi += (unsigned char) s[13]; result.hi <<= 8;
  result.hi += (unsigned char) s[12]; result.hi <<= 8;
  result.hi += (unsigned char) s[11]; result.hi <<= 8;
  result.hi += (unsigned char) s[10]; result.hi <<= 8;
  result.hi += (unsigned char)  s[9]; result.hi <<= 8;
  result.hi += (unsigned char)  s[8];   //correct

  result.lo += (unsigned char)  s[7]; result.lo <<= 8;
  result.lo += (unsigned char)  s[6]; result.lo <<= 8;
  result.lo += (unsigned char)  s[5]; result.lo <<= 8;
  result.lo += (unsigned char)  s[4]; result.lo <<= 8;
  result.lo += (unsigned char)  s[3]; result.lo <<= 8;
  result.lo += (unsigned char)  s[2]; result.lo <<= 8;
  result.lo += (unsigned char)  s[1]; result.lo <<= 8;
  result.lo += (unsigned char)  s[0];
  // clang-format on

  *u = result;
}
void uint128_unpack_big(char s[16], uint128 *u)
{
  uint128 result;
  result.hi = result.lo = 0ULL;

  // clang-format off
  result.hi =  (unsigned char) s[0]; result.hi <<= 8;
  result.hi += (unsigned char) s[1]; result.hi <<= 8;
  result.hi += (unsigned char) s[2]; result.hi <<= 8;
  result.hi += (unsigned char) s[3]; result.hi <<= 8;
  result.hi += (unsigned char) s[4]; result.hi <<= 8;
  result.hi += (unsigned char) s[5]; result.hi <<= 8;
  result.hi += (unsigned char) s[6]; result.hi <<= 8;
  result.hi += (unsigned char) s[7];

  result.lo += (unsigned char)  s[8]; result.lo <<= 8;
  result.lo += (unsigned char)  s[9]; result.lo <<= 8;
  result.lo += (unsigned char) s[10]; result.lo <<= 8;
  result.lo += (unsigned char) s[11]; result.lo <<= 8;
  result.lo += (unsigned char) s[12]; result.lo <<= 8;
  result.lo += (unsigned char) s[13]; result.lo <<= 8;
  result.lo += (unsigned char) s[14]; result.lo <<= 8;
  result.lo += (unsigned char) s[15];
  // clang-format on

  *u = result;
}