V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ruiyi1994
V2EX  ›  C++

__builtin_expect(!(x), 1) 这里的 ! 有什么特殊作用吗?

  •  
  •   ruiyi1994 · Jul 18, 2021 · 2324 views
    This topic created in 1747 days ago, the information mentioned may be changed or developed.

    阅读源码的时候看到一个宏, 不太理解为啥要加 !!, 就是下面的 __builtin_expect 负负得正,不是不用加吗

    #if (defined(__GNUC__) && __GNUC__ >= 3) || defined(__clang__)
    #define  unlikely(x)  __builtin_expect(!!(x), 0)
    #else
    #define  unlikely(x)  (x)
    #endif
    

    抱歉, 标题打了两个 ! , 结果被吞了一个

    4 replies    2021-07-18 12:18:25 +08:00
    ai277014717
        1
    ai277014717  
       Jul 18, 2021   ❤️ 1
    这样可以保证输入非 0 即 1
    BrettD
        2
    BrettD  
       Jul 18, 2021 via iPhone   ❤️ 1
    把任何数值变成只有两个可能:0 和 1
    ruiyi1994
        3
    ruiyi1994  
    OP
       Jul 18, 2021
    @ai277014717 @BrettD 感谢, 学到了.
    Baymaxyu
        4
    Baymaxyu  
       Jul 18, 2021 via iPhone
    这样就可以转化成布尔类型(内核代码里好多这样的)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1137 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.