File: bitwise.cpp

package info (click to toggle)
blitz%2B%2B 1%3A1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,568 kB
  • sloc: cpp: 57,803; python: 1,941; fortran: 1,510; f90: 852; makefile: 833; sh: 321
file content (86 lines) | stat: -rw-r--r-- 1,464 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "testsuite.h"
#include <blitz/array.h>

using namespace blitz;

typedef TinyVector<uint8_t,4> tv;

int main()
{

  {
    // First test bitwise update operators taking a scalar
    const tv a(1,2,4,8);
    tv b(a);
    b>>=1;
    BZTEST(all(b==tv(0,1,2,4)));

    b&=3;
    BZTEST(all(b==tv(0,1,2,0)));

    b|=4;
    BZTEST(all(b==tv(4,5,6,4)));

    b^=3;
    BZTEST(all(b==tv(7,6,5,7)));

    b= ~b;
    BZTEST(all(b==tv(~7,~6,~5,~7)));
  }
  
  {
    // now bitwise update operators taking a tv
    const tv a(1,2,4,8);
    tv b(a);
    b>>= tv(0,1,2,3);
    BZTEST(all(b==tv(1,1,1,1)));

    b<<= tv(3,2,1,0);
    BZTEST(all(b==tv(8,4,2,1)));

    b=tv(0xff, 0xff, 0xff, 0xff);
    b&=tv(0,1,3,5);
    BZTEST(all(b==tv(0,1,3,5)));

    b|=tv(5,3,1,0);
    BZTEST(all(b==tv(5,3,3,5)));

    b^=tv(1,3,5,0);
    BZTEST(all(b==tv(4,0,6,5)));
  }

  {
    // bitwise operators taking a scalar
    const tv a(1,2,4,8);

    BZTEST(all((a>>1)==tv(0,1,2,4)));
    BZTEST(all((a<<1)==tv(2,4,8,16)));

    BZTEST(all((a&3)==tv(1,2,0,0)));

    BZTEST(all((a|4)==tv(5,6,4,12)));

    BZTEST(all((a^3)==tv(2,1,7,11)));
  }

  {
    // bitwise operators taking two tvs
    const tv a(1,2,4,8);
    const tv b(0,1,2,3);
    const tv c(3,1,7,1);

    BZTEST(all((a>>b)==tv(1,1,1,1)));

    BZTEST(all((a<<b)==tv(1,4,16,64)));

    BZTEST(all((a&c)==tv(1,0,4,0)));

    BZTEST(all((a|c)==tv(3,3,7,9)));

    BZTEST(all((a^c)==tv(2,3,3,9)));

  }
  
    return 0;
}