|
11 | 11 |
|
12 | 12 |
|
13 | 13 |
|
14 |
| -void struct_packIEEE32(struct tagTExecutor *exec) |
| 14 | +void struct_packIEEE32BE(struct tagTExecutor *exec) |
15 | 15 | {
|
16 | 16 | Number n = top(exec->stack)->number; pop(exec->stack);
|
17 | 17 |
|
18 | 18 | float x = number_to_float(n);
|
19 | 19 | 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; |
21 | 25 | r->type = cBytes;
|
22 | 26 |
|
23 | 27 | push(exec->stack, r);
|
24 | 28 | }
|
25 | 29 |
|
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) |
27 | 67 | {
|
28 | 68 | Number n = top(exec->stack)->number; pop(exec->stack);
|
29 | 69 |
|
30 | 70 | double x = number_to_double(n);
|
31 | 71 | 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; |
33 | 81 | r->type = cBytes;
|
34 | 82 |
|
35 | 83 | push(exec->stack, r);
|
36 | 84 | }
|
37 | 85 |
|
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) |
39 | 101 | {
|
40 | 102 | TString *b = top(exec->stack)->string;
|
41 | 103 |
|
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; |
44 | 109 |
|
45 | 110 | pop(exec->stack);
|
46 | 111 | push(exec->stack, cell_fromNumber(number_from_float(x)));
|
47 | 112 | }
|
48 | 113 |
|
49 |
| -void struct_unpackIEEE64(struct tagTExecutor *exec) |
| 114 | +void struct_unpackIEEE64BE(struct tagTExecutor *exec) |
50 | 115 | {
|
51 | 116 | TString *b = top(exec->stack)->string;
|
52 | 117 |
|
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; |
55 | 147 |
|
56 | 148 | pop(exec->stack);
|
57 | 149 | push(exec->stack, cell_fromNumber(number_from_double(x)));
|
|
0 commit comments