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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/* "bytenumb.scm" Byte integer and IEEE floating-point conversions.
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
/* Author: Aubrey Jaffer */
/* For documentation see:
http://cvs.savannah.gnu.org/viewcvs/slib/slib/bytenumb.scm?view=markup */
#include <stdlib.h>
#include <math.h>
#include "scm.h"
int get_bytes_length(obj)
SCM obj;
{
array_dim *s;
if (IMP(obj)) return -1;
switch (TYP7(obj)) {
case tc7_string:
case tc7_VfixN8:
case tc7_VfixZ8:
return LENGTH(obj);
case tc7_smob:
if (!ARRAYP(obj)) return -1;
if (1 != ARRAY_NDIM(obj)) return -1;
s = ARRAY_DIMS(obj);
if (1 != s[0].inc) return -1;
return s[0].ubnd - s[0].lbnd;
default: return -1;
}
}
static char s_wrong_length[] = "wrong length";
static SCM list_of_0;
char * get_bytes(obj, minlen, s_name)
SCM obj;
int minlen;
const char *s_name;
{
ASRTER(NIMP(obj) && (TYP7(obj)==tc7_string ||
TYP7(obj)==tc7_VfixN8 ||
TYP7(obj)==tc7_VfixZ8),
obj, ARG1, s_name);
{
int byvlen = get_bytes_length(obj);
ASRTER((minlen < 0) ? byvlen >= -minlen : byvlen == minlen,
MAKINUM(byvlen), s_wrong_length, s_name);
return (char*)scm_addr(cons(obj, list_of_0), s_name);
}
}
static char s_bytes_to_integer[] = "bytes->integer";
SCM scm_bytes_to_integer(sbyts, sn)
SCM sbyts;
SCM sn;
{
long n = INUM(sn);
if (!(n)) return INUM0;
{
int cnt = abs(n);
char *byts = get_bytes(sbyts, -cnt, s_bytes_to_integer);
int iu = 0, id = cnt - sizeof(BIGDIG);
sizet ndigs = (cnt + sizeof(BIGDIG) - 1) / sizeof(BIGDIG);
int negp = (0x80 & byts[0]) && (0 > n);
SCM retval = mkbig(ndigs, negp);
BIGDIG *digs = BDIGITS(retval), carry = 1;
if (negp)
for (; iu < ndigs; iu++) {
int j = 0;
UBIGLONG dig = 0;
for (; j < sizeof(BIGDIG); j++) {
dig = (dig<<8) +
(0xFF ^ ((id + j >= 0) ? (((unsigned char *)byts)[id + j]) : 255));
/* printf("byts[%d + %d] = %lx\n", id, j, 0xFF & dig); */
}
dig = dig + carry;
digs[iu] = dig;
carry = dig >> (8 * sizeof(BIGDIG));
/* printf("id = %d; iu = %d; dig = %04lx\n", id, iu, dig); */
id = id - sizeof(BIGDIG);
} else
for (; iu < ndigs; iu++) {
int j = 0;
BIGDIG dig = 0;
for (; j < sizeof(BIGDIG); j++) {
dig = (dig<<8) +
((id + j >= 0) ? (((unsigned char *)byts)[id + j]) : 0);
}
digs[iu] = dig;
/* printf("id = %d; iu = %d; dig = %04x\n", id, iu, dig); */
id = id - sizeof(BIGDIG);
}
return normbig(retval);
}
}
static char s_integer_to_bytes[] = "integer->bytes";
SCM scm_integer_to_bytes(sn, slen)
SCM sn;
SCM slen;
{
ASRTER(INUMP(slen), slen, ARG2, s_integer_to_bytes);
{
int len = INUM(slen);
SCM sbyts = make_string(scm_iabs(slen), MAKICHR(0));
char *byts = CHARS(sbyts);
if (INUMP(sn)) {
int idx = -1 + (abs(len));
long n = num2long(sn, (char *)ARG1, s_integer_to_bytes);
if ((0 > n) && (0 > len)) {
long res = -1 - n;
while (!(0 > idx)) {
byts[idx--] = 0xFF ^ (res % 0x100);
res = res>>8;
}
}
else {
UBIGLONG res = n;
while (!(0 > idx)) {
byts[idx--] = res % 0x100;
res = res>>8;
}
}
} else {
ASRTER(NIMP(sn) && BIGP(sn), sn, ARG1, s_integer_to_bytes);
{
BIGDIG *digs = BDIGITS(sn), borrow = 1;
sizet ndigs = NUMDIGS(sn);
int iu = 0, id = abs(len) - 1;
UBIGLONG dig;
if ((0 > len) && (TYP16(sn)==tc16_bigneg))
for (; 0 <= id ; iu++) {
sizet j = sizeof(BIGDIG);
dig = (iu < ndigs) ? digs[iu] : 0;
dig = dig ^ ((1 << (8 * sizeof(BIGDIG))) - 1);
/* printf("j = %d; id = %d; iu = %d; dig = %04x; borrow = %d\n", j, id, iu, dig, borrow); */
for (; 0 < j-- && 0 <= id;) {
/* printf("byts[%d] = %02x\n", id, 0xFF & dig); */
int dg = (0xFF & dig) + borrow;
borrow = dg >> 8;
((unsigned char *)byts)[id--] = dg;
dig = (dig)>>8;
}
}
else
for (; 0 <= id ; iu++) {
BIGDIG dig = (iu < ndigs) ? digs[iu] : 0;
sizet j = sizeof(BIGDIG);
/* printf("j = %d; id = %d; iu = %d; dig = %04x\n", j, id, iu, dig); */
for (; 0 < j-- && 0 <= id;) {
/* printf("byts[%d] = %02x\n", id, 0xFF & dig); */
((unsigned char *)byts)[id--] = 0xFF & dig;
dig = (dig>>8);
}
}
}
}
return sbyts;
}
}
static char s_bytes_to_ieee_float[] = "bytes->ieee-float";
SCM scm_bytes_to_ieee_float(sbyts)
SCM sbyts;
{
char *byts = get_bytes(sbyts, 4, s_bytes_to_ieee_float);
int len = LENGTH(sbyts);
int s = (1<<(7)) & ((((unsigned char*)(byts))[0]));
int e = ((0x7f&((((unsigned char*)(byts))[0])))<<1)
+ ((0x80&((((unsigned char*)(byts))[1])))>>7);
float f = (((unsigned char*)(byts))[ -1 + (len)]);
int idx = -2 + (len);
while (!((idx)<=1)) {
{
int T_idx = -1 + (idx);
f = ((((unsigned char*)(byts))[idx])) + ((f) / 0x100);
idx = T_idx;
}
}
f = ((0x7f&((((unsigned char*)(byts))[1]))) + ((f) / 0x100)) / 0x80;
if ((0<(e))
&& ((e)<0xff))
return makdbl(ldexpf((s ? -1 : 1) * (1 + (f)), (e) - 0x7f), 0.0);
else if (!(e))
if (!(f)) return flo0;
else return makdbl(ldexpf((s ? -1 : 1) * (f), -126), 0.0);
else if (f)
return scm_narn;
else return makdbl((s ? -(1.0) : 1.0) / 0.0, 0.0);
}
static char s_bytes_to_ieee_double[] = "bytes->ieee-double";
SCM scm_bytes_to_ieee_double(sbyts)
SCM sbyts;
{
char *byts = get_bytes(sbyts, 8, s_bytes_to_ieee_double);
int len = LENGTH(sbyts);
int s = (1<<(7)) & ((((unsigned char*)(byts))[0]));
int e = ((0x7f&((((unsigned char*)(byts))[0])))<<4)
+ ((0xf0&((((unsigned char*)(byts))[1])))>>4);
double f = (((unsigned char*)(byts))[ -1 + (len)]);
int idx = -2 + (len);
while (!((idx)<=1)) {
{
int T_idx = -1 + (idx);
f = ((((unsigned char*)(byts))[idx])) + ((f) / 0x100);
idx = T_idx;
}
}
f = ((0xf&((((unsigned char*)(byts))[1]))) + ((f) / 0x100)) / 0x10;
if ((0<(e))
&& ((e)<0x7ff))
return makdbl(ldexp((s ? -1 : 1) * (1 + (f)), (e) - 0x3ff), 0.0);
else if (!(e))
if (!(f)) return flo0;
else return makdbl(ldexp((s ? -1 : 1) * (f), -1022), 0.0);
else if (f)
return scm_narn;
else return makdbl((s ? -(1.0) : 1.0) / 0.0, 0.0);
}
static char s_ieee_float_to_bytes[] = "ieee-float->bytes";
SCM scm_ieee_float_to_bytes(in_flt)
SCM in_flt;
{
double dbl = num2dbl(in_flt, (char *)ARG1, s_ieee_float_to_bytes);
float flt = (float) dbl;
SCM sbyts = make_string(MAKINUM(4), MAKICHR(0));
char *byts = CHARS(sbyts);
int s = flt < 0.0;
int scl = 0x7f;
flt = fabs(flt);
if (0.0==flt) {
if (s)
byts[0] = 0x80;
return sbyts;
}
else if (flt != flt) {
byts[0] = 0x7f;
byts[1] = 0xc0;
return sbyts;
}
else goto L_scale;
L_out:
{
float T_flt = 0x80 * (flt);
int val = (int)(floor(0x80 * (flt)));
int idx = 1;
float flt = T_flt;
while (!((idx) > 3)) {
byts[idx] = val;
{
float T_flt = 0x100 * ((flt) - (val));
int T_val = (int)(floor(0x100 * ((flt) - (val))));
idx = 1 + (idx);
flt = T_flt;
val = T_val;
}
}
byts[1] = (0x80 & (scl<<7)) | (0x7f & (((unsigned char*)(byts))[1]));
byts[0] = (s ? 0x80 : 0) + ((scl)>>1);
return sbyts;
}
L_scale:
if (!(scl)) {
flt = (flt)/2;
goto L_out;
}
else if ((flt)>=0x10) {
float flt16 = (flt) / 0x10;
if ((flt16)==(flt)) {
byts[0] = s ? 0xff : 0x7f;
byts[1] = 0x80;
return sbyts;
}
else {
flt = flt16;
scl = (scl) + 4;
goto L_scale;
}
}
else if ((flt) >= 2) {
flt = (flt) / 2;
scl = (scl) + 1;
goto L_scale;
}
else if (((scl) >= 4) && ((0x10 * (flt))<1)) {
flt = (flt) * 0x10;
scl = (scl)+ -4;
goto L_scale;
}
else if ((flt)<1) {
flt = (flt) * 2;
scl = (scl) + -1;
goto L_scale;
}
else {
flt = -1+(flt);
goto L_out;
}
}
static char s_ieee_double_to_bytes[] = "ieee-double->bytes";
SCM scm_ieee_double_to_bytes(in_flt)
SCM in_flt;
{
double flt = num2dbl(in_flt, (char *)ARG1, s_ieee_double_to_bytes);
SCM sbyts = make_string(MAKINUM(8), MAKICHR(0));
char *byts = CHARS(sbyts);
int s = flt < 0.0;
int scl = 0x3ff;
flt = fabs(flt);
if (0.0==flt) {
if (s)
byts[0] = 0x80;
return sbyts;
}
else if (flt != flt) {
byts[0] = 0x7f;
byts[1] = 0xf8;
return sbyts;
}
else goto L_scale;
L_out:
{
double T_flt = 0x10 * (flt);
int val = (int)(floor(0x10 * (flt)));
int idx = 1;
double flt = T_flt;
while (!((idx) > 7)) {
byts[idx] = val;
{
double T_flt = 0x100 * (flt - val);
int T_val = (int)floor(0x100 * (flt - val));
idx = 1 + (idx);
flt = T_flt;
val = T_val;
}
}
byts[1] = (0xf0 & (scl<<4)) | (0x0f & (((unsigned char*)(byts))[1]));
byts[0] = (s ? 0x80 : 0) + ((scl)>>4);
return sbyts;
}
L_scale:
if (!(scl)) {
flt = (flt) / 2;
goto L_out;
}
else if ((flt) >= 0x10) {
double flt16 = (flt) / 0x10;
if ((flt16)==(flt)) {
byts[0] = s ? 0xff : 0x7f;
byts[1] = 0xf0;
return sbyts;
}
else {
flt = flt16;
scl = (scl) + 4;
goto L_scale;
}
}
else if ((flt) >= 2) {
flt = (flt) / 2;
scl = (scl) + 1;
goto L_scale;
}
else if (((scl) >= 4) && ((0x10 * flt) < 1)) {
flt = (flt) * 0x10;
scl = (scl) + -4;
goto L_scale;
}
else if ((flt) < 1) {
flt = (flt) * 2;
scl = (scl) + -1;
goto L_scale;
}
else {
flt = -1 + (flt);
goto L_out;
}
}
static char s_integer_byte_collate_M[] = "integer-byte-collate!";
SCM scm_integer_byte_collate_M(byte_vector)
SCM byte_vector;
{
char* bv = get_bytes(byte_vector, -1, s_integer_byte_collate_M);
bv[0] = 0x80^(bv[0]);
return byte_vector;
}
static char s_ieee_byte_collate_M[] = "ieee-byte-collate!";
SCM scm_ieee_byte_collate_M(byte_vector)
SCM byte_vector;
{
char* byv = get_bytes(byte_vector, 4, s_ieee_byte_collate_M);
int byvlen = get_bytes_length(byte_vector);
if (0x80&(byv[0])) {
int idx = -1 + byvlen;
while (!(0 > (idx))) {
byv[idx] = 0xff^(byv[idx]);
idx = -1+(idx);
}
}
else
byv[0] = 0x80^(byv[0]);
return byte_vector;
}
static char s_ieee_byte_decollate_M[] = "ieee-byte-decollate!";
SCM scm_ieee_byte_decollate_M(byte_vector)
SCM byte_vector;
{
char* byv = get_bytes(byte_vector, 4, s_ieee_byte_collate_M);
int byvlen = get_bytes_length(byte_vector);
if (!(0x80&(byv[0]))) {
int idx = -1 + byvlen;
while (!(0 > (idx))) {
byv[idx] = 0xff^(byv[idx]);
idx = -1+(idx);
}
}
else
byv[0] = 0x80^(byv[0]);
return byte_vector;
}
static iproc subr1s[] = {
{s_bytes_to_ieee_float, scm_bytes_to_ieee_float},
{s_bytes_to_ieee_double, scm_bytes_to_ieee_double},
{s_ieee_float_to_bytes, scm_ieee_float_to_bytes},
{s_ieee_double_to_bytes, scm_ieee_double_to_bytes},
{s_integer_byte_collate_M, scm_integer_byte_collate_M},
{s_ieee_byte_collate_M, scm_ieee_byte_collate_M},
{s_ieee_byte_decollate_M, scm_ieee_byte_decollate_M},
{0, 0}};
void init_bytenumb()
{
list_of_0 = cons(INUM0, EOL);
scm_gc_protect(list_of_0);
make_subr(s_bytes_to_integer, tc7_subr_2, scm_bytes_to_integer);
make_subr(s_integer_to_bytes, tc7_subr_2, scm_integer_to_bytes);
init_iprocs(subr1s, tc7_subr_1);
scm_ldstr("\n\
(define (integer-byte-collate byte-vector)\n\
(integer-byte-collate! (bytes-copy byte-vector)))\n\
(define (ieee-byte-collate byte-vector)\n\
(ieee-byte-collate! (bytes-copy byte-vector)))\n\
(define (ieee-byte-decollate byte-vector)\n\
(ieee-byte-decollate! (bytes-copy byte-vector)))\n\
");
/* add_feature("byte-number"); */
}
|