schwehr / generic-sensor-format

Sonar Generic Sensor Format (gsf) codec
Other
13 stars 8 forks source link

Simplification of TimePointToSeconds #88

Open HowardHinnant opened 8 years ago

HowardHinnant commented 8 years ago

Free code:

// src/gsfxx/util.cc

double TimePointToSeconds(system_clock::time_point time_point) {
  return duration<double>{time_point.time_since_epoch()}.count();
}

What you have is correct, but this has <chrono> do the conversion, and is actually smaller and a little more efficient:

    .cfi_def_cfa_register %rbp
    cvtsi2sdq   %rdi, %xmm0
    divsd   LCPI0_0(%rip), %xmm0
    popq    %rbp
    req

vs:

    .cfi_def_cfa_register %rbp
    imulq   $1000, %rdi, %rcx       ## imm = 0x3E8
    movabsq $4835703278458516699, %rdx ## imm = 0x431BDE82D7B634DB
    movq    %rdi, %rax
    imulq   %rdx
    movq    %rdx, %rax
    shrq    $63, %rax
    sarq    $18, %rdx
    addq    %rax, %rdx
    cvtsi2sdq   %rdx, %xmm1
    cvtsi2sdq   %rcx, %xmm2
    movsd   LCPI0_0(%rip), %xmm0    ## xmm0 = mem[0],zero
    mulsd   %xmm1, %xmm0
    addsd   %xmm2, %xmm0
    mulsd   LCPI0_1(%rip), %xmm0
    addsd   %xmm1, %xmm0
    popq    %rbp
    req

Explanation: duration<double> is a double-based duration representing seconds. The proposed code gets the duration out of time_point and converts it to double-based seconds, and then retrieves the double with .count().