1/* 2 * Copyright (c) 2013, Andreas Sandberg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following 13 * disclaimer in the documentation and/or other materials provided 14 * with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 27 * OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <fputils/fp80.h> 31 32#include "test_helper.h" 33 34#include <assert.h> 35#include <math.h> 36#include <stdio.h> 37#include <stdlib.h> 38 39static void 40test_cvfd_class(const char *name, double fin) 41{ 42 test_diag("converting '%e' -> fp80...", fin); 43 fp80_t v = fp80_cvfd(fin); 44 int class_ok; 45 int fp64_class = fpclassify(fin); 46 int class = fp64_class == FP_SUBNORMAL ? FP_NORMAL : fp64_class; 47 int fp80_class = fp80_classify(v); 48 49 printf("# "); 50 fp80_debug_dump(stdout, v); 51 test_diag("isnan: %i, isinf: %i, iszero: %i, isnormal: %i, issubnormal: %i", 52 fp80_isnan(v), fp80_isinf(v), fp80_iszero(v), 53 fp80_isnormal(v), fp80_issubnormal(v)); 54 test_diag("class(fp64): %i, expected class: %i, class(fp80): %i", 55 fp64_class, class, fp80_classify(v)); 56 switch (class) { 57 case FP_NAN: 58 class_ok = fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 59 !fp80_isnormal(v) && !fp80_issubnormal(v); 60 break; 61 62 case FP_INFINITE: 63 class_ok = !fp80_isnan(v) && fp80_isinf(v) && !fp80_iszero(v) && 64 !fp80_isnormal(v) && !fp80_issubnormal(v); 65 break; 66 case FP_ZERO: 67 class_ok = !fp80_isnan(v) && !fp80_isinf(v) && fp80_iszero(v) && 68 !fp80_isnormal(v) && !fp80_issubnormal(v); 69 break; 70 71 case FP_SUBNORMAL: 72 class_ok = !fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 73 !fp80_isnormal(v) && fp80_issubnormal(v); 74 break; 75 76 case FP_NORMAL: 77 class_ok = !fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 78 fp80_isnormal(v) && !fp80_issubnormal(v); 79 break; 80 81 default: 82 test_bail("unexpected FP class (%i)", class); 83 } 84 85 if (!class_ok) { 86 test_diag("inconsistent classification"); 87 test_fail(name); 88 } else if (fp80_class != class) { 89 test_diag("class mismatch"); 90 test_fail(name); 91 } else { 92 test_ok(name); 93 } 94} 95 96static void 97test_cvfd_class_exp(const char *name, double x, int exp) 98{ 99 double val = ldexp(x, exp); 100 test_cvfd_class(name, val); 101} 102 103int 104main(int argc, char *argv[]) 105{ 106 test_init(9); 107 108 test_cvfd_class("double->fp80 +inf", -INFINITY); 109 test_cvfd_class("double->fp80 -inf", INFINITY); 110 test_cvfd_class("double->fp80 +nan", NAN); 111 test_cvfd_class("double->fp80 -nan", -NAN); 112 test_cvfd_class("double->fp80 +0", 0); 113 test_cvfd_class("double->fp80 PI", M_PI); 114 115 test_cvfd_class_exp("double->fp80 smallest normal", 1.0, -1022); 116 test_cvfd_class_exp("double->fp80 denormal1", 0.5, -1022); 117 test_cvfd_class_exp("double->fp80 denormal2", 0.25, -1022); 118 119 test_exit(); 120} 121