]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/inspect_fp.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / math / example / inspect_fp.cpp
1 // inspect.cpp
2
3 // Copyright (c) 2006 Johan Rade
4
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt
7 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9 //-------------------------------------
10
11 #include <cstring>
12
13 #include <iomanip>
14 #include <iostream>
15 #include <limits>
16 #include <boost/detail/endian.hpp>
17
18 //------------------------------------------------------------------------------
19
20 bool is_big_endian()
21 {
22 float x = 1.0f;
23 unsigned char first_byte;
24 memcpy(&first_byte, &x, 1);
25 return first_byte != 0;
26 }
27
28 //------------------------------------------------------------------------------
29
30 void print_processor();
31 void print_endianness();
32 template<class T> void print_table();
33 template<class T> void print_row(const char* name, T val, bool ok = true);
34
35 //------------------------------------------------------------------------------
36
37 int main()
38 {
39 std::cout << '\n';
40
41 print_processor();
42
43 print_endianness();
44
45 std::cout << "---------- float --------------------\n\n";
46 print_table<float>();
47
48 std::cout << "---------- double -------------------\n\n";
49 print_table<double>();
50
51 std::cout << "---------- long double --------------\n\n";
52 print_table<long double>();
53
54 return 0;
55 }
56
57 //------------------------------------------------------------------------------
58
59 void print_processor()
60 {
61 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) \
62 || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) \
63 || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)
64
65 std::cout << "Processor: x86 or x64\n\n";
66
67 #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
68
69 std::cout << "Processor: ia64\n\n";
70
71 #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
72 || defined(__ppc) || defined(__ppc__) || defined(__PPC__)
73
74 std::cout << "Processor: PowerPC\n\n";
75
76 #elif defined(__m68k) || defined(__m68k__) \
77 || defined(__mc68000) || defined(__mc68000__) \
78
79 std::cout << "Processor: Motorola 68K\n\n";
80
81 #else
82
83 std::cout << "Processor: Unknown\n\n";
84
85 #endif
86 }
87
88 void print_endianness()
89 {
90 if(is_big_endian())
91 std::cout << "This platform is big-endian.\n";
92 else
93 std::cout << "This platform is little-endian.\n";
94
95 #ifdef BOOST_BIG_ENDIAN
96 std::cout << "BOOST_BIG_ENDIAN is defined.\n\n";
97 #endif
98 #if defined BOOST_LITTLE_ENDIAN
99 std::cout << "BOOST_LITTTLE_ENDIAN is defined.\n\n";
100 #endif
101 }
102
103 //..............................................................................
104
105 template<class T> void print_table()
106 {
107 print_row("0", (T)0);
108 print_row("sn.min", std::numeric_limits<T>::denorm_min(),
109 std::numeric_limits<T>::has_denorm);
110 print_row("-sn.min", -std::numeric_limits<T>::denorm_min(),
111 std::numeric_limits<T>::has_denorm);
112 print_row("n.min/256", (std::numeric_limits<T>::min)()/256);
113 print_row("n.min/2", (std::numeric_limits<T>::min)()/2);
114 print_row("-n.min/2", -(std::numeric_limits<T>::min)()/2);
115 print_row("n.min", (std::numeric_limits<T>::min)());
116 print_row("1", (T)1);
117 print_row("3/4", (T)3/(T)4);
118 print_row("4/3", (T)4/(T)3);
119 print_row("max", (std::numeric_limits<T>::max)());
120 print_row("inf", std::numeric_limits<T>::infinity(),
121 std::numeric_limits<T>::has_infinity);
122 print_row("q.nan", std::numeric_limits<T>::quiet_NaN(),
123 std::numeric_limits<T>::has_quiet_NaN);
124 print_row("s.nan", std::numeric_limits<T>::signaling_NaN(),
125 std::numeric_limits<T>::has_signaling_NaN);
126
127 std::cout << "\n\n";
128 }
129
130 template<class T>
131 void print_row(const char* name, T val, bool ok)
132 {
133 std::cout << std::left << std::setw(10) << name << std::right;
134
135 std::cout << std::hex << std::setfill('0');
136
137 if(ok) {
138 if(is_big_endian()) {
139 for(size_t i = 0; i < sizeof(T); ++i) {
140 unsigned char c = *(reinterpret_cast<unsigned char*>(&val) + i);
141 std::cout << std::setw(2)
142 << static_cast<unsigned int>(c) << ' ';
143 }
144 }
145 else {
146 for(size_t i = sizeof(T) - 1; i < sizeof(T); --i) {
147 unsigned char c = *(reinterpret_cast<unsigned char*>(&val) + i);
148 std::cout << std::setw(2)
149 << static_cast<unsigned int>(c) << ' ';
150 }
151 }
152 }
153 else {
154 for(size_t i = 0; i < sizeof(T); ++i)
155 std::cout << "-- ";
156 }
157
158 std::cout << '\n';
159 std::cout << std::dec << std::setfill(' ');
160 }
161
162 /*
163
164 Sample output on an AMD Quadcore running MSVC 10
165
166 Processor: x86 or x64
167
168 This platform is little-endian.
169 BOOST_LITTTLE_ENDIAN is defined.
170
171 ---------- float --------------------
172
173 0 00 00 00 00
174 sn.min 00 00 00 01
175 -sn.min 80 00 00 01
176 n.min/256 00 00 80 00
177 n.min/2 00 40 00 00
178 -n.min/2 80 40 00 00
179 n.min 00 80 00 00
180 1 3f 80 00 00
181 3/4 3f 40 00 00
182 4/3 3f aa aa ab
183 max 7f 7f ff ff
184 inf 7f 80 00 00
185 q.nan 7f c0 00 00
186 s.nan 7f c0 00 01
187
188
189 ---------- double -------------------
190
191 0 00 00 00 00 00 00 00 00
192 sn.min 00 00 00 00 00 00 00 01
193 -sn.min 80 00 00 00 00 00 00 01
194 n.min/256 00 00 10 00 00 00 00 00
195 n.min/2 00 08 00 00 00 00 00 00
196 -n.min/2 80 08 00 00 00 00 00 00
197 n.min 00 10 00 00 00 00 00 00
198 1 3f f0 00 00 00 00 00 00
199 3/4 3f e8 00 00 00 00 00 00
200 4/3 3f f5 55 55 55 55 55 55
201 max 7f ef ff ff ff ff ff ff
202 inf 7f f0 00 00 00 00 00 00
203 q.nan 7f f8 00 00 00 00 00 00
204 s.nan 7f f8 00 00 00 00 00 01
205
206
207 ---------- long double --------------
208
209 0 00 00 00 00 00 00 00 00
210 sn.min 00 00 00 00 00 00 00 01
211 -sn.min 80 00 00 00 00 00 00 01
212 n.min/256 00 00 10 00 00 00 00 00
213 n.min/2 00 08 00 00 00 00 00 00
214 -n.min/2 80 08 00 00 00 00 00 00
215 n.min 00 10 00 00 00 00 00 00
216 1 3f f0 00 00 00 00 00 00
217 3/4 3f e8 00 00 00 00 00 00
218 4/3 3f f5 55 55 55 55 55 55
219 max 7f ef ff ff ff ff ff ff
220 inf 7f f0 00 00 00 00 00 00
221 q.nan 7f f8 00 00 00 00 00 00
222 s.nan 7f f8 00 00 00 00 00 01
223
224 */