fp80_cvfd.c revision 9888
19888Sandreas@sandberg.pp.se/* 29888Sandreas@sandberg.pp.se * Copyright (c) 2013, Andreas Sandberg 39888Sandreas@sandberg.pp.se * All rights reserved. 49888Sandreas@sandberg.pp.se * 59888Sandreas@sandberg.pp.se * Redistribution and use in source and binary forms, with or without 69888Sandreas@sandberg.pp.se * modification, are permitted provided that the following conditions 79888Sandreas@sandberg.pp.se * are met: 89888Sandreas@sandberg.pp.se * 99888Sandreas@sandberg.pp.se * 1. Redistributions of source code must retain the above copyright 109888Sandreas@sandberg.pp.se * notice, this list of conditions and the following disclaimer. 119888Sandreas@sandberg.pp.se * 2. Redistributions in binary form must reproduce the above 129888Sandreas@sandberg.pp.se * copyright notice, this list of conditions and the following 139888Sandreas@sandberg.pp.se * disclaimer in the documentation and/or other materials provided 149888Sandreas@sandberg.pp.se * with the distribution. 159888Sandreas@sandberg.pp.se * 169888Sandreas@sandberg.pp.se * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179888Sandreas@sandberg.pp.se * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189888Sandreas@sandberg.pp.se * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 199888Sandreas@sandberg.pp.se * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 209888Sandreas@sandberg.pp.se * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 219888Sandreas@sandberg.pp.se * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 229888Sandreas@sandberg.pp.se * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 239888Sandreas@sandberg.pp.se * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249888Sandreas@sandberg.pp.se * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 259888Sandreas@sandberg.pp.se * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 269888Sandreas@sandberg.pp.se * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 279888Sandreas@sandberg.pp.se * OF THE POSSIBILITY OF SUCH DAMAGE. 289888Sandreas@sandberg.pp.se */ 299888Sandreas@sandberg.pp.se 309888Sandreas@sandberg.pp.se#include <fputils/fp80.h> 319888Sandreas@sandberg.pp.se 329888Sandreas@sandberg.pp.se#include "test_helper.h" 339888Sandreas@sandberg.pp.se 349888Sandreas@sandberg.pp.se#include <assert.h> 359888Sandreas@sandberg.pp.se#include <math.h> 369888Sandreas@sandberg.pp.se#include <stdio.h> 379888Sandreas@sandberg.pp.se#include <stdlib.h> 389888Sandreas@sandberg.pp.se 399888Sandreas@sandberg.pp.sestatic void 409888Sandreas@sandberg.pp.setest_cvfd_class(const char *name, double fin) 419888Sandreas@sandberg.pp.se{ 429888Sandreas@sandberg.pp.se test_diag("converting '%e' -> fp80...", fin); 439888Sandreas@sandberg.pp.se fp80_t v = fp80_cvfd(fin); 449888Sandreas@sandberg.pp.se int class_ok; 459888Sandreas@sandberg.pp.se int fp64_class = fpclassify(fin); 469888Sandreas@sandberg.pp.se int class = fp64_class == FP_SUBNORMAL ? FP_NORMAL : fp64_class; 479888Sandreas@sandberg.pp.se int fp80_class = fp80_classify(v); 489888Sandreas@sandberg.pp.se 499888Sandreas@sandberg.pp.se printf("# "); 509888Sandreas@sandberg.pp.se fp80_debug_dump(stdout, v); 519888Sandreas@sandberg.pp.se test_diag("isnan: %i, isinf: %i, iszero: %i, isnormal: %i, issubnormal: %i", 529888Sandreas@sandberg.pp.se fp80_isnan(v), fp80_isinf(v), fp80_iszero(v), 539888Sandreas@sandberg.pp.se fp80_isnormal(v), fp80_issubnormal(v)); 549888Sandreas@sandberg.pp.se test_diag("class(fp64): %i, expected class: %i, class(fp80): %i", 559888Sandreas@sandberg.pp.se fp64_class, class, fp80_classify(v)); 569888Sandreas@sandberg.pp.se switch (class) { 579888Sandreas@sandberg.pp.se case FP_NAN: 589888Sandreas@sandberg.pp.se class_ok = fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 599888Sandreas@sandberg.pp.se !fp80_isnormal(v) && !fp80_issubnormal(v); 609888Sandreas@sandberg.pp.se break; 619888Sandreas@sandberg.pp.se 629888Sandreas@sandberg.pp.se case FP_INFINITE: 639888Sandreas@sandberg.pp.se class_ok = !fp80_isnan(v) && fp80_isinf(v) && !fp80_iszero(v) && 649888Sandreas@sandberg.pp.se !fp80_isnormal(v) && !fp80_issubnormal(v); 659888Sandreas@sandberg.pp.se break; 669888Sandreas@sandberg.pp.se case FP_ZERO: 679888Sandreas@sandberg.pp.se class_ok = !fp80_isnan(v) && !fp80_isinf(v) && fp80_iszero(v) && 689888Sandreas@sandberg.pp.se !fp80_isnormal(v) && !fp80_issubnormal(v); 699888Sandreas@sandberg.pp.se break; 709888Sandreas@sandberg.pp.se 719888Sandreas@sandberg.pp.se case FP_SUBNORMAL: 729888Sandreas@sandberg.pp.se class_ok = !fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 739888Sandreas@sandberg.pp.se !fp80_isnormal(v) && fp80_issubnormal(v); 749888Sandreas@sandberg.pp.se break; 759888Sandreas@sandberg.pp.se 769888Sandreas@sandberg.pp.se case FP_NORMAL: 779888Sandreas@sandberg.pp.se class_ok = !fp80_isnan(v) && !fp80_isinf(v) && !fp80_iszero(v) && 789888Sandreas@sandberg.pp.se fp80_isnormal(v) && !fp80_issubnormal(v); 799888Sandreas@sandberg.pp.se break; 809888Sandreas@sandberg.pp.se 819888Sandreas@sandberg.pp.se default: 829888Sandreas@sandberg.pp.se test_bail("unexpected FP class (%i)", class); 839888Sandreas@sandberg.pp.se } 849888Sandreas@sandberg.pp.se 859888Sandreas@sandberg.pp.se if (!class_ok) { 869888Sandreas@sandberg.pp.se test_diag("inconsistent classification"); 879888Sandreas@sandberg.pp.se test_fail(name); 889888Sandreas@sandberg.pp.se } else if (fp80_class != class) { 899888Sandreas@sandberg.pp.se test_diag("class mismatch"); 909888Sandreas@sandberg.pp.se test_fail(name); 919888Sandreas@sandberg.pp.se } else { 929888Sandreas@sandberg.pp.se test_ok(name); 939888Sandreas@sandberg.pp.se } 949888Sandreas@sandberg.pp.se} 959888Sandreas@sandberg.pp.se 969888Sandreas@sandberg.pp.sestatic void 979888Sandreas@sandberg.pp.setest_cvfd_class_exp(const char *name, double x, int exp) 989888Sandreas@sandberg.pp.se{ 999888Sandreas@sandberg.pp.se double val = ldexp(x, exp); 1009888Sandreas@sandberg.pp.se test_cvfd_class(name, val); 1019888Sandreas@sandberg.pp.se} 1029888Sandreas@sandberg.pp.se 1039888Sandreas@sandberg.pp.seint 1049888Sandreas@sandberg.pp.semain(int argc, char *argv[]) 1059888Sandreas@sandberg.pp.se{ 1069888Sandreas@sandberg.pp.se test_init(9); 1079888Sandreas@sandberg.pp.se 1089888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 +inf", -INFINITY); 1099888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 -inf", INFINITY); 1109888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 +nan", NAN); 1119888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 -nan", -NAN); 1129888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 +0", 0); 1139888Sandreas@sandberg.pp.se test_cvfd_class("double->fp80 PI", M_PI); 1149888Sandreas@sandberg.pp.se 1159888Sandreas@sandberg.pp.se test_cvfd_class_exp("double->fp80 smallest normal", 1.0, -1022); 1169888Sandreas@sandberg.pp.se test_cvfd_class_exp("double->fp80 denormal1", 0.5, -1022); 1179888Sandreas@sandberg.pp.se test_cvfd_class_exp("double->fp80 denormal2", 0.25, -1022); 1189888Sandreas@sandberg.pp.se 1199888Sandreas@sandberg.pp.se test_exit(); 1209888Sandreas@sandberg.pp.se} 121