Skip to content
Snippets Groups Projects
Commit 33a2f642 authored by xleroy's avatar xleroy
Browse files

Test for int/float conversions

git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@943 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
parent deebc6fa
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ ASFLAGS= ...@@ -9,7 +9,7 @@ ASFLAGS=
VPATH=../harness ../lib VPATH=../harness ../lib
PROGS=fib integr qsort fft sha1 aes almabench manyargs lists \ PROGS=fib integr qsort fft sha1 aes almabench manyargs lists \
stopcopy marksweep stopcopy marksweep switchtbl conversions
all_s: $(PROGS:%=%.s) all_s: $(PROGS:%=%.s)
...@@ -77,6 +77,11 @@ switchtbl: switchtbl.o mainswitchtbl.o ...@@ -77,6 +77,11 @@ switchtbl: switchtbl.o mainswitchtbl.o
clean:: clean::
rm -f switchtbl rm -f switchtbl
conversions: conversions.o mainconversions.o
$(CC) $(CFLAGS) -o conversions conversions.o mainconversions.o
clean::
rm -f conversions
.SUFFIXES: .SUFFIXES:
.SUFFIXES: .cmp .cm .s .o .c .S .SUFFIXES: .cmp .cm .s .o .c .S
......
"intoffloat" (r, x): int -> int -> void
{
int32[r] = intoffloat(float64[x]);
}
"intuoffloat" (r, x): int -> int -> void
{
int32[r] = intuoffloat(float64[x]);
}
"floatofint" (r, x): int -> int -> void
{
float64[r] = floatofint(int32[x]);
}
"floatofintu" (r, x): int -> int -> void
{
float64[r] = floatofintu(int32[x]);
}
#include <stdio.h>
#include <math.h>
extern void intoffloat(int * r, double * x);
extern void intuoffloat(unsigned int * r, double * x);
extern void floatofint(double * r, int * x);
extern void floatofintu(double * r, unsigned int * x);
/* Linear congruential PRNG */
static unsigned int random_seed = 0;
unsigned int random_uint(void)
{
random_seed = random_seed * 69069 + 25173;
return random_seed;
}
double random_double(void)
{
/* In range 0 .. 2^32+1 */
unsigned int h = random_uint();
unsigned int l = random_uint();
return (double) h + ldexp((double) l, -32);
}
/* Individual test runs */
void test_intoffloat(double x)
{
int r;
intoffloat(&r, &x);
if (r != (int) x)
printf("intoffloat(%g): expected %d, got %d\n", x, r, (int) x);
}
void test_intuoffloat(double x)
{
unsigned int r;
intuoffloat(&r, &x);
if (r != (unsigned int) x)
printf("intuoffloat(%g): expected %d, got %d\n", x, r, (unsigned int) x);
}
void test_floatofint(int x)
{
double r;
floatofint(&r, &x);
if (r != (double) x)
printf("floatofint(%d): expected %g, got %g\n", x, r, (double) x);
}
void test_floatofintu(unsigned int x)
{
double r;
floatofintu(&r, &x);
if (r != (double) x)
printf("floatofint(%u): expected %g, got %g\n", x, r, (double) x);
}
/* Limit cases */
double cases_intoffloat[] = {
0.0, 0.1, 0.5, 0.9, 1.0, 1.1, 1.6,
-0.1, -0.5, -0.9, -1.0, -1.1, -1.6,
2147483647.0, 2147483647.6, 2147483648.0, 2147483647.5,
2147483648.0, 2147483648.5, 2147483649.0, 10000000000.0,
-2147483647.0, -2147483647.6, -2147483648.0, -2147483647.5,
-2147483648.0, -2147483648.5, -2147483649.0, -10000000000.0
};
double cases_intuoffloat[] = {
0.0, 0.1, 0.5, 0.9, 1.0, 1.1, 1.6,
-0.1, -0.5, -0.9, -1.0, -1.1, -1.6,
2147483647.0, 2147483647.6, 2147483648.0, 2147483647.5,
2147483648.0, 2147483648.5, 2147483649.0,
4294967295.0, 4294967295.6, 4294967296.0, 4294967296.5,
10000000000.0
};
int cases_floatofint[] = {
0, 1, 2, -1, -2, 2147483647, -2147483648
};
unsigned int cases_floatofintu[] = {
0U, 1U, 2U, 2147483647U, 2147483648U, 4294967295U
};
#define TEST(testfun, cases, tyarg, gen) \
for (i = 0; i < sizeof(cases) / sizeof(tyarg); i++) \
testfun(cases[i]); \
for (i = 0; i < numtests; i++) \
testfun(gen);
int main(int argc, char ** argv)
{
int i;
int numtests = 1000000;
TEST(test_intoffloat, cases_intoffloat, double,
(random_double() - 2147483648.0) * 1.1);
TEST(test_intuoffloat, cases_intuoffloat, double,
random_double() * 1.1);
TEST(test_floatofint, cases_floatofint, int,
(int) random_uint());
TEST(test_floatofintu, cases_floatofintu, unsigned int,
random_uint());
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment