Skip to content

Commit

Permalink
update csapp 2.3 and 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SliverHorn committed Jun 5, 2024
1 parent 95b3aff commit badabee
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
2 changes: 0 additions & 2 deletions content/csapp/2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,3 @@ $x_7=1$

| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | $x_7$ | $x_6$ | $x_5$ | $x_4$ | $x_3$ | $x_2$ | $x_1$ | $x_0$ |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|


76 changes: 71 additions & 5 deletions content/csapp/2.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ Integer Arithmetic(整数运算)

<!--more-->

## Unsigned Addition
## Unsigned

### Addition

For x and y $0 \leq x \leq 2^w$ , $0 \leq y \leq 2^w$

$$
x+_{w}^{u}y= \begin{cases} x+y, & \text {x + y < $2^w$} \\\\ 3n+1, & \text{$2^{w}\leq{x+y}{\leq}2^{w+1}$} \end{cases}
$$

## Additive Inverse
### Inverse

For x,$0 \leq x \leq 2^w$

Expand All @@ -40,15 +42,28 @@ $$
-_{w}^{u}y= \begin{cases} x, & \text {x = 0} \\\\ 2^{w}-x, & \text{x $\geq$ 0} \end{cases}
$$

## Two's Complement Addition
### Multiplication

| x | $x_{w-1},x_{w-2}...x_{0}$ |
|:-------------:| ---------------------------------------- |
| y | $y_{w-1},y_{w-2}...y_{0}$ |
| $z=x{\cdot}y$ | $\underbrace{Z_{w-1},Z_{w-2}...Z_0}_{w}$ |

$$
x *_{w}^{u}y= (x{\cdot}y) mod 2^w
$$

## Two's Complement

### Addition

For x and y $-2^{w-1}{\leq}{x}{\leq}2^{w-1}-1, -2^{w-1}{\leq}{y}{\leq}2^{w-1}-1$

$$
x+_{w}^{t}y= \begin{cases} x+y-2^{w}, & \text {$2^{w-1}{\leq}{x+y}$(Positive overflow)} \\\\ x+y, & \text{$-2^{w-1}\leq{x+y}{<}2^{w-1}$} \\\\ x+y+2^{w}, & \text{x+y<$-2^{w-1}$(Negative overflow)} \end{cases}
$$

## Two's Complement Addition Code
### Code

```c
#include <stdio.h>
Expand All @@ -62,7 +77,7 @@ int main() {
}
```

## Two's Complement Negation
### Negation

For x $-2^{w-1}{\leq}x<2^{w-1}-1$

Expand All @@ -82,4 +97,55 @@ $$
Tmin_w+_{w}^{t}Tmain_w = -2^w+2^w=0
$$

### Multiplication

$$
x *_{w}^{t}y=U2T_w((x{\cdot}y)\mod{2^w})
$$

### Three-bit Multiplication Example

| Mode | x | y | ${x}{\cdot}{y}$ | Truncated ${x}{\cdot}{y}$ |
|:----------------:|:--------- |:--------- |:--------------- |:------------------------- |
| Unsigned | $5[101]$ | $3[011]$ | $15[001 111]$ | $7[111]$ |
| Two's Complement | $-3[101]$ | $3[011]$ | $-9[110 111]$ | $-1[111]$ |
| Unsigned | $4[100]$ | $7[111]$ | $28[001 100]$ | $4[100]$ |
| Two's Complement | $-4[100]$ | $-1[111]$ | $4[000 100]$ | $-4[100]$ |
| Unsigned | $3[011]$ | $3[011]$ | $9[001 001]$ | $1[001]$ |
| Two's Complement | $3[011]$ | $3[011]$ | $9[001 001]$ | $1[001]$ |

For signed integer x, y and unsigned integer $x^{'}, y^{'}$

$$
B2U_w(x^{'})=x_{w-1}{\cdot}2^{w-1}+x_{w-2}{\cdot}2^{w-2}+...+x_{0}{\cdot}2^{0} \\\\
B2T_w(x)=x_{w-1}{\cdot}-2^{w-1}+x_{w-2}{\cdot}2^{w-2}+...+x_{0}{\cdot}2^{0} \\\\
B2U_w(x^{'})-B2T_w(x)=x^{w-1}{\cdot}2^{w} \\\\
x^{'} = x + x_{w-1}{\cdot}2^{w} \\\\
y^{'}=y+{y_{w-1}}{\cdot}2^w \\\\
({x^{'}}{\cdot}{y^{'}})\mod{2^w} = [(x+x_{w-1}{\cdot}{2^w}){\cdot}(y+{y_{w-1}}{\cdot}{2^w})] \mod 2^w \\\\
({x^{'}}{\cdot}{y^{'}})\mod{2^w} = [x{\cdot}y+(x_{w-1}y+y_{w-1}x)2^w]+x_{w-1}y_{w-1}2^{2w}] \mod 2^w \\\\
({x^{'}}{\cdot}{y^{'}})\mod{2^w} = (x{\cdot}y\mod2^w)
$$

## Multiplying by Constants

| $x{\cdot}2$ | $x<<1$ |
| --------------- | ------ |
| $x{\cdot}4$ | $x<<2$ |
| ... | ... |
| $x{\cdot}2^{k}$ | $x<<k$ |

For unsigned integer x

$$
x_{w-1}|x_{w-2}|...|x_0 \\\\
B2U_w(x)=x_{w-1}{\cdot}2^{w-1}+x_{w-2}{\cdot}2^{w-2}+...+x_0{\cdot}2^0=\sum_{i=0}^{w-1}x_{i}2^{i} \\\\
x_{w-1}|x_{w-2}|...|x_0|\underbrace{0|...|0}_{k} \\\\
$$

$$
B2U_{w+k}(x^{'})=x_{w-1}{\cdot}2^{w-1+k}+x_{w-2}{\cdot}2^{w-2+k}+...+x_0{\cdot}2^k+0{\cdot}2^{k-1}+0{\cdot}2^{k-2}+...+0{\cdot}2^{0} \\\\
B2U_{w+k}(x^{'})=\sum_{i=0}^{w-1}x_{i}2^{i}{\cdot}2^k=x2^k
$$


0 comments on commit badabee

Please sign in to comment.