[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / misc / cbits / ByteOps.c
1 #if 0
2 %---------------------------------------------------------------*
3 %
4 \section{Underlying code for converting to/from ``bytes''}
5 %
6 %---------------------------------------------------------------*
7
8 Stolen from HBC, more or less.
9
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.
13
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.
18
19 \begin{code}
20 #endif /* 0 */
21
22 #include "Rts.h"
23 #include "ByteOps.h"
24
25 #if __STDC__
26     /* need the ANSI arg decl, so "short" and "float" args dont get promoted */
27 #define X2BYTES(type)                           \
28 I_                                              \
29 type##2bytes(type in, unsigned char *arr)       \
30 {                                               \
31     union {                                     \
32         type i;                                 \
33         unsigned char cs[sizeof (type)];        \
34     } u;                                        \
35     int k;                                      \
36                                                 \
37     u.i = in;                                   \
38     for (k = sizeof (type) - 1; k >= 0; k--)    \
39         arr[k] = u.cs[k];                       \
40                                                 \
41     return(sizeof (type));                      \
42 }
43
44 #else /* not STDC */
45 #define X2BYTES(type)                           \
46 I_                                              \
47 type##2bytes(type in, unsigned char *arr)       \
48 {                                               \
49     union {                                     \
50         type i;                                 \
51         unsigned char cs[sizeof (type)];        \
52     } u;                                        \
53     int k;                                      \
54                                                 \
55     u.i = in;                                   \
56     for (k = sizeof (type) - 1; k >= 0; k--)    \
57         arr[k] = u.cs[k];                       \
58                                                 \
59     return(sizeof (type));                      \
60 }
61 #endif /* not STDC */
62
63 X2BYTES(long)
64 X2BYTES(int)
65 X2BYTES(short)
66 X2BYTES(float)
67 X2BYTES(double)
68     
69 #define BYTES2X(ctype,htype)                    \
70 I_                                              \
71 bytes2##ctype##__(P_ in, htype *out)            \
72 {                                               \
73     union {                                     \
74         ctype i;                                \
75         unsigned char cs[sizeof (ctype)];       \
76     } u;                                        \
77     unsigned int k;                             \
78     unsigned char *arr = (unsigned char *) in;  \
79                                                 \
80     for (k = 0; k < sizeof(ctype); k++)         \
81         u.cs[k] = arr[k];                       \
82                                                 \
83     *out = (htype) u.i;                         \
84                                                 \
85     return(sizeof (ctype));                     \
86 }
87     
88 #define BYTES2FX(ctype,htype,assign_fx)         \
89 I_                                              \
90 bytes2##ctype##__(P_ in, htype *out)            \
91 {                                               \
92     union {                                     \
93         ctype i;                                \
94         unsigned char cs[sizeof (ctype)];       \
95     } u;                                        \
96     unsigned int k;                             \
97     unsigned char *arr = (unsigned char *) in;  \
98                                                 \
99     for (k = 0; k < sizeof(ctype); k++)         \
100         u.cs[k] = arr[k];                       \
101                                                 \
102     assign_fx((P_)out, (htype) u.i);            \
103                                                 \
104     return(sizeof (ctype));                     \
105 }
106     
107 BYTES2X(long,I_)
108 BYTES2X(int,I_)
109 BYTES2X(short,I_)
110
111 BYTES2FX(float,StgFloat,ASSIGN_FLT)
112 BYTES2FX(double,StgDouble,ASSIGN_DBL)