2 %---------------------------------------------------------------*
4 \section{Underlying code for converting to/from ``bytes''}
6 %---------------------------------------------------------------*
8 Stolen from HBC, more or less.
10 A \tr{I_ foo2bytes__(foo in, ptr arr)} routine takes a \tr{foo}
11 input \tr{in}, scribbles some appropriate bytes into the array passed
12 to it, \tr{arr}, and returns the number of bytes so put.
14 A \tr{I_ bytes2foo__(ptr arr, foo *out)} routine looks at the
15 array of bytes given to it (\tr{arr}) and gives them back interpreted
16 as a \tr{foo} (sticks it in the place pointed to by \tr{out}). It
17 returns the number of bytes taken.
26 /* need the ANSI arg decl, so "short" and "float" args dont get promoted */
27 #define X2BYTES(type) \
29 type##2bytes(type in, unsigned char *arr) \
33 unsigned char cs[sizeof (type)]; \
38 for (k = sizeof (type) - 1; k >= 0; k--) \
41 return(sizeof (type)); \
45 #define X2BYTES(type) \
47 type##2bytes(type in, unsigned char *arr) \
51 unsigned char cs[sizeof (type)]; \
56 for (k = sizeof (type) - 1; k >= 0; k--) \
59 return(sizeof (type)); \
69 #define BYTES2X(ctype,htype) \
71 bytes2##ctype##__(P_ in, htype *out) \
75 unsigned char cs[sizeof (ctype)]; \
78 unsigned char *arr = (unsigned char *) in; \
80 for (k = 0; k < sizeof(ctype); k++) \
85 return(sizeof (ctype)); \
88 #define BYTES2FX(ctype,htype,assign_fx) \
90 bytes2##ctype##__(P_ in, htype *out) \
94 unsigned char cs[sizeof (ctype)]; \
97 unsigned char *arr = (unsigned char *) in; \
99 for (k = 0; k < sizeof(ctype); k++) \
102 assign_fx((P_)out, (htype) u.i); \
104 return(sizeof (ctype)); \
111 BYTES2FX(float,StgFloat,ASSIGN_FLT)
112 BYTES2FX(double,StgDouble,ASSIGN_DBL)