File: bit_helpers.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (52 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright (C) 2019-2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once
#include <cassert>
#include <cstdint>
#include <limits>

namespace NEO {

constexpr bool isBitSet(uint64_t field, uint64_t bitPosition) {
    assert(bitPosition < std::numeric_limits<uint64_t>::digits); // undefined behavior
    return (field & (1ull << bitPosition));
}

constexpr bool isAnyBitSet(uint64_t field, uint64_t checkedBits) {
    return ((field & checkedBits) != 0);
}

constexpr bool isValueSet(uint64_t field, uint64_t value) {
    assert(value != 0);
    return ((field & value) == value);
}

constexpr bool isFieldValid(uint64_t field, uint64_t acceptedBits) {
    return ((field & (~acceptedBits)) == 0);
}

constexpr uint64_t setBits(uint64_t field, bool newValue, uint64_t bitsToModify) {
    if (newValue) {
        return (field | bitsToModify);
    }
    return (field & (~bitsToModify));
}

constexpr uint64_t shiftLeftBy(uint64_t bitPosition) {
    return (1ull << bitPosition);
}

constexpr uint32_t getMostSignificantSetBitIndex(uint64_t field) {
    uint32_t index = 0;
    while (field >>= 1) {
        index++;
    }
    return index;
}

} // namespace NEO