0

Basically, the question is what instruction takes less time to execute (or they take the exact same time):

add rax, rbx
; or
or rax, rbx

For example, if I want to access efficiently a virtual CPU core and OR executes more fast, than the code I should write would be something like this (C):

struct CPUCore { /* ... */ };
sizeof(CPUCore); // 64 e.g.

// normal allocation
CPUCore* allocNormal() {
    CPUCore* inst = (CPUCore*)malloc(sizeof(CPUCore));
    return inst;
};

// aligning allocation
struct Result {
    // free memory by 'res.free(block)'
    // use object by 'res.ptr'
    CPUCore* block;
    CPUCore* ptr;
};
CPUCore* allocAlign() {
    // allocating 2 times the bytes in order to ensure 64 byte block
    // with address & 63 == 0, so instead of 'ptr + shift'
    // we can use 'ptr | shift'
    Result ret;
    ret.block = malloc(sizeof(CPUCore) * 2);
    ret.ptr = ret.block + 0x40 - (ret.block & 0x3F);
    return ret;
};
Tom
  • 1
  • 2
    They run at the same speed even back to 8086 ([1](https://www.oocities.org/mc_introtocomputers/Instruction_Timing.PDF)). – xiver77 Jul 18 '22 at 08:52
  • 2
    why don't just check the [instruction latency/throughput](https://agner.org/optimize/instruction_tables.pdf)? The only case where `and` and `or` differ is in some newer microachitectures where [`and` can macro-fuse but `or` can't](https://stackoverflow.com/a/33724806/995714) – phuclv Jul 18 '22 at 10:41
  • @phuclv OP wrote a**d**d. – xiver77 Jul 18 '22 at 11:58
  • 1
    @xiver77: Same applies to `add` macro-fusion on SnB-family (like sub/cmp), but not `or`. – Peter Cordes Jul 18 '22 at 13:05
  • 1
    C `+` operators don't have to compile to `add`. They can often be combined with other work into an LEA, so yes it can make sense to use `+` instead of `|` for this reason. Check compiler asm output. – Peter Cordes Jul 18 '22 at 13:06

0 Answers0