How undefined are __builtin_ctz(0) or __builtin_clz(0)?

后端 未结 2 1489
轮回少年
轮回少年 2020-12-14 01:47

Background

For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for <

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 02:33

    Unfortunately, even x86-64 implementations can differ - from Intel's instruction set reference,BSF and BSR, with a source operand value of (0), leaves the destination undefined, and sets the ZF (zero flag). So the behaviour may not be consistent between micro-architectures or, say, AMD and Intel. (I believe AMD leaves the destination unmodified.)

    The newer LZCNT and TZCNT instructions are not ubiquitous. Both are present only as of the Haswell architecture (for Intel).

提交回复
热议问题