[Tinyos-devel] Testing FTSP

Branislav Kusy kusy at stanford.edu
Thu Aug 14 14:28:34 PDT 2008


>>> 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


More information about the Tinyos-devel mailing list