Nios II Custom Instruction User Guide

ID 683242
Date 4/27/2020
Public
Document Table of Contents

3.2. Built-in Functions and User-defined Macros

The Nios II processor uses GCC built-in functions to map to custom instructions.

By default, the integer type custom instruction is defined in a system.h file. However, by using built-in functions, software can use 32 bit non-integer types with custom instructions. Fifty-two built-in functions are available to accommodate the different combinations of supported types.

Built-in function names have the following format:

__builtin_custom_ <return type> n <parameter types>

<return type> and <parameter types> represent the input and output types, encoded as follows:

  • iint
  • ffloat
  • pvoid *
  • (empty)—void

The following example shows the prototype definitions for two built-in functions.

void __builtin_custom_nf (int n, float dataa);
float __builtin_custom_fnp (int n, void * dataa);

n is the selection index. The built-in function __builtin_custom_nf() accepts a float as an input, and does not return a value. The built-in function__builtin_custom_fnp() accepts a pointer as input, and returns a float.

To support non-integer input types, define macros with mnemonic names that map to the specific built-in function required for the application.

The following example shows user-defined custom instruction macros used in an application.

1. /* define void udef_macro1(float data);		*/
2. #define UDEF_MACRO1_N 0x00
3. #define UDEF_MACRO1(A) __builtin_custom_nf(UDEF_MACRO1_N, (A));
4. /* define float udef_macro2(void *data); */
5. #define UDEF_MACRO2_N 0x01
6. #define UDEF_MACRO2(B) __builtin_custom_fnp(UDEF_MACRO2_N, (B));
7. 
8. int main (void)
9. {
10. float a = 1.789;
11. float b = 0.0;
12. float *pt_a = &a;
13. 
14. UDEF_MACRO1(a);
15. b = UDEF_MACRO2((void *)pt_a);
16. return 0;
17. }
	 

On lines 2 through 6, the user-defined macros are declared and mapped to the appropriate built-in functions. The macro UDEF_MACRO1() accepts a float as an input parameter and does not return anything. The macro UDEF_MACRO2() accepts a pointer as an input parameter and returns a float. Lines 14 and 15 show code that uses the two user-defined macros.