PandA-2024.02
add.c
Go to the documentation of this file.
1 /*
2 +--------------------------------------------------------------------------+
3 | CHStone : a suite of benchmark programs for C-based High-Level Synthesis |
4 | ======================================================================== |
5 | |
6 | * Collected and Modified : Y. Hara, H. Tomiyama, S. Honda, |
7 | H. Takada and K. Ishii |
8 | Nagoya University, Japan |
9 | |
10 | * Remark : |
11 | 1. This source code is modified to unify the formats of the benchmark |
12 | programs in CHStone. |
13 | 2. Test vectors are added for CHStone. |
14 | 3. If "main_result" is 0 at the end of the program, the program is |
15 | correctly executed. |
16 | 4. Please follow the copyright of each benchmark program. |
17 +--------------------------------------------------------------------------+
18 */
19 /*
20  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
21  * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
22  * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
23  */
24 
25 /* $Header: /home/kbs/jutta/src/gsm/gsm-1.0/src/RCS/add.c,v 1.5 1994/12/30 22:35:09 jutta Exp $ */
26 
27 /*
28  * See private.h for the more commonly used macro versions.
29  */
30 
31 #define saturate(x) \
32  ((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x))
33 
34 word
36 {
37  longword sum;
38  sum = (longword) a + (longword) b;
39  return saturate (sum);
40 }
41 
42 word
44 {
45  if (a == MIN_WORD && b == MIN_WORD)
46  return MAX_WORD;
47  else
48  return SASR ((longword) a * (longword) b, 15);
49 }
50 
51 word
53 {
54  longword prod;
55  if (b == MIN_WORD && a == MIN_WORD)
56  return MAX_WORD;
57  else
58  {
59  prod = (longword) a *(longword) b + 16384;
60  prod >>= 15;
61  return prod & 0xFFFF;
62  }
63 }
64 
65 word
67 {
68  return a < 0 ? (a == MIN_WORD ? MAX_WORD : -a) : a;
69 }
70 
71 const unsigned char bitoff[256] = {
72  8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
73  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
74  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
75  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
77  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
78  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
88 };
89 
90 word
92 /*
93  * the number of left shifts needed to normalize the 32 bit
94  * variable L_var1 for positive values on the interval
95  *
96  * with minimum of
97  * minimum of 1073741824 (01000000000000000000000000000000) and
98  * maximum of 2147483647 (01111111111111111111111111111111)
99  *
100  *
101  * and for negative values on the interval with
102  * minimum of -2147483648 (-10000000000000000000000000000000) and
103  * maximum of -1073741824 ( -1000000000000000000000000000000).
104  *
105  * in order to normalize the result, the following
106  * operation must be done: L_norm_var1 = L_var1 << norm( L_var1 );
107  *
108  * (That's 'ffs', only from the left, not the right..)
109  */
110 {
111 
112  if (a < 0)
113  {
114  if (a <= -1073741824)
115  return 0;
116  a = ~a;
117  }
118 
119  return a & 0xffff0000 ?
120  (a & 0xff000000 ? -1 + bitoff[0xFF & (a >> 24)] :
121  7 + bitoff[0xFF & (a >> 16)])
122  : (a & 0xff00 ? 15 + bitoff[0xFF & (a >> 8)] : 23 + bitoff[0xFF & a]);
123 }
124 
125 word
126 gsm_div (word num, word denum)
127 {
128  longword L_num;
129  longword L_denum;
130  word div;
131  int k;
132 
133  L_num = num;
134  L_denum = denum;
135  div = 0;
136  k = 15;
137  /* The parameter num sometimes becomes zero.
138  * Although this is explicitly guarded against in 4.2.5,
139  * we assume that the result should then be zero as well.
140  */
141 
142  if (num == 0)
143  return 0;
144 
145  while (k--)
146  {
147  div <<= 1;
148  L_num <<= 1;
149 
150  if (L_num >= L_denum)
151  {
152  L_num -= L_denum;
153  div++;
154  }
155  }
156 
157  return div;
158 }
#define MIN_WORD
Definition: private.h:33
#define saturate(x)
Definition: add.c:31
const unsigned char bitoff[256]
Definition: add.c:71
word gsm_mult(word a, word b)
Definition: add.c:43
int sum
Definition: dotproduct.h:3
word gsm_mult_r(word a, word b)
Definition: add.c:52
static const uint32_t k[]
Definition: sha-256.c:22
word gsm_add(word a, word b)
Definition: add.c:35
#define MAX_WORD
Definition: private.h:34
word gsm_norm(longword a)
Definition: add.c:91
#define SASR(x, by)
Definition: private.h:36
word gsm_abs(word a)
Definition: add.c:66
short word
Definition: private.h:30
long longword
Definition: private.h:31
word gsm_div(word num, word denum)
Definition: add.c:126

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13