11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*-
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/cdefs.h>
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <fenv.h>
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <math.h>
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef type
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* __FBSDID("$FreeBSD: src/lib/msun/src/s_lrint.c,v 1.1 2005/01/11 23:12:55 das Exp $"); */
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define type		double
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	roundit		rint
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define dtype		long
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define	fn		lrint
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * C99 says we should not raise a spurious inexact exception when an
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * invalid exception is raised.  Unfortunately, the set of inputs
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * that overflows depends on the rounding mode when 'dtype' has more
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * significant bits than 'type'.  Hence, we bend over backwards for the
441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * sake of correctness; an MD implementation could be more efficient.
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectdtype
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfn(type x)
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	fenv_t env;
501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	dtype d;
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	feholdexcept(&env);
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	d = (dtype)roundit(x);
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	if (fetestexcept(FE_INVALID))
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project		feclearexcept(FE_INEXACT);
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	feupdateenv(&env);
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	return (d);
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
59