| 1 |
/* PERMUTE_ARGS: -O |
| 2 |
* Test floating point code generation |
| 3 |
*/ |
| 4 |
|
| 5 |
import core.stdc.stdio; |
| 6 |
import core.stdc.stdlib; |
| 7 |
|
| 8 |
double value_1() { |
| 9 |
return 1; |
| 10 |
} |
| 11 |
|
| 12 |
double value_2() { |
| 13 |
return 2; |
| 14 |
} |
| 15 |
|
| 16 |
/***************************************/ |
| 17 |
|
| 18 |
void testcse1(T)() // common subexpressions |
| 19 |
{ |
| 20 |
T a = value_1(); |
| 21 |
T b = value_2(); |
| 22 |
T x = a*a + a*a + a*a + a*a + a*a + a*a + a*a + |
| 23 |
a*b + a*b; |
| 24 |
printf("%g\n", cast(double)x); // destroy scratch reg contents |
| 25 |
T y = a*a + a*a + a*a + a*a + a*a + a*a + a*a + |
| 26 |
a*b + a*b; |
| 27 |
assert(x == 11); |
| 28 |
assert(x == y); |
| 29 |
} |
| 30 |
|
| 31 |
void test240() |
| 32 |
{ |
| 33 |
testcse1!float(); |
| 34 |
testcse1!double(); |
| 35 |
testcse1!real(); |
| 36 |
} |
| 37 |
|
| 38 |
/***************************************/ |
| 39 |
|
| 40 |
void testcse2(T)() // common subexpressions |
| 41 |
{ |
| 42 |
T a = value_1(); |
| 43 |
T b = value_2(); |
| 44 |
T x = a*a + a*a + a*a + a*a + a*a + a*a + a*a + |
| 45 |
a*b + a*b + 1; |
| 46 |
printf("%g\n", cast(double)x); // destroy scratch reg contents |
| 47 |
int i = (a*a + a*a + a*a + a*a + a*a + a*a + a*a + a*b + a*b) != 0; |
| 48 |
assert(i); |
| 49 |
assert(x == 12); |
| 50 |
} |
| 51 |
|
| 52 |
void test241() |
| 53 |
{ |
| 54 |
testcse2!float(); |
| 55 |
testcse2!double(); |
| 56 |
testcse2!real(); |
| 57 |
} |
| 58 |
|
| 59 |
/***************************************/ |
| 60 |
|
| 61 |
void test1(float f) |
| 62 |
{ |
| 63 |
real r = f; |
| 64 |
double d = f; |
| 65 |
} |
| 66 |
|
| 67 |
void test2(long l) |
| 68 |
{ |
| 69 |
real r = l; |
| 70 |
double d = l; |
| 71 |
} |
| 72 |
|
| 73 |
void test3(float f) |
| 74 |
{ |
| 75 |
real r = f * f; |
| 76 |
double d = f * f; |
| 77 |
} |
| 78 |
|
| 79 |
void test3(long l) |
| 80 |
{ |
| 81 |
real r = l * l; |
| 82 |
double d = l * l; |
| 83 |
} |
| 84 |
|
| 85 |
/***************************************/ |
| 86 |
|
| 87 |
double foo4(int i, double d) |
| 88 |
{ |
| 89 |
return ((i << 1) - d) + ((i << 1) - d); |
| 90 |
} |
| 91 |
|
| 92 |
void test4() |
| 93 |
{ |
| 94 |
double d = foo4(3, 4); |
| 95 |
assert(d == 4); |
| 96 |
} |
| 97 |
|
| 98 |
/***************************************/ |
| 99 |
|
| 100 |
import core.math; // trigger use of sqrt intrinsic |
| 101 |
|
| 102 |
void test5x(double p) |
| 103 |
{ |
| 104 |
bool b = p >= 0; |
| 105 |
double mean = (1 - p) / p; |
| 106 |
double skew = sqrt(1 - p); |
| 107 |
} |
| 108 |
|
| 109 |
void test5() |
| 110 |
{ |
| 111 |
test5x(2); |
| 112 |
} |
| 113 |
|
| 114 |
/***************************************/ |
| 115 |
|
| 116 |
import core.math; // trigger use of sqrt intrinsic |
| 117 |
|
| 118 |
void dstatsEnforce(bool, string) { } |
| 119 |
|
| 120 |
ulong invNegBinomCDF(double pVal, ulong n, double p) |
| 121 |
{ |
| 122 |
dstatsEnforce(p >= 0 && p <= 1, |
| 123 |
"p must be between 0, 1 for negative binomial distribution."); |
| 124 |
dstatsEnforce(pVal >= 0 && pVal <= 1, |
| 125 |
"P-values must be between 0, 1."); |
| 126 |
|
| 127 |
// Normal or gamma approx, then adjust. |
| 128 |
double mean = n * (1 - p) / p; |
| 129 |
double var = n * (1 - p) / (p * p); |
| 130 |
double skew = (2 - p) / sqrt(n * (1 - p)); |
| 131 |
double kk = 4.0L / (skew * skew); |
| 132 |
double theta = sqrt(var / kk); |
| 133 |
double offset = (kk * theta) - mean + 0.5L; |
| 134 |
ulong guess; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
void test6() |
| 139 |
{ |
| 140 |
invNegBinomCDF(2.0, 3, 4.0); |
| 141 |
} |
| 142 |
|
| 143 |
/***************************************/ |
| 144 |
|
| 145 |
float expDigamma(F)(in F x) |
| 146 |
{ |
| 147 |
return x; |
| 148 |
} |
| 149 |
|
| 150 |
float nextDown(float f) { return f; } |
| 151 |
|
| 152 |
void test7() |
| 153 |
{ |
| 154 |
foreach (i; 1 .. 2) |
| 155 |
{ |
| 156 |
assert(expDigamma(float(i)) >= expDigamma(float(i).nextDown)); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/***************************************/ |
| 161 |
|
| 162 |
void foo8_1(double x) |
| 163 |
{ |
| 164 |
printf("x = %g\n", x); |
| 165 |
assert(x == 0); |
| 166 |
} |
| 167 |
|
| 168 |
void foo8_2(double x) |
| 169 |
{ |
| 170 |
printf("x = %g\n", x); |
| 171 |
assert(x != 0); |
| 172 |
} |
| 173 |
|
| 174 |
void test8() |
| 175 |
{ |
| 176 |
foo8_1(0.0); |
| 177 |
foo8_2(1.0); |
| 178 |
} |
| 179 |
|
| 180 |
/***************************************/ |
| 181 |
|
| 182 |
void test9() |
| 183 |
{ |
| 184 |
double a = 9; |
| 185 |
double b = 3; |
| 186 |
double c = a * b + 1; |
| 187 |
double d = a + b + 1; |
| 188 |
printf("%g %g\n", c, d); // clobber XMM registers |
| 189 |
assert(c == 28 && d == 13); |
| 190 |
double e = a * b - 1; |
| 191 |
double f = a + b - 1; // reload 2 CSEs |
| 192 |
printf("%g %g\n", e, f); |
| 193 |
assert(e == 26 && f == 11); |
| 194 |
} |
| 195 |
|
| 196 |
/***************************************/ |
| 197 |
// https://issues.dlang.org/show_bug.cgi?id=20349 |
| 198 |
|
| 199 |
double f20349(double a, int b) |
| 200 |
{ |
| 201 |
import core.math; |
| 202 |
return core.math.sqrt(-a / b) / b; |
| 203 |
} |
| 204 |
|
| 205 |
void test20349() |
| 206 |
{ |
| 207 |
assert(f20349(-9, 1) == 3); |
| 208 |
} |
| 209 |
|
| 210 |
/****************************************/ |
| 211 |
// https://issues.dlang.org/show_bug.cgi?id=20963 |
| 212 |
|
| 213 |
void test20963() |
| 214 |
{ |
| 215 |
ulong v = 0xE3251BACB112CB8B; |
| 216 |
double d = cast(double)v; |
| 217 |
printf("%a\n", d); //0x1.c64a37596225ap+63 |
| 218 |
assert(d == 0x1.c64a375962259p+63); |
| 219 |
} |
| 220 |
|
| 221 |
/***************************************/ |
| 222 |
|
| 223 |
|
| 224 |
int main() |
| 225 |
{ |
| 226 |
test240(); |
| 227 |
test241(); |
| 228 |
test4(); |
| 229 |
test5(); |
| 230 |
test6(); |
| 231 |
test7(); |
| 232 |
test8(); |
| 233 |
test9(); |
| 234 |
test20349(); |
| 235 |
test20963(); |
| 236 |
|
| 237 |
printf("Success\n"); |
| 238 |
return EXIT_SUCCESS; |
| 239 |
} |