chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / karatsuba.h
1 /* -*-c-*-
2  *
3  * Macros for Karatsuba functions
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef CATACOMB_KARATSUBA_H
29 #define CATACOMB_KARATSUBA_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #ifndef CATACOMB_MPW_H
38 #  include "mpw.h"
39 #endif
40
41 /*----- Normal arithmetic macros ------------------------------------------*/
42
43 #define UADD(dv, av, avl) do {                                          \
44   mpw *_dv = (dv);                                                      \
45   const mpw *_av = (av), *_avl = (avl);                                 \
46   mpw _c = 0;                                                           \
47                                                                         \
48   while (_av < _avl) {                                                  \
49     mpw _a, _b;                                                         \
50     mpd _x;                                                             \
51     _a = *_av++;                                                        \
52     _b = *_dv;                                                          \
53     _x = (mpd)_a + (mpd)_b + _c;                                        \
54     *_dv++ = MPW(_x);                                                   \
55     _c = _x >> MPW_BITS;                                                \
56   }                                                                     \
57   while (_c) {                                                          \
58     mpd _x = (mpd)*_dv + (mpd)_c;                                       \
59     *_dv++ = MPW(_x);                                                   \
60     _c = _x >> MPW_BITS;                                                \
61   }                                                                     \
62 } while (0)
63
64 #define UADD2(dv, dvl, av, avl, bv, bvl) do {                           \
65   mpw *_dv = (dv), *_dvl = (dvl);                                       \
66   const mpw *_av = (av), *_avl = (avl);                                 \
67   const mpw *_bv = (bv), *_bvl = (bvl);                                 \
68   mpw _c = 0;                                                           \
69                                                                         \
70   while (_av < _avl || _bv < _bvl) {                                    \
71     mpw _a, _b;                                                         \
72     mpd _x;                                                             \
73     _a = (_av < _avl) ? *_av++ : 0;                                     \
74     _b = (_bv < _bvl) ? *_bv++ : 0;                                     \
75     _x = (mpd)_a + (mpd)_b + _c;                                        \
76     *_dv++ = MPW(_x);                                                   \
77     _c = _x >> MPW_BITS;                                                \
78   }                                                                     \
79   *_dv++ = _c;                                                          \
80   while (_dv < _dvl)                                                    \
81     *_dv++ = 0;                                                         \
82 } while (0)
83
84 #define USUB(dv, av, avl) do {                                          \
85   mpw *_dv = (dv);                                                      \
86   const mpw *_av = (av), *_avl = (avl);                                 \
87   mpw _c = 0;                                                           \
88                                                                         \
89   while (_av < _avl) {                                                  \
90     mpw _a, _b;                                                         \
91     mpd _x;                                                             \
92     _a = *_av++;                                                        \
93     _b = *_dv;                                                          \
94     _x = (mpd)_b - (mpd)_a - _c;                                        \
95     *_dv++ = MPW(_x);                                                   \
96     if (_x >> MPW_BITS)                                                 \
97       _c = 1;                                                           \
98     else                                                                \
99       _c = 0;                                                           \
100   }                                                                     \
101   while (_c) {                                                          \
102     mpd _x = (mpd)*_dv - (mpd)_c;                                       \
103     *_dv++ = MPW(_x);                                                   \
104     if (_x >> MPW_BITS)                                                 \
105       _c = 1;                                                           \
106     else                                                                \
107       _c = 0;                                                           \
108   }                                                                     \
109 } while (0)
110
111 /*----- Binary polynomial arithmetic macros -------------------------------*/
112
113 #define UXOR(dv, av, avl) do {                                          \
114   mpw *_dv = (dv);                                                      \
115   const mpw *_av = (av), *_avl = (avl);                                 \
116                                                                         \
117   while (_av < _avl)                                                    \
118     *_dv++ ^= *_av++;                                                   \
119 } while (0)
120
121 #define UXOR2(dv, dvl, av, avl, bv, bvl) do {                           \
122   mpw *_dv = (dv), *_dvl = (dvl);                                       \
123   const mpw *_av = (av), *_avl = (avl);                                 \
124   const mpw *_bv = (bv), *_bvl = (bvl);                                 \
125                                                                         \
126   while (_av < _avl || _bv < _bvl) {                                    \
127     mpw _a, _b;                                                         \
128     _a = (_av < _avl) ? *_av++ : 0;                                     \
129     _b = (_bv < _bvl) ? *_bv++ : 0;                                     \
130     *_dv++ = _a ^ _b;                                                   \
131   }                                                                     \
132   while (_dv < _dvl)                                                    \
133     *_dv++ = 0;                                                         \
134 } while (0)
135
136 /*----- That's all, folks -------------------------------------------------*/
137
138 #ifdef __cplusplus
139   }
140 #endif
141
142 #endif