aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mp_amd64/mp_asm.h
blob: 17f33224c6681e6ebfc163bec0ca752698e1470d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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