[Tinyos-devel] Testing FTSP
Miklos Maroti
mmaroti at math.u-szeged.hu
Thu Aug 14 15:14:36 PDT 2008
On Thu, Aug 14, 2008 at 11:28 PM, Branislav Kusy <kusy at stanford.edu> wrote:
>>>> inline uint32_t time16to32(uint16_t time, uint32_t recent_time)
>>>> {
>>>> return recent_time + ((int16_t)time - (int16_t)receint_time);
>>>> }
>>>
>>> this will not work, for example for recent_time=0x18001 and time=0x7FFF,
>>> perhaps, you meant to say
>>> return recent_time + (int16_t)(time - recent_time);
>>
>> These two are the same by the C conversion rules. Probably yours is
>> more readable, but there first the time is converted to 32 bits then
>> the 32-bit subtraction occurs then the result is converted to 16 bit
>> signed value which is extended to 32bit signed back (ignoring the high
>> 2 bytes) and added to the 32-bit signed value. Mine does a 16-bit
>> subtration, but I guess GCC optimizes both to the same OP codes.
>>
>> By the way, with your numbers:
>>
>> receint_time = 0x18001 uint32_t
>> time = 0x7FFF uint16_t
>> (int16_t)receint_time = 0x8001 int16_t
>> (int16_t)time = 0x7FFF int16_t
>> (int16_t)time - (int16_t)receint_time = 0xFFFE int16_t
>> before the + the (int16_t)time - (int16_t)receint_time is converted to
>> 0xFFFFFFFE int32_t
>
> no, it's converted to uint32_t and that's the problem
>
>> recent_time + ((int16_t)time - (int16_t)receint_time) = 0x00017FFF int32_t
>> which is converted to uint32_t
>
> $ cat foo.c
> #include<stdio.h>
> int main()
> {
> unsigned int r = 0x18001;
> unsigned short t = 0x7FFF;
> printf("t: \t%u\n", t);
> printf("r: \t%u\n", r);
> printf("r+(_t-_r):\t%u\n", r+((short)t-(short)r));
> printf("r+_(t-r): \t%u\n", r+(short)(t-r));
> }
>
>
> $ gcc foo.c -o foo && ./foo
> t: 32767
> r: 98305
> r+(_t-_r): 163839
> r+_(t-r): 98303
>
> :)
>
> brano
>
You made me install gcc :) Ok, the porblem is that everything is
converted to int IF the operand does not fit in an int. So the
((short)t-(short)r) ends up as an integer wihch is 32 bit on a PC. Try
this one to see what I mean:
#include<stdio.h>
int main()
{
unsigned long r = 0x18001;
unsigned int t = 0x7FFF;
printf("t: \t%lu\n", t);
printf("r: \t%lu\n", r);
printf("r+(_t-_r):\t%lu\n", r+((int)t-(int)r));
printf("r+_(t-r): \t%lu\n", r+(int)(t-r));
}
On the mote int is 16 bit, so I think the code works there, but I
stand corrected and your version is clearly more robust. Please use
that.
Miklos
More information about the Tinyos-devel
mailing list