xref: /relibc/openlibm/src/fpmath.h (revision 7e5585aaca6c7e52cf4608f26bdcac06d0f1ef6c)
1 /*-
2  * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3  * Copyright (c) 2002 David Schultz <das@FreeBSD.ORG>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/lib/libc/include/fpmath.h,v 1.4 2008/12/23 22:20:59 marcel Exp $
28  */
29 #ifndef _FPMATH_H_
30 #define _FPMATH_H_
31 
32 // Currently assumes Intel platform
33 #if defined (__i386__) || defined(__x86_64__)
34 #ifdef __LP64__
35 #include "amd64_fpmath.h"
36 #else
37 #include "i386_fpmath.h"
38 #endif
39 #endif
40 
41 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
42 
43 /* Definitions provided directly by GCC and Clang. */
44 #define _LITTLE_ENDIAN    __ORDER_LITTLE_ENDIAN__
45 #define _BIG_ENDIAN       __ORDER_BIG_ENDIAN__
46 #define _PDP_ENDIAN       __ORDER_PDP_ENDIAN__
47 #define _BYTE_ORDER       __BYTE_ORDER__
48 
49 #elif defined(__linux)
50 
51 #include <features.h>
52 #include <endian.h>
53 #define _LITTLE_ENDIAN  __LITTLE_ENDIAN
54 #define _BIG_ENDIAN     __BIG_ENDIAN
55 #define _PDP_ENDIAN     __PDP_ENDIAN
56 #define _BYTE_ORDER     __BYTE_ORDER
57 
58 #elif defined(__APPLE__)
59 
60 #include <machine/endian.h>
61 #define _LITTLE_ENDIAN  LITTLE_ENDIAN
62 #define _BIG_ENDIAN     BIG_ENDIAN
63 #define _PDP_ENDIAN     PDP_ENDIAN
64 #define _BYTE_ORDER     BYTE_ORDER
65 
66 #elif defined(__FreeBSD__)
67 
68 #include <machine/endian.h>
69 
70 #elif defined(_WIN32)
71 
72 #define _LITTLE_ENDIAN 1234
73 #define _BIG_ENDIAN    4321
74 #define _PDP_ENDIAN    3412
75 #define _BYTE_ORDER       _LITTLE_ENDIAN
76 #define _FLOAT_WORD_ORDER _LITTLE_ENDIAN
77 #define LITTLE_ENDIAN  _LITTLE_ENDIAN
78 #define BIG_ENDIAN     _BIG_ENDIAN
79 #define PDP_ENDIAN     _PDP_ENDIAN
80 #define BYTE_ORDER     _BYTE_ORDER
81 
82 #endif
83 
84 #ifndef _IEEE_WORD_ORDER
85 #define	_IEEE_WORD_ORDER	_BYTE_ORDER
86 #endif
87 
88 union IEEEf2bits {
89 	float	f;
90 	struct {
91 #if _BYTE_ORDER == _LITTLE_ENDIAN
92 		unsigned int	man	:23;
93 		unsigned int	exp	:8;
94 		unsigned int	sign	:1;
95 #else /* _BIG_ENDIAN */
96 		unsigned int	sign	:1;
97 		unsigned int	exp	:8;
98 		unsigned int	man	:23;
99 #endif
100 	} bits;
101 };
102 
103 #define	DBL_MANH_SIZE	20
104 #define	DBL_MANL_SIZE	32
105 
106 union IEEEd2bits {
107 	double	d;
108 	struct {
109 #if _BYTE_ORDER == _LITTLE_ENDIAN
110 #if _IEEE_WORD_ORDER == _LITTLE_ENDIAN
111 		unsigned int	manl	:32;
112 #endif
113 		unsigned int	manh	:20;
114 		unsigned int	exp	:11;
115 		unsigned int	sign	:1;
116 #if _IEEE_WORD_ORDER == _BIG_ENDIAN
117 		unsigned int	manl	:32;
118 #endif
119 #else /* _BIG_ENDIAN */
120 		unsigned int	sign	:1;
121 		unsigned int	exp	:11;
122 		unsigned int	manh	:20;
123 		unsigned int	manl	:32;
124 #endif
125 	} bits;
126 };
127 
128 #endif
129