Hardware Reference
In-Depth Information
To call a function, simply put down the name of the function and replace the argument
declarations by actual arguments or values and terminate it with a semicolon.
Example 5.4
Write a function to find the square root of a 32-bit integer using the successive approxima-
tion method described in Section 4.7.2.
Solution: The C function that computes the square root of a 32-bit unsigned integer is as
follows:
// ********************************************************************************
// This function computes the square root of a 32-bit unsigned integer
// using the successive approximation method.
// Incoming parameter: a 32-bit number of which the square root is to be found.
// ********************************************************************************
unsigned long int FindSqr (unsigned long int xval)
{
unsigned
long
int
mask,temp,sar;
unsigned
int
ix;
mask 5 0x8000;
// Initialize mask for making bit value guessing
sar 5 0;
for (ix 5 0; ix , 16; ix 11 ){
temp 5 sar | mask;
if((temp * temp) ,5 xval)
sar
5 temp;
mask ..5 1;
}
if ((xval 2 (sar * sar)) . ((sar 1 1)*(sar 1 1) 2 xval))
sar
15 1;
return sar;
}
Example 5.5
Write a function to test whether a 32-bit nonnegative integer is a prime number.
Solution: The integer 1 is not a prime number. A number is a prime if it cannot be divided by
any integer between 2 and its square root. The prime test function needs to call the FindSqr
function to find the test divide limit. The prime test function is
unsigned int PrimeTest(unsigned long int xval)
{
unsigned long int TLimit;
unsigned int ptest;
if ((xval 55 1) || (xval 55 2))
return 0;
 
Search WWH ::




Custom Search