summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/ship.c
blob: 6aebff71aa2f5df669d74b0bb2c6324eb75b767e (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 "c3/c3.h"
#include "types.h"
#include "imprison.h"
#include "retrieve.h"
#include "vortex.h"
#include "ship.h"

static inline void
_s_chub_to_bytes(c3_y byt_y[8], c3_d num_d)
{
  byt_y[0] = num_d & 0xff;
  byt_y[1] = (num_d >>  8) & 0xff;
  byt_y[2] = (num_d >> 16) & 0xff;
  byt_y[3] = (num_d >> 24) & 0xff;
  byt_y[4] = (num_d >> 32) & 0xff;
  byt_y[5] = (num_d >> 40) & 0xff;
  byt_y[6] = (num_d >> 48) & 0xff;
  byt_y[7] = (num_d >> 56) & 0xff;
}

static inline c3_d
_s_bytes_to_chub(c3_y byt_y[8])
{
  return (c3_d)byt_y[0]
       | (c3_d)byt_y[1] << 8
       | (c3_d)byt_y[2] << 16
       | (c3_d)byt_y[3] << 24
       | (c3_d)byt_y[4] << 32
       | (c3_d)byt_y[5] << 40
       | (c3_d)byt_y[6] << 48
       | (c3_d)byt_y[7] << 56;
}

void
u3_ship_to_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y)
{
  c3_y sip_y[16] = {0};

  _s_chub_to_bytes(sip_y, who_u[0]);
  _s_chub_to_bytes(sip_y + 8, who_u[1]);

  memcpy(buf_y, sip_y, c3_min(16, len_y));
}

void
u3_ship_of_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y)
{
  c3_y sip_y[16] = {0};
  memcpy(sip_y, buf_y, c3_min(16, len_y));

  who_u[0] = _s_bytes_to_chub(sip_y);
  who_u[1] = _s_bytes_to_chub(sip_y + 8);
}

u3_atom
u3_ship_to_noun(u3_ship who_u)
{
  return u3i_chubs(2, who_u);
}

c3_c*
u3_ship_to_string(u3_ship who_u)
{
  u3_noun ser = u3dc("scot", c3__p, u3_ship_to_noun(who_u));
  c3_c* who_c = u3r_string(ser);
  u3z(ser);
  return who_c;
}

void
u3_ship_of_noun(u3_ship who_u, u3_noun who)
{
  u3r_chubs(0, 2, who_u, who);
}

c3_o
u3_ships_equal(u3_ship sip_u, u3_ship sap_u)
{
  return __((sip_u[0] == sap_u[0]) && (sip_u[1] == sap_u[1]));
}

void
u3_ship_copy(u3_ship des_u, u3_ship src_u)
{
  des_u[0] = src_u[0];
  des_u[1] = src_u[1];
}

c3_l
u3_ship_rank(u3_ship who_u)
{
  if      ( who_u[1] )       return c3__pawn;
  else if ( who_u[0] >> 32 ) return c3__earl;
  else if ( who_u[0] >> 16 ) return c3__duke;
  else if ( who_u[0] >> 8 )  return c3__king;
  else                       return c3__czar;
}

c3_y
u3_ship_czar(u3_ship who_u) { return who_u[0] & 0xFF; }

c3_s
u3_ship_king(u3_ship who_u) { return who_u[0] & 0xffff; }

c3_w
u3_ship_duke(u3_ship who_u) { return who_u[0] & 0xffffffff; }

c3_d
u3_ship_earl(u3_ship who_u) { return who_u[0]; }