在softfolat中双精度浮点、单精度浮点对异常值的处理,代码是什么
时间: 2024-02-23 10:00:02 浏览: 125
单双精度浮点数解析.c
在softfloat中,双精度浮点数和单精度浮点数对异常值的处理方式是一致的,都是根据IEEE 754标准中定义的规则进行处理的。以下是softfloat中处理双精度浮点异常值的部分代码示例:
```c
/*----------------------------------------------------------------------------
| Handle invalid operation exception.
*----------------------------------------------------------------------------*/
void softfloat_raiseFlags( int32_t flags )
{
float_exception_flags |= flags;
}
/*----------------------------------------------------------------------------
| Raise the invalid operation exception for a NaN operand.
*----------------------------------------------------------------------------*/
void softfloat_raiseNaNFlags( uint_fast64_t uiA )
{
if ( softfloat_isSigNaNF64UI( uiA ) ) {
softfloat_raiseFlags( float_flag_invalid );
}
}
/*----------------------------------------------------------------------------
| Determine whether the result of a double addition is NaN.
*----------------------------------------------------------------------------*/
bool softfloat_isNaNF64UI( uint_fast64_t uiA )
{
return
( ( uiA & UINT64_C( 0x7FF0000000000000 ) ) == UINT64_C( 0x7FF0000000000000 ) )
&& ( uiA & UINT64_C( 0x0007FFFFFFFFFFFF ) );
}
/*----------------------------------------------------------------------------
| Perform a float-to-int conversion, rounding to nearest, with double rounding
| to even in the presence of a tie.
*----------------------------------------------------------------------------*/
int32_t softfloat_roundPackToF64( bool sign, int_fast16_t exp, uint_fast64_t sig )
{
int_fast8_t roundingMode;
bool roundNearEven;
int32_t absZ;
roundingMode = float_rounding_mode;
roundNearEven = ( roundingMode == float_round_nearest_even );
if ( 0x433 <= (uint16_t) exp ) {
if ( ( exp < 0 ) || ( sig | (UINT64_C( 0x0010000000000000 ) >> exp) ) ) {
softfloat_raiseFlags( float_flag_invalid );
return sign ? (int32_t) ui64_to_i64(UINT64_C( 0x8000000000000000 )) : 0x7FFFFFFF;
}
return sign ? (int32_t) ui64_to_i64(UINT64_C( 0x8000000000000000 )) : 0x7FFFFFFF;
}
if ( exp <= -0x3FE ) {
if ( exp < -0x433 ) {
softfloat_roundPackFloatToExtF80M(
sign, exp + 0x3FF, sig | UINT64_C( 0x0010000000000000 ), 0, &absZ );
return absZ;
}
return 0;
}
return
softfloat_roundPackMToF64(
sign, 0x3FF + exp, sig | UINT64_C( 0x0010000000000000 ), roundingMode, roundNearEven
);
}
```
这里只是部分代码示例,softfloat中对不同类型浮点数的异常值处理代码比较复杂,需要对整个库进行详细的分析和理解。
阅读全文