Calculate square root, signed square root, or reciprocal of square root
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations
TheSqrtblock calculates the square root, signed square root, or reciprocal of square root on the input signal. Select one of the following functions from theFunctionparameter list.
Function | Description | Mathematical Expression | MATLAB®Equivalent |
---|---|---|---|
sqrt |
Square root of the input |
|
sqrt |
signedSqrt |
Square root of the absolute value of the input, multiplied by the sign of the input |
|
— |
rSqrt |
Reciprocal of the square root of the input |
|
— |
The block icon changes to match the function.
Port_1
— Input signalInput signal to the block to calculate the square root, signed square root, or reciprocal of square root. Thesqrt
function accepts real or complex inputs, except for complex fixed-point signals.signedSqrt
andrSqrt
do not accept complex inputs. The input signal must be a floating point number.
This table summarizes the support for complex types and negative values for floating point, integer, and fixed-point data types forsqrt
,rSqrt
, andsignedSqrt
functions.
Function | Data Type | Complex | Negative Values | |
---|---|---|---|---|
Input | Output | |||
sqrt |
Floating point | Yes | Yes | Yes |
Integer and fixed-point | No | No | No | |
|
Floating point | No | No | Yes |
Integer and fixed-point | No | No | No | |
signedSqrt |
Floating point | No | Yes | Yes |
Integer and fixed-point | No | No | No |
If the input is negative, set theOutput signalto complex for all functions exceptsignedSqrt
.
Data Types:single
|double
|half
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|fixed point
Port_1
— Output signalOutput signal that is the square root, signed square root, or reciprocal of square root of the input signal. When the input is an integer or fixed-point type, the output must be floating point.
Data Types:single
|double
|half
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|fixed point
Function
— Function the block performssqrt
(default) |signedSqrt
|rSqrt
Specify the mathematical function that the block calculates. The block icon changes to match the function you select.
Function | Block Icon |
---|---|
sqrt |
|
signedSqrt |
|
rSqrt |
When this parameter is set tosignedSqrt
, theIntermediate results data typeparameter is disabled.
Block Parameter:Operator |
Type: character vector |
Values:'sqrt' |'signedSqrt' |'rSqrt' |
Default:'sqrt' |
Output signal type
— Output signal typeauto
(default) |real
|complex
Specify the output signal type of the block.
Function | Input Signal Type | Output Signal Type | ||
---|---|---|---|---|
Auto | Real | Complex | ||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Block Parameter:OutputSignalType |
Type: character vector |
Values:'auto' |'real' |'complex' |
Default:'auto' |
Sample time
——指定样本以外的价值-1
-1
(default) | scalar | vectorSpecify the sample time as a value other than -1. For more information, seeSpecify Sample Time.
This parameter is not visible unless it is explicitly set to a value other than-1
. To learn more, seeBlocks for Which Sample Time Is Not Recommended.
Block Parameter:SampleTime |
Type:character vector |
Values:scalar or vector |
Default:'-1' |
Method
— Method to compute reciprocal of square rootExact
(default) |Newton-Raphson
Specify the method for computing the reciprocal of a square root. This parameter is only valid for therSqrt
function.
Method | Data Types Supported | When to Use This Method |
---|---|---|
Exact |
Floating point |
You do not want an approximation. Note The input or output must be floating point. |
Newton-Raphson |
Floating-point, fixed-point, and built-in integer types |
You want a fast, approximate calculation. |
TheExact
method provides results that are consistent with MATLAB computations.
Note
The algorithms forsqrt
andsignedSqrt
are always ofExact
type, no matter what selection appears on the block dialog box.
Block Parameter:AlgorithmType |
Type: character vector |
Values:'Exact' |'Newton-Raphson' |
Default:'Exact' |
Number of iterations
— Number of iterations used for Newton Raphson algorithm3
(default) | integerSpecify the number of iterations to perform the Newton-Raphson algorithm. This parameter is valid with therSqrt
function and theNewton-Raphson
value forMethod.
Note
If you enter 0, the block output is the initial guess of the Newton-Raphson algorithm.
Block Parameter:Iterations |
Type: character vector |
Values: integer |
Default:'3' |
Click theShow data type assistantbuttonto display theData Type Assistant, which helps you set the data type attributes. For more information, seeSpecify Data Types Using Data Type Assistant.
Intermediate results data type
— Data type of intermediate results继承:Inherit via internal rule
(default) |继承:Inherit from input
|继承:Inherit from output
|double
|single
|int8
|uint8
|int16
|uint16
|int32
|uint32
|int64
|uint64
|fixdt(1,16,,0)
|fixdt(1,16,2^0,0)
|
Specify the data type for intermediate results when you setFunctiontosqrt
orrSqrt
on theMainpane.
可以inhe类型rited, specified directly, or expressed as a data type object such asSimulink.NumericType
.
Note
To avoid overflow, the intermediate data type must be larger than or equal to a data type that can contain the square of the output data type.
Follow these guidelines on setting an intermediate data type explicitly for the square root function,sqrt
:
Input and Output Data Types | Intermediate Data Type |
---|---|
Input or output is double. | Use double. |
Input or output is single, and any non-single data type isnotdouble. | Use single or double. |
Input and output are fixed point. | Use fixed point. |
Follow these guidelines on setting an intermediate data type explicitly for the reciprocal square root function,rSqrt
:
Input and Output Data Types | Intermediate Data Type |
---|---|
Input is double and output is not single. | Use double. |
Input is not single and output is double. | Use double. |
Input and output are fixed point. | Use fixed point. |
Caution
Do not setIntermediate results data typeto继承:Inherit from output
when:
You selectNewton-Raphson
to compute the reciprocal of a square root.
The input data type is floating point.
The output data type is fixed point.
Under these conditions, selecting继承:Inherit from output
yields suboptimal performance and produces an error.
To avoid this error, convert the input signal from a floating-point to fixed-point data type. For example, insert aData Type Conversionblock in front of the Sqrt block to perform the conversion.
This parameter is disabled when theFunctionparameter is set tosignedSqrt
.
Block Parameter:IntermediateResultsDataTypeStr |
Type: character vector |
Values:'Inherit: Inherit via internal rule' |'Inherit: Inherit from input' |'Inherit: Inherit from output' |'double' |'single' ,'int8' ,'uint8' ,int16 ,'uint16' ,'int32' ,'uint32' ,'int64' ,'uint64' ,fixdt(1,16,0) ,fixdt(1,16,2^0,0) .'' |
Default:'Inherit: Inherit via internal rule' |
Output
— Output data type继承:Same as first input
(default) |继承:Inherit via internal rule
|继承:Inherit via back propagation
|double
|single
|half
|int8
|int32
|uint32
|int64
|uint64
|fixdt(1,16,2^0,0)
|
| ...指定输出数据类型。可以inhe类型rited, specified directly, or expressed as a data type object such asSimulink.NumericType
.
When input is a floating-point data type smaller than single precision, the继承:Inherit via internal rule
output data type depends on the setting of theInherit floating-point output type smaller than single precisionconfiguration parameter. Data types are smaller than single precision when the number of bits needed to encode the data type is less than the 32 bits needed to encode the single-precision data type. For example,half
andint16
are smaller than single precision.
Block Parameter:OutDataTypeStr |
Type: character vector |
Values:'Inherit: Inherit via internal rule' |'Inherit: Inherit via back propagation' |'Inherit: Same as first input' |'double' |'single' |'half' |'int8' |'uint8' |int16 |'uint16' |'int32' |'uint32' |'int64' |'uint64' |fixdt(1,16,0) |fixdt(1,16,2^0,0) |fixdt(1,16,2^0,0) |'' |
Default:'Inherit: Same as first input' |
Minimum
——最小输出值范围检查[]
(default) | scalarSpecify the lower value of the output range that Simulink®checks as a finite, real, double, scalar value.
Note
If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, seeSimulink.BusElement
.
Simulink uses the minimum to perform:
Parameter range checking (seeSpecify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (seeSpecify Signal RangesandEnable Simulation Range Checking).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, seeOptimize using the specified minimum and maximum values(Embedded Coder).
Note
Output minimumdoes not saturate or clip the actual output signal. Use theSaturationblock instead.
Block Parameter:OutMin |
Type: character vector |
Values: scalar |
Default:'[ ]' |
Maximum
— Maximum output value for range checking[]
(default) | scalarSpecify the upper value of the output range that Simulink checks as a finite, real, double, scalar value.
Note
If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, seeSimulink.BusElement
.
Simulink uses the maximum value to perform:
Parameter range checking (seeSpecify Minimum and Maximum Values for Block Parameters) for some blocks.
Simulation range checking (seeSpecify Signal RangesandEnable Simulation Range Checking).
Automatic scaling of fixed-point data types.
Optimization of the code that you generate from the model. This optimization can remove algorithmic code and affect the results of some simulation modes such as SIL or external mode. For more information, seeOptimize using the specified minimum and maximum values(Embedded Coder).
Note
Output maximumdoes not saturate or clip the actual output signal. Use theSaturationblock instead.
Block Parameter:OutMax |
Type: character vector |
Values: scalar |
Default:'[ ]' |
Integer rounding mode
— Rounding mode for fixed-point operationsFloor
(default) |Ceiling
|Convergent
|Nearest
|Round
|Simplest
|Zero
Specify the rounding mode for fixed-point operations.For more information, seeRounding(Fixed-Point Designer).
Block Parameter:RndMeth |
Type:character vector |
Values:'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero' |
Default:'Floor' |
Lock output data type setting against changes by the fixed-point tools
— Prevent fixed-point tools from overriding data typesoff
(default) |on
Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, seeUse Lock Output Data Type Setting(Fixed-Point Designer).
Block Parameter:LockScale |
Type: character vector |
Values:'off' |'on' |
Default:'off' |
Saturate on integer overflow
— Choose the behavior when integer overflow occursoff
(default) |on
Action | Reasons for Taking This Action | What Happens for Overflows | Example |
---|---|---|---|
Select this check box. |
Your model has possible overflow, and you want explicit saturation protection in the generated code. |
Overflows saturate to either the minimum or maximum value that the data type can represent. |
The maximum value that the |
Do not select this check box. |
You want to optimize efficiency of your generated code. You want to avoid overspecifying how a block handles out-of-range signals. For more information, seeTroubleshoot Signal Range Errors. |
Overflows wrap to the appropriate value that is representable by the data type. |
The maximum value that the |
When you select this check box, saturation applies to every internal operation on the block, not just the output or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.
Block Parameter:DoSatur |
Type: character vector |
Value:'off' |'on' |
Default:'off' |
Data Types |
|
Direct Feedthrough |
|
多维信号 |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.
For theSqrtblock withFunctionset tosqrt
, the code generator supports various architectures and data types.
Thesqrtfunction
architecture supports code generation for fixed-point types and floating-point types. When you use floating-point types, setFloating Point IP Library(HDL Coder)toNative Floating Point
. You can specify theLatencyStrategyandCustomLatencyHDL properties to choose from a range of frequency values when targeting your design on the hardware platform.
Use theUseMultiplierHDL block property in combination with theLatencyStrategyandCustomLatencyproperties to specify whether to compute the square root by using a pipelined shift and add or multiplication algorithm
For this architecture, you can specify theHandleDenormalsandLatencyStrategysettings from theNative Floating Pointtab in the HDL Block Properties dialog box.
Architecture | Fixed-Point | Native Floating-Point | HandleDenormals | LatencyStrategy |
---|---|---|---|---|
sqrtfunction |
✓ | ✓ | ✓ | ✓ |
sqrtnewton |
✓ | — | — | — |
sqrtnewtonsinglerate |
✓ | — | — | — |
recipsqrtnewton |
✓ | — | — | — |
recipsqrtnewtonsinglerate |
✓ | — | — | — |
This block has multi-cycle implementations that introduce additional latency in the generated code. To see the added latency, view the generated model or validation model. SeeGenerated Model and Validation Model(HDL Coder).
Architecture | Parameter | Additional cycles of latency | Description |
---|---|---|---|
SqrtFunction (default) |
|
依赖s on parameter choices, output word length, and input and output fraction lengths. |
To specify this architecture, setFunctionto Compute the square root by using a pipelined shift/addition algorithm or multiplication-based algorithm. The To see the latency calculation, at the MATLAB command prompt, enter: HDLMathLib Improve design frequency and reduce resource utilization by setting theUseMultiplierto |
SqrtNewton |
Iterations |
Iterations + 3 |
To specify this architecture, setFunctionto Use the iterative Newton method. Select this option to optimize area. The default value for The recommended value for |
SqrtNewtonSingleRate |
Iterations |
(Iterations * 4) + 6 |
To specify this architecture, setFunctionto Use the single rate pipelined Newton method. Select this option to optimize speed, or if you want a single rate implementation. The default value for The recommended value for |
RecipSqrtNewton |
Iterations |
Iterations + 2 |
To specify this architecture, setFunctionto Use the iterative Newton method. Select this option to optimize area. |
RecipSqrtNewtonSingleRate |
Iterations |
(Iterations * 4) + 5 |
To specify this architecture, setFunctionto Use the single rate pipelined Newton method. Select this option to optimize speed, or if you want a single rate implementation. |
The Newton-Raphson iterative method:
ReciprocalRsqrtBasedNewton
andReciprocalRsqrtBasedNewtonSingleRate
implement the Newton-Raphson method with:
General | |
---|---|
ConstrainedOutputPipeline | Number of registers to place at the outputs by moving existing delays within your design. Distributed pipelining does not redistribute these registers. The default is |
Iterations | Number of iterations for |
InputPipeline | Number of input pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is |
OutputPipeline | Number of output pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is |
UseMultiplier | Select algorithm for Increase design frequency and reduce resource utilization by setting theUseMultiplierto |
LatencyStrategy | Specify whether to map the blocks in your design to Increase design frequency and reduce resource utilization by setting theUseMultiplierto |
CustomLatency | WhenLatencyStrategyis set to |
Native Floating Point | |
---|---|
HandleDenormals | Specify whether you want HDL Coder to insert additional logic to handle denormal numbers in your design. Denormal numbers are numbers that have magnitudes less than the smallest floating-point number that can be represented without leading zeros in the mantissa. The default is |
Input must be an unsigned scalar value.
Output is a fixed-point scalar value.
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
Selectweb siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.