UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Visitor nagendrakr
Visitor
473 Views
Registered: ‎01-17-2019

sqrtf() in Microblaze is taking huge time to execute.

Hi 

I am using Artix7 board with microblaze. My BRAM clock is 100MHz.

Microblze is taking large number of cycle to execute sqrtf() function, around 17,000.

Number of cycle i can check on ILA.

The code i am using is 

float val=16.0F;
float pp;
int ver_reg = 0;
int interfacetest = 0;
int code = 0;
ver_reg = *(unsigned int *)(M_WRD_ADDRESS + 4*(0 + 0));
interfacetest = *(unsigned int *)(M_WRD_ADDRESS + 4*(0 + 1));

pp = sqrtf(val);

interfacetest = *(unsigned int *)(M_WRD_ADDRESS + 4*(0 + 1));
code = *( int *)(M_WRD_ADDRESS + 4*(0 + 2));

ver_reg, interfacetest and code is value i am reading from PL.

In my design, i have enable FPU, integer Multiplier, integer Divider.

FPU i have given Extended.

 

Please tell me what is wrong in my code?

Thanks in advance

Capture.PNG
0 Kudos
17 Replies
464 Views
Registered: ‎07-23-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

 

I can't believe that. In my opinion, you are introducing some artefact with your way of measurement.

I suggest you add a timer to the microblaze and use it to count clock cycles before and after the operation.

Alternatively, a simpler but not so accurate way is to make a loop of, say, a million times with the square root calculation, printing something to the terminal before and after the loop and simply use your phone timer... then divide by a million and reckon that in clock cycles. It simply cannot take 17000 cycles a square root, not even float or double. Microblaze is not that much crap (actually, the math library is what is responsible for the number of cycles).

A third experiment I suggest: comment out your operation or replace it with an assignment like:

pp = val;

It should take a few cycles, right? I guess with your technique it will show above 10,000. Bet a drink?

0 Kudos
Visitor nagendrakr
Visitor
448 Views
Registered: ‎01-17-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

Thanks for reply

Instead of giving val parameter, if i am giving value 16.0F directly it is working fine.

pp = sqrtf(16.0F)

For this it is taking 100 number of cycle.

I am not able to understand why?

 

Capture_2.PNG
0 Kudos
429 Views
Registered: ‎07-23-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

 

Be carefully with compiler smartness... you are feeding a constant so its square root is also a constant. 'nothing to calculate' the compiler may think.

That, or else the remaining 16900 cycles are because of retrieving the variable content (which I don't believe)

It's a good finding! again, I suggest having a plain assignement.

0 Kudos
Visitor nagendrakr
Visitor
377 Views
Registered: ‎01-17-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

Please tell how i can resolve this problem?

0 Kudos
Xilinx Employee
Xilinx Employee
319 Views
Registered: ‎11-30-2007

Re: sqrtf() in Microblaze is taking huge time to execute.

sqrtf() is a function provided by mathlib. It is to my knowledge not accelerated in any way.

Therefore this can cost a lot of time.

 

by replacing the parameter with a fixed number will cause the compiler to eliminate the calculation by replacing it with the result of sqrtf(16.0f).

sqrtf is an expensive operation - and it uses float operations quite heavily.

if you need a faster implementation -- then think of moving away from float calculations and to an acceleration of sqrtf by PL IPs.

 

Note sqrt is for double values - and it will be even slower than sqrtf - than is a special version made for floats.

0 Kudos
Scholar drjohnsmith
Scholar
305 Views
Registered: ‎07-09-2009

Re: sqrtf() in Microblaze is taking huge time to execute.

re design your code to use fixed points not floats will increase the speed significantly,

https://www.xilinx.com/support/documentation/white_papers/wp491-floating-to-fixed-point.pdf

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Visitor nagendrakr
Visitor
249 Views
Registered: ‎01-17-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

Thanks for reply

Same output is coming for 'int' also.

int val=16;

pp = sqrtf(val);

0 Kudos
Xilinx Employee
Xilinx Employee
235 Views
Registered: ‎11-30-2007

Re: sqrtf() in Microblaze is taking huge time to execute.

This is a simple beginner failure:

int val=16;

pp = sqrtf(val);

This is equivalent to:

int val=16;

pp = sqrtf((float)val);

And therefore the execution time is identical (or even a little bit more - due to the additional conversion required from int format to float format before executing sqrtf() function.
Scholar drjohnsmith
Scholar
210 Views
Registered: ‎07-09-2009

Re: sqrtf() in Microblaze is taking huge time to execute.

Even us none beginners make many mistakes,
( with my spelling you can guess how the tools react ..)
<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Visitor nagendrakr
Visitor
182 Views
Registered: ‎01-17-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

Thanks for your reply

Can anybody tell me how i can use "fsqrt" in my C code.

Since sqrtf() is not performing as desire so i want to use assembly code for square root.

Also tell me if any header file is required for that.

 

Thnaks 

0 Kudos
Xilinx Employee
Xilinx Employee
175 Views
Registered: ‎11-30-2007

Re: sqrtf() in Microblaze is taking huge time to execute.

As far as I know there is no function available called fsqrt().

Btw. - did you enabled the FPU? Microblaze provides an FPU (optional, not enabled by default). This will increase the size of the Microblaze significantly.

But it should move all floating point calculations over to the FPU (double precision calculations are still done by the mathlib).

When FPU is enabled - the computation of sqrtf(float) should be faster, computation of sqrt(double) will stay the same.

 

If this doesn't help - and you need a faster execution time - you need to add an IP core.

What's the target number of clock cycles you are willing to spend here?

Note: Single clock cycle latency is unreachable...

Karsten

0 Kudos
Xilinx Employee
Xilinx Employee
173 Views
Registered: ‎11-30-2007

Re: sqrtf() in Microblaze is taking huge time to execute.

see ug984:

sqare root, fsqrt and dsqrt are available when the extended FPU is enabled

 

Means try to enable the FPU - this should do the trick. The compiler should then know that the extended FPU is available and will automatically use the intrinsics.

0 Kudos
Scholar drjohnsmith
Scholar
161 Views
Registered: ‎07-09-2009

Re: sqrtf() in Microblaze is taking huge time to execute.

Its the floating function that is taking the time,

   your best way of gainning speed is to move to fixed point system,

 

what actual dynamic range in the numbers does your algorithum NEED to be able to meet your constraints,

What is your complete algorithum ?

If its that time critical an dmust be done in a procesor, you need to either use a hardware processor such as the Zynq range or move the complete code into real fpga hardware  as an IP block.

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Visitor nagendrakr
Visitor
143 Views
Registered: ‎01-17-2019

Re: sqrtf() in Microblaze is taking huge time to execute.

FPU is enable and set to Extedned.
I want to use fsqrt in assembly code in c.
0 Kudos
Scholar drjohnsmith
Scholar
137 Views
Registered: ‎07-09-2009

Re: sqrtf() in Microblaze is taking huge time to execute.

Its the floating function that is taking the time,

   your best way of gainning speed is to move to fixed point system,

 

what actual dynamic range in the numbers does your algorithum NEED to be able to meet your constraints,

What is your complete algorithum ?

If its that time critical and must be done in a procesor, you need to either use a hardware processor such as the Zynq range or move the complete code into real fpga hardware  as an IP block.

 

Any reason you can't answer these questoins , then we can try better to help you.

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos
Xilinx Employee
Xilinx Employee
112 Views
Registered: ‎11-30-2007

Re: sqrtf() in Microblaze is taking huge time to execute.

Hi,

 

I build today a little test design ofr an Artix board - with extended FPU enabled (Vivado 2018.3). I use an AXI Timer to count clock cycles. AXI Timer runs with same speed like Microblaze. Microblaze is using only LMB memory to avoid any issues with external DDR memory (and potential latencies due to caching).

Microblaze is the 5 stage version.

 

Ansi-C code looks this one:

// start timer:
XTmrCtr_Reset(TmrCtrInstancePtr,0x0);
XTmrCtr_Start(TmrCtrInstancePtr,0x0);

sqrt_val = f_arr[0];

// stop timer
XTmrCtr_Stop(TmrCtrInstancePtr,0x0);
u32 timervalue = XTmrCtr_GetValue(TmrCtrInstancePtr,0x0);

printf("Timer val = %d\n",(int)timervalue);

 

Execution time is 55 cycles - and this takes even some portions from the call for the timer start/stop here and the reading of the data from the float-arry - and assigning result to sqrt_val.

That means the total execution is around 30..35 cycles.

0 Kudos
Scholar drjohnsmith
Scholar
107 Views
Registered: ‎07-09-2009

Re: sqrtf() in Microblaze is taking huge time to execute.

that works for me..

 

<== If this was helpful, please feel free to give Kudos, and close if it answers your question ==>
0 Kudos