threshold-signextend4to8

Sign extend a 4-bit signed integer to 8 bits.

Function

Copies the sign bit (MSB) to the upper 4 bits:

  • Input: x3 (sign), x2, x1, x0
  • Output: x3, x3, x3, x3, x3, x2, x1, x0

Truth Table (selected examples)

4-bit signed 8-bit signed
0000 0 00000000 0
0001 1 00000001 1
0111 7 00000111 7
1000 -8 11111000 -8
1111 -1 11111111 -1

Architecture

Single layer with 8 neurons. Each neuron passes through one input bit:

  • y0-y3: direct passthrough of x0-x3
  • y4-y7: passthrough of x3 (sign bit)
Output Input Weights Bias
y0 x0 [0,0,0,1] -1
y1 x1 [0,0,1,0] -1
y2 x2 [0,1,0,0] -1
y3 x3 [1,0,0,0] -1
y4 x3 [1,0,0,0] -1
y5 x3 [1,0,0,0] -1
y6 x3 [1,0,0,0] -1
y7 x3 [1,0,0,0] -1

Parameters

Inputs 4
Outputs 8
Neurons 8
Layers 1
Parameters 40
Magnitude 16

Usage

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def signextend4to8(x3, x2, x1, x0):
    inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
    return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
            for i in range(8)]

# Sign extend -3 (1101 in 4-bit) to 8-bit
result = signextend4to8(1, 1, 0, 1)
print(result)  # [1, 0, 1, 1, 1, 1, 1, 1] = 11111101 = -3

License

MIT

Downloads last month
14
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Collection including phanerozoic/threshold-signextend4to8