Skip to content

Commit 22f1229

Browse files
committed
using hamming distance algorithm in GPR example
1 parent fbea718 commit 22f1229

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

GPR/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,19 @@ Navigate to the folder `project\mdk` and open the MDK project `ACI-Example.uvmpw
305305
You can compile the project by pressing `F7`. You should see no errors (and probably some warnings) in the **Build Output**:
306306
307307
```
308-
Program Size: Code=11224 RO-data=972 RW-data=20 ZI-data=11580
309308
".\Objects\ACI-Example.axf" - 0 Error(s), 0 Warning(s).
310309
```
311310
312311
After that you can start a debug session by pressing `CTRL`+`F5`, in the **Debug (printf) View**, we can find the test report:
313312
314313
```c
315314
-[Cycle Report]------------------------------------
316-
Calculate Population Count in C total cycle count: 830394 [000cabba]
317-
Correctness Validation: 0x00025e3b
315+
Calculate Hamming distance in C total cycle count: 585307 [0008ee5b]
318316
319317
-[Cycle Report]------------------------------------
320-
Calculate Population Count in ACI total cycle count: 220394 [00035cea]
321-
Correctness Validation: 0x00025e3b
318+
Calculate Hamming distance in ACI total cycle count: 40601 [00009e99]
319+
320+
Validate the output...PASS!
322321
```
323322

324323
> [!CAUTION]

GPR/example/main.c

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,53 +64,66 @@ uint32_t s_wInput[ITERATION_COUNT];
6464
__NO_INIT
6565
static
6666
volatile
67-
uint32_t s_wOutputC[ITERATION_COUNT];
67+
uint32_t s_wOutputC[ITERATION_COUNT-1];
6868

6969
__NO_INIT
7070
static
7171
volatile
72-
uint32_t s_wOutputACI[ITERATION_COUNT];
72+
uint32_t s_wOutputACI[ITERATION_COUNT-1];
7373

7474
/*============================ IMPLEMENTATION ================================*/
7575

7676
static
77-
uint32_t popcount(uint32_t x)
77+
uint32_t popcount (uint32_t x)
7878
{
79-
uint32_t n = 0;
80-
for (int i = 0; i < 32; ++i) {
81-
n += (x >> i) & 1;
79+
uint32_t count;
80+
for (count=0; x; count++) {
81+
x &= x - 1;
8282
}
83+
return count;
84+
}
8385

84-
return n;
86+
// Function to calculate Hamming distance using population count
87+
uint32_t hamming_distance_in_c(uint32_t x, uint32_t y)
88+
{
89+
// XOR the two numbers, then count the number of 1's in the result
90+
return popcount (x ^ y); // population count
8591
}
8692

93+
// Function to calculate Hamming distance using population count
94+
uint32_t hamming_distance_in_aci(uint32_t x, uint32_t y)
95+
{
96+
// XOR the two numbers, then count the number of 1's in the result
97+
return popc_u32 (x ^ y); // population count
98+
}
8799

88-
void population_count_test_in_c( uint32_t *pwInput,
89-
volatile uint32_t *pwOutput,
90-
size_t tSize)
100+
void test_in_c( uint32_t *pwInput,
101+
volatile uint32_t *pwOutput,
102+
size_t tSize)
91103
{
92104
assert(NULL != pwInput);
93105
assert(NULL != pwOutput);
94-
assert(tSize > 0);
106+
assert(tSize > 1);
107+
tSize--;
95108

96109
do {
97-
*pwOutput++ = popcount(*pwInput++);
110+
*pwOutput++ = hamming_distance_in_c(*pwInput, *(pwInput+1));
111+
pwInput++;
98112
} while(--tSize);
99113
}
100114

101-
102-
void population_count_test_in_aci( uint32_t *pwInput,
103-
volatile uint32_t *pwOutput,
104-
size_t tSize)
115+
void test_in_aci( uint32_t *pwInput,
116+
volatile uint32_t *pwOutput,
117+
size_t tSize)
105118
{
106119
assert(NULL != pwInput);
107120
assert(NULL != pwOutput);
108-
assert(tSize > 0);
109-
110-
uint32_t wTotal = 0;
121+
assert(tSize > 1);
122+
tSize--;
111123

112124
do {
113-
*pwOutput++ = popc_u32(*pwInput++);
125+
*pwOutput++ = hamming_distance_in_aci(*pwInput, *(pwInput+1));
126+
pwInput++;
114127
} while(--tSize);
115128
}
116129

@@ -132,7 +145,7 @@ bool validation_result(void)
132145
{
133146
printf("\r\n""Validate the output...");
134147
uint32_t wErrors = 0;
135-
for (size_t n = 0; n < dimof(s_wInput); n++) {
148+
for (size_t n = 0; n < (ITERATION_COUNT - 1); n++) {
136149
if (s_wOutputC[n] != s_wOutputACI[n]) {
137150
printf("\r\nERROR: Expect 0x%08x, but get 0x%08x at index: %d",
138151
s_wOutputC[n],
@@ -162,12 +175,12 @@ int main (void)
162175

163176
prepare_test(s_wInput, dimof(s_wInput));
164177

165-
__cycleof__("Calculate Population Count in C") {
166-
population_count_test_in_c(s_wInput, s_wOutputC, dimof(s_wInput));
178+
__cycleof__("Calculate Hamming distance in C") {
179+
test_in_c(s_wInput, s_wOutputC, dimof(s_wInput));
167180
}
168181

169-
__cycleof__("Calculate Population Count in ACI") {
170-
population_count_test_in_aci(s_wInput, s_wOutputACI, dimof(s_wInput));
182+
__cycleof__("Calculate Hamming distance in ACI") {
183+
test_in_aci(s_wInput, s_wOutputACI, dimof(s_wInput));
171184
}
172185

173186
return validation_result() ? 0 : -1;

0 commit comments

Comments
 (0)