modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / byteswap.c
blob96fbd58488fbdd14c9e65f65bd43c1c01c4bf328
1 /*
2 * byteswap - byte swapping routines
4 * Copyright (C) 1999 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: byteswap.c,v 30.2 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/byteswap.c,v $
24 * Under source code control: 1995/10/11 04:44:01
25 * File existed as early as: 1995
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 #include "cmath.h"
33 #include "byteswap.h"
37 * swap_b8_in_HALFs - swap 8 and if needed, 16 bits in an array of HALFs
39 * given:
40 * dest - pointer to where the swapped src wil be put or
41 * NULL to allocate the storage
42 * src - pointer to a HALF array to swap
43 * len - length of the src HALF array
45 * returns:
46 * pointer to where the swapped src has been put
48 HALF *
49 swap_b8_in_HALFs(HALF *dest, HALF *src, LEN len)
51 HALF *ret;
52 LEN i;
55 * allocate storage if needed
57 if (dest == NULL) {
58 dest = alloc(len);
60 ret = dest;
63 * swap the array
65 for (i=0; i < len; ++i, ++dest, ++src) {
66 SWAP_B8_IN_HALF(dest, src);
70 * return the result
72 return ret;
77 * swap_b8_in_ZVALUE - swap 8 and if needed, 16 bits in a ZVALUE
79 * given:
80 * dest - pointer to where the swapped src wil be put or
81 * NULL to allocate the storage
82 * src - pointer to a ZVALUE to swap
83 * all - TRUE => swap every element, FALSE => swap only the
84 * multiprecision storage
86 * returns:
87 * pointer to where the swapped src has been put
89 * This macro will either switch to the opposite byte sex (Big Endian vs.
90 * Little Endian) the elements of a ZVALUE.
92 ZVALUE *
93 swap_b8_in_ZVALUE(ZVALUE *dest, ZVALUE *src, BOOL all)
96 * allocate storage if needed
98 if (dest == NULL) {
101 * allocate the storage
103 dest = malloc(sizeof(ZVALUE));
104 if (dest == NULL) {
105 math_error("swap_b8_in_ZVALUE: swap_b8_in_ZVALUE: "
106 "Not enough memory");
107 /*NOTREACHED*/
111 * allocate (by forcing swap_b8_in_ZVALUE) and swap storage
113 dest->v = swap_b8_in_HALFs(NULL, src->v, src->len);
115 } else {
118 * swap storage
120 if (dest->v != NULL) {
121 zfree(*dest);
123 dest->v = swap_b8_in_HALFs(NULL, src->v, src->len);
127 * swap or copy the rest of the ZVALUE elements
129 if (all) {
130 dest->len = (LEN)SWAP_B8_IN_LEN(&dest->len, &src->len);
131 dest->sign = (BOOL)SWAP_B8_IN_BOOL(&dest->sign, &src->sign);
132 } else {
133 dest->len = src->len;
134 dest->sign = src->sign;
138 * return the result
140 return dest;
145 * swap_b8_in_NUMBER - swap 8 and if needed, 16 bits in a NUMBER
147 * given:
148 * dest - pointer to where the swapped src wil be put or
149 * NULL to allocate the storage
150 * src - pointer to a NUMBER to swap
151 * all - TRUE => swap every element, FALSE => swap only the
152 * multiprecision storage
154 * returns:
155 * pointer to where the swapped src has been put
157 * This macro will either switch to the opposite byte sex (Big Endian vs.
158 * Little Endian) the elements of a NUMBER.
160 NUMBER *
161 swap_b8_in_NUMBER(NUMBER *dest, NUMBER *src, BOOL all)
164 * allocate storage if needed
166 if (dest == NULL) {
169 * allocate the storage
171 dest = malloc(sizeof(NUMBER));
172 if (dest == NULL) {
173 math_error("swap_b8_in_NUMBER: Not enough memory");
174 /*NOTREACHED*/
178 * allocate (by forcing swap_b8_in_ZVALUE) and swap storage
180 dest->num = *swap_b8_in_ZVALUE(NULL, &src->num, all);
181 dest->den = *swap_b8_in_ZVALUE(NULL, &src->den, all);
183 } else {
186 * swap storage
188 dest->num = *swap_b8_in_ZVALUE(&dest->num, &src->num, all);
189 dest->den = *swap_b8_in_ZVALUE(&dest->den, &src->den, all);
193 * swap or copy the rest of the NUMBER elements
195 if (all) {
196 dest->links = (long)SWAP_B8_IN_LONG(&dest->links, &src->links);
197 } else {
198 dest->links = src->links;
202 * return the result
204 return dest;
209 * swap_b8_in_COMPLEX - swap 8 and if needed, 16 bits in a COMPLEX
211 * given:
212 * dest - pointer to where the swapped src wil be put or
213 * NULL to allocate the storage
214 * src - pointer to a COMPLEX to swap
215 * all - TRUE => swap every element, FALSE => swap only the
216 * multiprecision storage
218 * returns:
219 * pointer to where the swapped src has been put
221 * This macro will either switch to the opposite byte sex (Big Endian vs.
222 * Little Endian) the elements of a COMPLEX.
224 COMPLEX *
225 swap_b8_in_COMPLEX(COMPLEX *dest, COMPLEX *src, BOOL all)
228 * allocate storage if needed
230 if (dest == NULL) {
233 * allocate the storage
235 dest = malloc(sizeof(COMPLEX));
236 if (dest == NULL) {
237 math_error("swap_b8_in_COMPLEX: Not enough memory");
238 /*NOTREACHED*/
242 * allocate (by forcing swap_b8_in_ZVALUE) and swap storage
244 dest->real = swap_b8_in_NUMBER(NULL, src->real, all);
245 dest->imag = swap_b8_in_NUMBER(NULL, src->imag, all);
247 } else {
250 * swap storage
252 dest->real = swap_b8_in_NUMBER(dest->real, src->real, all);
253 dest->imag = swap_b8_in_NUMBER(dest->imag, src->imag, all);
257 * swap or copy the rest of the NUMBER elements
259 if (all) {
260 dest->links = (long)SWAP_B8_IN_LONG(&dest->links, &src->links);
261 } else {
262 dest->links = src->links;
266 * return the result
268 return dest;
273 * swap_b16_in_HALFs - swap 16 bits in an array of HALFs
275 * given:
276 * dest - pointer to where the swapped src wil be put or
277 * NULL to allocate the storage
278 * src - pointer to a HALF array to swap
279 * len - length of the src HALF array
281 * returns:
282 * pointer to where the swapped src has been put
284 HALF *
285 swap_b16_in_HALFs(HALF *dest, HALF *src, LEN len)
287 HALF *ret;
288 LEN i;
291 * allocate storage if needed
293 if (dest == NULL) {
294 dest = alloc(len);
296 ret = dest;
299 * swap the array
301 for (i=0; i < len; ++i, ++dest, ++src) {
302 SWAP_B16_IN_HALF(dest, src);
306 * return the result
308 return ret;
313 * swap_b16_in_ZVALUE - swap 16 bits in a ZVALUE
315 * given:
316 * dest - pointer to where the swapped src wil be put or
317 * NULL to allocate the storage
318 * src - pointer to a ZVALUE to swap
319 * all - TRUE => swap every element, FALSE => swap only the
320 * multiprecision storage
322 * returns:
323 * pointer to where the swapped src has been put
325 * This macro will either switch to the opposite byte sex (Big Endian vs.
326 * Little Endian) the elements of a ZVALUE.
328 ZVALUE *
329 swap_b16_in_ZVALUE(ZVALUE *dest, ZVALUE *src, BOOL all)
332 * allocate storage if needed
334 if (dest == NULL) {
337 * allocate the storage
339 dest = malloc(sizeof(ZVALUE));
340 if (dest == NULL) {
341 math_error("swap_b16_in_ZVALUE: Not enough memory");
342 /*NOTREACHED*/
346 * allocate (by forcing swap_b16_in_ZVALUE) and swap storage
348 dest->v = swap_b16_in_HALFs(NULL, src->v, src->len);
350 } else {
353 * swap storage
355 if (dest->v != NULL) {
356 zfree(*dest);
358 dest->v = swap_b16_in_HALFs(NULL, src->v, src->len);
362 * swap or copy the rest of the ZVALUE elements
364 if (all) {
365 dest->len = (LEN)SWAP_B16_IN_LEN(&dest->len, &src->len);
366 dest->sign = (BOOL)SWAP_B16_IN_BOOL(&dest->sign, &src->sign);
367 } else {
368 dest->len = src->len;
369 dest->sign = src->sign;
373 * return the result
375 return dest;
380 * swap_b16_in_NUMBER - swap 16 bits in a NUMBER
382 * given:
383 * dest - pointer to where the swapped src wil be put or
384 * NULL to allocate the storage
385 * src - pointer to a NUMBER to swap
386 * all - TRUE => swap every element, FALSE => swap only the
387 * multiprecision storage
389 * returns:
390 * pointer to where the swapped src has been put
392 * This macro will either switch to the opposite byte sex (Big Endian vs.
393 * Little Endian) the elements of a NUMBER.
395 NUMBER *
396 swap_b16_in_NUMBER(NUMBER *dest, NUMBER *src, BOOL all)
399 * allocate storage if needed
401 if (dest == NULL) {
404 * allocate the storage
406 dest = malloc(sizeof(NUMBER));
407 if (dest == NULL) {
408 math_error("swap_b16_in_NUMBER: Not enough memory");
409 /*NOTREACHED*/
413 * allocate (by forcing swap_b16_in_ZVALUE) and swap storage
415 dest->num = *swap_b16_in_ZVALUE(NULL, &src->num, all);
416 dest->den = *swap_b16_in_ZVALUE(NULL, &src->den, all);
418 } else {
421 * swap storage
423 dest->num = *swap_b16_in_ZVALUE(&dest->num, &src->num, all);
424 dest->den = *swap_b16_in_ZVALUE(&dest->den, &src->den, all);
428 * swap or copy the rest of the NUMBER elements
430 if (all) {
431 dest->links = (long)SWAP_B16_IN_LONG(&dest->links, &src->links);
432 } else {
433 dest->links = src->links;
437 * return the result
439 return dest;
444 * swap_b16_in_COMPLEX - swap 16 bits in a COMPLEX
446 * given:
447 * dest - pointer to where the swapped src wil be put or
448 * NULL to allocate the storage
449 * src - pointer to a COMPLEX to swap
450 * all - TRUE => swap every element, FALSE => swap only the
451 * multiprecision storage
453 * returns:
454 * pointer to where the swapped src has been put
456 * This macro will either switch to the opposite byte sex (Big Endian vs.
457 * Little Endian) the elements of a COMPLEX.
459 COMPLEX *
460 swap_b16_in_COMPLEX(COMPLEX *dest, COMPLEX *src, BOOL all)
463 * allocate storage if needed
465 if (dest == NULL) {
468 * allocate the storage
470 dest = malloc(sizeof(COMPLEX));
471 if (dest == NULL) {
472 math_error("swap_b16_in_COMPLEX: Not enough memory");
473 /*NOTREACHED*/
477 * allocate (by forcing swap_b16_in_ZVALUE) and swap storage
479 dest->real = swap_b16_in_NUMBER(NULL, src->real, all);
480 dest->imag = swap_b16_in_NUMBER(NULL, src->imag, all);
482 } else {
485 * swap storage
487 dest->real = swap_b16_in_NUMBER(dest->real, src->real, all);
488 dest->imag = swap_b16_in_NUMBER(dest->imag, src->imag, all);
492 * swap or copy the rest of the NUMBER elements
494 if (all) {
495 dest->links = (long)SWAP_B16_IN_LONG(&dest->links, &src->links);
496 } else {
497 dest->links = src->links;
501 * return the result
503 return dest;
508 * swap_HALF_in_ZVALUE - swap HALFs in a ZVALUE
510 * given:
511 * dest - pointer to where the swapped src wil be put or
512 * NULL to allocate the storage
513 * src - pointer to a ZVALUE to swap
514 * all - TRUE => swap every element, FALSE => swap only the
515 * multiprecision storage
517 * returns:
518 * pointer to where the swapped src has been put
520 * This macro will either switch to the opposite byte sex (Big Endian vs.
521 * Little Endian) the elements of a ZVALUE.
523 ZVALUE *
524 swap_HALF_in_ZVALUE(ZVALUE *dest, ZVALUE *src, BOOL all)
527 * allocate storage if needed
529 if (dest == NULL) {
532 * allocate the storage
534 dest = malloc(sizeof(ZVALUE));
535 if (dest == NULL) {
536 math_error("swap_HALF_in_ZVALUE: Not enough memory");
537 /*NOTREACHED*/
541 * copy storage because we are dealing with HALFs
543 dest->v = (HALF *) zcopyval(*src, *dest);
545 } else {
548 * copy storage because we are dealing with HALFs
550 if (dest->v != NULL) {
551 zfree(*dest);
552 dest->v = alloc(src->len);
554 zcopyval(*src, *dest);
558 * swap or copy the rest of the ZVALUE elements
560 if (all) {
561 dest->len = (LEN)SWAP_HALF_IN_LEN(&dest->len, &src->len);
562 dest->sign = (BOOL)SWAP_HALF_IN_BOOL(&dest->sign, &src->sign);
563 } else {
564 dest->len = src->len;
565 dest->sign = src->sign;
569 * return the result
571 return dest;
576 * swap_HALF_in_NUMBER - swap HALFs in a NUMBER
578 * given:
579 * dest - pointer to where the swapped src wil be put or
580 * NULL to allocate the storage
581 * src - pointer to a NUMBER to swap
582 * all - TRUE => swap every element, FALSE => swap only the
583 * multiprecision storage
585 * returns:
586 * pointer to where the swapped src has been put
588 * This macro will either switch to the opposite byte sex (Big Endian vs.
589 * Little Endian) the elements of a NUMBER.
591 NUMBER *
592 swap_HALF_in_NUMBER(NUMBER *dest, NUMBER *src, BOOL all)
595 * allocate storage if needed
597 if (dest == NULL) {
600 * allocate the storage
602 dest = malloc(sizeof(NUMBER));
603 if (dest == NULL) {
604 math_error("swap_HALF_in_NUMBER: Not enough memory");
605 /*NOTREACHED*/
609 * allocate (by forcing swap_HALF_in_ZVALUE) and swap storage
611 dest->num = *swap_HALF_in_ZVALUE(NULL, &src->num, all);
612 dest->den = *swap_HALF_in_ZVALUE(NULL, &src->den, all);
614 } else {
617 * swap storage
619 dest->num = *swap_HALF_in_ZVALUE(&dest->num, &src->num, all);
620 dest->den = *swap_HALF_in_ZVALUE(&dest->den, &src->den, all);
624 * swap or copy the rest of the NUMBER elements
626 if (all) {
627 dest->links = (long)SWAP_HALF_IN_LONG(&dest->links,&src->links);
628 } else {
629 dest->links = src->links;
633 * return the result
635 return dest;
640 * swap_HALF_in_COMPLEX - swap HALFs in a COMPLEX
642 * given:
643 * dest - pointer to where the swapped src wil be put or
644 * NULL to allocate the storage
645 * src - pointer to a COMPLEX to swap
646 * all - TRUE => swap every element, FALSE => swap only the
647 * multiprecision storage
649 * returns:
650 * pointer to where the swapped src has been put
652 * This macro will either switch to the opposite byte sex (Big Endian vs.
653 * Little Endian) the elements of a COMPLEX.
655 COMPLEX *
656 swap_HALF_in_COMPLEX(COMPLEX *dest, COMPLEX *src, BOOL all)
659 * allocate storage if needed
661 if (dest == NULL) {
664 * allocate the storage
666 dest = malloc(sizeof(COMPLEX));
667 if (dest == NULL) {
668 math_error("swap_HALF_in_COMPLEX: Not enough memory");
669 /*NOTREACHED*/
673 * allocate (by forcing swap_HALF_in_ZVALUE) and swap storage
675 dest->real = swap_HALF_in_NUMBER(NULL, src->real, all);
676 dest->imag = swap_HALF_in_NUMBER(NULL, src->imag, all);
678 } else {
681 * swap storage
683 dest->real = swap_HALF_in_NUMBER(dest->real, src->real, all);
684 dest->imag = swap_HALF_in_NUMBER(dest->imag, src->imag, all);
688 * swap or copy the rest of the NUMBER elements
690 if (all) {
691 dest->links = (long)SWAP_HALF_IN_LONG(&dest->links,&src->links);
692 } else {
693 dest->links = src->links;
697 * return the result
699 return dest;