PandA-2024.02
fp_accum.cpp
Go to the documentation of this file.
1 /************************************************
2 Copyright (c) 2016, Xilinx, Inc.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 
15 3. Neither the name of the copyright holder nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ************************************************/
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <ap_int.h>
33 #include <ap_fixed.h>
34 
35 #define NUM_ELEM 128
36 
37 #define DB_OPTIMIZED
38 #ifndef DB_OPTIMIZED
39 
40 
41 float hls_fp_accumulator(float window[NUM_ELEM])
42 {
43 
44  float result = 0.0;
45 
46  L1:for(unsigned char x=0; x<NUM_ELEM;x++)
47  {
48  result = result + window[x];
49  }
50 
51  return result;
52 }
53 
54 #else
55 
56 float hls_fp_accumulator(float window0[NUM_ELEM])
57 {
58 
59  float window1[NUM_ELEM/2] = {0.0};
60  float window2[NUM_ELEM/4] = {0.0};
61  float window3[NUM_ELEM/8] = {0.0};
62  float window4[NUM_ELEM/16]= {0.0};
63  float window5[NUM_ELEM/32]= {0.0};
64  float window6[NUM_ELEM/64]= {0.0};
65 
66  float result = 0.0;
67 
68  L1: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/2; x++)
69  {
70 #pragma HLS PIPELINE
71  window1[x] = window0[x] + window0[NUM_ELEM/2+x];
72  }
73  L2: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/4; x++)
74  {
75 #pragma HLS PIPELINE
76 
77  window2[x] = window1[x] + window1[NUM_ELEM/4+x];
78  }
79  L3: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/8; x++)
80  {
81 #pragma HLS PIPELINE
82 
83  window3[x] = window2[x] + window2[NUM_ELEM/8+x];
84  }
85  L4: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/16; x++)
86  {
87 #pragma HLS PIPELINE
88 
89  window4[x] = window3[x] + window3[NUM_ELEM/16+x];
90  }
91  L5: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/32; x++)
92  {
93 #pragma HLS PIPELINE
94 
95  window5[x] = window4[x] + window4[NUM_ELEM/32+x];
96  }
97  L6: for(ap_uint<7> x=0; x.to_uint()<NUM_ELEM/64; x++)
98  {
99 #pragma HLS PIPELINE
100 
101  window6[x] = window5[x] + window5[NUM_ELEM/64+x];
102  }
103 
104  result = window6[0] + window6[1];
105 
106  return result;
107 }
108 
109 #endif
110 
111 
112 
113 
114 float ref_fp_accumulator(float window[NUM_ELEM])
115 {
116 
117  float result = 0.0;
118 
119  for(unsigned char x=0; x<NUM_ELEM;x++)
120  {
121  result = result + window[x];
122  }
123 
124  return result;
125 }
126 
127 
128 
129 // DB
130 //random = pseudoCasual2(3, 7^5, 1, 2^32-1, 1920*1080);
131 float PseudoCasual(void)
132 {
133 
134  static unsigned long int k = 0;
135  static unsigned long int series;
136 
137  float result;
138  unsigned long long mult;
139 
140  const unsigned long long int x0 = 3;
141  const unsigned long long int a = 16807;
142  const unsigned int c = 1;
143  const unsigned int module = 4294967295ULL;
144  const float recipr_module = (float) 2.3283064e-10f;
145 
146  if (k==0)
147  {
148  mult = a*x0;
149  }
150  else
151  {
152  mult = a*series;
153  }
154 
155  series = (mult+c) % module;
156 
157  k++;
158 
159  //result = (float) series / (float) module;
160 
161  result = (float) series * recipr_module;
162 
163  return result;
164 
165 }
166 
167 
168 int main(void)
169 {
170  int x, y;
171  int ret_val = 0;
172 
173  float ref_window[NUM_ELEM];
174  float hls_window[NUM_ELEM];
175  float threshold = ((float)1.0)/1024;
176 
177  for (x=0; x < NUM_ELEM; x++)
178  {
179  ref_window[x] = (65536)*PseudoCasual();
180  hls_window[x] = ref_window[x] ;
181  }
182 
183  // REF
184  float ref_res = ref_fp_accumulator(ref_window);
185 
186  // DUT
187  float hls_res = hls_fp_accumulator(hls_window);
188 
189 
190  // check results
191  float total_error = 9.5367e-07f;
192 
193  float diff = ref_res - hls_res;
194  if (diff < threshold) diff = 0-diff; // take absolute value
195  if (diff > threshold)
196  {
197  total_error += (float) diff;
198  }
199 
200  printf("\n%010.4f\t%010.4f\t%010.4f\n", ref_res, hls_res, total_error);
201 
202 
203  if (total_error < 1.0)
204  {
205  ret_val=0;
206  printf("TEST OK!\n");
207  }
208  else
209  {
210  ret_val=1;
211  printf("TEST FAILED!\n");
212  }
213 
214 
215  return ret_val;
216 
217 }
float PseudoCasual(void)
Definition: fp_accum.cpp:131
float hls_fp_accumulator(float window0[NUM_ELEM])
Definition: fp_accum.cpp:56
#define NUM_ELEM
Definition: fp_accum.cpp:35
static const uint32_t k[]
Definition: sha-256.c:22
float ref_fp_accumulator(float window[NUM_ELEM])
Definition: fp_accum.cpp:114
int main(void)
Definition: fp_accum.cpp:168
int result[SIZE]
Definition: adpcm.c:800
x
Return the smallest n such that 2^n >= _x.
This class describes a generic module.

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