Here's the best I have been able to come up with (not debugged yet, so may have errors):
Code: Select all
inline void Clip(int &sample)
//Clips an audio value in 4.28 format to max of 1
{
    int temp;
    asm("clz %0, %1" : "=r" (temp) : "r" (sample));
    if(temp)
    {
        //Signal positive, test for overflow
        if(temp < 4)
        {
            sample = 0x7FFFFFF;
        }
        else
        {
            sample <<= 4;
        }
    }
    else
    {
        //Signal negative
        asm("not %0, %1" : "=r" (temp) : "r" (sample));
        asm("clz %0, %0" : "=r" (temp) :);
        if(temp < 4)
        {
            sample = 0xF8000001;
        }
        else
        {
            sample <<= 4;
        }
    }
}
