7d5fa32a |
1 | /* -*-c-*- |
7d5fa32a |
2 | * |
3 | * Macros for Karatsuba functions |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
7d5fa32a |
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. |
45c0fd36 |
16 | * |
7d5fa32a |
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. |
45c0fd36 |
21 | * |
7d5fa32a |
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 | |
b1195526 |
28 | #ifndef CATACOMB_KARATSUBA_H |
29 | #define CATACOMB_KARATSUBA_H |
7d5fa32a |
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 | |
b1195526 |
41 | /*----- Normal arithmetic macros ------------------------------------------*/ |
7d5fa32a |
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 | |
b1195526 |
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 | |
7d5fa32a |
136 | /*----- That's all, folks -------------------------------------------------*/ |
137 | |
138 | #ifdef __cplusplus |
139 | } |
140 | #endif |
141 | |
142 | #endif |