]> Dogcows Code - chaz/tar/blob - src/arith.h
GNU tar 1.12
[chaz/tar] / src / arith.h
1 /* Simple arithmetic for numbers greater than a long int, for GNU tar.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 /* Also, see comments at beginning of arith.c. */
19
20 #define BITS_PER_BYTE 8 /* number of bits in each sizeof unit */
21 #define BITS_PER_TARLONG 42 /* wanted number of bits in each tarlong */
22
23 /* In all cases, tarlong is the proper type for a big number.
24
25 For simulated arithmetic, SUPERDIGIT is the base, TARLONG_FORMAT is the
26 format to print a single super-digit filled with zeroes to the left, and
27 BITS_PER_SUPERDIGIT is the smallest number of bits required to fully
28 represent each super-digit. LONGS_PER_TARLONG says how many longs are
29 required for a full tarlong, and SIZEOF_TARLONG is the size of a tarlong
30 in bytes.
31
32 For straight compiler arithmetic, SUPERDIGIT is zero and TARLONG_FORMAT
33 is the format to directly print a tarlong (without zero-filling).
34
35 The values of SIZEOF_LONG_LONG and SIZEOF_UNSIGNED_LONG, below, are
36 obtained through the configuration process. */
37
38 #if BITS_PER_BYTE * SIZEOF_UNSIGNED_LONG >= BITS_PER_TARLONG
39 # define SUPERDIGIT 0
40 # define TARLONG_FORMAT "%uld"
41 typedef unsigned long tarlong;
42 #else
43 # if BITS_PER_BYTE * SIZEOF_LONG_LONG >= BITS_PER_TARLONG + 1
44 # define SUPERDIGIT 0
45 # define TARLONG_FORMAT "%lld"
46 typedef long long tarlong;
47 # else
48 # if BITS_PER_BYTE * SIZEOF_UNSIGNED_LONG >= 64
49 # define SUPERDIGIT 1000000000L
50 # define BITS_PER_SUPERDIGIT 29
51 # define TARLONG_FORMAT "%09uld"
52 # else
53 # if BITS_PER_BYTE * SIZEOF_UNSIGNED_LONG >= 32
54 # define SUPERDIGIT 10000L
55 # define BITS_PER_SUPERDIGIT 14
56 # define TARLONG_FORMAT "%04uld"
57 # endif
58 # endif
59 # endif
60 #endif
61
62 #if SUPERDIGIT
63
64 # define LONGS_PER_TARLONG \
65 ((BITS_PER_TARLONG + BITS_PER_SUPERDIGIT - 1) / BITS_PER_SUPERDIGIT)
66 # define SIZEOF_TARLONG (LONGS_PER_TARLONG * sizeof (unsigned long))
67
68 /* The NEC EWS 4.2 C compiler gets confused by a pointer to a typedef that
69 is an array. So we wrap the array into a struct. (Pouah!) */
70
71 struct tarlong
72 {
73 unsigned long digit[LONGS_PER_TARLONG];
74 };
75
76 typedef struct tarlong tarlong;
77
78 int zerop_tarlong_helper PARAMS ((unsigned long *));
79 int lessp_tarlong_helper PARAMS ((unsigned long *, unsigned long *));
80 void clear_tarlong_helper PARAMS ((unsigned long *));
81 void add_to_tarlong_helper PARAMS ((unsigned long *, int));
82 void mult_tarlong_helper PARAMS ((unsigned long *, int));
83 void print_tarlong_helper PARAMS ((unsigned long *, FILE *));
84
85 # define zerop_tarlong(Accumulator) \
86 zerop_tarlong_helper (&(Accumulator).digit[0])
87
88 # define lessp_tarlong(First, Second) \
89 lessp_tarlong_helper (&(First).digit[0], &(Second).digit[0])
90
91 # define clear_tarlong(Accumulator) \
92 clear_tarlong_helper (&(Accumulator).digit[0])
93
94 # define add_to_tarlong(Accumulator, Value) \
95 add_to_tarlong_helper (&(Accumulator).digit[0], (Value))
96
97 # define mult_tarlong(Accumulator, Value) \
98 mult_tarlong_helper (&(Accumulator).digit[0], (Value))
99
100 # define print_tarlong(Accumulator, File) \
101 print_tarlong_helper (&(Accumulator).digit[0], (File))
102
103 #else /* not SUPERDIGIT */
104
105 # define zerop_tarlong(Accumulator) \
106 ((Accumulator) == 0)
107
108 # define lessp_tarlong(First, Second) \
109 ((First) < (Second))
110
111 # define clear_tarlong(Accumulator) \
112 ((Accumulator) = 0)
113
114 # define add_to_tarlong(Accumulator, Value) \
115 ((Accumulator) += (Value))
116
117 # define mult_tarlong(Accumulator, Value) \
118 ((Accumulator) *= (Value))
119
120 # define print_tarlong(Accumulator, File) \
121 (fprintf ((File), TARLONG_FORMAT, (Accumulator)))
122
123 #endif /* not SUPERDIGIT */
This page took 0.042804 seconds and 5 git commands to generate.