Skip to content

wrong processing of uint64 #109

Open
@ricardomsmonteiro

Description

@ricardomsmonteiro

When manipulating uint64_t, variables declared in a class in a translation unit, the results of operations are wrong if there is an access to other same class variables.

3 files were used for the purpose of showing this in the same arduino sketch:
issue.ino:

#include "Arduino.h"
#include "tu.h"

#define ISSUE_HAPPENS

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  foo.baz = 2;

#if defined(ISSUE_HAPPENS)
  if( (foo.bar  & (1ULL << 9)) && (foo.bar  & (1ULL << 10)) )
    Serial.println("right");
  else
    Serial.println("wrong");
#else
  uint64_t a = 0x8000000010000602;
  uint64_t b = 0x8000000010000602;
  
  if( (a  & (1ULL << 9)) && (b  & (1ULL << 10)) )
    Serial.println("right");
  else
    Serial.println("wrong");
#endif
  
  delay(1000);
}

tu.cpp:

#include "tu.h"

Foo foo;

tu.h

#include <Arduino.h>

#pragma once

class Foo
{
public:
  uint64_t bar = 0x8000000010000602;

  uint8_t baz = 4;
};

extern Foo foo;

The output, when #define ISSUE_HAPPENS is uncommented, is wrong. When it is commented it is right as it is supposed to be.

This happens in arduino 1.8.8 but not in arduino 1.8.5. Using a different tool, sloeber arduino plugin for eclipse, it happens when the toolchain has gcc version 5.4.0 but not when it has the version 4.9.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions