4.3 十进制浮点模拟例程
软件十进制浮点库实现了IEEE 754-2008十进制浮点算术运算,并且只在选定的目标机上起作用。
软件十进制浮点库支持DPD 编码(Densely Packed Decimal,密集十进制数) 或BID编码(Binary Integer Decimal,用二进制整数表示的十进制数),可以在配置时进行选择。
4.3.1 算术函数
— Runtime Function: _Decimal32
__dpd_addsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal32
__bid_addsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal64
__dpd_adddd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal64
__bid_adddd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal128
__dpd_addtd3 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: _Decimal128
__bid_addtd3 (
_Decimal128 a, _Decimal128 b)
这些函数返回a和b的和。
— Runtime Function: _Decimal32
__dpd_subsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal32
__bid_subsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal64
__dpd_subdd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal64
__bid_subdd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal128
__dpd_subtd3 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: _Decimal128
__bid_subtd3 (
_Decimal128 a, _Decimal128 b)
这些函数返回a和b的差,也就是a - b。
— Runtime Function: _Decimal32
__dpd_mulsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal32
__bid_mulsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal64
__dpd_muldd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal64
__bid_muldd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal128
__dpd_multd3 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: _Decimal128
__bid_multd3 (
_Decimal128 a, _Decimal128 b)
这些函数返回a和b的积。
— Runtime Function: _Decimal32
__dpd_divsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal32
__bid_divsd3 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: _Decimal64
__dpd_divdd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal64
__bid_divdd3 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: _Decimal128
__dpd_divtd3 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: _Decimal128
__bid_divtd3 (
_Decimal128 a, _Decimal128 b)
这些函数返回a和b的商,也就是a / b。
— Runtime Function: _Decimal32
__dpd_negsd2 (
_Decimal32 a)
— Runtime Function: _Decimal32
__bid_negsd2 (
_Decimal32 a)
— Runtime Function: _Decimal64
__dpd_negdd2 (
_Decimal64 a)
— Runtime Function: _Decimal64
__bid_negdd2 (
_Decimal64 a)
— Runtime Function: _Decimal128
__dpd_negtd2 (
_Decimal128 a)
— Runtime Function: _Decimal128
__bid_negtd2 (
_Decimal128 a)
这些函数返回a的负数。它们只是简单的反转符号位,因此能够产生负0和负NaN。
4.3.2 转换函数
— Runtime Function: _Decimal64
__dpd_extendsddd2 (
_Decimal32 a)
— Runtime Function: _Decimal64
__bid_extendsddd2 (
_Decimal32 a)
— Runtime Function: _Decimal128
__dpd_extendsdtd2 (
_Decimal32 a)
— Runtime Function: _Decimal128
__bid_extendsdtd2 (
_Decimal32 a)
— Runtime Function: _Decimal128
__dpd_extendddtd2 (
_Decimal64 a)
— Runtime Function: _Decimal128
__bid_extendddtd2 (
_Decimal64 a)
— Runtime Function: _Decimal32
__dpd_truncddsd2 (
_Decimal64 a)
— Runtime Function: _Decimal32
__bid_truncddsd2 (
_Decimal64 a)
— Runtime Function: _Decimal32
__dpd_trunctdsd2 (
_Decimal128 a)
— Runtime Function: _Decimal32
__bid_trunctdsd2 (
_Decimal128 a)
— Runtime Function: _Decimal64
__dpd_trunctddd2 (
_Decimal128 a)
— Runtime Function: _Decimal64
__bid_trunctddd2 (
_Decimal128 a)
这些函数将a的值从十进制浮点类型转换为其它类型。
— Runtime Function: _Decimal64
__dpd_extendsfdd (
float a)
— Runtime Function: _Decimal64
__bid_extendsfdd (
float a)
— Runtime Function: _Decimal128
__dpd_extendsftd (
float a)
— Runtime Function: _Decimal128
__bid_extendsftd (
float a)
— Runtime Function: _Decimal128
__dpd_extenddftd (
double a)
— Runtime Function: _Decimal128
__bid_extenddftd (
double a)
— Runtime Function: _Decimal128
__dpd_extendxftd (
long double a)
— Runtime Function: _Decimal128
__bid_extendxftd (
long double a)
— Runtime Function: _Decimal32
__dpd_truncdfsd (
double a)
— Runtime Function: _Decimal32
__bid_truncdfsd (
double a)
— Runtime Function: _Decimal32
__dpd_truncxfsd (
long double a)
— Runtime Function: _Decimal32
__bid_truncxfsd (
long double a)
— Runtime Function: _Decimal32
__dpd_trunctfsd (
long double a)
— Runtime Function: _Decimal32
__bid_trunctfsd (
long double a)
— Runtime Function: _Decimal64
__dpd_truncxfdd (
long double a)
— Runtime Function: _Decimal64
__bid_truncxfdd (
long double a)
— Runtime Function: _Decimal64
__dpd_trunctfdd (
long double a)
— Runtime Function: _Decimal64
__bid_trunctfdd (
long double a)
这些函数将的值从a二进制浮点类型转换为不同大小(size)的十进制浮点类型。
— Runtime Function: float
__dpd_truncddsf (
_Decimal64 a)
— Runtime Function: float
__bid_truncddsf (
_Decimal64 a)
— Runtime Function: float
__dpd_trunctdsf (
_Decimal128 a)
— Runtime Function: float
__bid_trunctdsf (
_Decimal128 a)
— Runtime Function: double
__dpd_extendsddf (
_Decimal32 a)
— Runtime Function: double
__bid_extendsddf (
_Decimal32 a)
— Runtime Function: double
__dpd_trunctddf (
_Decimal128 a)
— Runtime Function: double
__bid_trunctddf (
_Decimal128 a)
— Runtime Function: long double
__dpd_extendsdxf (
_Decimal32 a)
— Runtime Function: long double
__bid_extendsdxf (
_Decimal32 a)
— Runtime Function: long double
__dpd_extendddxf (
_Decimal64 a)
— Runtime Function: long double
__bid_extendddxf (
_Decimal64 a)
— Runtime Function: long double
__dpd_trunctdxf (
_Decimal128 a)
— Runtime Function: long double
__bid_trunctdxf (
_Decimal128 a)
— Runtime Function: long double
__dpd_extendsdtf (
_Decimal32 a)
— Runtime Function: long double
__bid_extendsdtf (
_Decimal32 a)
— Runtime Function: long double
__dpd_extendddtf (
_Decimal64 a)
— Runtime Function: long double
__bid_extendddtf (
_Decimal64 a)
这些函数将a的值从十进制浮点类型转换为不同大小的二进制浮点类型。
— Runtime Function: _Decimal32
__dpd_extendsfsd (
float a)
— Runtime Function: _Decimal32
__bid_extendsfsd (
float a)
— Runtime Function: _Decimal64
__dpd_extenddfdd (
double a)
— Runtime Function: _Decimal64
__bid_extenddfdd (
double a)
— Runtime Function: _Decimal128
__dpd_extendtftd (
long double a)
— Runtime Function: _Decimal128
__bid_extendtftd (
long double a)
— Runtime Function: float
__dpd_truncsdsf (
_Decimal32 a)
— Runtime Function: float
__bid_truncsdsf (
_Decimal32 a)
— Runtime Function: double
__dpd_truncdddf (
_Decimal64 a)
— Runtime Function: double
__bid_truncdddf (
_Decimal64 a)
— Runtime Function: long double
__dpd_trunctdtf (
_Decimal128 a)
— Runtime Function: long double
__bid_trunctdtf (
_Decimal128 a)
这些函数将a的值在相同大小的十进制浮点类型和二进制浮点类型之间转换。
— Runtime Function: int
__dpd_fixsdsi (
_Decimal32 a)
— Runtime Function: int
__bid_fixsdsi (
_Decimal32 a)
— Runtime Function: int
__dpd_fixddsi (
_Decimal64 a)
— Runtime Function: int
__bid_fixddsi (
_Decimal64 a)
— Runtime Function: int
__dpd_fixtdsi (
_Decimal128 a)
— Runtime Function: int
__bid_fixtdsi (
_Decimal128 a)
这些函数将a转换为有符号整数。
— Runtime Function: long
__dpd_fixsddi (
_Decimal32 a)
— Runtime Function: long
__bid_fixsddi (
_Decimal32 a)
— Runtime Function: long
__dpd_fixdddi (
_Decimal64 a)
— Runtime Function: long
__bid_fixdddi (
_Decimal64 a)
— Runtime Function: long
__dpd_fixtddi (
_Decimal128 a)
— Runtime Function: long
__bid_fixtddi (
_Decimal128 a)
这些函数将a转换为有符号长整数。
— Runtime Function: unsigned int
__dpd_fixunssdsi (
_Decimal32 a)
— Runtime Function: unsigned int
__bid_fixunssdsi (
_Decimal32 a)
— Runtime Function: unsigned int
__dpd_fixunsddsi (
_Decimal64 a)
— Runtime Function: unsigned int
__bid_fixunsddsi (
_Decimal64 a)
— Runtime Function: unsigned int
__dpd_fixunstdsi (
_Decimal128 a)
— Runtime Function: unsigned int
__bid_fixunstdsi (
_Decimal128 a)
这些函数将a转换为无符号整数。将负值都变为0。
— Runtime Function: unsigned long
__dpd_fixunssddi (
_Decimal32 a)
— Runtime Function: unsigned long
__bid_fixunssddi (
_Decimal32 a)
— Runtime Function: unsigned long
__dpd_fixunsdddi (
_Decimal64 a)
— Runtime Function: unsigned long
__bid_fixunsdddi (
_Decimal64 a)
— Runtime Function: unsigned long
__dpd_fixunstddi (
_Decimal128 a)
— Runtime Function: unsigned long
__bid_fixunstddi (
_Decimal128 a)
这些函数将a转换为无符号长整数。将负值都变为0。
— Runtime Function: _Decimal32
__dpd_floatsisd (
int i)
— Runtime Function: _Decimal32
__bid_floatsisd (
int i)
— Runtime Function: _Decimal64
__dpd_floatsidd (
int i)
— Runtime Function: _Decimal64
__bid_floatsidd (
int i)
— Runtime Function: _Decimal128
__dpd_floatsitd (
int i)
— Runtime Function: _Decimal128
__bid_floatsitd (
int i)
这些函数将有符号整数i转换为十进制浮点数。
— Runtime Function: _Decimal32
__dpd_floatdisd (
long i)
— Runtime Function: _Decimal32
__bid_floatdisd (
long i)
— Runtime Function: _Decimal64
__dpd_floatdidd (
long i)
— Runtime Function: _Decimal64
__bid_floatdidd (
long i)
— Runtime Function: _Decimal128
__dpd_floatditd (
long i)
— Runtime Function: _Decimal128
__bid_floatditd (
long i)
这些函数将有符号长整数i转换为十进制浮点数。
— Runtime Function: _Decimal32
__dpd_floatunssisd (
unsigned int i)
— Runtime Function: _Decimal32
__bid_floatunssisd (
unsigned int i)
— Runtime Function: _Decimal64
__dpd_floatunssidd (
unsigned int i)
— Runtime Function: _Decimal64
__bid_floatunssidd (
unsigned int i)
— Runtime Function: _Decimal128
__dpd_floatunssitd (
unsigned int i)
— Runtime Function: _Decimal128
__bid_floatunssitd (
unsigned int i)
这些函数将无符号整数i转换为十进制浮点数。
— Runtime Function: _Decimal32
__dpd_floatunsdisd (
unsigned long i)
— Runtime Function: _Decimal32
__bid_floatunsdisd (
unsigned long i)
— Runtime Function: _Decimal64
__dpd_floatunsdidd (
unsigned long i)
— Runtime Function: _Decimal64
__bid_floatunsdidd (
unsigned long i)
— Runtime Function: _Decimal128
__dpd_floatunsditd (
unsigned long i)
— Runtime Function: _Decimal128
__bid_floatunsditd (
unsigned long i)
这些函数将无符号长整数i转换为十进制浮点数。
4.3.3 比较函数
— Runtime Function: int
__dpd_unordsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_unordsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_unorddd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_unorddd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_unordtd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_unordtd2 (
_Decimal128 a, _Decimal128 b)
如果有任一参数为NaN,则这些函数返回非0,否则返回0。
还有一个直接对应于比较运算符的高级函数的完备群。它们按照ISO C语义实现了浮点数比较运算,将NaN考虑了进来。要仔细注意每组函数的返回值定义。究
其原因,所有这些函数都采用以下方式实现:
if (__bid_unordXd2 (a, b))
return E;
return __bid_cmpXd2 (a, b);
其中E是一个常数,被选来给出针对NaN的适当行为。因此,对于每组函数的返回值其意义都有所不同。所以不要依赖于这些函数实现;只有在下文
明确说明的语义才是可以保证的。
— Runtime Function: int
__dpd_eqsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_eqsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_eqdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_eqdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_eqtd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_eqtd2 (
_Decimal128 a, _Decimal128 b)
如果参数都不为NaN,并且a和b相等,则这些函数返回0。
— Runtime Function: int
__dpd_nesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_nesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_nedd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_nedd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_netd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_netd2 (
_Decimal128 a, _Decimal128 b)
如果有任一参数为NaN,或者a和b不相等,则这些函数返回非0。
— Runtime Function: int
__dpd_gesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_gesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_gedd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_gedd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_getd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_getd2 (
_Decimal128 a, _Decimal128 b)
如果参数都不为NaN,并且a大于或等于b,则这些函数返回一个大于或等于0的值。
— Runtime Function: int
__dpd_ltsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_ltsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_ltdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_ltdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_lttd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_lttd2 (
_Decimal128 a, _Decimal128 b)
如果参数都不为NaN,并且a严格小于b,则这些函数返回一个小于0的值。
— Runtime Function: int
__dpd_lesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_lesd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_ledd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_ledd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_letd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_letd2 (
_Decimal128 a, _Decimal128 b)
如果参数都不为NaN,并且a小于或等于b,则这些函数返回一个小于或等于0的值。
— Runtime Function: int
__dpd_gtsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__bid_gtsd2 (
_Decimal32 a, _Decimal32 b)
— Runtime Function: int
__dpd_gtdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__bid_gtdd2 (
_Decimal64 a, _Decimal64 b)
— Runtime Function: int
__dpd_gttd2 (
_Decimal128 a, _Decimal128 b)
— Runtime Function: int
__bid_gttd2 (
_Decimal128 a, _Decimal128 b)
如果参数都不为NaN,并且a严格大于b,则这些函数返回一个大于0的值。