UCR CS 122B, Adv. Embedded and Real Time Systems Homework 3 Prof. Frank Vahid 1. Create a straightforward "naive" C routine for reversing the bits of a 32-bit integer, using masks and shifts. Compare the naive routine to the fast bit reversal approach in the code below. Clearly explain why the approach below is faster. Note: an important part of embedded system design is not just creating code or designs, but being to understand existing code, and understanding its strengths and weaknesses. Turn in the assignment electronically. Name your file hw3_firstname_lastname.txt. ---- /*----------------------------------------------------------------------- // file: brev.c // // Copyright (c) 1994 Motorola, Inc. All Rights Reserved. //----------------------------------------------------------------------- */ #define LOOPS 1 /* #define LOOPS 89000 */ #include "Powerstone.h" #ifdef STATIC_SIZE unsigned long src[1] = {0}; unsigned long dst[1]; #else unsigned long src[80] = { 0x00005678,0x12340000,0x02040608,0x00000001, 0x12345678,0x12345678,0x12345678,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x12345678,0x12345678,0x12345678,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x12345678,0x12345678,0x12345678,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x12345678,0x12345678,0x12345678,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x12345678,0x12345678,0x12345678,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x10101010,0x12345678,0x10101010,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x10101010,0x12345678,0x10101010,0x12345678, 0x00005678,0x12340000,0x02040608,0x00000001, 0x10101010,0x12345678,0x10101010,0x12345678, 0 }; unsigned long dst[80]; #endif int main() { int i; unsigned long *s, *d; unsigned long x; INIT_BOARD(); do { START_TIMING(); for (i = 0 ; i < LOOPS ; i++) { for (s = src, d = dst ; *s ; s += 4, d += 4) { x = s[0]; x = (x >>16) | (x <<16); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); d[0] = x; x = s[1]; x = (x >>16) | (x <<16); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); d[1] = x; x = s[2]; x = (x >>16) | (x <<16); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); d[2] = x; x = s[3]; x = (x >>16) | (x <<16); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); d[3] = x; } } END_TIMING(); } while (PROFILE==0); exit(0); }