1 /**
2  ** ordswap.c ---- multibyte value order swaping
3  **
4  ** Copyright (c) 1998 Hartmut Schirmer
5  **
6  ** This file is part of the GRX graphics library.
7  **
8  ** The GRX graphics library is free software; you can redistribute it
9  ** and/or modify it under some conditions; see the "copying.grx" file
10  ** for details.
11  **
12  ** This library is distributed in the hope that it will be useful,
13  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  **
16  **/
17 
18 
19 #include "libgrx.h"
20 #include "mempeek.h"
21 #include "ordswap.h"
22 
23 #if 1
24 #define SWAPBYTE(ty,mb,src,dst) (((ty)((GR_int8u)((mb) >> (8*(src)))) << (8*(dst))))
25 
_GR_swap16(GR_int16 far * w)26 void _GR_swap16(GR_int16 far *w) {
27   GR_int16 res;
28   GRX_ENTER();
29   res  = SWAPBYTE(GR_int16,*w,1,0);
30   res |= SWAPBYTE(GR_int16,*w,0,1);
31   *w = res;
32   GRX_LEAVE();
33 }
34 
_GR_swap32(GR_int32 far * l)35 void _GR_swap32(GR_int32 far *l) {
36   GR_int32 res;
37   GRX_ENTER();
38   res  = SWAPBYTE(GR_int32,*l,3,0);
39   res |= SWAPBYTE(GR_int16,*l,0,3);
40   res |= SWAPBYTE(GR_int16,*l,2,1);
41   res |= SWAPBYTE(GR_int16,*l,1,2);
42   *l = res;
43   GRX_LEAVE();
44 }
45 
46 #ifdef GR_int64
47 
_GR_swap64(GR_int64 far * h)48 void _GR_swap64(GR_int64 far *h) {
49   GR_int64 res;
50   GRX_ENTER();
51   res  = SWAPBYTE(GR_int64,*h,7,0);
52   res |= SWAPBYTE(GR_int64,*h,0,7);
53   res |= SWAPBYTE(GR_int64,*h,6,1);
54   res |= SWAPBYTE(GR_int64,*h,1,6);
55   res |= SWAPBYTE(GR_int64,*h,5,2);
56   res |= SWAPBYTE(GR_int64,*h,2,5);
57   res |= SWAPBYTE(GR_int64,*h,4,3);
58   res |= SWAPBYTE(GR_int64,*h,3,4);
59   *h = res;
60   GRX_LEAVE();
61 }
62 #endif
63 
64 #else
swapbytes(GR_int8 far * b1,GR_int8 far * b2)65 static void swapbytes(GR_int8 far *b1, GR_int8 far *b2) {
66   GR_int8 b;
67   GRX_ENTER();
68   b = peek_b(b1);
69   poke_b(b1, peek_b(b2));
70   poke_b(b2, b);
71   GRX_LEAVE();
72 }
73 
_GR_swap16(GR_int16 far * w)74 void _GR_swap16(GR_int16 far *w) {
75   GRX_ENTER();
76   swapbytes((GR_int8 far *)w, ((GR_int8 far *)w)+1);
77   GRX_LEAVE();
78 }
79 
_GR_swap32(GR_int32 far * l)80 void _GR_swap32(GR_int32 far *l) {
81   GRX_ENTER();
82   swapbytes(((GR_int8 far *)l)  , ((GR_int8 far *)l)+3);
83   swapbytes(((GR_int8 far *)l)+1, ((GR_int8 far *)l)+2);
84   GRX_LEAVE();
85 }
86 
87 #ifdef GR_int64
88 
_GR_swap64(GR_int64 far * h)89 void _GR_swap64(GR_int64 far *h) {
90   GRX_ENTER();
91   swapbytes(((GR_int8 far *)h)  , ((GR_int8 far *)h)+7);
92   swapbytes(((GR_int8 far *)h)+1, ((GR_int8 far *)h)+6);
93   swapbytes(((GR_int8 far *)h)+2, ((GR_int8 far *)h)+5);
94   swapbytes(((GR_int8 far *)h)+3, ((GR_int8 far *)h)+4);
95   GRX_LEAVE();
96 }
97 
98 #endif
99 
100 
101 #endif
102