Skip to content

Commit 35705e5

Browse files
committed
Add new endian-based float packing to cnex
1 parent 11c9640 commit 35705e5

File tree

4 files changed

+122
-21
lines changed

4 files changed

+122
-21
lines changed

Diff for: exec/cnex/global.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,14 @@ TDispatch gfuncDispatch[] = {
301301
PDFUNC("string$upper", string_upper),
302302

303303
// struct - C struct serialization functions
304-
PDFUNC("struct$packIEEE32", struct_packIEEE32),
305-
PDFUNC("struct$packIEEE64", struct_packIEEE64),
306-
PDFUNC("struct$unpackIEEE32", struct_unpackIEEE32),
307-
PDFUNC("struct$unpackIEEE64", struct_unpackIEEE64),
304+
PDFUNC("struct$packIEEE32BE", struct_packIEEE32BE),
305+
PDFUNC("struct$packIEEE32LE", struct_packIEEE32LE),
306+
PDFUNC("struct$packIEEE64BE", struct_packIEEE64BE),
307+
PDFUNC("struct$packIEEE64LE", struct_packIEEE64LE),
308+
PDFUNC("struct$unpackIEEE32BE", struct_unpackIEEE32BE),
309+
PDFUNC("struct$unpackIEEE32LE", struct_unpackIEEE32LE),
310+
PDFUNC("struct$unpackIEEE64BE", struct_unpackIEEE64BE),
311+
PDFUNC("struct$unpackIEEE64LE", struct_unpackIEEE64LE),
308312

309313
// sys - System level calls
310314
PDFUNC("sys$exit", sys_exit),

Diff for: exec/cnex/lib/struct.c

+102-10
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,139 @@
1111

1212

1313

14-
void struct_packIEEE32(struct tagTExecutor *exec)
14+
void struct_packIEEE32BE(struct tagTExecutor *exec)
1515
{
1616
Number n = top(exec->stack)->number; pop(exec->stack);
1717

1818
float x = number_to_float(n);
1919
Cell *r = cell_createStringCell(sizeof(x));
20-
memcpy(r->string->data, &x, sizeof(x));
20+
uint32_t i = *(uint32_t *)&x;
21+
r->string->data[0] = (i >> 24) & 0xff;
22+
r->string->data[1] = (i >> 16) & 0xff;
23+
r->string->data[2] = (i >> 8) & 0xff;
24+
r->string->data[3] = (i ) & 0xff;
2125
r->type = cBytes;
2226

2327
push(exec->stack, r);
2428
}
2529

26-
void struct_packIEEE64(struct tagTExecutor *exec)
30+
void struct_packIEEE32LE(struct tagTExecutor *exec)
31+
{
32+
Number n = top(exec->stack)->number; pop(exec->stack);
33+
34+
float x = number_to_float(n);
35+
Cell *r = cell_createStringCell(sizeof(x));
36+
uint32_t i = *(uint32_t *)&x;
37+
r->string->data[3] = (i >> 24) & 0xff;
38+
r->string->data[2] = (i >> 16) & 0xff;
39+
r->string->data[1] = (i >> 8) & 0xff;
40+
r->string->data[0] = (i ) & 0xff;
41+
r->type = cBytes;
42+
43+
push(exec->stack, r);
44+
}
45+
46+
void struct_packIEEE64BE(struct tagTExecutor *exec)
47+
{
48+
Number n = top(exec->stack)->number; pop(exec->stack);
49+
50+
double x = number_to_double(n);
51+
Cell *r = cell_createStringCell(sizeof(x));
52+
uint64_t i = *(uint64_t *)&x;
53+
r->string->data[0] = (i >> 56) & 0xff;
54+
r->string->data[1] = (i >> 48) & 0xff;
55+
r->string->data[2] = (i >> 40) & 0xff;
56+
r->string->data[3] = (i >> 32) & 0xff;
57+
r->string->data[4] = (i >> 24) & 0xff;
58+
r->string->data[5] = (i >> 16) & 0xff;
59+
r->string->data[6] = (i >> 8) & 0xff;
60+
r->string->data[7] = (i ) & 0xff;
61+
r->type = cBytes;
62+
63+
push(exec->stack, r);
64+
}
65+
66+
void struct_packIEEE64LE(struct tagTExecutor *exec)
2767
{
2868
Number n = top(exec->stack)->number; pop(exec->stack);
2969

3070
double x = number_to_double(n);
3171
Cell *r = cell_createStringCell(sizeof(x));
32-
memcpy(r->string->data, &x, sizeof(x));
72+
uint64_t i = *(uint64_t *)&x;
73+
r->string->data[7] = (i >> 56) & 0xff;
74+
r->string->data[6] = (i >> 48) & 0xff;
75+
r->string->data[5] = (i >> 40) & 0xff;
76+
r->string->data[4] = (i >> 32) & 0xff;
77+
r->string->data[3] = (i >> 24) & 0xff;
78+
r->string->data[2] = (i >> 16) & 0xff;
79+
r->string->data[1] = (i >> 8) & 0xff;
80+
r->string->data[0] = (i ) & 0xff;
3381
r->type = cBytes;
3482

3583
push(exec->stack, r);
3684
}
3785

38-
void struct_unpackIEEE32(struct tagTExecutor *exec)
86+
void struct_unpackIEEE32BE(struct tagTExecutor *exec)
87+
{
88+
TString *b = top(exec->stack)->string;
89+
90+
uint32_t i = (b->data[0] << 24) |
91+
(b->data[1] << 16) |
92+
(b->data[2] << 8) |
93+
(b->data[3] );
94+
float x = *(float *)&i;
95+
96+
pop(exec->stack);
97+
push(exec->stack, cell_fromNumber(number_from_float(x)));
98+
}
99+
100+
void struct_unpackIEEE32LE(struct tagTExecutor *exec)
39101
{
40102
TString *b = top(exec->stack)->string;
41103

42-
float x;
43-
memcpy(&x, b->data, sizeof(x));
104+
uint32_t i = (b->data[3] << 24) |
105+
(b->data[2] << 16) |
106+
(b->data[1] << 8) |
107+
(b->data[0] );
108+
float x = *(float *)&i;
44109

45110
pop(exec->stack);
46111
push(exec->stack, cell_fromNumber(number_from_float(x)));
47112
}
48113

49-
void struct_unpackIEEE64(struct tagTExecutor *exec)
114+
void struct_unpackIEEE64BE(struct tagTExecutor *exec)
50115
{
51116
TString *b = top(exec->stack)->string;
52117

53-
double x;
54-
memcpy(&x, b->data, sizeof(x));
118+
uint64_t i = ((uint64_t)b->data[0] << 56) |
119+
((uint64_t)b->data[1] << 48) |
120+
((uint64_t)b->data[2] << 40) |
121+
((uint64_t)b->data[3] << 32) |
122+
((uint64_t)b->data[4] << 24) |
123+
((uint64_t)b->data[5] << 16) |
124+
((uint64_t)b->data[6] << 8) |
125+
((uint64_t)b->data[7] );
126+
127+
double x = *(double *)&i;
128+
129+
pop(exec->stack);
130+
push(exec->stack, cell_fromNumber(number_from_double(x)));
131+
}
132+
133+
void struct_unpackIEEE64LE(struct tagTExecutor *exec)
134+
{
135+
TString *b = top(exec->stack)->string;
136+
137+
uint64_t i = ((uint64_t)b->data[7] << 56) |
138+
((uint64_t)b->data[6] << 48) |
139+
((uint64_t)b->data[5] << 40) |
140+
((uint64_t)b->data[4] << 32) |
141+
((uint64_t)b->data[3] << 24) |
142+
((uint64_t)b->data[2] << 16) |
143+
((uint64_t)b->data[1] << 8) |
144+
((uint64_t)b->data[0] );
145+
146+
double x = *(double *)&i;
55147

56148
pop(exec->stack);
57149
push(exec->stack, cell_fromNumber(number_from_double(x)));

Diff for: exec/cnex/lib/struct.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
struct tagTExecutor;
55

6-
void struct_packIEEE32(struct tagTExecutor *exec);
7-
void struct_packIEEE64(struct tagTExecutor *exec);
8-
void struct_unpackIEEE32(struct tagTExecutor *exec);
9-
void struct_unpackIEEE64(struct tagTExecutor *exec);
6+
void struct_packIEEE32BE(struct tagTExecutor *exec);
7+
void struct_packIEEE32LE(struct tagTExecutor *exec);
8+
9+
void struct_packIEEE64BE(struct tagTExecutor *exec);
10+
void struct_packIEEE64LE(struct tagTExecutor *exec);
11+
12+
void struct_unpackIEEE32BE(struct tagTExecutor *exec);
13+
void struct_unpackIEEE32LE(struct tagTExecutor *exec);
14+
15+
void struct_unpackIEEE64BE(struct tagTExecutor *exec);
16+
void struct_unpackIEEE64LE(struct tagTExecutor *exec);
1017

1118
#endif

Diff for: lib/nd.proj/Menu.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Timestamp: Generated on day month, year
4141

4242

4343
File: global (global.neon)
44-
File: bigint (bigint.neon)
4544
File: binary (binary.neon)
4645
File: cformat (cformat.neon)
4746
File: complex (complex.neon)
@@ -50,7 +49,6 @@ File: datetime (datetime.neon)
5049
File: debugger (debugger.neon)
5150
File: encoding (encoding.neon)
5251
File: file (file.neon)
53-
File: http (http.neon)
5452
File: io (io.neon)
5553
File: json (json.neon)
5654
File: math (math.neon)
@@ -60,8 +58,8 @@ File: net (net.neon)
6058
File: os (os.neon)
6159
File: posix (posix.neon)
6260
File: random (random.neon)
63-
File: regex (regex.neon)
6461
File: runtime (runtime.neon)
62+
File: simplehttp (simplehttp.neon)
6563
File: sqlite (sqlite.neon)
6664
File: string (string.neon)
6765
File: struct (struct.neon)

0 commit comments

Comments
 (0)