diff options
author | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
commit | a2c99d3270eb73ef2db5704fc54356c6b75096f8 (patch) | |
tree | ad3d6c4fcc8dd0f403f8105598943616246fe172 /modules/mp_amd64 |
Initial checkin1.5.6
Diffstat (limited to 'modules/mp_amd64')
-rw-r--r-- | modules/mp_amd64/modinfo.txt | 15 | ||||
-rw-r--r-- | modules/mp_amd64/mp_asm.h | 84 | ||||
-rw-r--r-- | modules/mp_amd64/mp_asmi.h | 174 |
3 files changed, 273 insertions, 0 deletions
diff --git a/modules/mp_amd64/modinfo.txt b/modules/mp_amd64/modinfo.txt new file mode 100644 index 000000000..12537da0e --- /dev/null +++ b/modules/mp_amd64/modinfo.txt @@ -0,0 +1,15 @@ +realname "MPI Assembler Backend for x86-64/AMD64 Systems" + +mp_bits 64 + +replace_file mp_asm.h +replace_file mp_asmi.h + +<arch> +amd64 +</arch> + +<cc> +gcc +icc +</cc> diff --git a/modules/mp_amd64/mp_asm.h b/modules/mp_amd64/mp_asm.h new file mode 100644 index 000000000..17f33224c --- /dev/null +++ b/modules/mp_amd64/mp_asm.h @@ -0,0 +1,84 @@ +/************************************************* +* Lowest Level MPI Algorithms Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MP_ASM_H__ +#define BOTAN_MP_ASM_H__ + +#include <botan/mp_types.h> + +#if (BOTAN_MP_WORD_BITS != 64) + #error The mp_amd64 module requires that BOTAN_MP_WORD_BITS == 64 +#endif + +namespace Botan { + +extern "C" { + +/************************************************* +* Word Multiply * +*************************************************/ +inline word word_madd2(word a, word b, word c, word* carry) + { + asm( + "mulq %1\n\t" // a(in eax) * b(wherever) -> edx:eax + "addq %5,%0\n\t" // add c to low word (eax) + "adcq $0,%2" // add carry from previous to high word (edx) + : "=a"(a), "=rm"(b), "=&d"(*carry) + : "0"(a), "1"(b), "g"(c) : "cc"); + return a; + } + +/************************************************* +* Word Multiply/Add * +*************************************************/ +inline word word_madd3(word a, word b, word c, word d, word* carry) + { + asm( + "mulq %1\n\t" // a(in eax) * b(wherever) -> edx:eax + "addq %5,%0\n\t" // add c to low word (eax) + "adcq $0,%2\n\t" // add carry from previous to high word (edx) + "addq %6,%0\n\t" // add d to low word (eax) + "adcq $0,%2" // add carry from previous to high word (edx) + : "=a"(a), "=rm"(b), "=&d"(*carry) + : "0"(a), "1"(b), "g"(c), "g"(d) : "cc"); + return a; + } + +/************************************************* +* Multiply-Add Accumulator * +*************************************************/ +inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b) + { + asm("mulq %[b]\n\t" // a(in eax) * b(wherever) -> edx:eax + "addq %3,%[w0]\n\t" // add c to low word (eax) + "adcq %4,%[w1]\n\t" // add carry from previous to high word (edx) + "adcq $0,%[w2]\n\t" // add carry from previous to high word (edx) + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : "a"(a), [b]"d"(b), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + +/************************************************* +* Multiply-Add Accumulator * +*************************************************/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b) + { + asm("mulq %[b]\n\t" // a(in eax) * b(wherever) -> edx:eax + "addq %3,%[w0]\n\t" // add c to low word (eax) + "adcq %4,%[w1]\n\t" // add carry from previous to high word (edx) + "adcq $0,%[w2]\n\t" // add carry from previous to high word (edx) + "addq %3,%[w0]\n\t" // add c to low word (eax) + "adcq %4,%[w1]\n\t" // add carry from previous to high word (edx) + "adcq $0,%[w2]\n\t" // add carry from previous to high word (edx) + : [w0]"=r"(*w0), [w1]"=r"(*w1), [w2]"=r"(*w2) + : "a"(a), [b]"d"(b), "0"(*w0), "1"(*w1), "2"(*w2) + : "cc"); + } + +} + +} + +#endif diff --git a/modules/mp_amd64/mp_asmi.h b/modules/mp_amd64/mp_asmi.h new file mode 100644 index 000000000..daa1243ca --- /dev/null +++ b/modules/mp_amd64/mp_asmi.h @@ -0,0 +1,174 @@ +/************************************************* +* Lowest Level MPI Algorithms Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_types.h> + +namespace Botan { + +/************************************************* +* Helper Macros for amd64 Assembly * +*************************************************/ +#define ASM(x) x "\n\t" + +#define ADDSUB2_OP(OPERATION, INDEX) \ + ASM("movq 8*" INDEX "(%[y]), %[carry]") \ + ASM(OPERATION " %[carry], 8*" INDEX "(%[x])") \ + +#define ADDSUB3_OP(OPERATION, INDEX) \ + ASM("movq 8*" INDEX "(%[x]), %[carry]") \ + ASM(OPERATION " 8*" INDEX "(%[y]), %[carry]") \ + ASM("movq %[carry], 8*" INDEX "(%[z])") \ + +#define LINMUL_OP(WRITE_TO, INDEX) \ + ASM("movq 8*" INDEX "(%[x]),%%rax") \ + ASM("mulq %[y]") \ + ASM("addq %[carry],%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("movq %%rdx,%[carry]") \ + ASM("movq %%rax, 8*" INDEX "(%[" WRITE_TO "])") + +#define MULADD_OP(IGNORED, INDEX) \ + ASM("movq 8*" INDEX "(%[x]),%%rax") \ + ASM("mulq %[y]") \ + ASM("addq %[carry],%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("addq 8*" INDEX "(%[z]),%%rax") \ + ASM("adcq $0,%%rdx") \ + ASM("movq %%rdx,%[carry]") \ + ASM("movq %%rax, 8*" INDEX " (%[z])") + +#define DO_8_TIMES(MACRO, ARG) \ + MACRO(ARG, "0") MACRO(ARG, "1") MACRO(ARG, "2") MACRO(ARG, "3") \ + MACRO(ARG, "4") MACRO(ARG, "5") MACRO(ARG, "6") MACRO(ARG, "7") + +#define ADD_OR_SUBTRACT(CORE_CODE) \ + ASM("rorq %[carry]") \ + CORE_CODE \ + ASM("sbbq %[carry],%[carry]") \ + ASM("negq %[carry]") + +/************************************************* +* Word Addition * +*************************************************/ +inline word word_add(word x, word y, word* carry) + { + asm( + ADD_OR_SUBTRACT(ASM("adcq %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; + } + +/************************************************* +* Eight Word Block Addition, Two Argument * +*************************************************/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "adcq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/************************************************* +* Eight Word Block Addition, Three Argument * +*************************************************/ +inline word word8_add3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "adcq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/************************************************* +* Word Subtraction * +*************************************************/ +inline word word_sub(word x, word y, word* carry) + { + asm( + ADD_OR_SUBTRACT(ASM("sbbq %[y],%[x]")) + : [x]"=r"(x), [carry]"=r"(*carry) + : "0"(x), [y]"rm"(y), "1"(*carry) + : "cc"); + return x; + } + +/************************************************* +* Eight Word Block Subtraction, Two Argument * +*************************************************/ +inline word word8_sub2(word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB2_OP, "sbbq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), "0"(carry) + : "cc", "memory"); + return carry; + } + +/************************************************* +* Eight Word Block Subtraction, Three Argument * +*************************************************/ +inline word word8_sub3(word z[8], const word x[8], const word y[8], word carry) + { + asm( + ADD_OR_SUBTRACT(DO_8_TIMES(ADDSUB3_OP, "sbbq")) + : [carry]"=r"(carry) + : [x]"r"(x), [y]"r"(y), [z]"r"(z), "0"(carry) + : "cc", "memory"); + return carry; + } + +/************************************************* +* Eight Word Block Linear Multiplication * +*************************************************/ +inline word word8_linmul2(word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "x") + : [carry]"=r"(carry) + : [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +/************************************************* +* Eight Word Block Linear Multiplication * +*************************************************/ +inline word word8_linmul3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(LINMUL_OP, "z") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +/************************************************* +* Eight Word Block Multiply/Add * +*************************************************/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + asm( + DO_8_TIMES(MULADD_OP, "") + : [carry]"=r"(carry) + : [z]"r"(z), [x]"r"(x), [y]"rm"(y), "0"(carry) + : "cc", "%rax", "%rdx"); + return carry; + } + +} + +#endif |