@@ -64,53 +64,66 @@ uint32_t s_wInput[ITERATION_COUNT];
6464__NO_INIT
6565static
6666volatile
67- uint32_t s_wOutputC [ITERATION_COUNT ];
67+ uint32_t s_wOutputC [ITERATION_COUNT - 1 ];
6868
6969__NO_INIT
7070static
7171volatile
72- uint32_t s_wOutputACI [ITERATION_COUNT ];
72+ uint32_t s_wOutputACI [ITERATION_COUNT - 1 ];
7373
7474/*============================ IMPLEMENTATION ================================*/
7575
7676static
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