storin.{tests,debug}-ref: Ancient versions of the test output.
[storin] / arith24.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Arithmetic mod %$2^{24}$%
6 *
7 * (c) 2000 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * Copyright (c) 2000 Mark Wooding
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met:
18 *
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * 2, Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * 3. The name of the authors may not be used to endorse or promote
27 * products derived from this software without specific prior written
28 * permission.
29 *
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
33 * NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Instead of accepting the above terms, you may redistribute and/or modify
43 * this software under the terms of either the GNU General Public License,
44 * or the GNU Library General Public License, published by the Free
45 * Software Foundation; either version 2 of the License, or (at your
46 * option) any later version.
47 */
48
49 /*----- Revision history --------------------------------------------------*
50 *
51 * $Log: arith24.c,v $
52 * Revision 1.2 2000/07/02 15:21:20 mdw
53 * Fix licence text.
54 *
55 * Revision 1.1 2000/05/21 11:28:30 mdw
56 * Initial check-in.
57 *
58 */
59
60 /*----- Header files ------------------------------------------------------*/
61
62 #include "arith24.h"
63 #include "bits.h"
64
65 /*----- Main code ---------------------------------------------------------*/
66
67 /* --- @inv24@ --- *
68 *
69 * Arguments: @uint24 x@ = a number to invert
70 *
71 * Returns: The multiplicative inverse %$x^{-1}$% of %$x$% (mod
72 * %$2^{24}$%, if %$x$% is odd, or zero if %$x$% is even (and
73 * hence uninvertible).
74 *
75 * Use: Computes multiplicative inverses mod %$2^{24}$%.
76 */
77
78 uint24 inv24(uint24 x)
79 {
80 uint32 m = MASK24 + 1;
81 long a = 1, b = 0;
82 uint32 n = x;
83
84 for (;;) {
85 uint32 q, r;
86 long t;
87 if (!(r = m % n))
88 break;
89 q = m / n;
90 m = n; n = r;
91 t = a; a = b - q * a; b = t;
92 }
93 if (n != 1)
94 return (0);
95 return (U24(a));
96 }
97
98 /*----- That's all, folks -------------------------------------------------*/