cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Observer
Observer
348 Views
Registered: ‎11-12-2018

Malfunction of ap_int on ofstream's insertion operator with hexadecimal format

I'm new to Vivado HLS and trying to write a test code, and I found a strange behavior about 'ap_int' as follows.

It makes inconvenience on writing test code, since I should handle a lot of long stream data with hex format.

 

// io manipulation error
int main(){
    using namespace std;
    int int_val = 256;
    ap_int<16> ap_int_val = int_val;
    cout << hex << setw(4) << setfill('0') << int_val    << endl; // 0100 -> OK
    cout << hex << setw(4) << setfill('0') << ap_int_val << endl; // 0x100 -> NG, setfill is ignored
}
// ofstream error
int main(){
    using namespace std;
    ofstream ofile("result.txt");
    int int_val = 256;
    ap_int<16> ap_int_val = int_val;
    ofile << hex << setw(4) << setfill('0') << int_val    << endl; // 0100 -> OK
    ofile << hex << setw(4) << setfill('0') << ap_int_val << endl; // 0256 -> NG, hex is ignored
    ofile.close();
}

 

I'm already aware that the reason is in the header files that are provided with Vivado HLS 2019.2 ('ap_int_base.h' and 'ap_private.h').

Is there anyone who knows if there is a plan to fix this problem, or good work around?

Thank you.

0 Kudos
4 Replies
Highlighted
Moderator
Moderator
291 Views
Registered: ‎11-21-2018

Hi @mash-knit 

Can you attach a testcase so I can look into the problem in more detail and rule out any issues in your code? If it is a bug, I can flag it with development. 

Regards, 

Aoife
Product Application Engineer - Xilinx Technical Support EMEA
**~ Don't forget to reply, give kudos, and accept as solution.~**
Highlighted
Scholar
Scholar
279 Views
Registered: ‎03-28-2016

@mash-knit ,

I suspect that this too simple of a fix to work, but have you tried casting the "ap_int" to an "int"?  Just a thought.

<< (int)ap_int_val 

 

Ted Booth | Tech. Lead FPGA Design Engineer | DesignLinx Solutions
https://www.designlinxhs.com
Highlighted
Observer
Observer
254 Views
Registered: ‎11-12-2018

Hi @aoifem 

Thank you for your reply.

I attached a VivadoHLS project, which has the test cases in it. And also, I added one more test case with my work-around function ('ap_int_to_hex.h'), to show my expectation.  Please see 'main.cpp' file. 

Thank you. 

0 Kudos
Highlighted
Observer
Observer
253 Views
Registered: ‎11-12-2018

Hi @tedbooth ,

Thank you for your suggestion!

I believe that casting to 'int' will work for many people, but unfortunately not for me. I want to dump 'ap_int<128>', 'ap_fixed<...>', ap_ufixed<...>', and so on. They have the same problem, and are not fit to 'int'.

0 Kudos