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/
37 * swap_b8_in_HALFs - swap 8 and if needed, 16 bits in an array of HALFs
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
46 * pointer to where the swapped src has been put
49 swap_b8_in_HALFs(HALF
*dest
, HALF
*src
, LEN len
)
55 * allocate storage if needed
65 for (i
=0; i
< len
; ++i
, ++dest
, ++src
) {
66 SWAP_B8_IN_HALF(dest
, src
);
77 * swap_b8_in_ZVALUE - swap 8 and if needed, 16 bits in a ZVALUE
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
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.
93 swap_b8_in_ZVALUE(ZVALUE
*dest
, ZVALUE
*src
, BOOL all
)
96 * allocate storage if needed
101 * allocate the storage
103 dest
= malloc(sizeof(ZVALUE
));
105 math_error("swap_b8_in_ZVALUE: swap_b8_in_ZVALUE: "
106 "Not enough memory");
111 * allocate (by forcing swap_b8_in_ZVALUE) and swap storage
113 dest
->v
= swap_b8_in_HALFs(NULL
, src
->v
, src
->len
);
120 if (dest
->v
!= NULL
) {
123 dest
->v
= swap_b8_in_HALFs(NULL
, src
->v
, src
->len
);
127 * swap or copy the rest of the ZVALUE elements
130 dest
->len
= (LEN
)SWAP_B8_IN_LEN(&dest
->len
, &src
->len
);
131 dest
->sign
= (BOOL
)SWAP_B8_IN_BOOL(&dest
->sign
, &src
->sign
);
133 dest
->len
= src
->len
;
134 dest
->sign
= src
->sign
;
145 * swap_b8_in_NUMBER - swap 8 and if needed, 16 bits in a NUMBER
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
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.
161 swap_b8_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
)
164 * allocate storage if needed
169 * allocate the storage
171 dest
= malloc(sizeof(NUMBER
));
173 math_error("swap_b8_in_NUMBER: Not enough memory");
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
);
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
196 dest
->links
= (long)SWAP_B8_IN_LONG(&dest
->links
, &src
->links
);
198 dest
->links
= src
->links
;
209 * swap_b8_in_COMPLEX - swap 8 and if needed, 16 bits in a COMPLEX
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
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.
225 swap_b8_in_COMPLEX(COMPLEX
*dest
, COMPLEX
*src
, BOOL all
)
228 * allocate storage if needed
233 * allocate the storage
235 dest
= malloc(sizeof(COMPLEX
));
237 math_error("swap_b8_in_COMPLEX: Not enough memory");
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
);
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
260 dest
->links
= (long)SWAP_B8_IN_LONG(&dest
->links
, &src
->links
);
262 dest
->links
= src
->links
;
273 * swap_b16_in_HALFs - swap 16 bits in an array of HALFs
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
282 * pointer to where the swapped src has been put
285 swap_b16_in_HALFs(HALF
*dest
, HALF
*src
, LEN len
)
291 * allocate storage if needed
301 for (i
=0; i
< len
; ++i
, ++dest
, ++src
) {
302 SWAP_B16_IN_HALF(dest
, src
);
313 * swap_b16_in_ZVALUE - swap 16 bits in a ZVALUE
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
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.
329 swap_b16_in_ZVALUE(ZVALUE
*dest
, ZVALUE
*src
, BOOL all
)
332 * allocate storage if needed
337 * allocate the storage
339 dest
= malloc(sizeof(ZVALUE
));
341 math_error("swap_b16_in_ZVALUE: Not enough memory");
346 * allocate (by forcing swap_b16_in_ZVALUE) and swap storage
348 dest
->v
= swap_b16_in_HALFs(NULL
, src
->v
, src
->len
);
355 if (dest
->v
!= NULL
) {
358 dest
->v
= swap_b16_in_HALFs(NULL
, src
->v
, src
->len
);
362 * swap or copy the rest of the ZVALUE elements
365 dest
->len
= (LEN
)SWAP_B16_IN_LEN(&dest
->len
, &src
->len
);
366 dest
->sign
= (BOOL
)SWAP_B16_IN_BOOL(&dest
->sign
, &src
->sign
);
368 dest
->len
= src
->len
;
369 dest
->sign
= src
->sign
;
380 * swap_b16_in_NUMBER - swap 16 bits in a NUMBER
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
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.
396 swap_b16_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
)
399 * allocate storage if needed
404 * allocate the storage
406 dest
= malloc(sizeof(NUMBER
));
408 math_error("swap_b16_in_NUMBER: Not enough memory");
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
);
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
431 dest
->links
= (long)SWAP_B16_IN_LONG(&dest
->links
, &src
->links
);
433 dest
->links
= src
->links
;
444 * swap_b16_in_COMPLEX - swap 16 bits in a COMPLEX
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
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.
460 swap_b16_in_COMPLEX(COMPLEX
*dest
, COMPLEX
*src
, BOOL all
)
463 * allocate storage if needed
468 * allocate the storage
470 dest
= malloc(sizeof(COMPLEX
));
472 math_error("swap_b16_in_COMPLEX: Not enough memory");
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
);
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
495 dest
->links
= (long)SWAP_B16_IN_LONG(&dest
->links
, &src
->links
);
497 dest
->links
= src
->links
;
508 * swap_HALF_in_ZVALUE - swap HALFs in a ZVALUE
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
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.
524 swap_HALF_in_ZVALUE(ZVALUE
*dest
, ZVALUE
*src
, BOOL all
)
527 * allocate storage if needed
532 * allocate the storage
534 dest
= malloc(sizeof(ZVALUE
));
536 math_error("swap_HALF_in_ZVALUE: Not enough memory");
541 * copy storage because we are dealing with HALFs
543 dest
->v
= (HALF
*) zcopyval(*src
, *dest
);
548 * copy storage because we are dealing with HALFs
550 if (dest
->v
!= NULL
) {
552 dest
->v
= alloc(src
->len
);
554 zcopyval(*src
, *dest
);
558 * swap or copy the rest of the ZVALUE elements
561 dest
->len
= (LEN
)SWAP_HALF_IN_LEN(&dest
->len
, &src
->len
);
562 dest
->sign
= (BOOL
)SWAP_HALF_IN_BOOL(&dest
->sign
, &src
->sign
);
564 dest
->len
= src
->len
;
565 dest
->sign
= src
->sign
;
576 * swap_HALF_in_NUMBER - swap HALFs in a NUMBER
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
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.
592 swap_HALF_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
)
595 * allocate storage if needed
600 * allocate the storage
602 dest
= malloc(sizeof(NUMBER
));
604 math_error("swap_HALF_in_NUMBER: Not enough memory");
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
);
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
627 dest
->links
= (long)SWAP_HALF_IN_LONG(&dest
->links
,&src
->links
);
629 dest
->links
= src
->links
;
640 * swap_HALF_in_COMPLEX - swap HALFs in a COMPLEX
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
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.
656 swap_HALF_in_COMPLEX(COMPLEX
*dest
, COMPLEX
*src
, BOOL all
)
659 * allocate storage if needed
664 * allocate the storage
666 dest
= malloc(sizeof(COMPLEX
));
668 math_error("swap_HALF_in_COMPLEX: Not enough memory");
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
);
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
691 dest
->links
= (long)SWAP_HALF_IN_LONG(&dest
->links
,&src
->links
);
693 dest
->links
= src
->links
;