1、计算机组成原理,Principles of Computer Organization,广义双语教学课程,http:/211.64.192.109/skyclass25/,青岛理工大学 校级精品课程,http:/ 计算机的运算方法,( 6 ),Floating-point computation in a computer can run into three kinds of problems:,An operation can be mathematically illegal, such as division by zero.,An operation can be legal in
2、principle, but not supported by the specific format, for example, calculating the square root of 1 or the inverse sine of 2 (both of which result in complex numbers).,An operation can be legal in principle, but the result can be impossible to represent in the specified format, because the exponent i
3、s too large or too small to encode in the exponent field. Such an event is called an overflow (exponent too large) or underflow (exponent too small).,3,6.4 浮点四则运算,Floating-Point Arithmetic,浮点加减法运算,浮点乘法运算,Floating-Point Multiplication,Floating-Point Addition & Subtraction,浮点除法运算,Floating-Point Divisi
4、on,4,浮点运算 Floating-Point Arithmetic,浮点运算要把阶码和尾数分别处理。,阶码的运算是定点整数运算,对阶码的运算有四种:阶码加1,阶码减1,两阶码求和,两阶码求差。,尾数的运算是定点小数运算。,运算过程中一般取双符号位。,浮点运算器总是由处理阶码和处理尾数的两部分组成。,Floating-point representation is similar in concept to scientific notation. The way in which the significand, exponent and sign bits are internally
5、stored on a computer is implementation-dependent.,5,浮点数的溢出 Overflow,当一个数的大小超出了浮点数的表示范围时,机器无法表示该数,就发生溢出。浮点数的溢出判断方法与定点数不同,是对规格化数的阶码Exponent进行判断。,当浮点数的阶码大于机器所能表示的最大阶码时(即阶码发生正溢出),称为溢出或上溢,此时机器应停止运算,进行出错中断处理。,当浮点数的阶码小于机器所能表示的最小阶码时(即阶码发生负溢出),称为下溢。这时一般规定把该浮点数的尾数强迫置零,作为零处理,机器可继续运行。,当一个浮点数的尾数为0,不论其阶码为何值,或者阶码的
6、值小于等于它能表示的最小数值(下溢)时,不论其尾数为何值,计算机都把该浮点数看成零值,称为机器零。,浮点数的尾数运算的溢出可以通过右规消除。,6,浮点加减法运算Floating-Point Addition & Subtraction,设有两个规格化浮点数,X=MX2Ex , Y=MY2Ey,若两数的阶码相等,即 EX=EY,有,X+Y =,将两浮点数的尾数相加,就可得到浮点形式的和。,一般情况下,EXEY,计算X+Y要用如下五个步骤来完成:,对阶 尾数相加 规格化 舍入 检查阶码是否溢出。,1、浮点加法运算 Floating-Point Addition,MX 2Ex + MY 2Ey,=(
7、MX + MY )2Ex,7,(1) 对阶Alignment,两数相加,必须使小数点对齐。对于浮点数来说,就是使阶码相等。使阶码相等的过程称为对阶。,对阶的原则是:小的阶码向大阶码看齐。,对阶操作,首先比较两数的阶码值的大小,即求E=EXEY,并保留其最大值 E=MAX(EX,EY)作为和的阶码。,当E0时,将阶码小的数的尾数右移|E|位。,尾数每右移一次将阶码加1,直至E0。,为了减少误差,可用附加线路(Guard bits保留位)保留右移出的1位或几位的高位,在以后的舍入操作时用。,(2) 尾数相加 Add the Significands(Mantissa),完成对阶后,将两浮点数的尾数
8、部分相加,方法与定点小数加法相同。,8,(3) 规格化处理 Normalize the Result,当运算结果的尾数部分不是规格化数(即不是00.1或11.0的形式)时,必须进行规格化处理。,规格化处理的规则:, 若结果尾数的两个符号位不同(01或10),表明尾数运算结果溢出,应进行右规。将结果尾数右移一位,并将阶码的值加1。, 若尾数的运算结果不溢出,但最高数值位与符号位同值(即11.1或00.0),则应进行左规。,将尾数连同符号位一起左移一位,并将和的阶码减1,如此反复直至尾数最高数值位与符号位不同为止。,9,(4) 舍入操作 Round,在进行对阶或右规操作时,尾数低位的一位或几位数值
9、被移出。如果采用“截断法”把移出的数位丢掉,会影响数值的精度。因此,可采用舍入法来减少误差。, “0”舍“1”入法,当移出部分的最高位为1时在尾数末位加1,为0时则舍去移出的数值。此方法的最大误差为2(n+1)。,“0”舍“1”入法由于有舍有入,舍入机会均等,有利于减少误差积累。但对末位加1的操作可能引起一连串的进位而使尾数溢出,此时还要再做一次右规。, 末位恒置1法,无论右移丢失的是何数值,一律将结果的末位置1。,把尾数最低位的0置成1,对于正数,是使其值变大,对于负数补码和反码,则使其值变小。而舍入前尾数最低位已经是1时,再置1无实际效用,与截断法无异。,10,(5) 检查阶码是否溢出 C
10、heck the Exponent Overflow or Underflow,若阶码正常,加减运算正常结束;若阶码下溢,要置运算结果为浮点形式的机器零;若阶码上溢,则置溢出标志。,【例1】浮点数的阶码为4位补码,尾数为9位补码。,X0.110110112010,Y=(0.10101100)2100,求X+Y=?,Rounding is used when the exact result of a floating-point operation (or a conversion to floating-point format) would need more digits than th
11、ere are digits in the significand. There are several different rounding schemes (or rounding modes).,11,例1浮点数的阶码为4位补码,尾数为9位补码。求X+Y=?,X0.110110112010,Y=(0.10101100)2100,,解:,EX补=0010, EY补= 0100, -EY补= 1100,MX补=0.11011011, MY补=1.01010100, 对阶,E 补= EX补EY补 = EX补-EY补= 00010+11100 = 11110,即E2。由于X的阶码小,应使MX右移
12、两位,EX加2,, 尾数相加,12, 规格化处理,结果的符号位与最高数值位同值,应进行左规。,尾数左移1位,阶码减1。,MX+Y补=11.00010101 10 , EX+Y补=00011, 舍入处理,采用0舍1入法, MX+Y补=11.00010110, 判断溢出,补码表示的阶码的符号位为00,不溢出。结果:,MX+Y补=1.00010110, EX+Y补=0011,XY(0.11101010)2011,例1浮点数的阶码为4位补码,尾数为9位补码。求X+Y=?,13,2、浮点数减法 Floating-Point Subtraction,对阶, 尾数相减, 规格化, 舍入, 检查阶码是否溢出。
13、,浮点数减法运算的步骤:,The fact that floating-point numbers cannot faithfully mimic the real numbers, and that floating-point operations cannot faithfully mimic true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent num
14、bers.,14,浮点乘法运算 Floating-Point Multiplication,两浮点数相乘,乘积的尾数为相乘两数的尾数之积,阶码为两数的阶码之和。即,XY,浮点乘法运算可分为四个步骤:,阶码相加,尾数相乘,规格化和舍入处理,判断溢出,(MX2Ex )(MY2Ey)=,(MXMY)2 Ex+ Ey,规格化浮点数,X=MX2Ex , Y=MY2Ey,To multiply, the significands are multiplied while the exponents are added, and the result is rounded and normalized.,
15、阶码相加 Add Exponent,乘数和被乘数的阶码按定点整数补码或移码加法的规则相加,得到乘积的阶码。, 尾数相乘 Multiply Significands(Mantissa),乘数和被乘数的尾数按定点小数(原码或补码)乘法运算的方法相乘,得到乘积的尾数。, 规格化和舍入处理 Normalizing & Round,规格化和舍入方法与浮点加减法处理的方法相同。但两个数值位是m位的数相乘,乘积的数值位为2m位。舍入处理后,尾数只保留m个数值位。,一般情况下,两个规格化数相乘,尾数最多左规一次,因为两个纯小数相乘是不会溢出的。但是有一个特例,当尾数做补码乘法的时候,如果乘数和被乘数尾数的值都
16、是-1,则乘积的尾数的值是+1,此时需要做一次右规。,16, 判断溢出 Check the Exponent Overflow or Underflow,检查阶码是否溢出。若阶码正常,加减运算正常结束;若阶码下溢,要置运算结果为浮点形式的机器零;若阶码上溢,则置溢出标志。,Floating-point arithmetic is at its best when it is simply being used to measure real-world quantities over a wide range of scales (such as the orbital period of I
17、o or the mass of the proton), and at its worst when it is expected to model the interactions of quantities expressed as decimal strings that are expected to be exact.,An example of the latter case is financial calculations. For this reason, financial software tends not to use a binary floating-point
18、 number representation.,17,【例2】浮点数,阶码为4位移码(含1符号位),尾数为8位补码(含1符号位),阶码以2为底。,X0.11100112-101,Y=(0.1110010)2011,求X*Y=?,解:,MX补=,MY补=, 阶码相加,EXEY移=,EX移=,0.1110011,= 00011,0011,EY移=,1.0001110,= 01011,1011,00110,= 00011+00011=,EX移EY补,EX补=,EY补=,1011,0011,00011,+ 00011,00110,例2 浮点数,阶码为4位移码(含1符号位),尾数为8位补码(含1符号位)
19、,阶码以2为底。, 尾数相乘, 规格化和舍入处理,判断溢出,MX补MY补=,已经是规格化数。,MX*Y补=1.0011010,移码表示的阶码为00,未溢出。,XY=,采用0舍1入法,将低n位舍去。,1.00110011001010,0.11100111.0001110=,EX*Y移= 00110,X0.11100112-101,Y=(0.1110010)2011,求X*Y=?,MX补= 0.1110011,MY补= 1.0001110,EX*Y移= 00110,2,(-0.1100110),010,19,浮点除法运算 Floating-Point Division,除了除数不能为0外,浮点除法
20、对除数和被除数的大小没有限制。,两浮点数相除,商的尾数部分是被除数的尾数除以除数的尾数所得的商,阶码部分是被除数的阶码减去除数的阶码所得的差。,XY=,浮点除法运算分以下五个步骤:,尾数调整,阶码求差,尾数相除,规格化,判断溢出,(MX2Ex)(MY2Ey),(MXMY )2 ExEy,20, 尾数调整 Dividend Alignment,检查 | MX | 是否小于 | MY | 。,若 | MX | MY | ,则将MX 右移一位并将阶码加1。, 阶码求差 Subtract Exponent,被除数的阶码减去除数的阶码得到商的阶码(按定点整数补码或移码减法的规则)。, 尾数相除 Divi
21、de Significands(Mantissa),两数的尾数按定点小数(原码或补码)除法的规则相除。,21, 判断溢出,检查阶码是否溢出。, 规格化 Normalize the Result,由于除数和被除数都是规格化数并经过尾数的调整,所以,尾数相除的结果一般是规格化定点小数。,只有一个例外,当补码表示的商为-0.5时,必须做一次左规。,In the IEEE 754 standard, zero is signed, meaning that there exist both a “positive zero“ (+0) and a “negative zero“ (-0). In mo
22、st run-time environments, positive zero is usually printed as “0“, while negative zero may be printed as “-0“. The two values behave as equal in numerical comparisons, but some operations return different results for +0 and 0.,例3 设浮点数基数为2,阶码为4位补码,尾数为5位原码(均包含1个符号位)。X= +6.5,Y= 2.25。,(1)写出X和Y的规格化浮点机器数。
23、,(2)计算XY (尾数运算用补码加减交替除法),解:,X=(+6.5)10 =(+110.1)2 = +0.11012+011,Y=(-2.25)10 =(-10.01)2 = -0.10012+010,EX补=0011 EY补=0010 MX原=0.1101 MY原=1.1001,设浮点数格式为:,浮点机器数:,X浮点=0 0011 1101 Y浮点=1 0010 1001,(1)写出X和Y的规格化浮点机器数。,(2)计算XY (尾数运算用补码加减交替除法),MX补=, 尾数调整, 阶码求差, 尾数相除,| MX | MY |,MX补=,-EY补=,EX补EY补=,MY补=1. 0111,
24、EX补=0011 EY补=0010 MX原=0.1101 MY原=1.1001,0.1101,MY补=,1. 0111,-MY补=,需进行尾数调整,,将MX右移1位,EX加1。,EX补=,= 00100+11110 =,EX补+-EY补,00010,0.01101= 0.0111,0100,1110,00010,0.1001,0舍1入,被除数/余数 0 0 . 0 1 1 1,商 0 . 0 0 0 0,X,Y异号,R补与Y补同号,商上1,末位商置1,R补与Y补异号,商上0,R补与Y补同号,商上1,R补与Y补异号,商上0,1 . 0 0 1 1,1 1 . 1 0 1 1,左移,1 1 . 0
25、 0 1 0,1 . 0 0 1,1 1 . 1 0 0 1,0 . 1 0 0 1,左移,0 0 . 0 0 1 0,0 . 1 0 0,0 0 . 0 0 0 1,0 . 0 1 0 0,加MY补 1 1 . 0 1 1 1,左移,0 0 . 1 0 1 0,0 . 0 1 0,0 0 . 0 1 0 1,0 . 0 0 1 0,加-MY补 0 0 . 1 0 0 1,左移,1 1 . 1 1 0 0,0 . 0 0 1,1 1 . 1 1 1 0,0 . 0 0 0 1,加MY补 1 1 . 0 1 1 1,MY补= 1. 0111,加MY补 1 1 . 0 1 1 1,加-MY补 0
26、0 . 1 0 0 1,25,规格化,判断溢出,MXY补=1 . 0 0 1 1,MXY原=,阶码(补码)的符号位为00,没有溢出。,尾数已经是规格化的。,结果: XY=,-0.11012+010,1 . 1 1 01,26,Homework,6-25,26,29,30,The range of floating-point numbers depends on the number of bits or digits used for representation of the significand (the significant digits of the number) and fo
27、r the exponent.,The IEEE has standardized the computer representation for binary floating-point numbers in IEEE 754. This standard is followed by almost all modern machines.,Single precision, called “float“ in the C language family, and “real“ or “real*4“ in Fortran. This is a binary format that occ
28、upies 32 bits and its significand has a precision of 24 bits (about 7 decimal digits).,Double precision, called “double“ in the C language family, and “double precision“ or “real*8“ in Fortran. This is a binary format that occupies 64 bits (8 bytes) and its significand has a precision of 53 bits (about 16 decimal digits).,思考6-28,27,今日要点,1、浮点数的溢出,2、浮点加减法运算,