9 #define XDR_DOUBLE_LENGTH 8
10 #define XDR_DOUBLE_NOF_EXP_BYTES 2
11 #define XDR_FLOAT_LENGTH 4
12 #define XDR_FLOAT_NOF_EXP_BYTES 1
21 bit = 1 << (numBits - 1);
23 for (i = 0; i < numBits; i++) {
24 printf(
"%d", (*((
unsigned char *)c) & bit) != 0);
35 sign = *numPointer >> 7;
36 exponent = (*numPointer << 1) | (*(numPointer + 1) >> 7);
38 printf(
"%f: sign = ", *((
float *)numPointer));
42 printf(
" mantissa = ");
51 static unsigned char clearMask[9] = {255, 128, 192, 224, 240,
56 #define ALL_NULL_CODE 2
57 #define ZERO_NULL_CODE 1
58 #define SOME_NULL_CODE 0
62 static void G_fpcompress_rearrangeEncodeFloats(
unsigned char *
src,
int size,
64 unsigned char *
dst,
int *length,
67 unsigned int nNullBits, nBits;
68 register unsigned char *srcStop;
69 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
70 unsigned char mask, isNull;
71 int gt8, gt16, srcIncrement, nofNull;
76 if ((precision >= 23) || (precision == -1)) {
81 while (srcStop !=
src) {
89 *offsetMantissa = size;
96 while (srcStop != (
unsigned char *)f)
99 if (nofNull == size) {
112 gt16 = precision > 16;
114 srcIncrement = 1 + (!gt8) + (!gt16);
120 cp0 = nullBits + size / 8 + ((size % 8) != 0);
123 cp3 = cp0 + size - nofNull;
124 cp2 = cp3 + size - nofNull;
125 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
127 mask = clearMask[precision];
128 nBits = nNullBits = 0;
130 while (srcStop !=
src) {
135 *nullBits |= ((
unsigned char)isNull << nNullBits++);
136 if (nNullBits == 8) {
142 *nullBits = (
unsigned char)isNull;
161 if (nBits && precision) {
162 *cp1 |= (
unsigned char)((
unsigned char)(*
src & mask) >> nBits);
166 if (8 - nBits < (
unsigned int)precision) {
171 *cp1 = (
unsigned char)((
unsigned char)((*
src & mask))
173 nBits += precision - 8;
176 nBits = (nBits + precision) % 8;
182 *cp1 = (
unsigned char)(*
src & mask);
184 nBits = (nBits + precision) % 8;
195 *length += size / 8 + ((size % 8) != 0);
198 *length += (gt8 + gt16 + (precision == 0) + 1) * (size - nofNull) +
199 ((precision * (size - nofNull)) / 8) +
200 (((precision * (size - nofNull)) % 8) != 0);
202 *offsetMantissa = size - nofNull;
207 static void G_fpcompress_rearrangeEncodeDoubles(
unsigned char *
src,
int size,
209 unsigned char *
dst,
int *length,
212 unsigned int nNullBits, nBits;
213 unsigned char isNull;
214 register unsigned char *srcStop;
215 register unsigned char *cp0, *cp1, *cp2, *cp3;
216 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
218 int gt8, gt16, gt24, gt32, gt40, gt48, srcIncrement, nofNull;
223 if ((precision >= 52) || (precision == -1)) {
233 while (srcStop !=
src) {
245 *offsetMantissa = size;
254 while (srcStop != (
unsigned char *)d)
257 if (nofNull == size) {
268 gt48 = precision > 48;
269 gt40 = precision > 40;
270 gt32 = precision > 32;
271 gt24 = precision > 24;
272 gt16 = precision > 16;
274 srcIncrement = 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
280 cp0 = nullBits + size / 8 + ((size % 8) != 0);
283 cp7 = cp0 + size - nofNull;
284 cp6 = cp7 + size - nofNull;
285 cp5 = cp6 + size - nofNull;
286 cp4 = cp5 + size - nofNull;
287 cp3 = cp4 + size - nofNull;
288 cp2 = cp3 + size - nofNull;
289 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
291 mask = clearMask[precision];
292 nBits = nNullBits = 0;
294 while (srcStop !=
src) {
299 *nullBits |= ((
unsigned char)isNull << nNullBits++);
300 if (nNullBits == 8) {
306 *nullBits = (
unsigned char)isNull;
338 if (nBits && precision) {
339 *cp1 |= (
unsigned char)((
unsigned char)(*
src & mask) >> nBits);
340 if (8 - nBits < (
unsigned int)precision) {
342 *cp1 = (
unsigned char)(((
unsigned char)(*
src & mask))
344 nBits += precision - 8;
347 nBits = (nBits + precision) % 8;
353 *cp1 = (
unsigned char)(*
src & mask);
354 nBits = (nBits + precision) % 8;
365 *length += size / 8 + ((size % 8) != 0);
367 *length += (1 + gt8 + gt16 + gt24 + gt32 + gt40 + gt48 + (precision == 0)) *
369 ((precision * (size - nofNull)) / 8) +
370 (((precision * (size - nofNull)) % 8) != 0);
373 *offsetMantissa = 2 * (size - nofNull);
375 *offsetMantissa = *length;
380 static void G_fpcompress_rearrangeDecodeFloats(
unsigned char *
src,
int size,
384 unsigned int nNullBits, nBits;
385 register unsigned char *dstStop;
386 register unsigned char *cp0, *cp1, *cp2, *cp3, *nullBits;
387 unsigned char mask, isNull;
388 int gt8, gt16, dstIncrement, nofNull;
391 if ((precision != -1) && (precision <= 15)) {
394 while (dstStop != cp3) {
399 if (precision <= 7) {
402 while (dstStop != cp3) {
411 if ((precision >= 23) || (precision == -1)) {
416 while (dstStop !=
dst) {
428 while (dstStop != (
unsigned char *)f)
436 gt16 = precision > 16;
438 dstIncrement = 1 + (!gt8) + (!gt16);
448 while (fStop != f++) {
449 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
450 if (nNullBits == 8) {
459 cp0 = nullBits + size / 8 + ((size % 8) != 0);
462 cp3 = cp0 + size - nofNull;
463 cp2 = cp3 + size - nofNull;
464 cp1 = cp3 + (gt8 + gt16) * (size - nofNull);
466 mask = clearMask[precision];
467 nBits = nNullBits = 0;
469 while (dstStop !=
dst) {
471 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
473 if (nNullBits == 8) {
491 if (nBits && precision) {
492 *
dst = (
unsigned char)((*cp1 << nBits) & mask);
494 if (8 - nBits < (
unsigned int)precision) {
496 *
dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
497 nBits += precision - 8;
500 nBits = (nBits + precision) % 8;
506 *
dst = (
unsigned char)(*cp1 & mask);
507 nBits = (nBits + precision) % 8;
518 static void G_fpcompress_rearrangeDecodeDoubles(
unsigned char *
src,
int size,
522 unsigned int nNullBits, nBits;
523 register unsigned char *dstStop;
524 register unsigned char *cp0, *cp1, *cp2, *cp3;
525 register unsigned char *cp4, *cp5, *cp6, *cp7, *nullBits;
526 unsigned char mask, isNull;
527 int gt8, gt16, gt24, gt32, gt40, gt48, dstIncrement, offs, nofNull;
530 if ((precision != -1) && (precision <= 44)) {
531 for (offs = 7; offs >= (precision + 19) / 8; offs--) {
534 while (dstStop != cp7) {
543 if ((precision >= 52) || (precision == -1)) {
553 while (dstStop !=
dst) {
570 while (dstStop != (
unsigned char *)d)
578 gt48 = precision > 48;
579 gt40 = precision > 40;
580 gt32 = precision > 32;
581 gt24 = precision > 24;
582 gt16 = precision > 16;
585 dstIncrement = 1 + (!gt8) + (!gt16) + (!gt24) + (!gt32) + (!gt40) + (!gt48);
595 while (dStop != d++) {
596 nofNull += ((*nullBits & ((
unsigned char)1 << nNullBits++)) != 0);
597 if (nNullBits == 8) {
606 cp0 = nullBits + size / 8 + ((size % 8) != 0);
609 cp7 = cp0 + size - nofNull;
610 cp6 = cp7 + size - nofNull;
611 cp5 = cp6 + size - nofNull;
612 cp4 = cp5 + size - nofNull;
613 cp3 = cp4 + size - nofNull;
614 cp2 = cp3 + size - nofNull;
615 cp1 = cp7 + (gt8 + gt16 + gt24 + gt32 + gt40 + gt48) * (size - nofNull);
617 mask = clearMask[precision];
618 nBits = nNullBits = 0;
620 while (dstStop !=
dst) {
622 isNull = *nullBits & ((
unsigned char)1 << nNullBits++);
624 if (nNullBits == 8) {
658 if (nBits && precision) {
659 *
dst = (
unsigned char)((*cp1 << nBits) & mask);
661 if (8 - nBits < (
unsigned int)precision) {
663 *
dst |= (
unsigned char)((*cp1 >> (8 - nBits)) & mask);
664 nBits += precision - 8;
667 nBits = (nBits + precision) % 8;
673 *
dst = (
unsigned char)(*cp1 & mask);
674 nBits = (nBits + precision) % 8;
686 int precision,
char *compressBuf,
694 G_fpcompress_rearrangeEncodeFloats(
695 (
unsigned char *)
src, nofNum, precision,
696 (
unsigned char *)(compressBuf + 1), &nBytes, &offsetMantissa);
698 G_fpcompress_rearrangeEncodeDoubles(
699 (
unsigned char *)
src, nofNum, precision,
700 (
unsigned char *)(compressBuf + 1), &nBytes, &offsetMantissa);
707 Rast3d_error(
"Rast3d_fpcompress_write_xdr_nums: write error");
717 int fileBytes,
int precision,
718 char *compressBuf,
int isFloat)
721 int lengthEncode, lengthDecode;
723 char *
src, *dest, *srcStop;
728 nofNum * nBytes + 1, 2);
731 Rast3d_error(
"Rast3d_fpcompress_read_xdr_nums: read error");
736 if (*compressBuf++ == 1) {
743 if (status == nofNum * nBytes)
744 status -= lengthDecode - lengthEncode;
746 src = compressBuf + status - 1;
747 srcStop = compressBuf + lengthEncode - 1;
748 dest = compressBuf + (status - lengthEncode) + lengthDecode - 1;
749 while (
src != srcStop)
753 srcStop =
src + lengthDecode;
755 while (
src != srcStop)
760 G_fpcompress_rearrangeDecodeFloats((
unsigned char *)compressBuf, nofNum,
761 precision, (
unsigned char *)
dst);
763 G_fpcompress_rearrangeDecodeDoubles((
unsigned char *)compressBuf,
765 (
unsigned char *)
dst);
int G_read_compressed(int, int, unsigned char *, int, int)
int G_write_compressed(int, unsigned char *, int, int)
int Rast3d_is_xdr_null_double(const double *)
void Rast3d_set_xdr_null_double(double *)
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_is_xdr_null_float(const float *)
void Rast3d_set_xdr_null_float(float *)
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_rle_decode(char *, char *, int, int, int *, int *)
dglInt32_t sign(dglInt32_t x)
int Rast3d_fpcompress_read_xdr_nums(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf, int isFloat)
int Rast3d_fpcompress_write_xdr_nums(int fd, char *src, int nofNum, int precision, char *compressBuf, int isFloat)
#define XDR_DOUBLE_LENGTH
void Rast3d_fpcompress_print_binary(char *c, int numBits)
void Rast3d_fpcompress_dissect_xdr_double(unsigned char *numPointer)