Professional Documents
Culture Documents
Tutorial
TU-N2FLTNGPNT-2.0
Copyright 2010 Altera Corporation. All rights reserved. Altera, The Programmable Solutions Company, the
stylized Altera logo, and specific device designations are trademarks and/or service marks of Altera Corporation
in the U.S. and other countries. All other words and logos identified as trademarks and/or service marks are the
property of Altera Corporation or their respective owners. Altera products are protected under numerous U.S. and
foreign patents and pending applications, maskwork rights, and copyrights. Altera warrants performance of its
semiconductor products to current specifications in accordance with Altera's standard warranty, but reserves the
right to make changes to any products and services at any time without notice. Altera assumes no responsibility or
liability arising out of the application or use of any information, product, or service described herein except as
expressly agreed to in writing by Altera. Altera customers are advised to obtain the latest version of device
specifications before relying on any published information and before placing orders for products or services.
Contents
Additional Information
Document Revision History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info1
How to Contact Altera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info1
Typographic Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Info1
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
iv Contents
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
1. Floating-Point Custom Instructions
This tutorial guides you through the basics elements of floating-point custom
instructions for use with the Nios II embedded processor. Nios II floating-point
custom instructions accelerate arithmetic functions executed on float variable types.
The tutorial is a good starting point if you are considering floating-point custom
instructions for inclusion in your own project. In this tutorial you add the floating-
point custom instructions to a Nios II example design, and create a software program
to demonstrate floating-point performance.
The tutorial covers the following topics:
How to add floating-point custom instructions to a Nios II processor
How to use floating-point custom instructions in a C program, and how the
custom instructions work with the Nios II Embedded Design Suite (EDS)
The advantages and disadvantages of floating-point custom instructions, and how
best to use them in your own system
When the floating-point custom instructions are present in your target hardware, the
Nios II Software Build Tools (SBT) for Eclipse compiles your code to use the custom
instructions for floating-point operations, including the four primitive arithmetic
operations (addition, subtraction, multiplication and division) and the ANSI C math
library. Table 12 on page 18 lists the ANSI C math functions.
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
12 Chapter 1: Floating-Point Custom Instructions
Preparing Your System
f The floating point custom instructions substantially comply with the IEEE 754-1985
floating point standard. For details, refer to Floating Point Instructions in the
Processor Architecture chapter of the Nios II Processor Reference Handbook.
Prerequisites
To make effective use of this tutorial, you should be familiar with the following topics:
Defining and generating Nios II hardware systems with SOPC Builder
Compiling Nios II hardware systems with the Quartus II development software
Creating, compiling, and running Nios II software projects
f To learn about defining, generating, and compiling Nios II systems, refer to the Nios II
Hardware Development Tutorial. To learn about Nios II software projects, refer to the
Getting Started with the Graphical User Interface chapter of the Nios II Software
Developers Handbook.
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
Chapter 1: Floating-Point Custom Instructions 13
Building and Programming the Hardware
f Altera provides several working Nios II reference designs which you can use as a
starting point for your own designs. After installing the Nios II EDS, refer to the
<Nios II EDS install path>/examples/verilog or the <Nios II EDS install
path>/examples/vhdl directory. Demonstration applications are also available in
newer development kit installations.
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
14 Chapter 1: Floating-Point Custom Instructions
Building and Running the Software
7. Click Finish to exit the Nios II Floating Point Hardware dialog box. Figure 11
shows the Custom Instructions page of the Nios II MegaWizard interface with the
floating-point hardware inserted.
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
Chapter 1: Floating-Point Custom Instructions 15
Building and Running the Software
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
16 Chapter 1: Floating-Point Custom Instructions
Building and Running the Software
1 You might have different speed results, depending on your target hardware
and on the actual values of the random operands.
f The tutorial software uses the Nios II performance counter component to collect
timing information on the floating-point operations. For more information, refer to
the Performance Counter Core chapter in volume 5 of the Quartus II Handbook.
Software Implementation
The tutorial software uses #pragma directives to compare hardware and software
implementations of the floating-point instructions.
The following #pragmas direct the Nios II compiler to ignore the floating-point
instructions and generate software implementations:
#pragma no_custom_faddsforces software implementation of floating-point
add
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
Chapter 1: Floating-Point Custom Instructions 17
Moving On to Your Own System
Can you use integer arithmetic? While the floating-point custom instructions are
faster than software-implemented floating-point, they are slower than integer
arithmetic. A common integer technique is to represent numerical values with an
implicit scaling factor. As a simple example, if you are calculating milliamperes,
you might represent your values internally as microamperes.
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
18 Chapter 1: Floating-Point Custom Instructions
Moving On to Your Own System
Are you taking full advantage of compiler optimization? You can increase the
Nios II compiler optimization level through the Properties dialog box of your
Nios II application and BSP projects.
When you omit the floating-point divide instruction, the Nios II SBT for Eclipse
implements floating-point division in software.
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
Chapter 1: Floating-Point Custom Instructions 19
Moving On to Your Own System
f For information about selecting the floating-point division hardware with the Nios II
MegaWizard interface, refer to the Instantiating the Nios II Processor in SOPC Builder
chapter of the Nios II Processor Reference Handbook.
Simulation
You can use the floating-point custom instructions with the ModelSim hardware
simulator.
1 Depending on the Quartus II routing and fitting, resource usage in your project might
differ considerably from the values shown in Table 13.
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
110 Chapter 1: Floating-Point Custom Instructions
Moving On to Your Own System
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation
Additional Information
Contact
Contact (1) Address
Method
Technical support Website www.altera.com/support
Website www.altera.com/training
Technical training
Email custrain@altera.com
Product literature Website www.altera.com/literature
Non-technical support (General) Email nacomp@altera.com
(Software Licensing) Email authorization@altera.com
Note to Table:
(1) You can also contact your local Altera sales office or sales representative.
Typographic Conventions
The following table shows the typographic conventions this document uses.
February 2010 Altera Corporation Using Nios II Floating-Point Custom Instructions Tutorial
Info2 Additional Information
Typographic Conventions
Using Nios II Floating-Point Custom Instructions Tutorial February 2010 Altera Corporation