# The worlds worst programming language!
#
# The final result needs to be moved to the "r" variable. The input sample is "x".
#
# Instructions
# ============
# add  [output] [a] [b] -> output = a + b
# sub  [output] [a] [b] -> output = a - b
# mul  [output] [a] [b] -> output = a * b
# div  [output] [a] [b] -> output = a / b
# shl  [output] [a] [b] -> output = a << b
# shr  [output] [a] [b] -> output = a >> b
# sig  [output] [b]     -> output = (sign bit in "a" is set) ? 1 : 0
# mov  [output] [a]     -> output = a;
# clip [output] [a]     -> output = clamp(a, -1, 1)
#
# int [name] -> Declare an uninitialized 32-bit integer
# lng [name] -> Declare an uninitialized 64-bit integer
# flt [name] -> Declare an uninitialized 32-bit float

# r = (x - 128) << 8
u8->s16 {
    sub r x 128;
    shl r r 8;
}

# r = (x - 128) << 16
u8->s24 {
    sub r x 128;
    shl r r 16;
}

# r = (x - 128) << 24
u8->s32 {
    sub r x 128;
    shl r r 24;
}

# r = (x / 255) * 2 - 1
#   = (x / 127.5) - 1
#   = (x * 0.00784313725490196078) - 1
u8->f32 {
    mul r x 0.00784313725490196078f;
    sub r r 1;
}



# r = (x >> 8) + 128
s16->u8 {
    shr r x 8;
    add r r 128;
}

# r = x << 8
s16->s24 {
    shl r x 8;
}

# r = x << 16
s16->s32 {
    shl r x 16;
}

# r = ((x + 32768) / 65535) * 2 - 1
#   = (x + 32768) / 32767.5) - 1
#   = (x + 32768) * 0.00003051804379339284) - 1
s16->f32 {
    add (flt)r x 32768;
    mul r r 0.00003051804379339284f;
    sub r r 1;
}



# r = (x >> 16) + 128
s24->u8 {
    shr r x 16;
    add r r 128;
}

# r = x >> 8
s24->s16 {
    shr r x 8;
}

# r = x << 8
s24->s32 {
    shl r x 8;
}

# r = ((x + 8388608) / 16777215) * 2 - 1
#   = (x + 8388608) / 8388607.5) - 1
#   = (x + 8388608) * 0.00000011920929665621) - 1
s24->f32 {
    add (flt)r x 8388608;
    mul r r 0.00000011920929665621f;
    sub r r 1;
}



# r = (x >> 24) + 128
s32->u8 {
    shr r x 24;
    add r r 128;
}

# r = x >> 16
s32->s16 {
    shr r x 16;
}

# r = x >> 8
s32->s24 {
    shr r x 8;
}

# r = ((x + 2147483648) * 0.0000000004656612873077392578125) - 1
s32->f32 {
    dbl t;
    add (dbl)t x 2147483647;
    add t t 1;
    mul t t 0.0000000004656612873077392578125;
    sub (flt)r t 1;
}



# r = (clip(x) + 1) * 127.5
f32->u8 {
    flt c;
    clip c x;
    add c c 1;
    mul (int)r c 127.5f;
}

# r = (clip(x) + 1) * 32767.5 - 32768
f32->s16 {
    flt c;
    clip c x;
    add c c 1;
    mul (int)r c 32767.5f;
    sub r r 32768;
}

# r = (clip(x) + 1) * 8388607.5 - 8388608
f32->s24 {
    flt c;
    clip c x;
    add c c 1;
    mul (int)r c 8388607.5f;
    sub r r 8388608;
}

# r = (clip(x) + 1) * 2147483647.5 - 2147483648
f32->s32 {
    flt c;
    lng t;
    clip c x;
    add c c 1;
    mul (lng)t c 2147483647.5;
    sub t t 2147483647;
    sub (int)r t 1;
}
