3 * $Id: mptypes.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
5 * Types for multiprecision arithmetic
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/09/03 08:41:12 mdw
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
52 /*----- Important types ---------------------------------------------------*/
54 /* --- Choose word and doubleword types --- *
56 * The types I need, and their properties, are as follows:
58 * * @mpw@ is the radix in which all the arithmetic is actually performed.
59 * It must be an unsigned type that is efficient on the host's hardware.
60 * @MPW_MAX@ is the largest value of type @mpw@ I'll use. (Although
61 * there may be bigger values available, I'll not use them.) @MPW_BITS@
62 * is the number of bits required to represent @MPW_MAX@.
64 * * @mpd@ must be twice as large as @mpw@: I must be able to multiply any
65 * two @mpw@ values and end up with an @mpd@ as the result. @MPD_MAX@ is
66 * the largest value of type @mpd@ I'll use, and @MPD_BITS@ is the number
67 * of bits required to represent it.
69 * The algorithm used to choose these types is:
71 * * Try to use @unsigned int@ as @mpw@ and @unsigned long@ as @mpd@. If
72 * @unsigned long@ looks to small to do this, try to use @unsigned long
73 * long@, if it exists.
75 * * If that's not good enough, then fall back to @unsigned short@ as @mpw@
76 * and choose one of @unsigned int@, @unsigned long@ or @unsigned long
77 * long@ (in decreasing order of preference) as @mpd@.
79 * * If that doesn't work either, choose @unsigned int@ as both, and use
80 * half its width only as @mpw@.
85 /* --- Hack for GCC until it catches up with C9X --- *
87 * GCC uses bogus names for the @long long@ minimum and maximum values in its
88 * <limits.h> file. This little hack makes the proper portable C9X names
92 #if defined(ULONG_LONG_MAX) && !defined(ULLONG_MAX)
93 # define LLONG_MIN LONG_LONG_MIN
94 # define LLONG_MAX LONG_LONG_MAX
95 # define ULLONG_MAX ULONG_LONG_MAX
98 /* --- Decide on the actual types to use --- *
100 * The division trick here makes sure that one type is twice another without
101 * the risk of overflowing the arithmetic provided by the preprocessor.
104 #if ULONG_MAX / UINT_MAX >= UINT_MAX
105 typedef unsigned int mpw
;
106 typedef unsigned long mpd
;
107 # define MPW_MAX UINT_MAX
108 #elif ULLONG_MAX / UINT_MAX >= UINT_MAX
109 typedef unsigned int mpw
;
110 typedef unsigned long long mpd
;
111 # define MPW_MAX UINT_MAX
112 #elif UINT_MAX / USHRT_MAX >= USHRT_MAX
113 typedef unsigned short mpw
;
114 typedef unsigned int mpd
;
115 # define MPW_MAX USHRT_MAX
116 #elif ULONG_MAX / USHRT_MAX >= USHRT_MAX
117 typedef unsigned short mpw
;
118 typedef unsigned long mpd
;
119 # define MPW_MAX USHRT_MAX
120 #elif ULLONG_MAX / USHRT_MAX >= USHRT_MAX
121 typedef unsigned short mpw
;
122 typedef unsigned long long mpd
;
123 # define MPW_MAX USHRT_MAX
125 typedef unsigned int mpw
;
126 typedef unsigned int mpd
;
127 # define MPD_MAX UINT_MAX
130 /* --- Fix a value for @MPD_MAX@ --- *
132 * This will then be the target for finding the bit widths and things.
136 # define MPD_MAX ((MPW_MAX + 1) * MPW_MAX + MW_WMAX)
139 /* --- Find the width of @MPD_MAX@ in bits --- *
141 * It must be at least 16 bits wide, because the smallest type I bother to
142 * try is @unsigned short@. I only bother testing for up to 64 bits, and in
143 * power-of-two chunks, because I don't care about efficiency on more bizarre
147 #if MPD_MAX <= 0xfffffffff
148 # if MPD_MAX == 0xffffffff
150 # elif MPD_MAX >= 0x0fffffff
152 # elif MPD_MAX >= 0x00ffffff
154 # elif MPD_MAX >= 0x000fffff
156 # elif MPD_MAX >= 0x0000ffff
159 # error "Abject failure deciding on type `mpw'"
161 # if MPD_MAX / 0xffffffff < 0xffffffff
164 # define MPD_BITS 64 /* Slightly dodgy */
167 /* --- Now sort out the other magical values --- */
172 #define MPW_BITS (MPD_BITS / 2)
173 #define MPD_MAX (((1 << (MPD_BITS - 1)) - 1) * 2 + 1)
174 #define MPW_MAX ((1 << MPW_BITS) - 1)
178 /*----- Macros for coercion and masking -----------------------------------*/
182 * Arguments: @x@ = an unsigned value
184 * Use: Expands to the value of @x@ masked and typecast to a
185 * multiprecision integer word.
188 #define MPW(x) ((mpw)((x) & MPW_MAX))
192 * Arguments: @n@ = number of words
194 * Use: Expands to the number of bytes occupied by a given number of
198 #define MPWS(n) ((n) * sizeof(mpw))
200 /*----- That's all, folks -------------------------------------------------*/