File: bit_test.py

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (31 lines) | stat: -rw-r--r-- 995 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
from bitcpp import bitcpp
from bitpy import bitpy
import numpy as np


def test(fn):
    # Note that in Halide, Buffer<bool> and Buffer<uint8> have identical memory
    # layout -- a bool takes an entire byte in a buffer -- but distinct types,
    # so we must construct a Buffer here that has that correct type. (If we
    # passed in a uint8 buffer, we'd fail with 'wrong type' exception.)
    #
    # Python's `array` module doesn't support boolean entries, but libraries
    # that support Buffer Protocol (eg numpy.ndarray) do, so we'll use one of those.
    input_bools = np.ndarray([4], dtype=bool)
    output_bools = np.ndarray([4], dtype=bool)

    for i in range(0, 4):
        input_bools[i] = (i & 1) != 0

    fn(input_bools, True, output_bools)
    for i in range(0, 4):
        assert output_bools[i]

    fn(input_bools, False, output_bools)
    for i in range(0, 4):
        assert output_bools[i] == ((i & 1) != 0)


if __name__ == "__main__":
    test(bitcpp)
    test(bitpy)