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

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: 
                     15: extern const uint8 bitrev_table[256];
                     16: 
                     17: // 8ビットを左右反転する (定数用)
                     18: static constexpr uint8 _rev8(uint8 x)
                     19: {
                     20:        x = ((x & 0x0f) << 4) | ((x >> 4) & 0x0f);
                     21:        x = ((x & 0x33) << 2) | ((x >> 2) & 0x33);
                     22:        x = ((x & 0x55) << 1) | ((x >> 1) & 0x55);
                     23:        return x;
                     24: }
                     25: 
                     26: // 8ビットを左右反転する (実行時用)
                     27: static inline uint8
                     28: bitrev(uint8 x)
                     29: {
                     30:        return bitrev_table[x];
                     31: }
                     32: 
1.1.1.2   root       33: // 右ローテート
                     34: static inline uint32
                     35: ROR32(uint32 a, int n)
                     36: {
                     37:        // TODO: 各種コンパイラごとにビルトインがあったりなかったりするらしい
                     38:        n &= 31;
                     39:        if (n == 0) {
                     40:                return a;
                     41:        } else {
                     42:                return (a >> n) | (a << (32 - n));
                     43:        }
                     44: }
                     45: 
1.1       root       46: // mask で指定したいずれかのビットが 0 -> 1 に変化したら true
                     47: static constexpr bool
                     48: bit_rising(uint before, uint after, uint mask)
                     49: {
1.1.1.3 ! root       50:        // before が 0 で mask が 1 のビットだけを立てる。
        !            51:        // それが 1 で after が 1 なら立ち上がり。
1.1       root       52:        return (~before & mask) & after;
                     53: }
                     54: 
                     55: // mask で指定したいずれかのビットが 1 -> 0 に変化したら true
                     56: static constexpr bool
                     57: bit_falling(uint before, uint after, uint mask)
                     58: {
1.1.1.3 ! root       59:        // before が 1 で mask が 1 のビットだけを立てる。
        !            60:        // それが 1 で after が 0 なら立ち下がり。
1.1       root       61:        return (before & mask) & ~after;
                     62: }

unix.superglobalmegacorp.com

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