]>
Dogcows Code - chaz/tar/blob - src/arith.c
1 /* Arithmetic for numbers greater than a long int, for GNU tar.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any later
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* common.h is needed to define FATAL_ERROR. It also includes arith.h. */
23 /* GNU tar needs handling numbers exceeding 32 bits, which is the size of
24 unsigned long ints for many C compilers. This module should provide
25 machinery for handling at least BITS_PER_TARLONG bits per number. If
26 `long long' ints are available and are sufficient for the task, they will
29 Russell Cattelan reports 165 Gb single tapes (digital video D2 tapes on
30 Ampex drives), so requiring 38 bits for the tape length in bytes. He
31 also reports breaking the terabyte limit with a single file (using SGI
32 xFS file system over 37 28GB disk arrays attached to a Power Challenge
33 XL; check out http://www.lcse.umn.edu/ for a picture), so requiring a
34 little more than 40 bits for the file size in bytes. The POSIX header
35 structure allows for 12 octal digits to represent file lengths, that is,
36 up to 36 bits for the byte size of files.
38 If `long long' is not supported by the compiler, SIZEOF_LONG_LONG will be
39 set to zero by configure. In this case, or if `long long' ints does not
40 have enough bits, then huge numbers are rather represented by an array of
41 longs, with the least significant super-digit at position 0. For making
42 multiplication and decimal input/output easy, the base of a super-digit
43 is an exact exponent of 10, and is such that base*base fits in a long. */
47 /*-------------------------------.
48 | Check if ACCUMULATOR is zero. |
49 `-------------------------------*/
52 zerop_tarlong_helper (unsigned long *accumulator
)
56 for (counter
= LONGS_PER_TARLONG
- 1; counter
>= 0; counter
--)
57 if (accumulator
[counter
])
63 /*----------------------------------------------.
64 | Check if FIRST is strictly less than SECOND. |
65 `----------------------------------------------*/
68 lessp_tarlong_helper (unsigned long *first
, unsigned long *second
)
72 for (counter
= LONGS_PER_TARLONG
- 1; counter
>= 0; counter
--)
73 if (first
[counter
] != second
[counter
])
74 return first
[counter
] < second
[counter
];
79 /*----------------------------.
80 | Reset ACCUMULATOR to zero. |
81 `----------------------------*/
84 clear_tarlong_helper (unsigned long *accumulator
)
88 for (counter
= 0; counter
< LONGS_PER_TARLONG
; counter
++)
89 accumulator
[counter
] = 0;
92 /*----------------------------.
93 | To ACCUMULATOR, add VALUE. |
94 `----------------------------*/
97 add_to_tarlong_helper (unsigned long *accumulator
, int value
)
102 for (counter
= 0; counter
< LONGS_PER_TARLONG
; counter
++)
104 if (accumulator
[counter
] >= -value
)
106 accumulator
[counter
] += value
;
109 accumulator
[counter
] += value
+ SUPERDIGIT
;
113 for (counter
= 0; counter
< LONGS_PER_TARLONG
; counter
++)
115 if (accumulator
[counter
] + value
< SUPERDIGIT
)
117 accumulator
[counter
] += value
;
120 accumulator
[counter
] += value
- SUPERDIGIT
;
123 FATAL_ERROR ((0, 0, _("Arithmetic overflow")));
126 /*--------------------------------.
127 | Multiply ACCUMULATOR by VALUE. |
128 `--------------------------------*/
131 mult_tarlong_helper (unsigned long *accumulator
, int value
)
136 for (counter
= 0; counter
< LONGS_PER_TARLONG
; counter
++)
138 carry
+= accumulator
[counter
] * value
;
139 accumulator
[counter
] = carry
% SUPERDIGIT
;
143 FATAL_ERROR ((0, 0, _("Arithmetic overflow")));
146 /*----------------------------------------------------------.
147 | Print the decimal representation of ACCUMULATOR on FILE. |
148 `----------------------------------------------------------*/
151 print_tarlong_helper (unsigned long *accumulator
, FILE *file
)
153 int counter
= LONGS_PER_TARLONG
- 1;
155 while (counter
> 0 && accumulator
[counter
] == 0)
158 fprintf (file
, "%uld", accumulator
[counter
]);
160 fprintf (file
, TARLONG_FORMAT
, accumulator
[--counter
]);
163 #endif /* SUPERDIGIT */
This page took 0.044436 seconds and 4 git commands to generate.