Floating Point Services¶
The kernel allows threads to use floating point registers on board configurations that support these registers.
Note
Floating point services are currently available only for boards based on ARM Cortex-M SoCs supporting the Floating Point Extension, the Intel x86 architecture and ARCv2 SoCs supporting the Floating Point Extension. The services provided are architecture specific.
The kernel does not support the use of floating point registers by ISRs.
Concepts¶
The kernel can be configured to provide only the floating point services required by an application. Three modes of operation are supported, which are described below. In addition, the kernel’s support for the SSE registers can be included or omitted, as desired.
No FP registers mode¶
This mode is used when the application has no threads that use floating point registers. It is the kernel’s default floating point services mode.
If a thread uses any floating point register, the kernel generates a fatal error condition and aborts the thread.
Implementation¶
Performing Floating Point Arithmetic¶
No special coding is required for a thread to use floating point arithmetic if the kernel is properly configured.
The following code shows how a routine can use floating point arithmetic to avoid overflow issues when computing the average of a series of integer values.
int average(int *values, int num_values)
{
double sum;
int i;
sum = 0.0;
for (i = 0; i < num_values; i++) {
sum += *values;
values++;
}
return (int)((sum / num_values) + 0.5);
}
Suggested Uses¶
Use the kernel floating point services when an application needs to perform floating point operations.
Configuration Options¶
To configure unshared FP registers mode, enable the CONFIG_FPU
configuration option and leave the CONFIG_FPU_SHARING
configuration
option disabled.
To configure shared FP registers mode, enable both the CONFIG_FPU
configuration option and the CONFIG_FPU_SHARING
configuration option.
Also, ensure that any thread that uses the floating point registers has
sufficient added stack space for saving floating point register values
during context switches, as described above.
Use the CONFIG_SSE
configuration option to enable support for
SSEx instructions (x86 only).
API Reference¶
-
group
float_apis
Functions
-
void
k_float_enable
(struct k_thread *thread, unsigned int options)¶ Enable preservation of floating point context information.
This routine informs the kernel that the specified thread (which may be the current thread) will be using the floating point registers. The options parameter indicates which floating point register sets will be used by the specified thread:
K_FP_REGS indicates x87 FPU and MMX registers only
K_SSE_REGS indicates SSE registers (and also x87 FPU and MMX registers)
Invoking this routine initializes the thread’s floating point context info to that of an FPU that has been reset. The next time the thread is scheduled by z_swap() it will either inherit an FPU that is guaranteed to be in a “sane” state (if the most recent user of the FPU was cooperatively swapped out) or the thread’s own floating point context will be loaded (if the most recent user of the FPU was preempted, or if this thread is the first user of the FPU). Thereafter, the kernel will protect the thread’s FP context so that it is not altered during a preemptive context switch.
- Warning
This routine should only be used to enable floating point support for a thread that does not currently have such support enabled already.
- Return
N/A
- Parameters
thread
: ID of thread.options
: Registers to be preserved (K_FP_REGS or K_SSE_REGS).
-
void