From 6f362523608fe15b536bcc8ea60c8588ce8b1ef9 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 6 Oct 2011 11:29:34 +0200 Subject: [PATCH 1/1] Tests for module 'frexpf'. * modules/frexpf-tests: New file. * tests/test-frexpf.c: New file. --- ChangeLog | 4 ++ modules/frexpf-tests | 18 +++++ tests/test-frexpf.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 modules/frexpf-tests create mode 100644 tests/test-frexpf.c diff --git a/ChangeLog b/ChangeLog index 74fc7bf84..fdf07f08a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2011-10-06 Bruno Haible + Tests for module 'frexpf'. + * modules/frexpf-tests: New file. + * tests/test-frexpf.c: New file. + New module 'frexpf'. * lib/math.in.h (frexpf): New declaration. * lib/frexpf.c: New file. diff --git a/modules/frexpf-tests b/modules/frexpf-tests new file mode 100644 index 000000000..34252a80f --- /dev/null +++ b/modules/frexpf-tests @@ -0,0 +1,18 @@ +Files: +tests/test-frexpf.c +tests/minus-zero.h +tests/infinity.h +tests/nan.h +tests/signature.h +tests/macros.h + +Depends-on: +isnanf-nolibm +signbit + +configure.ac: + +Makefile.am: +TESTS += test-frexpf +check_PROGRAMS += test-frexpf +test_frexpf_LDADD = $(LDADD) @FREXPF_LIBM@ diff --git a/tests/test-frexpf.c b/tests/test-frexpf.c new file mode 100644 index 000000000..fb6003eb3 --- /dev/null +++ b/tests/test-frexpf.c @@ -0,0 +1,192 @@ +/* Test of splitting a float into fraction and mantissa. + Copyright (C) 2007-2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2007. */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (frexpf, float, (float, int *)); + +#include + +#include "isnanf-nolibm.h" +#include "minus-zero.h" +#include "infinity.h" +#include "nan.h" +#include "macros.h" + +/* Avoid some warnings from "gcc -Wshadow". + This file doesn't use the exp() function. */ +#undef exp +#define exp exponent + +static float +my_ldexp (float x, int d) +{ + for (; d > 0; d--) + x *= 2.0f; + for (; d < 0; d++) + x *= 0.5f; + return x; +} + +int +main () +{ + int i; + volatile float x; + + { /* NaN. */ + int exp = -9999; + float mantissa; + x = NaNf (); + mantissa = frexpf (x, &exp); + ASSERT (isnanf (mantissa)); + } + + { /* Positive infinity. */ + int exp = -9999; + float mantissa; + x = Infinityf (); + mantissa = frexpf (x, &exp); + ASSERT (mantissa == x); + } + + { /* Negative infinity. */ + int exp = -9999; + float mantissa; + x = - Infinityf (); + mantissa = frexpf (x, &exp); + ASSERT (mantissa == x); + } + + { /* Positive zero. */ + int exp = -9999; + float mantissa; + x = 0.0f; + mantissa = frexpf (x, &exp); + ASSERT (exp == 0); + ASSERT (mantissa == x); + ASSERT (!signbit (mantissa)); + } + + { /* Negative zero. */ + int exp = -9999; + float mantissa; + x = minus_zerof; + mantissa = frexpf (x, &exp); + ASSERT (exp == 0); + ASSERT (mantissa == x); + ASSERT (signbit (mantissa)); + } + + for (i = 1, x = 1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5f); + } + for (i = 1, x = 1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5f); + } + for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5f); + } + + for (i = 1, x = -1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5f); + } + for (i = 1, x = -1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5f); + } + for (; i >= FLT_MIN_EXP - 100 && x < 0.0f; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5f); + } + + for (i = 1, x = 1.01f; i <= FLT_MAX_EXP; i++, x *= 2.0f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.505f); + } + for (i = 1, x = 1.01f; i >= FLT_MIN_EXP; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.505f); + } + for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa >= 0.5f); + ASSERT (mantissa < 1.0f); + ASSERT (mantissa == my_ldexp (x, - exp)); + } + + for (i = 1, x = 1.73205f; i <= FLT_MAX_EXP; i++, x *= 2.0f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.866025f); + } + for (i = 1, x = 1.73205f; i >= FLT_MIN_EXP; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.866025f); + } + for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f) + { + int exp = -9999; + float mantissa = frexpf (x, &exp); + ASSERT (exp == i || exp == i + 1); + ASSERT (mantissa >= 0.5f); + ASSERT (mantissa < 1.0f); + ASSERT (mantissa == my_ldexp (x, - exp)); + } + + return 0; +} -- 2.11.0