summaryrefslogtreecommitdiffstats
path: root/all_pairs/source/ammunition/arithm.c
diff options
context:
space:
mode:
authorJoshua Bakita <bakitajoshua@gmail.com>2019-10-07 19:13:39 -0400
committerJoshua Bakita <bakitajoshua@gmail.com>2019-10-07 19:13:39 -0400
commit386b7d3366f1359a265da207a9cafa3edf553b64 (patch)
treec76120c2c138faed822e4ae386be6ef22a738a78 /all_pairs/source/ammunition/arithm.c
parent54a3f7091a2146b29c73a6fdc4b62a5c4ad7a3d8 (diff)
Reorganize and commit all the modified TACLeBench code and run scripts
Diffstat (limited to 'all_pairs/source/ammunition/arithm.c')
-rw-r--r--all_pairs/source/ammunition/arithm.c1384
1 files changed, 1384 insertions, 0 deletions
diff --git a/all_pairs/source/ammunition/arithm.c b/all_pairs/source/ammunition/arithm.c
new file mode 100644
index 0000000..9480846
--- /dev/null
+++ b/all_pairs/source/ammunition/arithm.c
@@ -0,0 +1,1384 @@
1/*
2 FILE NAME: arithm.c
3
4 TITLE: Package for arbitrary precision integer arithmetic
5
6 DESCRIPTION: This abstract data implements arbitrary precision
7 integer and unsigned integer numbers by machine independent
8 way. The implementation of the package functions are not
9 sufficiently efficient in order to use for run-time. The
10 package functions are oriented to implement constant-folding in
11 compilers. This package is necessary because host machine may
12 not support such arithmetic for target machine. For example,
13 VAX does not support does not support more 32-bits integer
14 numbers arithmetic. The numbers are represented by bytes in
15 big endian mode, negative integer numbers are represented in
16 complementary code. All sizes are given in bytes and must be
17 positive. Results of executions of all functions can coincide
18 with a operand(s). All functions of addition, subtraction,
19 multiplication, division, evaluation of remainder, shift,
20 changing size and transformation of string into number fix
21 overflow. The overflow is fixed when result can not be
22 represented by number of given size.
23
24*/
25
26#include "arithm.h"
27#include "ammunition_string.h"
28
29
30/* This variable can have only two values 0 or 1. The value `1'
31 corresponds to overflow. The variable value are modified by all
32 functions of addition, subtract, multiplication, division,
33 evaluation of remainder, shift, changing size and transformation of
34 string into number fix overflow. */
35
36int ammunition_overflow_bit;
37
38
39/* The following function adds unsigned integers. The function
40 returns 1 if unsigned integer overflow is fixed, 0 otherwise.
41 Result can be placed in any operand. */
42
43int ammunition_add_unsigned_integer_without_overflow_reaction
44( int size, const void *op1, const void *op2, void *result )
45{
46 int digit_num;
47 int carry;
48 unsigned int sum;
49
50 _Pragma( "loopbound min 4 max 4" )
51 for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
52 sum = ( ( ( unsigned char * ) op1 ) [digit_num]
53 + ( ( unsigned char * ) op2 ) [digit_num] + carry );
54 if ( sum > UCHAR_MAX ) {
55 sum -= UCHAR_MAX + 1;
56 carry = 1;
57 } else
58 carry = 0;
59 ( ( unsigned char * ) result ) [digit_num] = sum;
60 }
61 return carry != 0;
62}
63
64/* The following function adds unsigned integers. The function
65 returns 1 if unsigned integer overflow (the first operand is less
66 than the second) is fixed, 0 otherwise. Result can be placed in
67 any operand. */
68
69int ammunition_subtract_unsigned_integer_without_overflow_reaction
70( int size, const void *op1, const void *op2, void *result )
71{
72 int digit_num;
73 int carry;
74 int subtraction;
75
76 _Pragma( "loopbound min 4 max 4" )
77 for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
78 subtraction = ( ( ( unsigned char * ) op1 ) [digit_num]
79 - ( ( unsigned char * ) op2 ) [digit_num] - carry );
80 if ( subtraction < 0 ) {
81 subtraction += UCHAR_MAX + 1;
82 carry = 1;
83 } else
84 carry = 0;
85 ( ( unsigned char * ) result ) [digit_num] = subtraction;
86 }
87 return carry != 0;
88}
89
90/* The following function makes complementary code of number. Result
91 can be placed in operand. */
92
93void ammunition_make_complementary_code
94( int size, const void *operand, void *result )
95{
96 int digit_num;
97 int carry;
98 int subtraction;
99
100 _Pragma( "loopbound min 2 max 6" )
101 for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
102 subtraction = ( 0 - ( ( unsigned char * ) operand ) [digit_num] - carry );
103 if ( subtraction != 0 ) {
104 subtraction += UCHAR_MAX + 1;
105 carry = 1;
106 } else
107 carry = 0;
108 ( ( unsigned char * ) result ) [digit_num] = subtraction;
109 }
110}
111
112/* The following function multiplys unsigned integer by digit (byte
113 size). The function returns 1 if unsigned integer overflow is
114 fixed, 0 otherwise. */
115
116int ammunition_multiply_unsigned_integer_by_digit_without_overflow_reaction
117( int size, void *operand, unsigned int digit )
118{
119 int digit_num;
120 unsigned int carry;
121 unsigned int sum;
122
123 _Pragma( "loopbound min 4 max 4" )
124 for ( digit_num = size - 1, carry = 0; digit_num >= 0; digit_num-- ) {
125 sum = ( ( ( unsigned char * ) operand ) [digit_num] * digit + carry );
126 if ( sum > UCHAR_MAX ) {
127 carry = sum / ( UCHAR_MAX + 1 );
128 sum %= UCHAR_MAX + 1;
129 } else
130 carry = 0;
131 ( ( unsigned char * ) operand ) [digit_num] = sum;
132 }
133 return carry != 0;
134}
135
136
137/* Originally reaction on all integer and unsigned integer overflow is
138 equal to the following function. The function does nothing. */
139
140void
141ammunition_arithmetic_overflow_reaction ( void )
142{}
143
144
145/* Originally reaction on all integer and unsigned integer overflow is
146 equal to the following function. The function does nothing. */
147
148void
149ammunition_arithmetic_unsigned_overflow_reaction ( void )
150{}
151
152
153/* This page contains functions for arbitrary precision addition. */
154
155/* The function adds unsigned integers and fixes overflow reaction if
156 it is needed. The function makes this with the aid of function
157 `add_unsigned_integer_without_overflow_reaction'. Result can be
158 placed in any operand. */
159
160void
161ammunition_add_unsigned_integer ( int size, const void *op1, const void *op2,
162 void *result )
163{
164 ammunition_overflow_bit
165 = ammunition_add_unsigned_integer_without_overflow_reaction (
166 size, op1, op2, result );
167 if ( ammunition_overflow_bit != 0 )
168 ammunition_arithmetic_unsigned_overflow_reaction();
169}
170
171/* The function adds integers and fixes overflow reaction if it is
172 needed. The function makes this with the aid of function
173 `add_unsigned_integer_without_overflow_reaction'. Result can be
174 placed in any operand. */
175
176void
177ammunition_add_integer ( int size, const void *op1, const void *op2,
178 void *result )
179{
180 int op1_sign;
181 int sign_equality;
182
183 op1_sign = INTEGER_SIGN ( op1 );
184 sign_equality = INTEGER_SIGN ( op1 ) == INTEGER_SIGN ( op2 );
185 ammunition_add_unsigned_integer_without_overflow_reaction (
186 size, op1, op2, result );
187 ammunition_overflow_bit = sign_equality &&
188 ( op1_sign != INTEGER_SIGN ( result ) );
189 if ( ammunition_overflow_bit != 0 )
190 ammunition_arithmetic_overflow_reaction();
191}
192
193