1140088Sdas/*- 2140088Sdas * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG> 3140088Sdas * All rights reserved. 4140088Sdas * 5140088Sdas * Redistribution and use in source and binary forms, with or without 6140088Sdas * modification, are permitted provided that the following conditions 7140088Sdas * are met: 8140088Sdas * 1. Redistributions of source code must retain the above copyright 9140088Sdas * notice, this list of conditions and the following disclaimer. 10140088Sdas * 2. Redistributions in binary form must reproduce the above copyright 11140088Sdas * notice, this list of conditions and the following disclaimer in the 12140088Sdas * documentation and/or other materials provided with the distribution. 13140088Sdas * 14140088Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15140088Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16140088Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17140088Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18140088Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19140088Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20140088Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21140088Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22140088Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23140088Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24140088Sdas * SUCH DAMAGE. 25140088Sdas */ 26140088Sdas 27140088Sdas#include <sys/cdefs.h> 28140088Sdas#include <sys/limits.h> 29140088Sdas#include <fenv.h> 30140088Sdas#include <math.h> 31140088Sdas 32140088Sdas#ifndef type 33140088Sdas__FBSDID("$FreeBSD$"); 34140088Sdas#define type double 35140088Sdas#define roundit round 36140088Sdas#define dtype long 37140088Sdas#define DTYPE_MIN LONG_MIN 38140088Sdas#define DTYPE_MAX LONG_MAX 39140088Sdas#define fn lround 40140088Sdas#endif 41140088Sdas 42140088Sdas/* 43140088Sdas * If type has more precision than dtype, the endpoints dtype_(min|max) are 44140088Sdas * of the form xxx.5; they are "out of range" because lround() rounds away 45140088Sdas * from 0. On the other hand, if type has less precision than dtype, then 46140088Sdas * all values that are out of range are integral, so we might as well assume 47144770Sdas * that everything is in range. At compile time, INRANGE(x) should reduce to 48144770Sdas * two floating-point comparisons in the former case, or TRUE otherwise. 49140088Sdas */ 50144770Sdasstatic const type dtype_min = DTYPE_MIN - 0.5; 51144770Sdasstatic const type dtype_max = DTYPE_MAX + 0.5; 52140088Sdas#define INRANGE(x) (dtype_max - DTYPE_MAX != 0.5 || \ 53140088Sdas ((x) > dtype_min && (x) < dtype_max)) 54140088Sdas 55140088Sdasdtype 56140088Sdasfn(type x) 57140088Sdas{ 58140088Sdas 59140088Sdas if (INRANGE(x)) { 60140088Sdas x = roundit(x); 61140088Sdas return ((dtype)x); 62140088Sdas } else { 63140088Sdas feraiseexcept(FE_INVALID); 64140088Sdas return (DTYPE_MAX); 65140088Sdas } 66140088Sdas} 67