help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fixed point shift


From: Peter Jensen
Subject: Re: Fixed point shift
Date: Thu, 25 Nov 2004 21:42:25 +0000
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.3) Gecko/20040910

David,

Excellent, Many thanks.

I will let you know now things are progressing.

Thanks

David Bateman wrote:

Hi Peter,

Good to actually see a report of use of this package... In the code
for the scalar real fixed-point values, there are two left and two
right shift operators/functions that differ in how they treat the
signbit.. The prototypes are

FixedPoint operator <<= (const int s)
FixedPoint operator << (const FixedPoint &x, const int s)

FixedPoint operator >>= (const int s)
FixedPoint operator >> (const FixedPoint &x, const int s)

FixedPoint rshift (const FixedPoint &x, int s)
FixedPoint lshift (const FixedPoint &x, int s)

Read the docs for the descriptions of how these work, as the current API
to the code is fully discussed..

I didn't create the equivalent operators/functions in the other three
classes. Also, the bitshift stuff is handled through a function called
bitshift in octave rather than with a "<<" or ">>" operator which have
other meanings in octave (file i/o).
How do you want to treat the overflow/underflow in the shift? Do you want
to respect the signbit, or is it just another bit to shift? If you want
to respect the signbit, and just let the overflow, then something like

function b = fbitshift (x, k)
 b = x;
 if (k < 0)
   while (k)
     b = b * fixed(1,1,0.5);
     k++;
   endwhile
 else
   while (k)
     b = b * fixed(2,0,2);
     k--;
   endwhile
 endif
endfunction

while work as long as the integer part of the fixed-point value is
represented by two bits, and teh decimal by at least one bit. The
reason to do it with a while loop is to avoid restrictions on the
number of bits in each part.

If you want consistent behaviour with octave's bitshift function, then
the fact is instead of writing a fixed-point version of these
functions, its probably easier to cast the fixed-points as int8,
int16, int32 or int64 within octave, perform the bitshft with the
existing code and then mask off the irrelevant bits...  The attached
file for an idea of how this might be done. The function needs a little tidying up and testing, but there is no reason not to include something like this in octave-forge. Maybe though it should have some
additional flag for issues of how to treat the signbit and the over/under
flow...

This could even be overloaded with dispatch over the existing bitshift
function and the details of it implementation hidden from the user..
With something like

## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed scalar")
## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed matrix")
## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed complex")
## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed complex matrix")

Cheers
David

------------------------------------------------------------------------

## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed scalar") ## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed matrix") ## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed complex") ## PKG_ADD: dispatch ("bitshift", "fbitshift", "fixed complex matrix") function b = fbitshift (a, k, n) if (!isfixed (a)) error ("fbitshift: argument must be a fixed-point value") else ## Find maximum of is+ds
   sz = max((a.int + a.dec)(:))
   mask = sz + 1;
   if (nargin == 3)
     mask = min(n, mask);
   endif

   ## Be careful to add back in the signbit and shift left by the decimal
   x = (abs(real(a.x)) + (real(a.sign) == -1) * (2 .^ real(a.int))) .* (2 .^ 
real(a.dec));

   if (iscomplex (a))
     y = (abs(imag(a.x)) + (imag(a.sign) == -1) * (2 .^ imag(a.int))) .*  (2 .^ 
imag(a.dec));
   endif

   if (sz < 8)
     x = uint8(x);
     if (iscomplex(a))
        y = uint8(y);
endif elseif (sz < 16)
     x = uint16(x);
     if (iscomplex(a))
        y = uint16(y);
endif elseif (sz < 32)
     x = uint32(x);
     if (iscomplex(a))
        y = int32(y);
endif else
     x = uint64(x);
     if (iscomplex(a))
        y = uint64(y);
endif endif

   x = bitshift (x, k, mask);
   b = fixed (a.int, a.dec, double(x) ./ (2 .^ a.dec));
endif
endfunction




-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]