Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
10 years ago

how to use the floating point hardware in nios II custom instructions

hi

i want to compare the speed difference of floating point calc between custom instruction floating point and software.

this is my code:# include <stdio.h># include <altera_avalon_pio_regs.h># include "altera_avalon_pio_regs.h"# include "system.h"

int main()

{

printf("Hello from Nios II!\n");

float a=3.1415926;

float b=1.2578;

float c=0;

while(1)

{

IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE,0xf);

c=a*b;

IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE,0x0);

c=a-b;

IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE,0xf);

c=b-a;

IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE,0x0);

}

return 0;

}

in the code,i use pio to estimate the calc time roughly,

firstly, i delete the floating point hardware, generate the hdl, compile the quartus project, build the software, then i find the calc time is 2.3us(the sys clk is 100M);

then, i add the floating point hardware, and redo these steps, i find the calc time is still 2.3us

i guess the nios did not use the floating point hardware, how can i fix this problem?

is there any examples to show me how to use it?

thanks!

14 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    thank you very much!

    when i change the system.h file, the objdump file becomes:

    c=a*b;

    1082c0: e13ffd17 ldw r4,-12(fp)

    1082c4: e17ffe17 ldw r5,-8(fp)

    1082c8: 01083c00 call 1083c0 <__mulsf3>

    1082cc: 1007883a mov r3,r2

    1082d0: e0ffff15 stw r3,-4(fp)

    c=ALT_CI_NIOS_CUSTOM_INSTR_FLOATING_POINT_0(0,b,a); //*

    10832c: e0bffe17 ldw r2,-8(fp)

    108330: e0fffd17 ldw r3,-12(fp)

    108334: 10c5ff32 custom 252,r2,r2,r3

    108338: e0bfff15 stw r2,-4(fp)

    and the c=ALT_CI_NIOS_CUSTOM_INSTR_FLOATING_POINT_0(0,b,a); return the right result, and the calc time is much lesss than the c=a*b

    how do you know to change the inii to fnff in system.h file?

    i am new to altera and nios, i want to learn more.

    it seems like my project still has some problems, but at least i know how to use the floating point hardware in nios II custom instructions

    but i still do not understand why the system.h in my project have to change manually? and why i can not use c=a*b.

    may be i can try floating point hardware2.

    thanks again!
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I think somehow the following lines are not included into your public.mk file found in your BSP folder:

    # Hardware Floating Point Custom Instruction without Divider present.

    ALT_CFLAGS += -mcustom-fpu-cfg=60-1

    ALT_LDFLAGS += -mcustom-fpu-cfg=60-1

    What Quartus version are you using? I guess you could open nios2-bsp-editor in the BSP folder and add "-mcustom-fpu-cfg=60-1" into the bsp_cflags_user_user_flags under main>>advanced tab.

    Or try to create a new BSP......

    The Altera Nios II GCC supports the following builtin functions: https://gcc.gnu.org/onlinedocs/gcc/altera-nios-ii-built-in-functions.html

    I guess I am a curious person that likes to try to understand things.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    this is what my public.mk shows :

    # Hardware Divider present.

    # setting HARDWARE_DIVIDE is false

    ALT_CFLAGS += -mno-hw-div

    # Hardware Multiplier present.

    # setting HARDWARE_MULTIPLY is true

    ALT_CFLAGS += -mhw-mul

    # Hardware Mulx present.

    # setting HARDWARE_MULX is false

    ALT_CFLAGS += -mno-hw-mulx

    ......

    # Enable BSP generation to query if SOPC system floating point custom

    # instruction with a divider is present. If true ignores export of 'ALT_CFLAGS

    # += -mcustom-fpu-cfg=60-2' and 'ALT_LDFLAGS += -mcustom-fpu-cfg=60-2' to

    # public.mk if the custom instruction is found in the system. none

    # setting hal.make.ignore_system_derived.hardware_fp_cust_inst_divider_present is false

    there are no

    "# Hardware Floating Point Custom Instruction without Divider present.

    ALT_CFLAGS += -mcustom-fpu-cfg=60-1

    ALT_LDFLAGS += -mcustom-fpu-cfg=60-1"

    in my project , the bsp editor=>settings=>advanced=>hal.make.ignore_system_derived

    i found the "hardware_fp_cust_inst_no_divider_present" is checked,

    i think maybe that is the reason, so i uncheck it, then regenerate the bsp

    rebuild the project, change the system.h again(the soft ware generate the "__builtin_custom_inii" again, i have to change it to "__builtin_custom_fnff" manually)

    then i find in objdump file:

    c=a*b;

    1082c0: e0fffd17 ldw r3,-12(fp)

    1082c4: e0bffe17 ldw r2,-8(fp)

    1082c8: 1885ff32 custom 252,r2,r3,r2

    1082cc: e0bfff15 stw r2,-4(fp)

    so problem solved!

    so it seems like if i want to use the floating point hardware in nios II custom instructions, i have to add the ip in Qsys=>change the bsp setting=>change the system.h

    why the user guide did tell me? "tt_floating_point_custom_instructions.pdf"