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