cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Explorer
Explorer
5,847 Views
Registered: ‎08-26-2014

Floating-point IP generated using HLS returning 0

Jump to solution

Hello,

 

I am implementing a very simple test IP making a multiplication of two 32-bit floating-point variables.

 

I have changed the IP to work with integers and it does work, but this is not the case when using floating-point variables.

 

I generate the block using Vivado HLS using an AXI-Lite interface. This is the code:

 

 

float mult(float a, float b)
{
#pragma HLS INTERFACE s_axilite port=b bundle=CTRLS
#pragma HLS INTERFACE s_axilite port=a bundle=CTRLS
#pragma HLS INTERFACE s_axilite port=return bundle=CTRLS

	return a*b;
}

 

Then, in Vivado I generate the Zynq system, add the IP, generate the bitstream and launch the SDK.

 

The code I am using in the SDK is the next (realize that I am considering the conversion from float to integer and viceversa before starting the operation in the IP):

 

 

#include <stdio.h>
#include "xparameters.h"
#include "xstatus.h"
#include "xmult.h"

XMult doMult;
XMult_Config *doMult_cfg;

unsigned int float_to_u32(float val)
{
	unsigned int result;
	union float_bytes {
		float v;
		unsigned char bytes[4];
	}data;
	data.v = val;

	result = (data.bytes[3] >> 24) + (data.bytes[2] << 16) + (data.bytes[1] << 8) + (data.bytes[0]);
	return result;
}

float u32_to_float(unsigned int val)
{
	union {
		float val_float;
		unsigned char bytes[4];
	} data;
	data.bytes[3] = (val >> (8*3)) & 0xff;
	data.bytes[2] = (val >> (8*2)) & 0xff;
	data.bytes[1] = (val >> (8*1)) & 0xff;
	data.bytes[0] = (val >> (8*0)) & 0xff;
	return data.val_float;
}

int main()
{
	int Status;
	float var = 1.0f, result;

	init_platform();

	doMult_cfg = XMult_LookupConfig(XPAR_MULT_0_DEVICE_ID);
	if (!doMult_cfg)
		printf("Error loading config for Mult_cfg\n");
	Status = XMult_CfgInitialize(&doMult, doMult_cfg);
	if (Status != XST_SUCCESS)
		printf("Error configuring doMult\n");

	while(1)
	{
		// Call Vivado HLS IP core

		// set inputs
		XMult_Set_a(&doMult,float_to_u32(var));
		XMult_Set_b(&doMult,float_to_u32(2.0f));

		// start execution
		XMult_Start(&doMult);
		while(XMult_IsDone(&doMult));

		result = u32_to_float(XMult_Get_return(&doMult));

		printf("var = %f - Result = %f\n",var, result);

		var++;

	}
	return 0;
}

 

What am I doing wrong?

 

Thanks in advance,

 

Cerilet

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Scholar
Scholar
10,549 Views
Registered: ‎04-26-2015

Re: Floating-point IP generated using HLS returning 0

Jump to solution

One issue is immediately apparent: in float_to_u32 you're shifting the top byte down 24 bits, rather than up 24 bits. Since it only has eight bits to begin with, this is going to produce eight zeros in the upper section.

 

 

 

View solution in original post

0 Kudos
4 Replies
Explorer
Explorer
5,834 Views
Registered: ‎08-26-2014

Re: Floating-point IP generated using HLS returning 0

Jump to solution

Ok, I found the solution.

 

I have used this code instead of the two functions posted before:

 

int main()
{

	int Status, uresult;
	float var1 = 1.0f, var2 = 2.0f, result;

	init_platform();

	xil_printf("Main\n\r");

	doMult_cfg = XMult_LookupConfig(XPAR_MULT_0_DEVICE_ID);
	if (!doMult_cfg)
		printf("Error loading config for Mult_cfg\n");
	Status = XMult_CfgInitialize(&doMult, doMult_cfg);
	if (Status != XST_SUCCESS)
		printf("Error configuring doMult\n");

	while(1)
	{
		// Call Vivado HLS IP core

		// set inputs
		XMult_Set_a(&doMult,*((u32*)&var1));
		XMult_Set_b(&doMult,*((u32*)&var2));

		// start execution
		XMult_Start(&doMult);
		while(XMult_IsDone(&doMult));

		uresult = XMult_Get_return(&doMult);
		result = *((float*)&uresult);

		printf("var = %f - Result = %f\n",var1, result);

		var1++;

	}
	return 0;
}

But I do not still understand why the other code does not work.

 

Thanks anyway,

 

Cerilet

Tags (4)
0 Kudos
Highlighted
Scholar
Scholar
10,550 Views
Registered: ‎04-26-2015

Re: Floating-point IP generated using HLS returning 0

Jump to solution

One issue is immediately apparent: in float_to_u32 you're shifting the top byte down 24 bits, rather than up 24 bits. Since it only has eight bits to begin with, this is going to produce eight zeros in the upper section.

 

 

 

View solution in original post

0 Kudos
Highlighted
Explorer
Explorer
5,791 Views
Registered: ‎08-26-2014

Re: Floating-point IP generated using HLS returning 0

Jump to solution
Oh my. Yes, that was the problem.

But I think I will use the version I added later using only pointers. It is more efficient.

Many thanks!
0 Kudos
Highlighted
Scholar
Scholar
5,779 Views
Registered: ‎04-26-2015

Re: Floating-point IP generated using HLS returning 0

Jump to solution

Yes, your new version is better. The only reason I'd use the old version would be to convert from a little-endian system to a big-endian one, or vice versa.