1 %---------------------------------------------------------------*
3 \section{Underlying code for converting to/from ``bytes''}
5 %---------------------------------------------------------------*
7 Stolen from HBC, more or less.
9 A \tr{I_ foo2bytes__(foo in, ptr arr)} routine takes a \tr{foo}
10 input \tr{in}, scribbles some appropriate bytes into the array passed
11 to it, \tr{arr}, and returns the number of bytes so put.
13 A \tr{I_ bytes2foo__(ptr arr, foo *out)} routine looks at the
14 array of bytes given to it (\tr{arr}) and gives them back interpreted
15 as a \tr{foo} (sticks it in the place pointed to by \tr{out}). It
16 returns the number of bytes taken.
23 /* need the ANSI arg decl, so "short" and "float" args dont get promoted */
24 #define X2BYTES(type) \
26 CAT2(type,2bytes__)(type in, unsigned char *arr)\
30 unsigned char cs[sizeof (type)]; \
35 for (k = sizeof (type) - 1; k >= 0; k--) \
38 return(sizeof (type)); \
42 #define X2BYTES(type) \
44 CAT2(type,2bytes__)(in, arr) \
50 unsigned char cs[sizeof (type)]; \
55 for (k = sizeof (type) - 1; k >= 0; k--) \
58 return(sizeof (type)); \
68 #define BYTES2X(ctype,htype) \
70 CAT3(bytes2,ctype,__)(in, out) \
76 unsigned char cs[sizeof (ctype)]; \
79 unsigned char *arr = (unsigned char *) in; \
81 for (k = 0; k < sizeof(ctype); k++) \
86 return(sizeof (ctype)); \
91 assign_flt(p_dest, src)
103 assign_dbl(p_dest, src)
109 p_dest[0] = y.du.dhi;
110 p_dest[1] = y.du.dlo;
113 #define BYTES2FX(ctype,htype,assign_fx) \
115 CAT3(bytes2,ctype,__)(in, out) \
121 unsigned char cs[sizeof (ctype)]; \
124 unsigned char *arr = (unsigned char *) in; \
126 for (k = 0; k < sizeof(ctype); k++) \
129 assign_fx(out, (htype) u.i); \
131 return(sizeof (ctype)); \
138 BYTES2FX(float,StgFloat,assign_flt)
139 BYTES2FX(double,StgDouble,assign_dbl)