| 1 |
// RUNNABLE_PHOBOS_TEST |
| 2 |
import core.math; |
| 3 |
import core.bitop; |
| 4 |
|
| 5 |
version (DigitalMars) |
| 6 |
{ |
| 7 |
version (X86_64) |
| 8 |
version = AnyX86; |
| 9 |
else version (X86) |
| 10 |
version = AnyX86; |
| 11 |
} |
| 12 |
|
| 13 |
bool isClose(real lhs, real rhs, real maxRelDiff = 1e-09L, real maxAbsDiff = 0.0) |
| 14 |
{ |
| 15 |
if (lhs == rhs) |
| 16 |
return true; |
| 17 |
if (lhs == lhs.infinity || rhs == rhs.infinity || |
| 18 |
lhs == -lhs.infinity || rhs == -rhs.infinity) |
| 19 |
return false; |
| 20 |
|
| 21 |
auto diff = fabs(lhs - rhs); |
| 22 |
return diff <= maxRelDiff*fabs(lhs) |
| 23 |
|| diff <= maxRelDiff*fabs(rhs) |
| 24 |
|| diff <= maxAbsDiff; |
| 25 |
} |
| 26 |
|
| 27 |
/*******************************************/ |
| 28 |
|
| 29 |
void test1() |
| 30 |
{ |
| 31 |
auto f = 6.8L; |
| 32 |
assert(sin(f) == sin(6.8L)); |
| 33 |
static assert(isClose(sin(6.8L), 0x1.f9f8d9aea10fdf1cp-2)); |
| 34 |
|
| 35 |
f = 6.8L; |
| 36 |
assert(cos(f) == cos(6.8L)); |
| 37 |
static assert(isClose(cos(6.8L), 0x1.bd21aaf88dcfa13ap-1)); |
| 38 |
} |
| 39 |
|
| 40 |
/*******************************************/ |
| 41 |
|
| 42 |
void test2() |
| 43 |
{ |
| 44 |
float i = 3; |
| 45 |
i = i ^^ 2; |
| 46 |
assert(i == 9); |
| 47 |
|
| 48 |
int j = 2; |
| 49 |
j = j ^^ 1; |
| 50 |
assert(j == 2); |
| 51 |
|
| 52 |
i = 4; |
| 53 |
i = i ^^ .5; |
| 54 |
assert(i == 2); |
| 55 |
} |
| 56 |
|
| 57 |
/**** https://issues.dlang.org/show_bug.cgi?id=5703 ****/ |
| 58 |
|
| 59 |
static assert({ |
| 60 |
int a = 0x80; |
| 61 |
int f = bsf(a); |
| 62 |
int r = bsr(a); |
| 63 |
a = 0x22; |
| 64 |
assert(bsf(a)==1); |
| 65 |
assert(bsr(a)==5); |
| 66 |
a = 0x8000000; |
| 67 |
assert(bsf(a)==27); |
| 68 |
assert(bsr(a)==27); |
| 69 |
a = 0x13f562c0; |
| 70 |
assert(bsf(a) == 6); |
| 71 |
assert(bsr(a) == 28); |
| 72 |
assert(bswap(0xAABBCCDD) == 0xDDCCBBAA); |
| 73 |
return true; |
| 74 |
}()); |
| 75 |
|
| 76 |
/*******************************************/ |
| 77 |
|
| 78 |
void test3() |
| 79 |
{ |
| 80 |
version (AnyX86) |
| 81 |
{ |
| 82 |
static assert( _popcnt( cast(ushort)0 ) == 0 ); |
| 83 |
static assert( _popcnt( cast(ushort)7 ) == 3 ); |
| 84 |
static assert( _popcnt( cast(ushort)0xAA )== 4); |
| 85 |
static assert( _popcnt( cast(ushort)0xFFFF ) == 16 ); |
| 86 |
static assert( _popcnt( cast(ushort)0xCCCC ) == 8 ); |
| 87 |
static assert( _popcnt( cast(ushort)0x7777 ) == 12 ); |
| 88 |
static assert( _popcnt( cast(uint)0 ) == 0 ); |
| 89 |
static assert( _popcnt( cast(uint)7 ) == 3 ); |
| 90 |
static assert( _popcnt( cast(uint)0xAA )== 4); |
| 91 |
static assert( _popcnt( cast(uint)0x8421_1248 ) == 8 ); |
| 92 |
static assert( _popcnt( cast(uint)0xFFFF_FFFF ) == 32 ); |
| 93 |
static assert( _popcnt( cast(uint)0xCCCC_CCCC ) == 16 ); |
| 94 |
static assert( _popcnt( cast(uint)0x7777_7777 ) == 24 ); |
| 95 |
version (X86_64) |
| 96 |
{ |
| 97 |
static assert( _popcnt( cast(ulong)0 ) == 0 ); |
| 98 |
static assert( _popcnt( cast(ulong)7 ) == 3 ); |
| 99 |
static assert( _popcnt( cast(ulong)0xAA )== 4); |
| 100 |
static assert( _popcnt( cast(ulong)0x8421_1248 ) == 8 ); |
| 101 |
static assert( _popcnt( cast(ulong)0xFFFF_FFFF_FFFF_FFFF ) == 64 ); |
| 102 |
static assert( _popcnt( cast(ulong)0xCCCC_CCCC_CCCC_CCCC ) == 32 ); |
| 103 |
static assert( _popcnt( cast(ulong)0x7777_7777_7777_7777 ) == 48 ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/*******************************************/ |
| 109 |
|
| 110 |
int main() |
| 111 |
{ |
| 112 |
test1(); |
| 113 |
test2(); |
| 114 |
test3(); |
| 115 |
return 0; |
| 116 |
} |