Boost logo

Boost :

From: Johan Råde (rade_at_[hidden])
Date: 2006-12-08 10:02:57


Boris,

Could you run the attached version of the inspect tool on both platforms
in VAX FP mode.

--Johan


// 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 <iomanip>
#include <iostream>
#include <limits>
#include <boost/detail/endian.hpp>

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

template<class T> void print(const char* s, T x, bool ok = true);
template<class T> void print();

inline bool is_big_endian()
{
    float x = 1.0f;
    unsigned char first_byte = *reinterpret_cast<unsigned char*>(&x);
    return first_byte != 0;
}

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

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

    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

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

    return 0;
}

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

template<class T>
void print(const char* s, T x, bool ok)
{
    std::cout << std::left << std::setw(10) << s << std::right;

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

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

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

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

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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk