-
Notifications
You must be signed in to change notification settings - Fork 17
Description
There's a different algorithm to calculate the exponent and B(ase), T(op) given a base and length that has the following properties:
- Maintains a distinct representable but non dereferencable region of size
$2^e$ , where$e$ is the (decoded) exponent (same as current algorithm) - Simpler hardware circuit
- Tighter bounds for any base, length compared to current algorithm
- Size of bound regions double all the way till
$2^{32}$ (not true in the current algorithm: there's a jump from$2^{14}$ to$2^{32}$ ) - Unique canonical representation of any cap(not true in the current algorithm: for instance, B = 1, T = 2, e = 5 is the same as B = 2, T = 4, e = 4)
Here's the algorithm. Given a base
countLeadingZeros and subtracting from countOnes is 1)
truncToMSB_variable where
bitSelect's (
The quantity T and B". (We are still not done because we need the mantissa a.k.a difference to be
Lemma 1:
Proof:
- Lemma is true if
$(d+i)\cdot 2^{e} \ge l + (b\mod{2^e})$ . - Substituting the equation for
$d$ , we get$l = d\cdot 2^{e} + (l\mod{2^e})$ . - Now, lemma is true if
$i \cdot 2^{e} \ge (l \mod{2^e}) + (b\mod{2^e})$ . - Substituting the equation for
$i$ , we need to prove
$\left\lceil \frac{(l\mod{2^e}) + (b\mod{2^e})}{2^e} \right\rceil \cdot 2^{e} \ge (l\mod{2^e}) + (b\mod{2^e})$ . - But we know that
$\left\lceil \frac{x}{y} \right\rceil \cdot y \ge x$ (Proof hint: let$x = p\cdot y - q$ ).
$\square$
Now let's get the bounds for
Lemma 2: if T and B is already guaranteed to be 1 when
Proof:
- Let
$l = x \cdot 2^S + y$ where$0 \le y \lt 2^S, 0 \le x \le 2^{F-S}$ . - Substituting for
$e$ , we have$e = \left\lceil lg(x) \right\rceil$ . If$x \le 1$ then$e = 0$ ; so$x \ge 2$ . - From
$\left\lceil lg \right\rceil$ aka$lgCeil$ , we have$2^e \ge x \gt 2^{e-1}$ . - Let
$x = 2^{e-1} + k, 1 \le k \le 2^{e-1}$ . Substituting for$l, x$ in equation for$d$ we have: -
$d = \left\lfloor \frac{(2^{e-1} + k) \cdot 2^S + y}{2^e} \right\rfloor$
$= 2^{S-1} + \left\lfloor \frac{k \cdot 2^S + y}{2^e} \right\rfloor$
$\ge 2^{S-1}$ .
$\square$
Lemma 3:
Proof:
- Let
$l = x \cdot 2^S + y$ where$0 \le y \lt 2^S, 0 \le x \le 2^{F-S}$ . - If
$x = 0$ then$e = 0$ , so$d = \left\lfloor l \right\rfloor = \left\lfloor y \right\rfloor \lt 2^S$ $\square$ - If
$x = 1$ then$e = 0$ , so$d = \left\lfloor l \right\rfloor = \left\lfloor 2^S + y \right\rfloor \lt 2^S + 2^S = 2^{S+1}$ $\square$ - If
$x \ge 2$ then from$\left\lceil lg \right\rceil$ aka$lgCeil$ , we have$2^e \ge x \gt 2^{e-1}$ - Thus,
$\frac{1}{2^e} \le \frac{1}{x} \rightarrow \frac{l}{2^e} \le \frac{l}{x} = 2^S + \frac{y}{x}$ - Thus,
$d = \left\lfloor \frac{l}{2^e} \right\rfloor \le 2^S + \left\lfloor \frac{y}{x} \right\rfloor \lt 2^S + 2^S = 2^{S+1}$
$\square$
Lemma 4:
Proof:
- If
$e = 0$ ,$i = 0$ $\square$ - If
$e = 1$ ,$i = \left\lceil \frac{(l\mod 2) + (b\mod 2)}{2} \right\rceil \le \left\lceil \frac{2}{2} \right\rceil = 1$ $\square$ - If
$e \ge 2$ ,$i = \left\lceil \frac{(l\mod{2^e}) + (b\mod{2^e})}{2^e} \right\rceil \le \left\lceil \frac{2^e-1 + 2^e-1}{2^e} \right\rceil = \left\lceil \frac{2^{e+1}-2}{2^e} \right\rceil = 2$
$\square$
Lemma 5:
Proof:
From Lemmas 3 and 4
Lemma 6:
Proof:
- Let
$x = 2p + q, 0 \le q \le 1$ . - Substituting, we need to prove
$(2p + q) \cdot 2^y \le (p + q) \cdot 2^{y+1}$ . - That is to prove
$(p \cdot 2^{y+1} + q \cdot 2^y) \le p \cdot 2^{y+1} + q \cdot 2^{y + 1}$ .
$\square$
Let the final value (after right shifts) of mantissa be T - B. B can be calculated using truncToMSB_variable. T can be calculated by adding B. (I am ignoring the distinction between MSB bits and the "mid" bits here.
Since T - B is guaranteed to have MSB set to 1 (when T with 9 bits, and instead we can use 8 bits to represent T (This part is Jon Woodruff's optimization suggestion; we need an adder to calculate the MSB of T. Alternatively, we can just store T, which will avoid the addition.)
Note that for all values of B and T or B and T or