| 1 |
//====================================================================== |
| 2 |
//----------------------------------------------------------------------- |
| 3 |
/** |
| 4 |
* @file FndIEEE80.h |
| 5 |
* @brief IEEE754 80bit ファイル |
| 6 |
* |
| 7 |
* @author t.sirayanagi |
| 8 |
* @version 1.0 |
| 9 |
* |
| 10 |
* @par copyright |
| 11 |
* Copyright (C) 2011-2012 Takazumi Shirayanagi\n |
| 12 |
* The new BSD License is applied to this software. |
| 13 |
* see iris_LICENSE.txt |
| 14 |
*/ |
| 15 |
//----------------------------------------------------------------------- |
| 16 |
//====================================================================== |
| 17 |
#ifndef INCG_IRIS_FndIEEE80_H_ |
| 18 |
#define INCG_IRIS_FndIEEE80_H_ |
| 19 |
|
| 20 |
//====================================================================== |
| 21 |
// include |
| 22 |
#include "FndIEEE.h" |
| 23 |
|
| 24 |
namespace iris { |
| 25 |
namespace fnd |
| 26 |
{ |
| 27 |
|
| 28 |
//====================================================================== |
| 29 |
// class |
| 30 |
/** |
| 31 |
* @brief IEEE754 80bit クラス |
| 32 |
* @note 符号bit : 1 |
| 33 |
* 仮数bit : 64 |
| 34 |
* 指数bit : 15 |
| 35 |
*/ |
| 36 |
class CIEEEBinary80 : public IIrisObject |
| 37 |
{ |
| 38 |
protected: |
| 39 |
u8 m_buf[10]; //!< バッファ(BIGENDIAN で格納) |
| 40 |
public: |
| 41 |
// コンストラクタ |
| 42 |
CIEEEBinary80(void) {} |
| 43 |
public: |
| 44 |
// メモリコピー |
| 45 |
void CopyBufferBE(const u8* buf) |
| 46 |
{ |
| 47 |
for( int i=0; i < elementof(m_buf); ++i, ++buf ) m_buf[i] = *buf; |
| 48 |
} |
| 49 |
void CopyBufferLE(const u8* buf) |
| 50 |
{ |
| 51 |
for( int i=0; i < elementof(m_buf); ++i, ++buf ) m_buf[9-i] = *buf; |
| 52 |
} |
| 53 |
|
| 54 |
public: |
| 55 |
operator float (void) const { return cast_to_float(); } |
| 56 |
operator double (void) const { return cast_to_double(); } |
| 57 |
|
| 58 |
public: |
| 59 |
u8* GetBuffer(void) { return m_buf; } |
| 60 |
const u8* GetBuffer(void) const { return m_buf; } |
| 61 |
|
| 62 |
public: |
| 63 |
// 符号部 |
| 64 |
u32 sign(void) const { return (m_buf[0]>>7) & 1; } |
| 65 |
// 指数部 |
| 66 |
u32 exp(void) const { return (static_cast<u32>(m_buf[0]&0x7F) << 8) | (static_cast<u32>(m_buf[1]) << 0); } |
| 67 |
// 仮数部 |
| 68 |
u64 frac(void) const |
| 69 |
{ |
| 70 |
return (static_cast<u64>(m_buf[2]) << 56) | (static_cast<u64>(m_buf[3]) << 48) | (static_cast<u64>(m_buf[4]) << 40) |
| 71 |
| (static_cast<u64>(m_buf[5]) << 32) | (static_cast<u64>(m_buf[6]) << 24) | (static_cast<u64>(m_buf[7]) << 16) |
| 72 |
| (static_cast<u64>(m_buf[8]) << 8) | (static_cast<u64>(m_buf[9])); |
| 73 |
} |
| 74 |
private: |
| 75 |
float cast_to_float(void) const |
| 76 |
{ |
| 77 |
CIEEEBinary32 ieee; |
| 78 |
u32 exp_ = exp(); |
| 79 |
if( exp_ == (1<<15)-1 ) |
| 80 |
{ |
| 81 |
exp_ = (1<<8)-1; |
| 82 |
} |
| 83 |
else if( exp_ != 0 ) |
| 84 |
{ |
| 85 |
exp_ -= (1<<14)-1; |
| 86 |
exp_ += (1<<7)-1; |
| 87 |
if( exp_ & ~0xFF ) |
| 88 |
{ |
| 89 |
// INF |
| 90 |
ieee.value() = 0x7F800000; |
| 91 |
ieee.sign(sign()); |
| 92 |
return ieee; |
| 93 |
} |
| 94 |
} |
| 95 |
ieee.sign(sign()); |
| 96 |
ieee.exp(exp_); |
| 97 |
ieee.frac((frac()>>40)&0x7FFFFF); // 暗黙の1bitを捨てる |
| 98 |
return ieee; |
| 99 |
} |
| 100 |
double cast_to_double(void) const |
| 101 |
{ |
| 102 |
CIEEEBinary64 ieee; |
| 103 |
u32 exp_ = exp(); |
| 104 |
if( exp_ == (1<<15)-1 ) |
| 105 |
{ |
| 106 |
exp_ = (1<<11)-1; |
| 107 |
} |
| 108 |
else if( exp_ != 0 ) |
| 109 |
{ |
| 110 |
exp_ -= (1<<14)-1; |
| 111 |
exp_ += (1<<10)-1; |
| 112 |
if( exp_ & ~0x7FF ) |
| 113 |
{ |
| 114 |
// INF |
| 115 |
ieee.value() = 0x7FF0000000000000; |
| 116 |
ieee.sign(sign()); |
| 117 |
return ieee; |
| 118 |
} |
| 119 |
} |
| 120 |
ieee.sign(sign()); |
| 121 |
ieee.exp(exp_); |
| 122 |
ieee.frac((frac()>>11)&0xFFFFFFFFFFFFF); // 暗黙の1bitを捨てる |
| 123 |
return ieee; |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
} // end of namespace fnd |
| 128 |
} // end of namespace iris |
| 129 |
|
| 130 |
#endif |