help-octave
[Top][All Lists]
Advanced

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

Re: Fixed point shift


From: David Bateman
Subject: Re: Fixed point shift
Date: Thu, 25 Nov 2004 00:12:57 +0100
User-agent: Mutt/1.4.1i

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

-- 
David Bateman                                address@hidden
Motorola CRM                                 +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 1 69 35 77 01 (Fax) 
91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

Attachment: fbitshift.m
Description: Text document


reply via email to

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