Method returns different data as the architecture of android

Asked

Viewed 39 times

1

I created a library for Android and it has a method that returns the milliseconds of the epoch date. I tested on 2 different architectures to validate if the returns were correct.

  • armeabi-v7a: Returns a negative number other than epoch
  • arm64-v8a: Returns correctly

Check if the data types could be influencing the returns and tried to return with long and had the same problem. I am using Clang (llvm) for cross-Compile and am passing the compilation parameters as per standalone toolchain and ABI android.

Compilation parameters

 -target thumbv7-linux-androideabi -march=armv7-a 
 -mthumb -mfpu=vfpv4-d16 -mfpu=vfpv3-d16 -mfpu=neon -msoft-float

Method

#include <sys/time.h>

long getTime(){
  struct timeval tp;
  gettimeofday(&tp, NULL);
  //Calcula os milisegundos da data epoch
  long ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
  return ms;
}

1 answer

1


After some attempts and errors, I identified that it was the type of variables and the calculation for the milliseconds. From what I could understand by the time the compiler was assembling my code block he was using the wrong primitive types long ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; at that point in the code.

I tried to maintain the initial structure by changing the type of ms of long for long long and yet I made the mistake.

It worked with this structure

#include <sys/time.h>

long long getTime(){
  struct timeval tp;
  gettimeofday(&tp, NULL);
  long long sec = tp.tv_sec;
  long long msec = tp.tv_usec;     
  sec *= 1000;        
  msec /= 1000;
  long long ms = sec + msec;
  return ms;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.