Forums - Another compiler bug? 16/32-bit arithmetic

1 post / 0 new
Another compiler bug? 16/32-bit arithmetic
Join Date: 29 Jul 16
Posts: 25
Posted: Wed, 2016-12-07 14:47

This is a bug report concerning compiler errors when manipulating 16-bit values that may result in a 32-bit result.

I have code that converts a Bluetooth time and date representation (Date Time characteristic UUID 0x2A08) into seconds since 1 January 2000, for subsequent time/date arithmetic. I am re-using code that works on other platforms, and which finishes with this:

*seconds = (i * 86400) + (buf[4] * 3600) + (buf[5] * 60) + buf[6] + 1;

where i is the number of days since 1/1/2000 and buf[4], buf[5] and buf[6] are hours, minutes and seconds from the BLE Date Time characteristic. seconds is a uint32 and buf[] is unit8.

The code works for hours up till 6pm but fails at 7pm. It turns out that 6pm is 18*3600=64,800 (<2^16) but 7pm is 19*3600=68,400 (>2^16). It is apparent that the compiler fails to perform arithmetic correctly when (buf[4] * 3600) exceeds 16 bits.

The following refactored code works correctly:

    *seconds = (i * 86400);
    *seconds += ((uint32) buf[4]) * 3600;
    *seconds += (buf[5] * 60) + buf[6] + 1;

Is CSR aware of this bug? Is CSR concerned about this bug? What other compiler bugs exist? Are they documented? Does anyone from CSR monitor this forum? I have not received any comments on my earlier CSR1010 compiler bug post:

  • Up0
  • Down0

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.