[LLVMbugs] [Bug 12411] New: Wrong value when casting double to long long in release arm

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu Mar 29 13:41:23 PDT 2012


http://llvm.org/bugs/show_bug.cgi?id=12411

             Bug #: 12411
           Summary: Wrong value when casting double to long long in
                    release arm
           Product: clang
           Version: unspecified
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: release blocker
          Priority: P
         Component: -New Bugs
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: justincohen at google.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified


Created attachment 8289
  --> http://llvm.org/bugs/attachment.cgi?id=8289
Xcode project

In release builds for iOS, a mixture of double and long long arithmetic and
static_casts yield the wrong value. I only see this when compiling for an iOS
device in release mode.  Debug, or either simulator modes don't have this
problem.  

I can work around the problem by pulling the offending multiplication, addition
and cast into a function in another file.  I don't even have to turn
optimization off in that other file.

Here's a snippet of the code in question.  I've also attached a zip of the
project files to build it for iOS.  Be sure to build for release and for an
actual iOS device, not the simulator.

  long long microsecondsPerSecond = 1000000LL;
  long long windowsEpochDeltaSeconds = 11644473600LL;
  long long windowsEpochDeltaMicroseconds = windowsEpochDeltaSeconds *
microsecondsPerSecond;

  CFGregorianDate date;
  date.second = 19 + 342 / static_cast<double>(1000); // 342 milliseconds
  date.minute = 52;
  date.hour = 2;
  date.day = 10;
  date.month = 12;
  date.year = 2011;

  // Conver to seconds.
  CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, NULL) +
kCFAbsoluteTimeIntervalSince1970;
  // Setting time manually doesn't reproduce the problem.
  // CFAbsoluteTime seconds = 1323485539.34200000762939453125;

  // Convert to microseconds.
  CFAbsoluteTime microseconds = seconds * microsecondsPerSecond;

  // This number should be 12967959139342000 but it is 12967954844374704.
  long long withCast = windowsEpochDeltaMicroseconds + static_cast<long
long>(microseconds);
  printf("withCast:\t\t%lld\n", withCast);

  // This is wrong since there's a loss of precision.  But in this case this
  // is the correct value.
  long long withoutCast = windowsEpochDeltaMicroseconds + microseconds;
  printf("withoutCast:\t%lld\n", withoutCast);

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list