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 CAT2(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 CAT2(type,2bytes__)(in, arr) \
53 unsigned char cs[sizeof (type)]; \
58 for (k = sizeof (type) - 1; k >= 0; k--) \
61 return(sizeof (type)); \
71 #define BYTES2X(ctype,htype) \
73 CAT3(bytes2,ctype,__)(P_ in, htype *out) \
77 unsigned char cs[sizeof (ctype)]; \
80 unsigned char *arr = (unsigned char *) in; \
82 for (k = 0; k < sizeof(ctype); k++) \
87 return(sizeof (ctype)); \
92 assign_flt(W_ p_dest[], StgFloat src)
102 assign_dbl(W_ p_dest[], StgDouble src)
106 p_dest[0] = y.du.dhi;
107 p_dest[1] = y.du.dlo;
110 #define BYTES2FX(ctype,htype,assign_fx) \
112 CAT3(bytes2,ctype,__)(P_ in, htype *out) \
116 unsigned char cs[sizeof (ctype)]; \
119 unsigned char *arr = (unsigned char *) in; \
121 for (k = 0; k < sizeof(ctype); k++) \
124 assign_fx(out, (htype) u.i); \
126 return(sizeof (ctype)); \
133 BYTES2FX(float,StgFloat,assign_flt)
134 BYTES2FX(double,StgDouble,assign_dbl)