2023年10月18日星期三

祭祖05

祭祖05

祭祖05

补遗

同步

单任务操作系统->多任务操作系统,需要处理好线程进程的同步,保证不被干扰。

例如MPU1 往一个内存单元写,MPU2恰好往一个内存单元读,这就会导致Data race if P1 and P2 don’t synchronize

Result depends of order of accesses

硬件支持原子读写操作,在这次写和读之间不允许其他进程/核访存这个内存单元位置。

ll 和 sc

Load linked: ll rt, offset(rs)

  1. 读,rt = rs + offset(就是Load)
  2. rs + offset送给一个特殊的寄存器,同时将cpu的寄存器中的一个不可见的bit位置为0.
    在后续每次访存中,地址会和这个寄存器进行比较,如果相等,不可见的bit位赋值1从而导致后续sc失败.因此可判断这个内存单元是否被其他处理器访问过。
    这样的寄存器和bit位每个处理器都有一个,会广播给其他进程。

Store conditional: sc rt, offset(rs)

  1. 如果不可见bit位仍然为0,rt赋值给内存单元rs+offset,rt:=1
  2. 否则,rt:=0表示写失败了。

如果失败了,接下来就取决于程序是要放弃这个操作还是再试一次了,不过至少它(ll&sc)不会生成一个隐性的(不被察觉的)竞争危害。

由于这样的bit位只有一位,存ll的寄存器也只有一个,因此,LL/SC无法实现嵌套,也即无法实现嵌套锁,这是程序员使用LL/SC所需要注意的。

实现互斥锁?
拓展阅读:
Linux 的 Spinlock 在 MIPS 多核处理器中的设计与实现 - Rogn - 博客园 (cnblogs.com)
深入理解Linux 内核同步:自旋锁(Spinlock) - 知乎 (zhihu.com)

数组处理

伪指令,硬件不支持仅写方便
这里所用move r, a-> add r ,a ,zero

void  sort  (int  v[],  int  n)  
{  
	int  i,  j;  
	for  (i  =  0;  i  <  n;  i  +=  1)  {  
		for  (j  =  i – 1;  j  >=  0  &&  v[j]  >  v[j  +  1];  j -=  1) 
			swap(v,j);  
	}  
}  

v in $a0, n in $a1, i in $s0, j in $s1

1.png
line1-2: 将a0,a1储存,因为swap中会用到这两个寄存器存临时变量。

Arthmetic for computer

CPU={运算器数据通路控制器 CPU = \begin{cases} 运算器\to数据通路\\ 控制器 \end{cases}
{参与运算{整数{unsignedsigned补码小数不参与运算字符 数\begin{cases} 参与运算\begin{cases} 整数\begin{cases} unsigned\\ signed \to 补码 \end{cases}\\\\ 小数\\ \end{cases}\\\\ 不参与运算\to字符 \end{cases}

整数

整数运算器{+ × ÷处理overflow 整数运算器\begin{cases} +\ -\\ \times\ \div\\ 处理overflow \end{cases}

加减法

有符号数,补码
已知
X+Y=[X+Y]XY=X+(Y)=[XY] X_补 + Y_补 = [X+Y]_补\\ X_补 - Y_补 = X_补 + (-Y)_补 = [X-Y]_补

如何处理overflow?,现有4位二进制位
2.png
3.png

add在发生溢出时会报告cpu从而进行except或者采用饱和运算,addu则不会监测

Some languages (e.g., C) ignore overflow

  • Use MIPS addu, addui, subu instructions

Other languages (e.g., Ada, Fortran) require raising an exception

  • Use MIPS add, addi, sub instructions
  • On overflow, invoke exception handler
    • Save PC in exception program counter (EPC)
      register
    • Jump to predefined handler address
    • mfc0 (move from coprocessor reg) instruction can retrieve EPC value, to return after corrective action

乘法除法

了解

小数

如何表示非整数

包括小数和大数->科学计数法
标准化例如

  • 2.34×1056-2.34 \times 10^{56}

非标准化

  • +976.02×105+976.02 \times 10^5

在二进制中则是

  • 1.xxxxx2×2yyyyyy21.xxxxx_2 \times 2^{{yyyyyy}_2}
有效数字位 指数浮动
1.xxxx21.xxxx_2 2yyyyy2^{yyyyy}
精度 大小

IEEE-754
ppt p11

  • 单精度 float 32bit
  • 双精度 double 64bit
S Exponent Fraction
single 8bits single 32bits
double 11bits double 52bits

X=(1)S×(1+Fraction)×2(ExponentBias) X = (-1)^S \times (1 + \text{Fraction})\times 2^{(\text{Exponent} - \text{Bias})}

  1. S 符号位,0表示非负,1表示负
  2. 标准化有效位: 1.0significand<2.01.0 \le |\text{significand}| < 2.0
    • 因为总有一个前置的二进制位1,所有不需要再显式的表示
  3. 指数:过度表示,实际指数+偏移
    • 确保指数是非负的
    • single Bias = 127, double Bias = 1023
    • 1261271254-126 \sim 127 \Rightarrow 1 \sim 254
    • 1022102312046-1022 \sim 1023 \Rightarrow 1 \sim 2046

单精度范围

指数 00000000 和 11111111 是保留的

  • min:Exp:00000001
    • 实际指数1-127 = -126
    • 有效位:000…00\to有效位1.0
    • ±1.0×21261.2×1038\pm 1.0 \times 2^{-126} \approx 1.2 \times 10^{-38}
  • max:Exp:1111110
    • 实际指数254 - 127 = +127
    • 有效位:111…112.0\to \approx 2.0
    • ±2.0×2+127±3.4×10+38\pm 2.0 \times 2^{+127} \approx \pm 3.4 \times 10^{+38}

表示例子

Represent –0.75

  • –0.75 = (1)1×1.12×21(–1)^1 × 1.1_2 × 2^{–1}
  • S = 1
  • Fraction = 1000…002_2
  • Exponent = –1 + Bias
    • Single: –1 + 127 = 126 = 011111102_ 2
    • Double: –1 + 1023 = 1022 = 011111111102_2
  • Single: 1011111101000…00
  • Double: 1011111111101000…00

denormal numbers

x=(1)S×(0+Fraction)×2(1Bias)x = (-1)^S \times(0+\text{Fraction})\times 2^{(1-\text{Bias})}
Smaller than normal numbers

  • allow for gradual underflow, with diminishing precision

特殊的,Denormal with fraction =000…0
x=(1)S×(0+0)×2(1Bias)=±0.0x=(-1)^S \times (0+0) \times 2^{(1-\text {Bias})} = \pm 0.0
因此有两种表示0的方法

示例,Tiny Floating Point

8-bit Floating Point Representation

  • the sign bit is in the most significant bit
  • the next four bits are the exponent, with a bias of 7
  • the last three bits are the frac

4.png

2 条评论:

  1. 实数范围里,有一些计算是没有结果,无法进行的。在标准里同样规定了一类数,用于保存这类结果,他们叫做非数值(not a number)。非数值与无穷一样使用全为1的指数位表示,为了区分开来,小数位全为0时表示无穷,其他所有情况表示非数值情况。
    https://www.cnblogs.com/ofnoname/p/15839927.html

    回复删除