Annotation of nono/lib/bitops.h, revision 1.1.1.4

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
1.1.1.2   root        7: //
                      8: // 各種ビット操作
                      9: //
                     10: 
1.1       root       11: #pragma once
                     12: 
                     13: #include "header.h"
                     14: 
1.1.1.4 ! root       15: #if defined(HAVE___BUILTIN_BITREVERSE8)
        !            16: #define bitrev(x)      __builtin_bitreverse8(x)
        !            17: #else
1.1       root       18: extern const uint8 bitrev_table[256];
                     19: 
1.1.1.4 ! root       20: // 8ビットを左右反転する
1.1       root       21: static inline uint8
                     22: bitrev(uint8 x)
                     23: {
                     24:        return bitrev_table[x];
                     25: }
1.1.1.4 ! root       26: #endif
        !            27: 
        !            28: // 左ローテート
        !            29: #if defined(HAVE___BUILTIN_ROTATELEFT32)
        !            30: #define ROL32(a, n)    __builtin_rotateleft32(a, n)
        !            31: #else
        !            32: static inline uint32
        !            33: ROL32(uint32 a, int n)
        !            34: {
        !            35:        n &= 31;
        !            36:        if (__predict_false(n == 0)) {
        !            37:                return a;
        !            38:        } else {
        !            39:                return (a << n) | (a >> (32 - n));
        !            40:        }
        !            41: }
        !            42: #endif
1.1       root       43: 
1.1.1.2   root       44: // 右ローテート
1.1.1.4 ! root       45: #if defined(HAVE___BUILTIN_ROTATERIGHT32)
        !            46: #define ROR32(a, n)    __builtin_rotateright32(a, n)
        !            47: #else
1.1.1.2   root       48: static inline uint32
                     49: ROR32(uint32 a, int n)
                     50: {
                     51:        n &= 31;
1.1.1.4 ! root       52:        if (__predict_false(n == 0)) {
1.1.1.2   root       53:                return a;
                     54:        } else {
                     55:                return (a >> n) | (a << (32 - n));
                     56:        }
                     57: }
1.1.1.4 ! root       58: #endif
1.1.1.2   root       59: 
1.1       root       60: // mask で指定したいずれかのビットが 0 -> 1 に変化したら true
                     61: static constexpr bool
                     62: bit_rising(uint before, uint after, uint mask)
                     63: {
1.1.1.3   root       64:        // before が 0 で mask が 1 のビットだけを立てる。
                     65:        // それが 1 で after が 1 なら立ち上がり。
1.1       root       66:        return (~before & mask) & after;
                     67: }
                     68: 
                     69: // mask で指定したいずれかのビットが 1 -> 0 に変化したら true
                     70: static constexpr bool
                     71: bit_falling(uint before, uint after, uint mask)
                     72: {
1.1.1.3   root       73:        // before が 1 で mask が 1 のビットだけを立てる。
                     74:        // それが 1 で after が 0 なら立ち下がり。
1.1       root       75:        return (before & mask) & ~after;
                     76: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.