You copied the Doc URL to your clipboard.

## Shared Functions.Common Pseudocode

### Library pseudocode for shared/functions/common/ASR

```// ASR()
// =====

bits(N) ASR(bits(N) x, integer shift)
assert shift >= 0;
if shift == 0 then
result = x;
else
(result, -) = ASR_C(x, shift);
return result;```

### Library pseudocode for shared/functions/common/ASR_C

```// ASR_C()
// =======

(bits(N), bit) ASR_C(bits(N) x, integer shift)
assert shift > 0;
shift = if shift > N then N else shift;
extended_x = SignExtend(x, shift+N);
result = extended_x<shift+N-1:shift>;
carry_out = extended_x<shift-1>;
return (result, carry_out);```

### Library pseudocode for shared/functions/common/Abs

```// Abs()
// =====

integer Abs(integer x)
return if x >= 0 then x else -x;

// Abs()
// =====

real Abs(real x)
return if x >= 0.0 then x else -x;```

### Library pseudocode for shared/functions/common/Align

```// Align()
// =======

integer Align(integer x, integer y)
return y * (x DIV y);

// Align()
// =======

bits(N) Align(bits(N) x, integer y)
return Align(UInt(x), y)<N-1:0>;```

### Library pseudocode for shared/functions/common/BitCount

```// BitCount()
// ==========

integer BitCount(bits(N) x)
integer result = 0;
for i = 0 to N-1
if x<i> == '1' then
result = result + 1;
return result;```

```// CountLeadingSignBits()
// ======================

```// CountLeadingZeroBits()
// ======================

return N - (HighestSetBit(x) + 1);```

### Library pseudocode for shared/functions/common/Elem

```// Elem[] - non-assignment form
// ============================

bits(size) Elem[bits(N) vector, integer e, integer size]
assert e >= 0 && (e+1)*size <= N;
return vector<e*size+size-1 : e*size>;

// Elem[] - non-assignment form
// ============================

bits(size) Elem[bits(N) vector, integer e]
return Elem[vector, e, size];

// Elem[] - assignment form
// ========================

Elem[bits(N) &vector, integer e, integer size] = bits(size) value
assert e >= 0 && (e+1)*size <= N;
vector<(e+1)*size-1:e*size> = value;
return;

// Elem[] - assignment form
// ========================

Elem[bits(N) &vector, integer e] = bits(size) value
Elem[vector, e, size] = value;
return;```

### Library pseudocode for shared/functions/common/Extend

```// Extend()
// ========

bits(N) Extend(bits(M) x, integer N, boolean unsigned)
return if unsigned then ZeroExtend(x, N) else SignExtend(x, N);

// Extend()
// ========

bits(N) Extend(bits(M) x, boolean unsigned)
return Extend(x, N, unsigned);```

### Library pseudocode for shared/functions/common/HighestSetBit

```// HighestSetBit()
// ===============

integer HighestSetBit(bits(N) x)
for i = N-1 downto 0
if x<i> == '1' then return i;
return -1;```

### Library pseudocode for shared/functions/common/Int

```// Int()
// =====

integer Int(bits(N) x, boolean unsigned)
result = if unsigned then UInt(x) else SInt(x);
return result;```

### Library pseudocode for shared/functions/common/IsOnes

```// IsOnes()
// ========

boolean IsOnes(bits(N) x)
return x == Ones(N);```

### Library pseudocode for shared/functions/common/IsZero

```// IsZero()
// ========

boolean IsZero(bits(N) x)
return x == Zeros(N);```

### Library pseudocode for shared/functions/common/IsZeroBit

```// IsZeroBit()
// ===========

bit IsZeroBit(bits(N) x)
return if IsZero(x) then '1' else '0';```

### Library pseudocode for shared/functions/common/LSL

```// LSL()
// =====

bits(N) LSL(bits(N) x, integer shift)
assert shift >= 0;
if shift == 0 then
result = x;
else
(result, -) = LSL_C(x, shift);
return result;```

### Library pseudocode for shared/functions/common/LSL_C

```// LSL_C()
// =======

(bits(N), bit) LSL_C(bits(N) x, integer shift)
assert shift > 0;
shift = if shift > N then N else shift;
extended_x = x : Zeros(shift);
result = extended_x<N-1:0>;
carry_out = extended_x<N>;
return (result, carry_out);```

### Library pseudocode for shared/functions/common/LSR

```// LSR()
// =====

bits(N) LSR(bits(N) x, integer shift)
assert shift >= 0;
if shift == 0 then
result = x;
else
(result, -) = LSR_C(x, shift);
return result;```

### Library pseudocode for shared/functions/common/LSR_C

```// LSR_C()
// =======

(bits(N), bit) LSR_C(bits(N) x, integer shift)
assert shift > 0;
shift = if shift > N then N else shift;
extended_x = ZeroExtend(x, shift+N);
result = extended_x<shift+N-1:shift>;
carry_out = extended_x<shift-1>;
return (result, carry_out);```

### Library pseudocode for shared/functions/common/LowestSetBit

```// LowestSetBit()
// ==============

integer LowestSetBit(bits(N) x)
for i = 0 to N-1
if x<i> == '1' then return i;
return N;```

### Library pseudocode for shared/functions/common/Max

```// Max()
// =====

integer Max(integer a, integer b)
return if a >= b then a else b;

// Max()
// =====

real Max(real a, real b)
return if a >= b then a else b;```

### Library pseudocode for shared/functions/common/Min

```// Min()
// =====

integer Min(integer a, integer b)
return if a <= b then a else b;

// Min()
// =====

real Min(real a, real b)
return if a <= b then a else b;```

### Library pseudocode for shared/functions/common/Ones

```// Ones()
// ======

bits(N) Ones(integer N)
return Replicate('1',N);

// Ones()
// ======

bits(N) Ones()
return Ones(N);```

### Library pseudocode for shared/functions/common/ROR

```// ROR()
// =====

bits(N) ROR(bits(N) x, integer shift)
assert shift >= 0;
if shift == 0 then
result = x;
else
(result, -) = ROR_C(x, shift);
return result;```

### Library pseudocode for shared/functions/common/ROR_C

```// ROR_C()
// =======

(bits(N), bit) ROR_C(bits(N) x, integer shift)
assert shift != 0;
m = shift MOD N;
result = LSR(x,m) OR LSL(x,N-m);
carry_out = result<N-1>;
return (result, carry_out);```

### Library pseudocode for shared/functions/common/Replicate

```// Replicate()
// ===========

bits(N) Replicate(bits(M) x)
assert N MOD M == 0;
return Replicate(x, N DIV M);

bits(M*N) Replicate(bits(M) x, integer N);```

### Library pseudocode for shared/functions/common/RoundDown

`integer RoundDown(real x);`

### Library pseudocode for shared/functions/common/RoundTowardsZero

```// RoundTowardsZero()
// ==================

integer RoundTowardsZero(real x)
return if x == 0.0 then 0 else if x >= 0.0 then RoundDown(x) else RoundUp(x);```

### Library pseudocode for shared/functions/common/RoundUp

`integer RoundUp(real x);`

### Library pseudocode for shared/functions/common/SInt

```// SInt()
// ======

integer SInt(bits(N) x)
result = 0;
for i = 0 to N-1
if x<i> == '1' then result = result + 2^i;
if x<N-1> == '1' then result = result - 2^N;
return result;```

### Library pseudocode for shared/functions/common/SignExtend

```// SignExtend()
// ============

bits(N) SignExtend(bits(M) x, integer N)
assert N >= M;
return Replicate(x<M-1>, N-M) : x;

// SignExtend()
// ============

bits(N) SignExtend(bits(M) x)
return SignExtend(x, N);```

### Library pseudocode for shared/functions/common/UInt

```// UInt()
// ======

integer UInt(bits(N) x)
result = 0;
for i = 0 to N-1
if x<i> == '1' then result = result + 2^i;
return result;```

### Library pseudocode for shared/functions/common/ZeroExtend

```// ZeroExtend()
// ============

bits(N) ZeroExtend(bits(M) x, integer N)
assert N >= M;
return Zeros(N-M) : x;

// ZeroExtend()
// ============

bits(N) ZeroExtend(bits(M) x)
return ZeroExtend(x, N);```

### Library pseudocode for shared/functions/common/Zeros

```// Zeros()
// =======

bits(N) Zeros(integer N)
return Replicate('0',N);

// Zeros()
// =======

bits(N) Zeros()
return Zeros(N);```