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,__)(P_ in, htype *out) \
74 unsigned char cs[sizeof (ctype)]; \
77 unsigned char *arr = (unsigned char *) in; \
79 for (k = 0; k < sizeof(ctype); k++) \
84 return(sizeof (ctype)); \
89 assign_flt(W_ p_dest[], StgFloat src)
99 assign_dbl(W_ p_dest[], StgDouble src)
103 p_dest[0] = y.du.dhi;
104 p_dest[1] = y.du.dlo;
107 #define BYTES2FX(ctype,htype,assign_fx) \
109 CAT3(bytes2,ctype,__)(P_ in, htype *out) \
113 unsigned char cs[sizeof (ctype)]; \
116 unsigned char *arr = (unsigned char *) in; \
118 for (k = 0; k < sizeof(ctype); k++) \
121 assign_fx(out, (htype) u.i); \
123 return(sizeof (ctype)); \
130 BYTES2FX(float,StgFloat,assign_flt)
131 BYTES2FX(double,StgDouble,assign_dbl)