Method returns different data as per Android Architecture

I created a library for Android and that 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 types of the data could be influencing the returns and I tried to return with long long and had the same problem. I'm using clang (llvm) for cross compile and I'm passing the build parameters as per Android's standalone toolchain and ABI.

Build 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;
}
Author: Jhonny Rm, 2019-06-22

1 answers

After some trial and error, I identified that it was the type of the variables and the calculation for the milliseconds. From what I could understand at the time that the compiler was assembling my code block it 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 ms type from long to long long and still got the error.

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;
}
 1
Author: Jhonny Rm, 2019-06-25 22:39:29