1 /* PERMUTE_ARGS: -O
2  * Test floating point code generation
3  */
4 
5 import core.stdc.stdio;
6 import core.stdc.stdlib;
7 
value_1()8 double value_1() {
9     return 1;
10 }
11 
value_2()12 double value_2() {
13     return 2;
14 }
15 
16 /***************************************/
17 
testcse1(T)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 
test240()31 void test240()
32 {
33     testcse1!float();
34     testcse1!double();
35     testcse1!real();
36 }
37 
38 /***************************************/
39 
testcse2(T)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 
test241()52 void test241()
53 {
54     testcse2!float();
55     testcse2!double();
56     testcse2!real();
57 }
58 
59 /***************************************/
60 
test1(float f)61 void test1(float f)
62 {
63     real r = f;
64     double d = f;
65 }
66 
test2(long l)67 void test2(long l)
68 {
69     real r = l;
70     double d = l;
71 }
72 
test3(float f)73 void test3(float f)
74 {
75     real r = f * f;
76     double d = f * f;
77 }
78 
test3(long l)79 void test3(long l)
80 {
81     real r = l * l;
82     double d = l * l;
83 }
84 
85 /***************************************/
86 
foo4(int i,double d)87 double foo4(int i, double d)
88 {
89     return ((i << 1) - d) + ((i << 1) - d);
90 }
91 
test4()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 
test5x(double p)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 
test5()109 void test5()
110 {
111     test5x(2);
112 }
113 
114 /***************************************/
115 
116 import core.math; // trigger use of sqrt intrinsic
117 
dstatsEnforce(bool,string)118 void dstatsEnforce(bool, string) { }
119 
invNegBinomCDF(double pVal,ulong n,double p)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 
test6()138 void test6()
139 {
140     invNegBinomCDF(2.0, 3, 4.0);
141 }
142 
143 /***************************************/
144 
expDigamma(F)145 float expDigamma(F)(in F x)
146 {
147     return x;
148 }
149 
nextDown(float f)150 float nextDown(float f) { return f; }
151 
test7()152 void test7()
153 {
154     foreach (i; 1 .. 2)
155     {
156         assert(expDigamma(float(i)) >= expDigamma(float(i).nextDown));
157     }
158 }
159 
160 /***************************************/
161 
foo8_1(double x)162 void foo8_1(double x)
163 {
164     printf("x = %g\n", x);
165     assert(x == 0);
166 }
167 
foo8_2(double x)168 void foo8_2(double x)
169 {
170     printf("x = %g\n", x);
171     assert(x != 0);
172 }
173 
test8()174 void test8()
175 {
176     foo8_1(0.0);
177     foo8_2(1.0);
178 }
179 
180 /***************************************/
181 
test9()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 
f20349(double a,int b)199 double f20349(double a, int b)
200 {
201     import core.math;
202     return core.math.sqrt(-a / b) / b;
203 }
204 
test20349()205 void test20349()
206 {
207     assert(f20349(-9, 1) == 3);
208 }
209 
210 /****************************************/
211 // https://issues.dlang.org/show_bug.cgi?id=20963
212 
test20963()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 
main()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 }
240