110c9ffa4SDavid Schultz /*- 210c9ffa4SDavid Schultz * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 310c9ffa4SDavid Schultz * All rights reserved. 410c9ffa4SDavid Schultz * 510c9ffa4SDavid Schultz * Redistribution and use in source and binary forms, with or without 610c9ffa4SDavid Schultz * modification, are permitted provided that the following conditions 710c9ffa4SDavid Schultz * are met: 810c9ffa4SDavid Schultz * 1. Redistributions of source code must retain the above copyright 910c9ffa4SDavid Schultz * notice, this list of conditions and the following disclaimer. 1010c9ffa4SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright 1110c9ffa4SDavid Schultz * notice, this list of conditions and the following disclaimer in the 1210c9ffa4SDavid Schultz * documentation and/or other materials provided with the distribution. 1310c9ffa4SDavid Schultz * 1410c9ffa4SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1510c9ffa4SDavid Schultz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1610c9ffa4SDavid Schultz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1710c9ffa4SDavid Schultz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1810c9ffa4SDavid Schultz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1910c9ffa4SDavid Schultz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2010c9ffa4SDavid Schultz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2110c9ffa4SDavid Schultz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2210c9ffa4SDavid Schultz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2310c9ffa4SDavid Schultz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2410c9ffa4SDavid Schultz * SUCH DAMAGE. 2510c9ffa4SDavid Schultz */ 2610c9ffa4SDavid Schultz 2710c9ffa4SDavid Schultz #include <sys/cdefs.h> 2810c9ffa4SDavid Schultz #include <sys/limits.h> 2910c9ffa4SDavid Schultz #include <fenv.h> 3010c9ffa4SDavid Schultz #include <math.h> 3110c9ffa4SDavid Schultz 3210c9ffa4SDavid Schultz #ifndef type 3310c9ffa4SDavid Schultz __FBSDID("$FreeBSD$"); 3410c9ffa4SDavid Schultz #define type double 3510c9ffa4SDavid Schultz #define roundit round 3610c9ffa4SDavid Schultz #define dtype long 3710c9ffa4SDavid Schultz #define DTYPE_MIN LONG_MIN 3810c9ffa4SDavid Schultz #define DTYPE_MAX LONG_MAX 3910c9ffa4SDavid Schultz #define fn lround 4010c9ffa4SDavid Schultz #endif 4110c9ffa4SDavid Schultz 4210c9ffa4SDavid Schultz /* 4310c9ffa4SDavid Schultz * If type has more precision than dtype, the endpoints dtype_(min|max) are 4410c9ffa4SDavid Schultz * of the form xxx.5; they are "out of range" because lround() rounds away 4510c9ffa4SDavid Schultz * from 0. On the other hand, if type has less precision than dtype, then 4610c9ffa4SDavid Schultz * all values that are out of range are integral, so we might as well assume 4710c9ffa4SDavid Schultz * that everything is in range. (The correct condition in this case is 4810c9ffa4SDavid Schultz * harder to express.) At compile time, INRANGE(x) should reduce to two 4910c9ffa4SDavid Schultz * floating-point comparisons in the former case, or TRUE otherwise. 5010c9ffa4SDavid Schultz */ 5110c9ffa4SDavid Schultz static const double dtype_min = DTYPE_MIN - 0.5; 5210c9ffa4SDavid Schultz static const double dtype_max = DTYPE_MAX + 0.5; 5310c9ffa4SDavid Schultz #define INRANGE(x) (dtype_max - DTYPE_MAX != 0.5 || \ 5410c9ffa4SDavid Schultz ((x) > dtype_min && (x) < dtype_max)) 5510c9ffa4SDavid Schultz 5610c9ffa4SDavid Schultz dtype 5710c9ffa4SDavid Schultz fn(type x) 5810c9ffa4SDavid Schultz { 5910c9ffa4SDavid Schultz 6010c9ffa4SDavid Schultz if (INRANGE(x)) { 6110c9ffa4SDavid Schultz x = roundit(x); 6210c9ffa4SDavid Schultz return ((dtype)x); 6310c9ffa4SDavid Schultz } else { 6410c9ffa4SDavid Schultz feraiseexcept(FE_INVALID); 6510c9ffa4SDavid Schultz return (DTYPE_MAX); 6610c9ffa4SDavid Schultz } 6710c9ffa4SDavid Schultz } 68