计组06
补遗
浮点数小结
例如在 1 + 4 + 3中
bias
= 7
浮点数 { 规格化浮点数 非规格化浮点数exp = 000...0 ( real exp = 1 − bias , M = 0. frac ) ± ∞ exp = 111...1 NaN frac ≠ 000.0 非数字编码
浮点数 \begin{cases}
规格化浮点数 \\
非规格化浮点数 \text{exp} = 000...0 \quad (\text{real exp} = 1-\text{bias}, M = 0.\text{frac})\\
\pm \infty \quad \text{exp} = 111...1\\
\text{NaN} \quad \text{frac} \ne 000.0 非数字编码
\end{cases}
浮点数 ⎩ ⎨ ⎧ 规格化浮点数 非规格化浮点数 exp = 000...0 ( real exp = 1 − bias , M = 0. frac ) ± ∞ exp = 111...1 NaN frac = 000.0 非数字编码
非规格浮点数的存在可以允许表示接近0的数
2 − 6 = 1 64 2^{-6} = \frac 1 {64} 2 − 6 = 64 1
用1.xxx表示? \用有效位用0.xxx
8 8 × 1 128 → 1 8 × 1 64 \frac 8 8 \times \frac 1 {128} \to \frac 1 8 \times \frac 1 {64} 8 8 × 128 1 → 8 1 × 64 1
浮点数加法
先以DEC进制为例
现有两个十进制数
9.999 × 1 0 1 9.999 \times 10^{1} 9.999 × 1 0 1 and1.610 × 1 0 − 1 1.610 \times 10^{-1} 1.610 × 1 0 − 1
step
detail
1. 对阶,指数归一,小指数向大指数转化
0.0161 × 1 0 1 0.0161 \times 10^1 0.0161 × 1 0 1
2. 有效数位相加
10.015 × 1 0 1 10.015 \times 10^1 10.015 × 1 0 1
3. 规格化结果,检查溢出(指数)
$1.0015 \times 10^2 $
4. 舍入,向偶数舍入
→ 1.002 × 1 0 2 \to 1.002 \times 10^2 → 1.002 × 1 0 2
四舍六 入,中间值以下舍,中间值以上入,特判中间值,向偶数舍入
如DEC中的0.5 ,这里2是偶数38
ori
approx
1.6
2
1.4
1
2.5
2
1.5
2
BIN中,例如取小数点后两位有效位
中间值则为.xx100
ori
approx
1.00101
1.01
1.00001
1.00
1.11100
10.00舍完后重新规整化
1.10100
1.10
这里0为偶数
这里流程设计加减
浮点数乘法
想想手工加法。
multiplicand * multiplier
这里优化可以将64bit的ALU节省到32bit的ALU
4bit两数相乘示例。
1000 * 1001
被乘数从右往左1stbit = 1, 这一位的积=乘数,| reg + cand = 1000
lier 2nd bit = 0 ,积为全0 | reg先右移1位,再+0 = 0100
lier 3rd bit = 0, 积为全0 | reg r shift ,再+0 = 0010
lier 4th bit = 1, 积=cand | reg r shift, result = 1001 000
4位数字,4次加法运算。
再优化后 p13
mips 乘法,HI和LO
Two 32-bit registers for product
◼ HI: most-significant 32 bits
◼ LO: least-significant 32-bits
不可见
用法
mult rs,rt
multu rs,rt
64bit的结果存放在HI/LO
mfhi rd
mflo rd
load
mul rd,rs,rt
结果低32位赋值给rd
浮点数乘法
指数相加,有效数位相乘
除法
协处理器
32个单精度 reg $f0 $f1 … $f31
组合例如$f0 and $f1 可存双精度
FP instructions operate only on FP registers
◼ Programs generally don’t do integer ops on FP data,or vice versa
◼ More registers with minimal code-size impact
lwcl ldcl swcl sdcl
ldcl $f8, 32($sp)
更多操作码
◼ Single-precision arithmetic
add.s, sub.s, mul.s, div.s
◼ e.g., add.s $f0, $f1, $f6
◼ Double-precision arithmetic
add.d, sub.d, mul.d, div.d
◼ e.g., mul.d $f4, $f4, $f6
◼ Single- and double-precision comparison
c.xx.s, c.xx.d (xx is eq, lt, le, ...)
◼ Sets or clears FP condition-code bit
◼ e.g. c.lt.s $f3, $f4
◼ Branch on FP condition code true or false
bc1t, bc1f
◼ e.g., bc1t TargetLabel
代码示例
float f2c ( float fahr) {
return ( ( 5.0 / 9.0 ) * ( fahr - 32.0 ) ) ;
}
◼ fahr in $f12, result in $f0, literals in global memory space
f2c: lwc1 $f16, const5($gp)
lwc1 $f18, const9($gp)
div.s $f16, $f16, $f18
lwc1 $f18, const32($gp)
sub.s $f18, $f12, $f18
mul.s $f0, $f16, $f18
jr $ra
MIPS single cycle processor
C P U { 数据通路 { A L U ( 主要是整数 ) R F a n d M ( 高速缓存 ) 中央处理器
CPU \begin{cases} 数据通路 \begin{cases} ALU (主要是整数)\\
RF\ and\ M(高速缓存)
\end{cases}\\
中央处理器
\end{cases}
CP U ⎩ ⎨ ⎧ 数据通路 { A LU ( 主要是整数 ) RF an d M ( 高速缓存 ) 中央处理器
取指令,
PC → \to → M
PC := PC + 4
执行指令
译码 -> 功能部件信号
Two types of functional units:
elements that operate on data values (combinational)
elements that contain state (sequential)
哈佛模型
Split memory (Harvard) model - one memory for instructions and one for data
全加器,以及其执行的AND, OR, a + b, a + b ˉ \bar b b ˉ
其他如NOR,SLT,BEQ