Pages: 1 [2]   Go Down
Author Topic: Signed number grief.  (Read 1801 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Noting that -1<<10 will generate the correct bit mask to sign-extend the ten-bit quantity to whatever word size… here you can see Bitlash doing the calculation on OS X with a 64 bit word size:
Code:
$ bitlash
bitlash here! v2.0 (c) 2012 Bill Roy -type HELP- 1000 bytes free
> print -1<<10:x
FFFFFFFFFFFFFC00

So, dhenry's proposal can be generalized to:

Code:
(signed short) ((x & 0x0200)?(x|(-1<<10)):x)

-br
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see how this helps:
Code:
typedef long device_t;
...
const device_t deviceWordLength = sizeof(device_t)<<2;
deviceWordLength is a characteristic of the input device.  You can call it the length of the device's reply, or you can call it the position of the reply's sign bit.  In this code, it depends only on the characteristics of the long data type.

What am I missing?
Sorry, I missed what that bit was doing.

Ignore me, I know nothing smiley-razz
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A little test code.
Code:
> ls
function extend10 {if arg(1)&0x200 return arg(1)|(-1<<10); else return arg(1); };
function px10 {print extend10(arg(1)):x;};
> px10(0x200)
FFFFFFFFFFFFFE00
> px10(0x1ff)
1FF
> px10(0x10)
10

-br
Logged

USA
Offline Offline
Sr. Member
****
Karma: 17
Posts: 386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A slow imprecise floating point divide to replace a fast precise bit shift.
I think the poster refers to this line:
Code:
    someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));

Looking at these concerns one by one, in reverse order:
  • floating point divide:  Indeed, there's a floating point division explicitly coded in that line.  But, the arguments are all constants, along with everything else inside the parentheses.  Based on the explorations of compiler's output for other complicated floating point expressions, I believe that the compiler will detect that the expression evaluates to a constant, optimize the internal calculations out of existence, and wind up with a single floating point constant. I don't see that there will be a division in the executed code.
  • imprecise:  I don't think so.  The operation that's being replaced is a bit shift right, equivalent to division by 2N, for an integer N.  The floating point representation of 2-N is a sign bit, an exponent, an implied one, and 24 zeroes.  Division will result in a change of exponent only, and won't result in any loss of precision.  It's true that, in general, precision is lost in a floating point division, but not in this case, and that's a not consequence of the particular parameters of this calculation, but rather of the very operation that's being performed.
  • slow:  Yes, a floating point division is slow.  But, because everything involved is a constant, it happens at compile time, rather than at runtime.  The Arduino won't execute a floating point division based on this code.

If you're skeptical about the compiler optimizing away the division, you can force it like this:
Code:
...
const float deviceScaleFactorComplete = deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits)));
...
    someFloatVariable = x * deviceScaleFactorComplete;

Quote
... just trying to turn a 10-bit value into 16 ...
Yes.  But, the OP expressed concern about the fact that some of the proposed techniques rely on the fact that an integer has a 16-bit representation - an implementation-dependent size - and wanted to know how to make his code more general.  That concern is of more than merely academic interest, because "on the Arduino Due, an int stores a 32-bit (4-byte) value." See it here:  http://arduino.cc/en/Reference/Int.  It's not at all unlikely that some of us will be porting code we write today to that platform, so there's certainly value in coding for the general case.

Quote
... load up the 10 bits into a 16-bit int and extend (copy) the sign in bit 9 from bit 10 to bit 15 ...
It's even easier than that.  The device provides a 10-bit signed number, left-justified in a 16-bit word.  For a 16-bit platform, it's as simple as loading the value into a signed int, and shifting it six bits to the right; the sign bit is automatically extended.  But that's not what the OP asked for:  he asked for help in writing code that could easily accommodate a different number of significant bits from the input device, and he expressed concern about algorithms that relied on a 16-bit integer size.  Because the issue has practical implications, it's getting a bit of attention.  It's certainly made me wonder how deeply I've embedded implementation-dependent parameters into some of my own favorite code.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I put this to some exercise:

Code:
#define FLP(pin)  {PORTD |= 1<<1; PORTD &=~1<<1;}

void setup(void) {
  PORTD &=~(1<<1);
  DDRD |= (1<<1);
}

const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = -(1l << (deviceWordLength - 1));
const float deviceScaleFactor = 0.25;
int x=511;
float someFloatVariable;

void loop(void) {

  FLP(OUT_PIN); FLP(OUT_PIN);  FLP(OUT_PIN); //flip out pin
  if (x & deviceMask) {
    x |= deviceMask;
  }
  someFloatVariable = x * (deviceScaleFactor * (1.0/(1 << (deviceWordLength-deviceBits))));
  FLP(OUT_PIN); FLP(OUT_PIN); //flip out pin
  someFloatVariable = (signed short) ((x & 0x0200)?(x|(-1<<10)):x);
  FLP(OUT_PIN);
  delay(10);
}

 

Question: how much time does the floating point approach take than the integer shift approach?
1: less time;
2: the same;
3: 8x;
4: all the above.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... load up the 10 bits into a 16-bit int and extend (copy) the sign in bit 9 from bit 10 to bit 15 ...
It's even easier than that.  The device provides a 10-bit signed number, left-justified in a 16-bit word.  For a 16-bit platform, it's as simple as loading the value into a signed int, and shifting it six bits to the right; the sign bit is automatically extended.  But that's not what the OP asked for:  he asked for help in writing code that could easily accommodate a different number of significant bits from the input device, and he expressed concern about algorithms that relied on a 16-bit integer size.  Because the issue has practical implications, it's getting a bit of attention.  It's certainly made me wonder how deeply I've embedded implementation-dependent parameters into some of my own favorite code.

For a 16-bit platform, it's as simple as loading the value into a signed int ** with the top bit as bit 15, maybe load it direct, shift 6 bits left after determining 6 bits for 16 bit int or some other way but there has to be that trick step or the next trick won't work **, and shifting it six bits to the right; the sign bit is automatically extended.

When you put in all the steps you get the true length of the code. Then the other simple ways don't seem so bad.

The best routine would run completely on CPU registers in less a microsecond, but that wouldn't be 'generic'.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For a 16-bit platform,

Try it and you would be surprised.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why? Did 16-bit platforms change? I wrote a lot of code on 8080's and 8088's and never had trouble with bits then. Come to think, I did a lot of 16 bit word code on 6502's (8 bit CPU, 16 bit languages) as well. No trouble with bits, they have bit rotate as well as bit shift commands.

When I use 16-bit ints on Arduino that uses 16-bit addressing, what is the platform? Isn't that set by address length?

Note that I quoted someone else in that post and added a step I saw left out. But yah that RTC doesn't give a straight 10 bits, does it? The bits have to practically be mapped to the target. Good news is 32-bits only needs either 0x0000 or 0xFFFF added to a 16-bit solution.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

USA
Offline Offline
Sr. Member
****
Karma: 17
Posts: 386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Having reflected a bit more on this, I see that in this code:
Code:
const long deviceBits = 10;
const long deviceWordLength = 16;
const long deviceMask = (1 << deviceBits) - 1;
there's no need for any of the constants to be declared long.  The counting constants, describing length of the significant data and length of the word it resides in, can obviously be byte-sized, at least until we start using 257-bit integers.  The mask doesn't need to be any bigger than the input data word, in this case int

Hmmph.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hmmph.

How about this piece:

Code:
(1 << deviceBits)

smiley
Logged

USA
Offline Offline
Sr. Member
****
Karma: 17
Posts: 386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's not the mask I wanted.  I wanted this one:
Code:
const long deviceMask = -(1 << (deviceWordLength - 1));
The mask isolates the sign bit for testing, and extends the sign bit when the sign bit is set.  It needs to be as long as the type of the input, which is int in this case, but it doesn't need to be longer.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should be int, not long, then.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That's not the mask I wanted.

I think you will find that a generic approach is not as optimal as having a series of specific approaches (one for 10-bits, one for 12-bits, etc.) that you can link in for your specific applications.

If those numbers came from an adc module, many allow left-aligned adcs making the shift approach a lot more appealing.
Logged

Pages: 1 [2]   Go Up
Jump to: