After testing, about 10 billion times, if imm640.1 nanoseconds faster than m64for AMD64, m64it seems faster, but I really don't understand. Is the address val_ptrin the following code an immediate value?
.section __TEXT,__text,regular,pure_instructions
.code64
.intel_syntax noprefix
.macosx_version_min 10,13,0
.globl _test1
.globl _test2
_test1:
movabs rax, 0xDEADBEEFFEEDFACE
ret
_test2:
mov rax, qword ptr [rip + val_ptr]
ret
.section __DATA,__data
val_ptr:
.quad 0xDEADBEEFFEEDFACE
Measurement Code:
#include <stdio.h> // For printf
#include <stdlib.h> // For EXIT_SUCCESS
#include <math.h> // For fabs
#include <stdint.h> // For uint64_t
#include <stddef.h> // For size_t
#include <string.h> // For memset
#include <mach/mach_time.h> // For time stuff
#define FUNCTION_COUNT 2
#define TEST_COUNT 0x10000000
typedef uint64_t rettype_t;
typedef rettype_t(*function_t)();
rettype_t test1();
rettype_t test2();
int main() {
mach_timebase_info_data_t info;
mach_timebase_info(&info);
double sums[FUNCTION_COUNT];
memset(&sums, 0, FUNCTION_COUNT * sizeof (double));
function_t functions[FUNCTION_COUNT] = {test1, test2};
rettype_t results[FUNCTION_COUNT];
for (size_t test_fn = 0; test_fn < FUNCTION_COUNT; test_fn++) {
for (size_t test_num = 0; test_num < TEST_COUNT; test_num++) {
double nanoseconds = mach_absolute_time();
results[test_fn] = functions[test_fn]();
nanoseconds = mach_absolute_time() - nanoseconds;
nanoseconds *= info.numer;
nanoseconds /= info.denom;
sums[test_fn] += nanoseconds;
}
}
for (size_t i = 0; i < FUNCTION_COUNT; i++) {
sums[i] /= TEST_COUNT;
}
if (FUNCTION_COUNT == 2) {
printf("Test 1 took %f nanoseconds average.\n", sums[0]);
printf("Test 2 took %f nanoseconds average.\n", sums[1]);
printf("Test %d was faster, with %f nanoseconds difference\n", sums[0] < sums[1] ? 1 : 2, fabs(sums[0] - sums[1]));
} else {
for (size_t fn_i = 0; fn_i < FUNCTION_COUNT; fn_i++) {
printf("Test %zu took %f clock ticks average.\n", fn_i + 1, sums[fn_i]);
}
}
return EXIT_SUCCESS;
}
So what is really faster, m64or imm64?
By the way, I use Intel I7 Ivy Bridge and DDR3 RAM. I am running macOS High Sierra.
EDIT : I entered the instructions retand now imm64turned out to be faster.
source
share