cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Contributor
Contributor
1,093 Views
Registered: ‎11-10-2017

Is implementation of lgamma() and tgamma() in HLS math library wrong?

Jump to solution

From this post. (https://forums.xilinx.com/t5/Vitis-SDAccel-and-SDSoC/SW-Emulation-Error-using-hls-math-function-in-C-Kernel-on/m-p/1036029)

In my development on SDAccel, I thought hls::lgamma() and hls::tgamma() does not calculate correct value.

In HLS use guide (UG902 v2019.1) , the limitation for input argument is as follows.

Fixed-Point Math Functions
Fixed-point implementations are also provided for the following math functions.
All fixed-point math functions support ap_[u]fixed and ap_[u]int data types with following bitwidth specification,
1. ap_fixed<W,I> where I<=33 and W-I<=32
2. ap_ufixed<W,I> where I<=32 and W-I<=32
3. ap_int<I> where I<=33
4. ap_uint<I> where I<=32

 

I have tested these functions on Vivado HLS 2019.1.

 

hls_result.png

Case1 and Case 3 is incorrect. only case2 is correct. 

Although both ap_fixed<32,10> and ap_fixed<10.5> satisfies the input argument limitation, I can't obtain correct result..

Any ideas? Thanks in advance.

0 Kudos
Reply
1 Solution

Accepted Solutions
Contributor
Contributor
946 Views
Registered: ‎11-10-2017

I realized that the implementation of "hls_lgamma_apfixed.h" is ported from original lgamma() compatible for IEE754.

The original source code is here.

https://github.com/JuliaMath/openlibm/blob/master/src/e_lgamma_r.c

And In this implementation, when x < 8.0, y is fractional part of x.

In HLS library code, i is the integer part of x, so y is the value obtained by subtracting i from x. I don't know why, but this code is commented out by default.(L264-266).

I could not understand what is doing in L255-262. I commented out this part, and uncomment L264-266. This works fine. I made my own my_lgamma() function because original source code is read-only.

 

View solution in original post

0 Kudos
Reply
2 Replies
Contributor
Contributor
958 Views
Registered: ‎11-10-2017

I am continuing to investigate this problem.

When to run lgamma(2.2), internal function lgamma_generic() is executed.

The implementation is in /tools/Xilinx/Vivado/2019.1/include/hls_lgamma_apfixed.h.

The source code of lgamma approximation is a little complicated, but I found the code block to be executed.

Here is the source code.

SnapCrab_NoName_2019-10-28_3-29-55_No-00.png

I still don't understand this algorighm, but I found suspicious code.

In Line 258, pos-51 is always negative value.

But the type of ys.exp is ap_uint<11>. Assigning negative value to ys.exp means assign INF or NaN to ys. By this code, the warning "assign NaN to fixed point value" is showed in the console, and the result is wrong.

The declaration of fp_struct is as the following. 

SnapCrab_NoName_2019-10-28_3-30-49_No-00.png

Is this a bug? or Is my understanding wrong?

0 Kudos
Reply
Contributor
Contributor
947 Views
Registered: ‎11-10-2017

I realized that the implementation of "hls_lgamma_apfixed.h" is ported from original lgamma() compatible for IEE754.

The original source code is here.

https://github.com/JuliaMath/openlibm/blob/master/src/e_lgamma_r.c

And In this implementation, when x < 8.0, y is fractional part of x.

In HLS library code, i is the integer part of x, so y is the value obtained by subtracting i from x. I don't know why, but this code is commented out by default.(L264-266).

I could not understand what is doing in L255-262. I commented out this part, and uncomment L264-266. This works fine. I made my own my_lgamma() function because original source code is read-only.

 

View solution in original post

0 Kudos
Reply