summaryrefslogtreecommitdiff
path: root/xc/lib/PEX5/pl_convert.h
blob: 96b269e6b632a6fd54c7eb52cc4b9d43772b9f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* $Xorg: pl_convert.h,v 1.3 2000/08/17 19:44:22 cpqbld Exp $ */
/*

Copyright 1992, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.

*/

/* -------------------------------------------------------------------------
 * Structures for fiddling with 16 and 32 bit data on 64 bit machines
 * ------------------------------------------------------------------------- */

#ifdef WORD64

typedef struct {
    int value   :32;
} Long;

typedef struct {
    int value   :16;
    int pad     :16;
} Short;
 
typedef struct {
    int value   :8;
    int pad     :24;
} Char;

typedef struct {
    unsigned int value :32;
} ULong;

typedef struct {
    unsigned int value :16;
} UShort;

typedef struct {
    unsigned char value;
} UChar;

#endif /* WORD64 */



/* -------------------------------------------------------------------------
 * Floating point conversion macros
 * ------------------------------------------------------------------------- */

/*
 * Host->Network
 * _srcVal = host floating point value
 * _dstBuf = buffer to store network float
 */

#define FP_CONVERT_HTON_BUFF(_srcVal, _dstBuf, _fpFormat) \
{ \
    PEX_fp_convert[NATIVE_FP_FORMAT-1][_fpFormat-1](&(_srcVal), _dstBuf); \
}


/*
 * Host->Network
 * _srcVal = host floating point value
 * _dstVal = store network float in this value (on 64 bit machines,
 *           _dstVal is most probably a bitfield, so we can't take &_dstVal.
 */

#ifndef WORD64

#define FP_CONVERT_HTON(_srcVal, _dstVal, _fpFormat) \
{ \
    FP_CONVERT_HTON_BUFF (_srcVal, &(_dstVal), _fpFormat); \
}

#define FP_CONVERT_DHTON(_srcVal, _dstVal, _fpFormat) \
{ \
    float single = _srcVal; \
    FP_CONVERT_HTON_BUFF (single, &(_dstVal), _fpFormat); \
}

#else /* WORD64 */

#define FP_CONVERT_HTON(_srcVal, _dstVal, _fpFormat) \
{ \
    Long temp; \
    FP_CONVERT_HTON_BUFF (_srcVal, &temp, _fpFormat); \
    _dstVal = temp.value; \
}

#define FP_CONVERT_DHTON(_srcVal, _dstVal, _fpFormat) \
{ \
    float single = _srcVal; \
    Long temp; \
    FP_CONVERT_HTON_BUFF (single, &temp, _fpFormat); \
    _dstVal = temp.value; \
}

#endif /* WORD64 */


/*
 * Network->Host
 * _srcBuf = buffer containing network floating point value
 * _dstVal = store host float in this value
 */

#define FP_CONVERT_NTOH_BUFF(_srcBuf, _dstVal, _fpFormat) \
{ \
    PEX_fp_convert[_fpFormat-1][NATIVE_FP_FORMAT-1](_srcBuf, &_dstVal); \
}


/*
 * Network->Host
 * _srcVal = network floating point value
 * _dstVal = store host float in this value
 */

#ifndef WORD64

#define FP_CONVERT_NTOH(_srcVal, _dstVal, _fpFormat) \
{ \
    FP_CONVERT_NTOH_BUFF (&(_srcVal), _dstVal, _fpFormat); \
}

#else /* WORD64 */

#define FP_CONVERT_NTOH(_srcVal, _dstVal, _fpFormat) \
{ \
    Long temp; \
    temp.value = _srcVal; \
    FP_CONVERT_NTOH_BUFF (&temp, _dstVal, _fpFormat); \
}

#endif /* WORD64 */



/* -------------------------------------------------------------------------
 * Macros for 64 bit conversion
 * ------------------------------------------------------------------------- */

#ifdef WORD64

#define CARD64_TO_32(_val, _pBuf) \
{ \
    Long _d; \
    _d.value = _val; \
    memcpy (_pBuf, &_d, SIZEOF (CARD32)); \
}

#define CARD64_TO_16(_val, _pBuf) \
{ \
    Short _d; \
    _d.value = _val; \
    memcpy (_pBuf, &_d, SIZEOF (CARD16)); \
}

#define INT64_TO_32(_val, _pBuf) \
{ \
    Long _d; \
    _d.value = _val; \
    memcpy (_pBuf, &_d, SIZEOF (INT32)); \
}

#define INT64_TO_16(_val, _pBuf) \
{ \
    Short _d; \
    _d.value = _val; \
    memcpy (_pBuf, &_d, SIZEOF (INT16)); \
}


#define CARD32_TO_64(_pBuf, _val) \
{ \
    _val = *(_pBuf + 0) & 0xff; 	/* 0xff incase _pBuf is signed */ \
    _val <<= 8; \
    _val |= *(_pBuf + 1) & 0xff;\
    _val <<= 8; \
    _val |= *(_pBuf + 2) & 0xff;\
    _val <<= 8; \
    _val |= *(_pBuf + 3) & 0xff;\
}

#define CARD16_TO_64(_pBuf, _val) \
{ \
    _val = *(_pBuf + 0) & 0xff; 	/* 0xff incase _pBuf is signed */ \
    _val <<= 8; \
    _val |= *(_pBuf + 1) & 0xff;\
}

#define INT32_TO_64(_pBuf, _val) \
{ \
    _val = *(_pBuf + 0) & 0xff; 	/* 0xff incase _pBuf is signed */ \
    _val <<= 8; \
    _val |= *(_pBuf + 1) & 0xff;\
    _val <<= 8; \
    _val |= *(_pBuf + 2) & 0xff;\
    _val <<= 8; \
    _val |= *(_pBuf + 3) & 0xff;\
}

#define INT16_TO_64(_pBuf, _val) \
{ \
    _val = *(_pBuf + 0) & 0xff; 	/* 0xff incase _pBuf is signed */ \
    _val <<= 8; \
    _val |= *(_pBuf + 1) & 0xff;\
}

#endif /* WORD64 */



/* -------------------------------------------------------------------------
 * DEC/IEEE float conversions
 * ------------------------------------------------------------------------- */

/* 
 * Copyright 1988-1991
 * Center for Information Technology Integration (CITI)
 * Information Technology Division
 * University of Michigan
 * Ann Arbor, Michigan
 *
 *                         All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the names of
 * CITI or THE UNIVERSITY OF MICHIGAN not be used in advertising or
 * publicity pertaining to distribution of the software without
 * specific, written prior permission.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS." CITI AND THE UNIVERSITY OF
 * MICHIGAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL CITI OR THE UNIVERSITY OF MICHIGAN BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#define BITMASK(n) ((((unsigned long)1)<<n)-1)
#define VAX_EXPONENT_BIAS	0x00000081
#define IEEE_EXPONENT_BIAS	0x0000007f

#define VAX_TO_IEEE_BIAS ((CARD32)-VAX_EXPONENT_BIAS + IEEE_EXPONENT_BIAS)
#define IEEE_TO_VAX_BIAS ((CARD32)-IEEE_EXPONENT_BIAS + VAX_EXPONENT_BIAS)

#define MAX_VAX_NEGATIVE 0xffffffff
#define MIN_VAX_NEGATIVE 0x00008000
#define MAX_VAX_POSITIVE 0xffff7fff
#define MIN_VAX_POSITIVE 0x00000000
#define VAX_SIGN_MASK 0xffff7fff

#define MIN_IEEE_NEGATIVE 0x80000000
#define MAX_IEEE_NEGATIVE 0xff800000
#define MIN_IEEE_POSITIVE 0x00000000
#define MAX_IEEE_POSITIVE 0x7f800000
#define IEEE_SIGN_MASK 0x7fffffff



/* -------------------------------------------------------------------------
 * Cray float conversions
 * ------------------------------------------------------------------------- */

#ifdef CRAY

struct	ieee_single {
	unsigned int	zero	: 32;	/* Upper 32 bits are junk */
	unsigned int	sign	: 1;
	unsigned int	exp	: 8;
	unsigned int	mantissa: 23;	/* 24-bit mantissa with 1 hidden bit */
};

/* Cray floating point, partitioned for easy conversion to IEEE single */

struct	cray_single {
	unsigned int	sign	 : 1;
	unsigned int	exp	 : 15;
	unsigned int	mantissa : 24;
	unsigned int	mantissa2: 24;
};

struct	cray_double {
	unsigned int	sign	 : 1;
	unsigned int	exp	 : 15;
	unsigned int	mantissa : 48;
};

#define SET_MIN_SNG_IEEE(_ieee) \
    _ieee.zero = 0; \
    _ieee.sign = 0; \
    _ieee.exp = 0x00; \
    _ieee.mantissa = 0;

#define SET_MAX_SNG_IEEE(_ieee) \
    _ieee.zero = 0; \
    _ieee.sign = 0; \
    _ieee.exp = 0xff; \
    _ieee.mantissa = 0;

#define SET_MAX_SNG_CRAY(_cray) \
    _cray.sign = 0; \
    _cray.exp = 0x6000; \
    _cray.mantissa = 0; \
    _cray.mantissa2 = 0;

#define SET_MAX_DBL_CRAY(_cray) \
    _cray.sign = 0; \
    _cray.exp = 0x6000; \
    _cray.mantissa = 0;


/* Cray exponent limits for conversion to IEEE single */

#define	MAX_CRAY_SNG	(0x100 + CRAY_BIAS - IEEE_SNG_BIAS)
#define	MIN_CRAY_SNG	(0x00 + CRAY_BIAS - IEEE_SNG_BIAS)

#define	CRAY_BIAS	040001
#define IEEE_SNG_BIAS   0x7f

#endif /* CRAY */