Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistencies and speed in C tests #11

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 46 additions & 30 deletions test/cpfloat_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ typedef union {
#define CONST_PI 3.14159265358979323846
#define CONST_SQRT2 1.41421356237309504880

#define MINFORMAT 0
#define MAXFORMAT 2
#define MINMODE -1
#define MAXMODE 8
#define NREPS 1000

/* Define default target formats. */
// binary16, bfloat16, TensorFloat-32
static size_t precision [] = {11, 8, 11};
static size_t emax [] = {15, 127, 127};
static size_t emin [] = {-14, -126, -126};
// E5M2, bfloat16
static size_t precision [] = {3, 8};
static size_t emax [] = {15, 127};
static size_t emin [] = {-14, -126};
static size_t nformats = 2;

/* Structure for options and fixtures. */
Expand Down Expand Up @@ -324,7 +322,7 @@ void check_equality_double(double *x, double *y, size_t n) {
for (size_t j = 0; j < n; j++) {
if (!nan_safe_compare_double(x[j], y[j])) {
printf("DOUBLE\n");
printf("***\nj = %ld\nx = %23.15e [%" PRIu64 "]\ny = %23.15e [%" PRIu64 "]\n",
printf("***\nj = %ld\nx = %23.15e [%" PRIu64 "]\ny = %23.15e [%" PRIu64 "]\n",
j,
x[j], *(uint64_t *)(x + j),
y[j], *(uint64_t *)(y + j));
Expand All @@ -338,7 +336,7 @@ void check_equality_double_int(double *x, int *y, size_t n) {
for (size_t j = 0; j < n; j++) {
if (!nan_safe_compare_double(x[j], y[j])) {
printf("DOUBLE\n");
printf("***\nj = %ld\nx = %23.15e [%" PRIu64 "]\ny = %23.15e\n",
printf("***\nj = %ld\nx = %23.15e [%" PRIu64 "]\ny = %23.15e\n",
j, x[j], *(uint64_t *)(x + j), (double)y[j]);
}
ck_assert(nan_safe_compare_double(x[j], (double)y[j]));
Expand Down Expand Up @@ -498,7 +496,9 @@ void check_array_stoc_double(double *tmpin, double *tmpout,
ck_abort_msg("Not rounding to either closest number.");
fpopts->round = mode;
if (fabs(counter[0]/NREPS - prounddown[i % 3]) > 0.1) {
printf("%e\n", fabs(counter[0]/NREPS - prounddown[i % 3]));
printf("DOUBLE\n");
printf("***\ni = %ld\nexp = %23.15e\nact = %23.15e\n",
i, counter[0]/NREPS, prounddown[i % 3]);
ck_abort_msg("Error in stochasting rounding.");
}
}
Expand Down Expand Up @@ -529,7 +529,9 @@ void check_array_stoc_float(float *tmpin, float *tmpout,
ck_abort_msg("Not rounding to either closest number.");
fpopts->round = mode;
if (fabs(counter[0]/(double)NREPS - prounddown[i % 3]) > 0.1) {
printf("%e\n", fabs(counter[0]/NREPS - prounddown[i % 3]));
printf("FLOAT\n");
printf("***\ni = %ld\nexp = %23.15e\nact = %23.15e\n",
i, counter[0]/NREPS, prounddown[i % 3]);
ck_abort_msg("Error in stochasting rounding.");
}
}
Expand All @@ -550,7 +552,9 @@ void check_array_equi_double(double *tmpin, double *tmpout,
else
counter[0]++;
if (fabs(counter[0]/NREPS - *prounddown) > 0.1) {
printf("%e\n", fabs(counter[0]/NREPS - prounddown[i % 3]));
printf("DOUBLE\n");
printf("***\ni = %ld\nexp = %23.15e\nact = %23.15e\n",
i, counter[0]/NREPS, prounddown[i % 3]);
ck_abort_msg("Error in stochasting rounding.");
}
}
Expand All @@ -571,7 +575,9 @@ void check_array_equi_float(float *tmpin, float *tmpout,
else
counter[0]++;
if (fabs(counter[0]/(double)NREPS - *prounddown) > 0.1) {
printf("%e\n", fabs(counter[0]/NREPS - prounddown[i % 3]));
printf("FLOAT\n");
printf("***\ni = %ld\nexp = %23.15e\nact = %23.15e\n",
i, counter[0]/NREPS, prounddown[i % 3]);
ck_abort_msg("Error in stochasting rounding.");
}
}
Expand Down Expand Up @@ -916,15 +922,15 @@ for (size_t mode = 1; mode < 3; mode++) {
double *zd = alloc_init_array_double(xd, n);
double *yd = allocate_array_double(xd, n, mode);
select_tests_det_double(yd, xd, zd, n, fpopts,
MINMODE, MAXMODE, 0, 2, -1, -1);
MINMODE, MAXMODE, 0, nformats - 1, -1, -1);
free(zd);
free_array_double(yd, mode);

float xf [] = {0, -0, inf_float(), -inf_float(), nan_float()};
float *zf = alloc_init_array_float(xf, n);
float *yf = allocate_array_float(xf, n, mode);
select_tests_det_float(yf, xf, zf, n, fpopts,
MINMODE, MAXMODE, 0, 2, -1, -1);
MINMODE, MAXMODE, 0, nformats - 1, -1, -1);
free(zf);
free_array_float(yf, mode);
}
Expand Down Expand Up @@ -976,7 +982,8 @@ for (size_t mode = 1; mode < 3; mode++) {
fpopts->precision = precision[i];
fpopts->emax = emax[i];
fpopts->emin = emin[i];
size_t n = ldexp(1., fpopts->precision-1) * 2 * fpopts->emax;
size_t n = ldexp(1., fpopts->precision-1) *
(fpopts->emax - fpopts->emin + 1);
uint64_t *xd = malloc(n * sizeof(*xd));
init_intarray_double(xd, n, intminnormal_double(fpopts),
1ul << (52-fpopts->precision + 1));
Expand Down Expand Up @@ -1692,7 +1699,8 @@ for (size_t mode = 1; mode < 3; mode++) {
fpopts->precision = precision[i];
fpopts->emax = emax[i];
fpopts->emin = emin[i];
size_t n = 3 * (ldexp(1., fpopts->precision-1) * 2 * fpopts->emax - 1);
size_t n = 3 *
(ldexp(1., fpopts->precision-1) * (fpopts->emax - fpopts->emin + 1) - 1);
uint64_t *xd_imm = malloc(n * sizeof(*xd_imm));
uint64_t *refd = malloc(n * sizeof(*refd));
double *xd = malloc(n * sizeof(*xd));
Expand Down Expand Up @@ -2105,7 +2113,8 @@ for (size_t mode = 1; mode < 3; mode++ ) {
fpopts->emax = emax[i];
fpopts->emin = emin[i];

size_t n = 3 * (ldexp(1., fpopts->precision-1) * 2 * fpopts->emax - 1);
size_t n = 3 * (ldexp(1., fpopts->precision-1)
* (fpopts->emax - fpopts->emin +1) - 1);
uint64_t *xd = malloc(n * sizeof(*xd));
double xmin = minnormal(fpopts);
uint64_t stepd = 1ul << (52-fpopts->precision + 1);
Expand Down Expand Up @@ -2624,13 +2633,16 @@ free(onef);
free(exp);
free(lexp);

#test enumeration_floating_point_numbers
printf("4c. Next and previous floating-point number\n");
#test floating_point_enumeration_subnormal_numbers
printf("4c. Next and previous floating-point number: subnormal numbers\n");
fpopts->infinity = CPFLOAT_INF_USE;
fpopts->explim = CPFLOAT_EXPRANGE_TARG;
fpopts->saturation = CPFLOAT_SAT_NO;
fpopts->subnormal = CPFLOAT_SUBN_USE;
// Subnormals
for (size_t mode = 2; mode < 3; mode++) {
fpopts->round = CPFLOAT_RND_NE;
for (size_t mode = 3; mode < 3; mode++) {
for (size_t i = 0; i < nformats; i++) {

fpopts->precision = precision[i];
fpopts->emax = emax[i];
fpopts->emin = emin[i];
Expand All @@ -2653,8 +2665,10 @@ for (size_t mode = 2; mode < 3; mode++) {
copy_array_double(ad, refd, n);
cpf_nexttoward(xd, ad, infl, n-1, fpopts);
check_equality_double(xd, refd+1, n-1);

csign_intarray_double((uint64_t *)infd, n);
for (size_t j = 0; j < n; j++) infl[j] = -infl[j];
for (size_t j = 0; j < n; j++)
infl[j] = -infl[j];
copy_array_double(ad, refd, n);
cpf_nextafter(xd, ad+1, infd, n-1, fpopts);
check_equality_double(xd, refd, n-1);
Expand All @@ -2669,8 +2683,10 @@ for (size_t mode = 2; mode < 3; mode++) {
copy_array_double(ad, refd, n);
cpf_nexttoward(xd, ad+1, infl, n-1, fpopts);
check_equality_double(xd, refd, n-1);

csign_intarray_double((uint64_t *) infd, n);
for (size_t j = 0; j < n; j++) infl[j] = -infl[j];
for (size_t j = 0; j < n; j++)
infl[j] = -infl[j];
copy_array_double(ad, refd, n);
cpf_nextafter(xd, ad, infd, n-1, fpopts);
check_equality_double(xd, refd+1, n-1);
Expand Down Expand Up @@ -2729,14 +2745,16 @@ for (size_t mode = 2; mode < 3; mode++) {
}
}

// Normals
for (size_t mode = 2; mode < 3; mode++) {
#test floating_point_enumeration_normal_numbers
printf("4d. Next and previous floating-point number: normal numbers\n");
for (size_t mode = 3; mode < 3; mode++) {
for (size_t i = 0; i < nformats; i++) {
fpopts->precision = precision[i];
fpopts->emax = emax[i];
fpopts->emin = emin[i];

size_t n = ldexp(1., fpopts->precision-1) * 2 * fpopts->emax + 2;
size_t n = ldexp(1., fpopts->precision-1) *
(fpopts->emax - fpopts->emin + 1) + 2;
double *ad = malloc(n * sizeof(*ad));
double *xd = allocate_array_double(ad, n, mode);
double *refd = malloc(n * sizeof(*refd));
Expand Down Expand Up @@ -2839,12 +2857,10 @@ for (size_t mode = 2; mode < 3; mode++) {
}

#test integer_rounding
printf("4c. Integer rounding\n");

printf("4e. Integer rounding\n");
fpopts->emax = emax[1];
fpopts->emin = emin[1];
fpopts->precision = precision[1];

size_t n = 32;
double *xd = malloc(n * sizeof(*xd));
int *xi = malloc(n * sizeof(*xi));
Expand Down Expand Up @@ -3012,7 +3028,7 @@ check_equality_double_long_long(rd7, xll, n-3);


#test arithmetic_operations_large
printf("4d. Arithmetic operations on large arrays\n");
printf("4f. Arithmetic operations on large arrays\n");
size_t i = 0;
fpopts->precision = precision[i];
fpopts->emax = emax[i];
Expand Down
Loading