File: builtin.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (116 lines) | stat: -rw-r--r-- 3,021 bytes parent folder | download | duplicates (4)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// RUNNABLE_PHOBOS_TEST
import core.math;
import core.bitop;

version (DigitalMars)
{
    version (X86_64)
        version = AnyX86;
    else version (X86)
        version = AnyX86;
}

bool isClose(real lhs, real rhs, real maxRelDiff = 1e-09L, real maxAbsDiff = 0.0)
{
    if (lhs == rhs)
        return true;
    if (lhs == lhs.infinity || rhs == rhs.infinity ||
        lhs == -lhs.infinity || rhs == -rhs.infinity)
        return false;

    auto diff = fabs(lhs - rhs);
    return diff <= maxRelDiff*fabs(lhs)
        || diff <= maxRelDiff*fabs(rhs)
        || diff <= maxAbsDiff;
}

/*******************************************/

void test1()
{
    auto f = 6.8L;
    assert(sin(f) == sin(6.8L));
    static assert(isClose(sin(6.8L), 0x1.f9f8d9aea10fdf1cp-2));

    f = 6.8L;
    assert(cos(f) == cos(6.8L));
    static assert(isClose(cos(6.8L), 0x1.bd21aaf88dcfa13ap-1));
}

/*******************************************/

void test2()
{
    float i = 3;
    i = i ^^ 2;
    assert(i == 9);

    int j = 2;
    j = j ^^ 1;
    assert(j == 2);

    i = 4;
    i = i ^^ .5;
    assert(i == 2);
}

/**** https://issues.dlang.org/show_bug.cgi?id=5703 ****/

static assert({
    int a = 0x80;
    int f = bsf(a);
    int r = bsr(a);
    a = 0x22;
    assert(bsf(a)==1);
    assert(bsr(a)==5);
    a = 0x8000000;
    assert(bsf(a)==27);
    assert(bsr(a)==27);
    a = 0x13f562c0;
    assert(bsf(a) == 6);
    assert(bsr(a) == 28);
    assert(bswap(0xAABBCCDD) == 0xDDCCBBAA);
    return true;
}());

/*******************************************/

void test3()
{
    version (AnyX86)
    {
        static assert( _popcnt( cast(ushort)0 ) == 0 );
        static assert( _popcnt( cast(ushort)7 ) == 3 );
        static assert( _popcnt( cast(ushort)0xAA )== 4);
        static assert( _popcnt( cast(ushort)0xFFFF ) == 16 );
        static assert( _popcnt( cast(ushort)0xCCCC ) == 8 );
        static assert( _popcnt( cast(ushort)0x7777 ) == 12 );
        static assert( _popcnt( cast(uint)0 ) == 0 );
        static assert( _popcnt( cast(uint)7 ) == 3 );
        static assert( _popcnt( cast(uint)0xAA )== 4);
        static assert( _popcnt( cast(uint)0x8421_1248 ) == 8 );
        static assert( _popcnt( cast(uint)0xFFFF_FFFF ) == 32 );
        static assert( _popcnt( cast(uint)0xCCCC_CCCC ) == 16 );
        static assert( _popcnt( cast(uint)0x7777_7777 ) == 24 );
        version (X86_64)
        {
            static assert( _popcnt( cast(ulong)0 ) == 0 );
            static assert( _popcnt( cast(ulong)7 ) == 3 );
            static assert( _popcnt( cast(ulong)0xAA )== 4);
            static assert( _popcnt( cast(ulong)0x8421_1248 ) == 8 );
            static assert( _popcnt( cast(ulong)0xFFFF_FFFF_FFFF_FFFF ) == 64 );
            static assert( _popcnt( cast(ulong)0xCCCC_CCCC_CCCC_CCCC ) == 32 );
            static assert( _popcnt( cast(ulong)0x7777_7777_7777_7777 ) == 48 );
        }
    }
}

/*******************************************/

int main()
{
    test1();
    test2();
    test3();
    return 0;
}