続 decode_ieee754_float
昨日の方針でとりあえず実装しました。
フォーマット毎にパラメータをtraitsにまとめました。
enum float_format { ieee754_single, ieee754_double }; template <float_format Format> struct float_traits; template <> struct float_traits<ieee754_single> { typedef boost::uint_fast32_t int_type; // エンコード用 typedef boost::int_fast32_t mant_type; // 仮数の計算用 static const int bits = 32; // 全体のサイズ static const int digits = 24; // 仮数の2進桁数(暗黙の最上位ビットを含む) static const int exponent_digits = 8; // 指数の2進桁数 static const int exponent_bias = 127; // 指数のバイアス }; template <> struct float_traits<ieee754_double> { typedef boost::uint_fast64_t int_type; typedef boost::int_fast64_t mant_type; static const int bits = 64; static const int digits = 53; static const int exponent_digits = 11; static const int exponent_bias = 1023; };
float_traitsのテンプレート引数がfloatやdoubleでないのは、そのフォーマットがIEE754に従っているとは限らないからです。
当然、これらのフォーマットを格納させる内部型もfloatやdoubleで決め打ちすることはできませんから、エンコード/デコード関数も、
template<typename T, float_format Format> T decode_ieee754(typename float_traits<Format>::int_type n); template<typename T, float_format Format> typename float_traits<Format>::int_type encode_ieee754(T x);
のようになります。
精度が許せばIEEE754 Doubleをfloatに読み込んでもいいですし、IEEE754 Singleを読み込むのにlong doubleが必要な場合もありえるでしょう。
あとはこれをwide_adaptorに組み込むだけです。
最終的にはIEEE754準拠な環境ではmemcpyを呼ぶようにするので、これらのコードはほとんど意味がないんですけどね。