From 692ff5406e94d786b5710c352a4dd4c3ca1fd4b6 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 23 Jul 2007 21:29:27 +0000 Subject: [PATCH] added: a method Big::FromDouble(double) for 64bit platforms git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@48 e52654a7-88a9-db11-a3e9-0013d4bc506e --- ttmath/ttmathbig.h | 94 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index da4efec..0dde52f 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -1785,6 +1785,9 @@ public: } } + +private: + void FromDouble_SetExpAndMan(int e, unsigned int mhighest, unsigned int m1, unsigned int m2) { @@ -1804,11 +1807,98 @@ public: } } + +#else + +public: + + // 64bit platforms + void FromDouble(double value) + { + // sizeof(double) should be 8 (64 bits), this is actually not a runtime + // error but I leave it at the moment as is + TTMATH_ASSERT( sizeof(double) == 8 ) + + // I am not sure what will be on a plaltform which has + // a different endianness... but we use this library only + // on x86 and amd (intel) 64 bits (as there's a lot of assembler code) + union + { + double d; + uint u; // one 64bit word + } temp; + + temp.d = value; + + info = 0; + if( temp.u & 0x8000000000000000ul ) + SetSign(); + + int e = (temp.u & 0x7FF0000000000000ul) >> 52; + uint m = (temp.u & 0xFFFFFFFFFFFFFul) << 11; + + if( e == 2047 ) + { + // If E=2047 and F is nonzero, then V=NaN ("Not a number") + // If E=2047 and F is zero and S is 1, then V=-Infinity + // If E=2047 and F is zero and S is 0, then V=Infinity + + // at the moment we do not support NaN, -Infinity and +Infinity + + SetZero(); + } + else + if( e > 0 ) + { + // If 0