Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to compute the square root of a real number in Verilog?

Status
Not open for further replies.

walkon

Junior Member level 1
Joined
Oct 21, 2004
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
165
verilog square root

Can anybody figure out how to compute the square root of a real number in Verilog?

Are there some math library in Verilog as that of in VHDL?

Thanks
 

verilog sqrt

If you have any plans to implement this in hardware .. You have to do some approximations....

for eg Sqroot(X^2 + Y^2) = X. Sqroot(1 + (Y/X)^2)

Sqroot(1 + (Y/X)^2) part can be expanded as a geometric series ... and take 3 or 4 factors depending on the accuracy...

(this is the way in which my company implemented for a WLAN baseband IC)
 

square root in verilog

walkon said:
Can anybody figure out how to compute the square root of a real number in Verilog?

Are there some math library in Verilog as that of in VHDL?

Thanks

I assume you are asking for a pure simulation purpose. If so, first of all - Verilog doesn't have the concept of library as much as in VHDL. SystemVerilog has extended it to have some thing similar called Packages that might do what you want. But tools supporting SV will be a while from now.

As a workaround, you could use PLI to do this, simply pass the real number to a C-function call and compute square root in C, return the value to Verilog - again works for simulation. For synthesis, refer to the other post, explore Look-up table approach etc.

HTH
Aji
http://www.noveldv.com
 

square root verilog

Thanks for you reply.
I tried to use PLI. But it seems I could not even
start to initialize the compile environment for PLI in Solaris..........
 

verilog math functions

btw: I don't know why Verilog don't intend to let its port support the
real numbers input and output.
 

pleaaaaaaaaaaaaaaaaaaaaase help me
I have to present my project next week
I didn't do any thing yet,
it's about generating a program in vhdl to calculate a 64 bits square root
 

Re: How to compute the square root of a real number in Veril

ammoulaa said:
pleaaaaaaaaaaaaaaaaaaaaase help me
altera quartus has altfp_sqrt mega-function with 64 bit support
 

Re: How to compute the square root of a real number in Veril

For a project about square root function, I would expect the implementation of a standard numerical algorithm from literature, that involves an iterative solution.

I'm not clear about the number format, because "real number" (not float or double) was said in the original post and you wrote "64 bit". The latter could be e.g. IEEE double or 64 bit integer, or a specified fractional format.
 

Re: How to compute the square root of a real number in Veril

one way is to convert from real to integer by shifting the mantissa by the exponent or in the case of ieee by exp - 127 and then use a simple routine like this to calculate the square root:

start a count from zero and loop from zero to the number you want the square root for (say N). In each iteration count up the counter if the (count * count) is less than or equal the number N. Otherwise stop and report the count as the square root for the number. The precision is is +/- .5
 

Re: How to compute the square root of a real number in Veril

Rakko, your algorithm works for integers but is very inefficient. I just wrote a routine to calculate the sqrt of a real number which converges very quickly. Here you go:

Code:
// task: calculate_sqrt
// --------------------
// Calculates square root accurate to 6 decimal places of a real number
// To use:  must have real values "argument" and "result" declared
// (1) Assign argument of square root function to "argument"
// (2) Call task "calculate_sqrt"
// (3) Result of sqrt will be stored in "result."
//
// Example code:
// real argument, result;
// argument = 25.0;
// calculate_sqrt;
// disp("result");
// Outputs "5.0" to stdout

task calculate_sqrt;
   real error, result_new;
   result = 1.0;
   error = 1.0;
   while (error > 0.001) begin
      result_new = argument/2.0/result + result/2.0;
      error = (result_new - result)/result;
      if (error < 0.0) error = -error;
      result = result_new;
   end
endtask

Unfortunately, this needs to be a task instead of a function, with the kludgy assigning of "assignment" and "result" outside of the task, because Verilog does not accept real numbers as I/O's of functions. You could easily transform this code into a true function in which the I/O's are integers instead of real numbers.

rakko said:
one way is to convert from real to integer by shifting the mantissa by the exponent or in the case of ieee by exp - 127 and then use a simple routine like this to calculate the square root:

start a count from zero and loop from zero to the number you want the square root for (say N). In each iteration count up the counter if the (count * count) is less than or equal the number N. Otherwise stop and report the count as the square root for the number. The precision is is +/- .5
Code:
Code:
 

Re: How to compute the square root of a real number in Veril

last i checked reals were not synthesizable. if you are implementing in hardware, you need to do things using integers.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top