Skip to content

Commit c49873e

Browse files
author
coder0xff
committed
bug fixes and stuff
1 parent e9d4892 commit c49873e

File tree

6 files changed

+108
-33
lines changed

6 files changed

+108
-33
lines changed

QPFloat/Helpers.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,23 +567,23 @@ int FindHeadAndApplyRounding( ui32* buffer, int headScanBackStart )
567567
#include "ManagedQuadruple.h"
568568
#include <stdio.h>
569569

570-
__float128 factorials[MAX_FACTORIAL];
571-
__float128 factorialReciprocals[MAX_FACTORIAL];
570+
__float128 factorials[MAX_FACTORIAL + 1];
571+
__float128 factorialReciprocals[MAX_FACTORIAL + 1];
572572

573573

574574

575575
int Initialize()
576576
{
577-
__float128 one = 1;
578-
/***** CANT USE QUADONE BECAUSE IT ISNT INITIALIZED YET *****/
579-
__float128 current = one;
577+
/***** Just in case QuadOne isn't initialized yet *****/
578+
QuadOne = 1;
579+
__float128 current = QuadOne;
580580
factorialReciprocals[0] = factorials[0] = current;
581581
for (int i = 1; i <= MAX_FACTORIAL; i++)
582582
{
583583
__float128 temp = (__float128)i;
584584
__float128::Mul(current, temp, current);
585585
factorials[i] = current;
586-
__float128::Div(one, current, factorialReciprocals[i]);
586+
__float128::Div(QuadOne, current, factorialReciprocals[i]);
587587
}
588588

589589
return 0;
@@ -595,8 +595,30 @@ int dontCare = Initialize();
595595
#ifdef TEST
596596
int main(void)
597597
{
598-
Quadruple x = 0.303e-2;
599-
String^ test = x.ToString();
598+
__float128 x = 1;
599+
DateTime start = DateTime::Now;
600+
// __float128 y = 10;
601+
__float128 b;
602+
// //math tests
603+
// __float128::Div(x, y, b);
604+
for (int i = 0; i < 100000; i++)
605+
b = __float128::Sin(x);
606+
Quadruple check = (Quadruple)b;
607+
DateTime stop = DateTime::Now;
608+
double elapsed = (stop - start).TotalSeconds;
609+
System::Console::WriteLine(elapsed);
610+
// //conversion test
611+
// double c;
612+
// __float128::ToDouble(b, c);
613+
// //string tests
614+
// Quadruple fromStringTest = Quadruple::FromString("1.567000000000001e+50");
615+
// String^ toStringTest = fromStringTest.ToString();
616+
//
617+
// __float128 test = __float128::Exp((__float128)1000);
618+
// double d;
619+
// __float128::ToDouble(test, d);
620+
621+
600622
}
601623

602624
#endif

QPFloat/Helpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ cd(sinQuarterPi);
6363
#define MAX_FACTORIAL 1754
6464
struct __float128;
6565

66-
extern __float128 factorials[MAX_FACTORIAL];
67-
extern __float128 factorialReciprocals[MAX_FACTORIAL];
66+
extern __float128 factorials[MAX_FACTORIAL + 1];
67+
extern __float128 factorialReciprocals[MAX_FACTORIAL + 1];
6868

6969
#define QUAD_SIGNIFICANT_BITS 112
7070
#define QUAD_EXPONENT_BITS 15

QPFloat/ManagedQuadruple.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace System
5757
{
5858
//no bits
5959
result = 0;
60-
result.Sign = sign;
60+
result.IsSigned = sign;
6161
return;
6262
}
6363
int currentExponent = biasedExponentAtScanStart + implicitPosition - headScanBackStart;
@@ -74,7 +74,7 @@ namespace System
7474
int expInc = currentExponent - biasedExponentAtScanStart;
7575
result.biasedExponent = (ui16)currentExponent;
7676
BitBlockTransfer(buffer, headScanBackStart + expInc - 112, rPtr, 0, 112);
77-
result.Sign = sign;
77+
result.IsSigned = sign;
7878
if (currentExponent == 0) Underflow();
7979
if (EnableInexactException) if (ReverseBitScan((ui32*)buffer, 0, headScanBackStart + expInc - 112 - 1) != -1) Inexact();
8080
}
@@ -112,28 +112,28 @@ namespace System
112112
__float128::Div(*(__float128*)aPtr, *(__float128*)bPtr, *(__float128*)rPtr);
113113
}
114114

115-
bool Quadruple::operator==( Quadruple %a, Quadruple %b )
115+
bool Quadruple::operator==( Quadruple a, Quadruple b )
116116
{
117117
pin_ptr<byte> aPtr = a.storage;
118118
pin_ptr<byte> bPtr = b.storage;
119119
return (*(__float128*)aPtr) == (*(__float128*)bPtr);
120120
}
121121

122-
bool Quadruple::operator!=( Quadruple %a, Quadruple %b )
122+
bool Quadruple::operator!=( Quadruple a, Quadruple b )
123123
{
124124
pin_ptr<byte> aPtr = a.storage;
125125
pin_ptr<byte> bPtr = b.storage;
126126
return (*(__float128*)aPtr) != (*(__float128*)bPtr);
127127
}
128128

129-
bool Quadruple::operator>( Quadruple %a, Quadruple %b )
129+
bool Quadruple::operator>( Quadruple a, Quadruple b )
130130
{
131131
pin_ptr<byte> aPtr = a.storage;
132132
pin_ptr<byte> bPtr = b.storage;
133133
return (*(__float128*)aPtr) > (*(__float128*)bPtr);
134134
}
135135

136-
bool Quadruple::operator<( Quadruple %a, Quadruple %b )
136+
bool Quadruple::operator<( Quadruple a, Quadruple b )
137137
{
138138
pin_ptr<byte> aPtr = a.storage;
139139
pin_ptr<byte> bPtr = b.storage;
@@ -206,7 +206,7 @@ namespace System
206206
void Quadruple::Abs( Quadruple %v, Quadruple %result )
207207
{
208208
result = v;
209-
result.Sign = false;
209+
result.IsSigned = false;
210210
}
211211

212212
Quadruple Quadruple::Ln( Quadruple v )
@@ -342,11 +342,11 @@ namespace System
342342
if (IsNaN) return "NaN";
343343
if (*this == PositiveInfinity) return "Infinity";
344344
if (*this == NegativeInfinity) return "-Infinity";
345-
if (IsZero) return Sign ? "-0" : "0";
345+
if (IsZero) return IsSigned ? "-0" : "0";
346346
System::Text::StringBuilder^ result = gcnew System::Text::StringBuilder();
347347
//if we are just appending zeros after the decimal, then put them in a temporary buffer
348348
System::Text::StringBuilder^ nonZeroWaitCache = gcnew System::Text::StringBuilder();
349-
bool sign = this->Sign;
349+
bool sign = this->IsSigned;
350350
if (sign) result->Append("-");
351351

352352
Quadruple currentValue = *this; //(Quadruple)System::Runtime::InteropServices::Marshal::PtrToStructure((IntPtr)GAHH, Quadruple::typeid);
@@ -403,14 +403,45 @@ namespace System
403403
return result->ToString();
404404
}
405405

406+
String^ Quadruple::ToString( String^ format, IFormatProvider^ provider )
407+
{
408+
//todo: Implement this correctly
409+
//see http://msdn.microsoft.com/en-us/library/6dx8etks%28v=vs.85%29.aspx
410+
//see http://msdn.microsoft.com/en-us/library/427bttx3%28v=VS.85%29.aspx
411+
//see http://msdn.microsoft.com/en-us/library/system.iformatprovider.getformat.aspx
412+
//see http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
413+
//
414+
return ToString();
415+
}
416+
417+
String^ Quadruple::ToString( String^ format)
418+
{
419+
//todo: Implement this correctly
420+
//see http://msdn.microsoft.com/en-us/library/6dx8etks%28v=vs.85%29.aspx
421+
//see http://msdn.microsoft.com/en-us/library/427bttx3%28v=VS.85%29.aspx
422+
//see http://msdn.microsoft.com/en-us/library/system.iformatprovider.getformat.aspx
423+
//see http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
424+
//
425+
return ToString();
426+
}
427+
String^ Quadruple::ToString(IFormatProvider^ provider )
428+
{
429+
//todo: Implement this correctly
430+
//see http://msdn.microsoft.com/en-us/library/6dx8etks%28v=vs.85%29.aspx
431+
//see http://msdn.microsoft.com/en-us/library/427bttx3%28v=VS.85%29.aspx
432+
//see http://msdn.microsoft.com/en-us/library/system.iformatprovider.getformat.aspx
433+
//see http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx
434+
//
435+
return ToString();
436+
}
406437
System::Quadruple Quadruple::FromString( String^ str )
407438
{
408439
str = str->Trim();
409440
System::Text::StringBuilder^ s = gcnew System::Text::StringBuilder(str);
410441
Quadruple result = 0;
411442
if (s->default[0] == '-')
412443
{
413-
result.Sign = true;
444+
result.IsSigned = true;
414445
s->Remove(0, 1);
415446
}
416447
Quadruple ten = 10;

QPFloat/ManagedQuadruple.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ using namespace System::Runtime::InteropServices;
5353

5454
namespace System
5555
{
56+
[Serializable]
5657
public ref class UnderflowException : ArithmeticException {};
58+
[Serializable]
5759
public ref class InexactException : ArithmeticException {};
60+
[Serializable]
5861
public ref class InvalidException : ArithmeticException {};
5962

6063
[System::Diagnostics::DebuggerDisplayAttribute("{ToString()}")]
@@ -86,7 +89,7 @@ namespace System
8689
array<byte>^ get();
8790
void set( array<byte>^ value );
8891
}
89-
//#define useDebugView
92+
#define useDebugView
9093
#ifdef useDebugView
9194
/* add a forward slash at the beginning of this line to toggle the DebugView type
9295
property String^ DebugView
@@ -120,7 +123,7 @@ namespace System
120123
FPU_EXCEPTION_DECLARATION(Invalid);
121124
FPU_EXCEPTION_DECLARATION(Inexact);
122125
public:
123-
property bool Sign
126+
property bool IsSigned
124127
{
125128
inline bool get()
126129
{
@@ -147,7 +150,7 @@ namespace System
147150
{
148151
if (value >= QUAD_EXPONENT_MAX)
149152
{
150-
if (Sign)
153+
if (IsSigned)
151154
*this = NegativeInfinity;
152155
else
153156
*this = PositiveInfinity;
@@ -226,6 +229,10 @@ namespace System
226229
Negate(a);
227230
return a;
228231
}
232+
static inline Quadruple operator+(Quadruple a)
233+
{
234+
return a;
235+
}
229236
static void Add( Quadruple %a, Quadruple %b, [Out] Quadruple %result );
230237
static void Sub( Quadruple %a, Quadruple %b, [Out] Quadruple %result );
231238
static void Mul( Quadruple %a, Quadruple %b, [Out] Quadruple %result );
@@ -264,29 +271,29 @@ namespace System
264271
Div(a, b, result);
265272
return result;
266273
}
267-
static bool operator ==(Quadruple %a, Quadruple %b);
268-
static bool operator !=(Quadruple %a, Quadruple %b);
269-
static bool operator >(Quadruple %a, Quadruple %b);
270-
static bool operator <(Quadruple %a, Quadruple %b);
271-
static bool inline operator >=(Quadruple %a, Quadruple %b)
274+
static bool operator ==(Quadruple a, Quadruple b);
275+
static bool operator !=(Quadruple a, Quadruple b);
276+
static bool operator >(Quadruple a, Quadruple b);
277+
static bool operator <(Quadruple a, Quadruple b);
278+
static bool inline operator >=(Quadruple a, Quadruple b)
272279
{
273280
if (a == b) return true; //ensure that equals test happens first because it's much faster
274281
else return a > b;
275282
}
276-
static bool inline operator <=(Quadruple %a, Quadruple %b)
283+
static bool inline operator <=(Quadruple a, Quadruple b)
277284
{
278285
if (a == b) return true; //ensure that equals test happens first because it's much faster
279286
else return a < b;
280287
}
281288

282289
#pragma warning(disable: 4460)
283-
static Quadruple inline operator++(Quadruple %a)
290+
static Quadruple inline operator++(Quadruple a)
284291
{
285292
Quadruple temp = One;
286293
Add(a, temp, temp);
287294
return temp;
288295
}
289-
static Quadruple inline operator--(Quadruple %a)
296+
static Quadruple inline operator--(Quadruple a)
290297
{
291298
Quadruple temp = One;
292299
Sub(a, temp, temp);
@@ -326,6 +333,10 @@ namespace System
326333
static Quadruple Base2Exp(Quadruple v);
327334
static Quadruple Exp(Quadruple v);
328335
static Quadruple Pow(Quadruple base, Quadruple exponent);
336+
static inline Quadruple Sqrt(Quadruple value)
337+
{
338+
return Pow(value, Quadruple::Half);
339+
}
329340
static Quadruple inline operator^(Quadruple a, Quadruple b)
330341
{
331342
return Pow(a, b);
@@ -376,8 +387,16 @@ namespace System
376387
static Quadruple ATan2(Quadruple y, Quadruple x);
377388

378389
virtual String^ ToString() override;
390+
String^ ToString(String^ format);
391+
String^ ToString(IFormatProvider^ provider);
392+
String^ ToString(String^ format, IFormatProvider^ provider);
379393
static Quadruple FromString(String^ s);
380394

395+
static inline int Sign(Quadruple v)
396+
{
397+
if (v.IsZero) return 0;
398+
return v.IsSigned ? -1 : 1;
399+
}
381400
#include "constants.h"
382401

383402
};

QPFloat/QPFloat.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
</PropertyGroup>
107107
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
108108
<LinkIncremental>false</LinkIncremental>
109+
<RunCodeAnalysis>true</RunCodeAnalysis>
110+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
109111
</PropertyGroup>
110112
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
111113
<LinkIncremental>false</LinkIncremental>
@@ -178,6 +180,7 @@
178180
<WarningLevel>Level4</WarningLevel>
179181
<PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
180182
<PrecompiledHeader>Use</PrecompiledHeader>
183+
<EnablePREfast>true</EnablePREfast>
181184
</ClCompile>
182185
<Link>
183186
<GenerateDebugInformation>true</GenerateDebugInformation>

QPFloat/__float128.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ __float128 __float128::PartialExp( __float128 &v )
607607
__float128 factorial = 1;
608608
int iteration = 1;
609609
__float128 x = v;
610-
__float128 temp1;
611610
bool affecting = true;
612611
while (affecting)
613612
{
@@ -822,10 +821,11 @@ void __float128::SinCos( __float128 &v, __float128 &resultSin, __float128 &resul
822821
int iIteration = 1;
823822
resultSin = QuadZero;
824823
resultCos = QuadZero;
825-
__float128 one = QuadOne;
824+
826825
bool affecting = true;
827826
while (affecting)
828827
{
828+
//double dIncrement = dCurrentX / dFactorial;
829829
__float128 factorialReciprocal = FactorialReciprocal(iIteration);
830830
iIteration += 2;
831831
__float128 incrementSin;

0 commit comments

Comments
 (0)