Andreas Sandberg | d3937f3 | 2013-09-30 09:40:26 +0200 | [diff] [blame] | 1 | /* |
Andreas Sandberg | bbcf7f0 | 2019-10-25 09:19:02 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013 Andreas Sandberg |
| 3 | * All rights reserved |
Andreas Sandberg | d3937f3 | 2013-09-30 09:40:26 +0200 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
Andreas Sandberg | bbcf7f0 | 2019-10-25 09:19:02 +0100 | [diff] [blame] | 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer; |
| 9 | * redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution; |
| 12 | * neither the name of the copyright holders nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived from |
| 14 | * this software without specific prior written permission. |
Andreas Sandberg | d3937f3 | 2013-09-30 09:40:26 +0200 | [diff] [blame] | 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
Andreas Sandberg | bbcf7f0 | 2019-10-25 09:19:02 +0100 | [diff] [blame] | 18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * Authors: Andreas Sandberg |
Andreas Sandberg | d3937f3 | 2013-09-30 09:40:26 +0200 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include <fputils/fp80.h> |
| 32 | |
| 33 | #include "test_helper.h" |
| 34 | |
| 35 | #include <assert.h> |
| 36 | #include <math.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | static void |
| 41 | test_cvtf(const char *name, double fin) |
| 42 | { |
| 43 | fp80_t v80 = fp80_cvfd(fin); |
| 44 | double v64 = fp80_cvtd(v80); |
| 45 | |
| 46 | test_diag("Conversion '%e' -> fp80 -> double: %e", fin, v64); |
| 47 | |
| 48 | printf("# "); |
| 49 | fp80_debug_dump(stdout, v80); |
| 50 | if (v64 == fin || |
| 51 | (isnan(fin) && isnan(v64) && signbit(fin) == signbit(v64))) { |
| 52 | test_ok(name); |
| 53 | } else { |
| 54 | test_diag("MISMATCH: %e != %e", fin, v64); |
| 55 | test_fail(name); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | test_cvtf_exp(const char *name, double x, int exp) |
| 61 | { |
| 62 | double val = ldexp(x, exp); |
| 63 | test_cvtf(name, val); |
| 64 | } |
| 65 | |
| 66 | int |
| 67 | main(int argc, char *argv[]) |
| 68 | { |
| 69 | test_init(9); |
| 70 | |
| 71 | test_cvtf("+inf", -INFINITY); |
| 72 | test_cvtf("-inf", INFINITY); |
| 73 | test_cvtf("+nan", NAN); |
| 74 | test_cvtf("-nan", -NAN); |
| 75 | test_cvtf("+0", 0); |
| 76 | test_cvtf("PI", M_PI); |
| 77 | |
| 78 | test_cvtf_exp("smallest normal", 1.0, -1022); |
| 79 | test_cvtf_exp("denormal1", 0.5, -1022); |
| 80 | test_cvtf_exp("denormal2", 0.25, -1022); |
| 81 | |
| 82 | test_exit(); |
| 83 | } |