summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/math/example/inspect_fp.cpp
blob: 71ea686f411d873b07d7a89fbc5a9df318ee1136 (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
// inspect.cpp

// Copyright (c) 2006 Johan Rade

// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

//-------------------------------------

#include <cstring>

#include <iomanip>
#include <iostream>
#include <limits>

#ifndef BOOST_MATH_STANDALONE
#include <boost/endian.hpp>
#else
#include <boost/math/tools/config.hpp>
#endif

//------------------------------------------------------------------------------

bool is_big_endian()
{
    float x = 1.0f;
    unsigned char first_byte;
  memcpy(&first_byte, &x, 1);
    return first_byte != 0;
}

//------------------------------------------------------------------------------

void print_processor();
void print_endianness();
template<class T> void print_table();
template<class T> void print_row(const char* name, T val, bool ok = true);

//------------------------------------------------------------------------------

int main()
{
    std::cout << '\n';

  print_processor();

  print_endianness();

    std::cout << "---------- float --------------------\n\n";
    print_table<float>();

    std::cout << "---------- double -------------------\n\n";
    print_table<double>();

    std::cout << "---------- long double --------------\n\n";
    print_table<long double>();

    return 0;
}

//------------------------------------------------------------------------------

void print_processor()
{
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) \
    || defined(__amd64) || defined(__amd64__)  || defined(_M_AMD64) \
    || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)

  std::cout << "Processor: x86 or x64\n\n";

#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)

  std::cout << "Processor: ia64\n\n";

#elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
    || defined(__ppc) || defined(__ppc__) || defined(__PPC__)

  std::cout << "Processor: PowerPC\n\n";

#elif defined(__m68k) || defined(__m68k__) \
    || defined(__mc68000) || defined(__mc68000__) \

  std::cout << "Processor: Motorola 68K\n\n";

#else

  std::cout << "Processor: Unknown\n\n";

#endif
}

void print_endianness()
{
    if(is_big_endian())
        std::cout << "This platform is big-endian.\n";
    else
        std::cout << "This platform is little-endian.\n";

#ifdef BOOST_BIG_ENDIAN
    std::cout << "BOOST_BIG_ENDIAN is defined.\n\n";
#endif
#if defined BOOST_LITTLE_ENDIAN
    std::cout << "BOOST_LITTTLE_ENDIAN is defined.\n\n";
#endif
}

//..............................................................................

template<class T> void print_table()
{
    print_row("0", (T)0);
    print_row("sn.min", std::numeric_limits<T>::denorm_min(),
          std::numeric_limits<T>::has_denorm);
    print_row("-sn.min", -std::numeric_limits<T>::denorm_min(),
          std::numeric_limits<T>::has_denorm);
    print_row("n.min/256", (std::numeric_limits<T>::min)()/256);
    print_row("n.min/2", (std::numeric_limits<T>::min)()/2);
    print_row("-n.min/2", -(std::numeric_limits<T>::min)()/2);
    print_row("n.min", (std::numeric_limits<T>::min)());
    print_row("1", (T)1);
    print_row("3/4", (T)3/(T)4);
    print_row("4/3", (T)4/(T)3);
    print_row("max", (std::numeric_limits<T>::max)());
    print_row("inf", std::numeric_limits<T>::infinity(),
          std::numeric_limits<T>::has_infinity);
    print_row("q.nan", std::numeric_limits<T>::quiet_NaN(),
          std::numeric_limits<T>::has_quiet_NaN);
    print_row("s.nan", std::numeric_limits<T>::signaling_NaN(),
          std::numeric_limits<T>::has_signaling_NaN);

    std::cout << "\n\n";
}

template<class T>
void print_row(const char* name, T val, bool ok)
{
    std::cout << std::left << std::setw(10) << name << std::right;

    std::cout << std::hex << std::setfill('0');

    if(ok) {
        if(is_big_endian()) {
      for(size_t i = 0; i < sizeof(T); ++i) {
        unsigned char c = *(reinterpret_cast<unsigned char*>(&val) + i);
                std::cout << std::setw(2)
                    << static_cast<unsigned int>(c) << ' ';
      }
        }
        else {
      for(size_t i = sizeof(T) - 1; i < sizeof(T); --i) {
        unsigned char c = *(reinterpret_cast<unsigned char*>(&val) + i);
                std::cout << std::setw(2)
                    << static_cast<unsigned int>(c) << ' ';
      }
        }
    }
    else {
        for(size_t i = 0; i < sizeof(T); ++i)
            std::cout << "-- ";
    }

    std::cout << '\n';
    std::cout << std::dec << std::setfill(' ');
}

/*

Sample output on an AMD Quadcore running MSVC 10

  Processor: x86 or x64

  This platform is little-endian.
  BOOST_LITTTLE_ENDIAN is defined.

  ---------- float --------------------

  0         00 00 00 00
  sn.min    00 00 00 01
  -sn.min   80 00 00 01
  n.min/256 00 00 80 00
  n.min/2   00 40 00 00
  -n.min/2  80 40 00 00
  n.min     00 80 00 00
  1         3f 80 00 00
  3/4       3f 40 00 00
  4/3       3f aa aa ab
  max       7f 7f ff ff
  inf       7f 80 00 00
  q.nan     7f c0 00 00
  s.nan     7f c0 00 01


  ---------- double -------------------

  0         00 00 00 00 00 00 00 00
  sn.min    00 00 00 00 00 00 00 01
  -sn.min   80 00 00 00 00 00 00 01
  n.min/256 00 00 10 00 00 00 00 00
  n.min/2   00 08 00 00 00 00 00 00
  -n.min/2  80 08 00 00 00 00 00 00
  n.min     00 10 00 00 00 00 00 00
  1         3f f0 00 00 00 00 00 00
  3/4       3f e8 00 00 00 00 00 00
  4/3       3f f5 55 55 55 55 55 55
  max       7f ef ff ff ff ff ff ff
  inf       7f f0 00 00 00 00 00 00
  q.nan     7f f8 00 00 00 00 00 00
  s.nan     7f f8 00 00 00 00 00 01


  ---------- long double --------------

  0         00 00 00 00 00 00 00 00
  sn.min    00 00 00 00 00 00 00 01
  -sn.min   80 00 00 00 00 00 00 01
  n.min/256 00 00 10 00 00 00 00 00
  n.min/2   00 08 00 00 00 00 00 00
  -n.min/2  80 08 00 00 00 00 00 00
  n.min     00 10 00 00 00 00 00 00
  1         3f f0 00 00 00 00 00 00
  3/4       3f e8 00 00 00 00 00 00
  4/3       3f f5 55 55 55 55 55 55
  max       7f ef ff ff ff ff ff ff
  inf       7f f0 00 00 00 00 00 00
  q.nan     7f f8 00 00 00 00 00 00
  s.nan     7f f8 00 00 00 00 00 01

  */