Added addmul and addmul_ui

This commit is contained in:
Alex Dyachenko 2014-03-14 12:28:53 -04:00
parent d94c45cb85
commit 59a9d5ddd9
3 changed files with 76 additions and 4 deletions

View File

@ -231,7 +231,9 @@ namespace MPIR.Tests.HugeIntTests
using (var c = new HugeInt("23094582093845093574093845093485039450934"))
using (var b = new HugeInt("-394580293847502987609283945873594873409587"))
{
a.AddProduct(c, b);
var expr = a + c*b;
Assert.IsInstanceOfType(expr, typeof(MpirAddProductIntIntExpression));
a.Value = expr;
Assert.AreEqual("-9112666988874677841199955832262586145147830205230375090322356322089362221491205901", a.ToString());
}
}
@ -243,11 +245,56 @@ namespace MPIR.Tests.HugeIntTests
using (var c = new HugeInt("-23094582093845093574093845093485039450934"))
{
ulong b = 498734523097853458;
a.AddProduct(c, b);
var expr = a + c*b;
Assert.IsInstanceOfType(expr, typeof(MpirAddProductIntUiExpression));
a.Value = expr;
Assert.AreEqual("-11518065386718058599763388064972875060082210203928832731415", a.ToString());
}
}
[TestMethod]
public void AddProductLimbTo()
{
using (var a = new HugeInt("98750293847520938457029384572093480498357"))
using (var c = new HugeInt("-23094582093845093574093845093485039450934"))
{
ulong b = 498734523097853458;
var expr = a + b*c;
Assert.IsInstanceOfType(expr, typeof(MpirAddProductIntUiExpression));
a.Value = expr;
Assert.AreEqual("-11518065386718058599763388064972875060082210203928832731415", a.ToString());
}
}
[TestMethod]
public void AddProductLimbTo2()
{
using (var a = new HugeInt("98750293847520938457029384572093480498357"))
using (var c = new HugeInt("-23094582093845093574093845093485039450934"))
{
ulong b = 498734523097853458;
var expr = b*c + a;
Assert.IsInstanceOfType(expr, typeof(MpirAddProductIntUiExpression));
a.Value = expr;
//TODO how can we test a single addmul was called?
Assert.AreEqual("-11518065386718058599763388064972875060082210203928832731415", a.ToString());
}
}
[TestMethod]
public void AddProductLimbTo3()
{
using (var a = new HugeInt("98750293847520938457029384572093480498357"))
using (var c = new HugeInt("-23094582093845093574093845093485039450934"))
using (var d = new HugeInt())
{
ulong b = 498734523097853458;
var expr = b*c + a;
d.Value = expr;
Assert.AreEqual("-11518065386718058599763388064972875060082210203928832731415", d.ToString());
}
}
[TestMethod]
public void SubtractProductHugeInt()
{

View File

@ -143,6 +143,16 @@ void Mpir##name##Expression::AssignTo(HugeInt^ destination) \
} \
DEFINE_ASSIGNMENT_EPILOG
#define DEFINE_ADDMUL_ASSIGNMENT(name, operation, left, right) \
DEFINE_ASSIGNMENT_PROLOG(name) \
if(destination->_value != Left->_value) \
mpz_set(src_destination, Left->_value); \
operation(src_destination, Right->left, Right->right); \
DEFINE_ASSIGNMENT_EPILOG
#define DEFINE_ADDMUL_ASSIGNMENT_REF_REF(name, operation) DEFINE_ADDMUL_ASSIGNMENT(name, operation, Left->_value, Right->_value)
#define DEFINE_ADDMUL_ASSIGNMENT_REF_VAL(name, operation) DEFINE_ADDMUL_ASSIGNMENT(name, operation, Left->_value, Right)
using namespace System::Runtime::InteropServices;
namespace MPIR
@ -261,6 +271,11 @@ namespace MPIR
MpirAddIntSiExpression^ HugeInt::operator+(HugeInt^ a, mpir_si b) { return gcnew MpirAddIntSiExpression (a, b); }
MpirAddIntSiExpression^ HugeInt::operator+(mpir_si a, HugeInt^ b) { return gcnew MpirAddIntSiExpression (b, a); }
MpirAddProductIntIntExpression^ HugeInt::operator+(HugeInt^ a, MpirMultiplyIntIntExpression^ b) { return gcnew MpirAddProductIntIntExpression(a, b); }
MpirAddProductIntIntExpression^ HugeInt::operator+(MpirMultiplyIntIntExpression^ a, HugeInt^ b) { return gcnew MpirAddProductIntIntExpression(b, a); }
MpirAddProductIntUiExpression^ HugeInt::operator+(HugeInt^ a, MpirMultiplyIntUiExpression^ b) { return gcnew MpirAddProductIntUiExpression(a, b); }
MpirAddProductIntUiExpression^ HugeInt::operator+(MpirMultiplyIntUiExpression^ a, HugeInt^ b) { return gcnew MpirAddProductIntUiExpression(b, a); }
MpirSubtractIntIntExpression^ HugeInt::operator-(HugeInt^ a, HugeInt^ b) { return gcnew MpirSubtractIntIntExpression(a, b); }
MpirSubtractIntUiExpression^ HugeInt::operator-(HugeInt^ a, mpir_ui b) { return gcnew MpirSubtractIntUiExpression (a, b); }
MpirSubtractUiIntExpression^ HugeInt::operator-(mpir_ui a, HugeInt^ b) { return gcnew MpirSubtractUiIntExpression (a, b); }
@ -287,11 +302,14 @@ namespace MPIR
DEFINE_BINARY_ASSIGNMENT_REF_VAL(MultiplyIntUi, mpz_mul_ui)
DEFINE_BINARY_ASSIGNMENT_REF_VAL(MultiplyIntSi, mpz_mul_si)
DEFINE_ADDMUL_ASSIGNMENT_REF_REF(AddProductIntInt, mpz_addmul)
DEFINE_ADDMUL_ASSIGNMENT_REF_VAL(AddProductIntUi, mpz_addmul_ui)
//DEFINE_VOID_FROM_MPZ_OR_UI(Add, add)
//DEFINE_VOID_FROM_MPZ_OR_UI(Subtract, sub)
//DEFINE_VOID_UI_FROM(SubtractFrom, sub)
//DEFINE_VOID_FROM_MPZ_OR_UI_OR_SI(MultiplyBy, mul)
DEFINE_VOID_FROM_MPZ_MPZ_OR_UI(AddProduct, addmul)
//DEFINE_VOID_FROM_MPZ_MPZ_OR_UI(AddProduct, addmul)
DEFINE_VOID_FROM_MPZ_MPZ_OR_UI(SubtractProduct, submul)
DEFINE_VOID_FROM_2EXP(ShiftLeft, mul)
DEFINE_VOID_FROM_NONE(Negate, neg)

View File

@ -107,6 +107,9 @@ namespace MPIR
DEFINE_BINARY_EXPRESSION(MultiplyIntUi, HugeInt^, mpir_ui)
DEFINE_BINARY_EXPRESSION(MultiplyIntSi, HugeInt^, mpir_si)
DEFINE_BINARY_EXPRESSION(AddProductIntInt, HugeInt^, MpirMultiplyIntIntExpression^)
DEFINE_BINARY_EXPRESSION(AddProductIntUi, HugeInt^, MpirMultiplyIntUiExpression^)
public ref class HugeInt sealed : IMpirExpression
{
internal:
@ -158,6 +161,10 @@ namespace MPIR
static MpirAddIntUiExpression^ operator+(mpir_ui a, HugeInt^ b);
static MpirAddIntSiExpression^ operator+(HugeInt^ a, mpir_si b);
static MpirAddIntSiExpression^ operator+(mpir_si a, HugeInt^ b);
static MpirAddProductIntIntExpression^ operator+(HugeInt^ a, MpirMultiplyIntIntExpression^ b);
static MpirAddProductIntIntExpression^ operator+(MpirMultiplyIntIntExpression^ a, HugeInt^ b);
static MpirAddProductIntUiExpression^ operator+(HugeInt^ a, MpirMultiplyIntUiExpression^ b);
static MpirAddProductIntUiExpression^ operator+(MpirMultiplyIntUiExpression^ a, HugeInt^ b);
static MpirSubtractIntIntExpression^ operator-(HugeInt^ a, HugeInt^ b);
static MpirSubtractIntUiExpression^ operator-(HugeInt^ a, mpir_ui b);
@ -175,7 +182,7 @@ namespace MPIR
//DECLARE_VOID_FROM_MPZ_OR_UI(Subtract)
//DECLARE_VOID_FROM_UI(SubtractFrom)
//DECLARE_VOID_FROM_MPZ_OR_UI_OR_SI(MultiplyBy)
DECLARE_VOID_FROM_MPZ_MPZ_OR_UI(AddProduct)
//DECLARE_VOID_FROM_MPZ_MPZ_OR_UI(AddProduct)
DECLARE_VOID_FROM_MPZ_MPZ_OR_UI(SubtractProduct)
DECLARE_VOID_FROM_2EXP(ShiftLeft)
DECLARE_VOID_FROM_NONE(Negate)