aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/mp
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/mp')
-rw-r--r--src/math/mp/info.txt2
-rw-r--r--src/math/mp/mp_generic/mp_asm.h57
-rw-r--r--src/math/mp/mp_types.h13
-rw-r--r--src/math/mp/mp_word64/info.txt18
-rw-r--r--src/math/mp/mp_word64/mp_asm.h57
5 files changed, 52 insertions, 95 deletions
diff --git a/src/math/mp/info.txt b/src/math/mp/info.txt
index 531eee4e4..8dcaa7481 100644
--- a/src/math/mp/info.txt
+++ b/src/math/mp/info.txt
@@ -19,5 +19,5 @@ mp_core.h
</header:internal>
<requires>
-mp_x86_64|mp_word64|mp_x86_32|mp_x86_32_msvc|mp_generic
+mp_x86_64|mp_x86_32|mp_x86_32_msvc|mp_generic
</requires>
diff --git a/src/math/mp/mp_generic/mp_asm.h b/src/math/mp/mp_generic/mp_asm.h
index 08f40aa67..ff00cc24b 100644
--- a/src/math/mp/mp_generic/mp_asm.h
+++ b/src/math/mp/mp_generic/mp_asm.h
@@ -1,6 +1,6 @@
/*
* Lowest Level MPI Algorithms
-* (C) 1999-2008 Jack Lloyd
+* (C) 1999-2008,2013 Jack Lloyd
* 2006 Luca Piccarreta
*
* Distributed under the terms of the Botan license
@@ -13,18 +13,6 @@
namespace Botan {
-#if (BOTAN_MP_WORD_BITS == 8)
- typedef u16bit dword;
-#elif (BOTAN_MP_WORD_BITS == 16)
- typedef u32bit dword;
-#elif (BOTAN_MP_WORD_BITS == 32)
- typedef u64bit dword;
-#elif (BOTAN_MP_WORD_BITS == 64)
- #error BOTAN_MP_WORD_BITS can be 64 only with assembly support
-#else
- #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64
-#endif
-
extern "C" {
/*
@@ -32,9 +20,23 @@ extern "C" {
*/
inline word word_madd2(word a, word b, word* c)
{
- dword z = (dword)a * b + *c;
- *c = (word)(z >> BOTAN_MP_WORD_BITS);
- return (word)z;
+#if defined(BOTAN_HAS_MP_DWORD)
+ const dword s = static_cast<dword>(a) * b + *c;
+ *c = static_cast<word>(s >> BOTAN_MP_WORD_BITS);
+ return static_cast<word>(s);
+#else
+ static_assert(BOTAN_MP_WORD_BITS == 64, "Unexpected word size");
+
+ word hi = 0, lo = 0;
+
+ mul64x64_128(a, b, &lo, &hi);
+
+ lo += *c;
+ hi += (lo < *c); // carry?
+
+ *c = hi;
+ return lo;
+#endif
}
/*
@@ -42,9 +44,26 @@ inline word word_madd2(word a, word b, word* c)
*/
inline word word_madd3(word a, word b, word c, word* d)
{
- dword z = (dword)a * b + c + *d;
- *d = (word)(z >> BOTAN_MP_WORD_BITS);
- return (word)z;
+#if defined(BOTAN_HAS_MP_DWORD)
+ const dword s = static_cast<dword>(a) * b + c + *d;
+ *d = static_cast<word>(s >> BOTAN_MP_WORD_BITS);
+ return static_cast<word>(s);
+#else
+ static_assert(BOTAN_MP_WORD_BITS == 64, "Unexpected word size");
+
+ word hi = 0, lo = 0;
+
+ mul64x64_128(a, b, &lo, &hi);
+
+ lo += c;
+ hi += (lo < c); // carry?
+
+ lo += *d;
+ hi += (lo < *d); // carry?
+
+ *d = hi;
+ return lo;
+#endif
}
}
diff --git a/src/math/mp/mp_types.h b/src/math/mp/mp_types.h
index 1648713ed..60282fb83 100644
--- a/src/math/mp/mp_types.h
+++ b/src/math/mp/mp_types.h
@@ -9,17 +9,30 @@
#define BOTAN_MPI_TYPES_H__
#include <botan/types.h>
+#include <botan/mul128.h>
namespace Botan {
#if (BOTAN_MP_WORD_BITS == 8)
typedef byte word;
+ typedef u16bit dword;
+ #define BOTAN_HAS_MP_DWORD
#elif (BOTAN_MP_WORD_BITS == 16)
typedef u16bit word;
+ typedef u32bit dword;
+ #define BOTAN_HAS_MP_DWORD
#elif (BOTAN_MP_WORD_BITS == 32)
typedef u32bit word;
+ typedef u64bit dword;
+ #define BOTAN_HAS_MP_DWORD
#elif (BOTAN_MP_WORD_BITS == 64)
typedef u64bit word;
+
+ #if defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
+ typedef uint128_t dword;
+ #define BOTAN_HAS_MP_DWORD
+ #endif
+
#else
#error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64
#endif
diff --git a/src/math/mp/mp_word64/info.txt b/src/math/mp/mp_word64/info.txt
deleted file mode 100644
index a12221f4e..000000000
--- a/src/math/mp/mp_word64/info.txt
+++ /dev/null
@@ -1,18 +0,0 @@
-mp_bits 64
-
-load_on dep
-
-<header:internal>
-mp_asm.h
-mp_generic:mp_asmi.h
-</header:internal>
-
-<arch>
-alpha
-ia64
-mips64
-ppc64
-s390x
-sparc64
-x86_64
-</arch>
diff --git a/src/math/mp/mp_word64/mp_asm.h b/src/math/mp/mp_word64/mp_asm.h
deleted file mode 100644
index 76d2bb918..000000000
--- a/src/math/mp/mp_word64/mp_asm.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
-* MPI Multiply-Add Core
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_MP_MADD_H__
-#define BOTAN_MP_MADD_H__
-
-#include <botan/mp_types.h>
-#include <botan/internal/mul128.h>
-
-namespace Botan {
-
-#if (BOTAN_MP_WORD_BITS != 64)
- #error The mp_word64 module requires that BOTAN_MP_WORD_BITS == 64
-#endif
-
-/*
-* Word Multiply/Add
-*/
-inline word word_madd2(word a, word b, word* c)
- {
- word z0 = 0, z1 = 0;
-
- mul64x64_128(a, b, &z1, &z0);
-
- z1 += *c;
- z0 += (z1 < *c);
-
- *c = z0;
- return z1;
- }
-
-/*
-* Word Multiply/Add
-*/
-inline word word_madd3(word a, word b, word c, word* d)
- {
- word z0 = 0, z1 = 0;
-
- mul64x64_128(a, b, &z1, &z0);
-
- z1 += c;
- z0 += (z1 < c);
-
- z1 += *d;
- z0 += (z1 < *d);
-
- *d = z0;
- return z1;
- }
-
-}
-
-#endif