[BACK]Return to s_floorl.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libm / src

File: [cvs.NetBSD.org] / src / lib / libm / src / s_floorl.c (download)

Revision 1.1, Mon Nov 11 23:57:34 2013 UTC (10 years, 4 months ago) by joerg
Branch: MAIN
CVS Tags: yamt-pagecache-base9, tls-maxphys-base, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-10-base, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, HEAD
Branch point for: yamt-pagecache, tls-maxphys

Add initial version of ceill, floorl and roundl from FreeBSD.

/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 *
 * From: @(#)s_floor.c 5.1 93/09/24
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: s_floorl.c,v 1.1 2013/11/11 23:57:34 joerg Exp $");
#if 0
__FBSDID("$FreeBSD: head/lib/msun/src/s_floorl.c 176280 2008-02-14 15:10:34Z bde $");
#endif

/*
 * floorl(x)
 * Return x rounded toward -inf to integral value
 * Method:
 *	Bit twiddling.
 * Exception:
 *	Inexact flag raised if x not equal to floorl(x).
 */
#include "namespace.h"

#include <float.h>
#include <math.h>
#include <stdint.h>
#include <machine/ieee.h>

#ifdef __HAVE_LONG_DOUBLE

#ifdef __weak_alias
__weak_alias(floorl, _floorl)
#endif

#ifdef LDBL_IMPLICIT_NBIT
#define	MANH_SIZE	(EXT_FRACHBITS + 1)
#define	INC_MANH(ux, c)	do {					\
	uint64_t oi = ux.extu_frach;				\
	ux.extu_frach += (c);					\
	if (ux.extu_frach < oi)					\
		ux.extu_exp++;					\
} while (0)
#else
#define	MANH_SIZE	EXT_FRACHBITS
#define	INC_MANH(ux, c)	do {					\
	uint64_t oi = ux.extu_frach;				\
	ux.extu_frach += (c);					\
	if (ux.extu_frach < oi) {				\
		ux.extu_exp++;					\
		ux.extu_frach |= 1llu << (EXT_FRACHBITS - 1);	\
	}							\
} while (0)
#endif

static const long double huge = 1.0e300;

long double
floorl(long double x)
{
	union ieee_ext_u ux = { .extu_ld = x, };
	int e = ux.extu_exp - LDBL_MAX_EXP + 1;

	if (e < MANH_SIZE - 1) {
		if (e < 0) {			/* raise inexact if x != 0 */
			if (huge + x > 0.0)
				if (ux.extu_exp > 0 ||
				    (ux.extu_frach | ux.extu_fracl) != 0)
					ux.extu_ld = ux.extu_sign ? -1.0 : 0.0;
		} else {
			uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
			if (((ux.extu_frach & m) | ux.extu_fracl) == 0)
				return (x);	/* x is integral */
			if (ux.extu_sign) {
#ifdef LDBL_IMPLICIT_NBIT
				if (e == 0)
					ux.extu_exp++;
				else
#endif
				INC_MANH(ux, 1llu << (MANH_SIZE - e - 1));
			}
			if (huge + x > 0.0) {	/* raise inexact flag */
				ux.extu_frach &= ~m;
				ux.extu_fracl = 0;
			}
		}
	} else if (e < LDBL_MANT_DIG - 1) {
		uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
		if ((ux.extu_fracl & m) == 0)
			return (x);	/* x is integral */
		if (ux.extu_sign) {
			if (e == MANH_SIZE - 1)
				INC_MANH(ux, 1);
			else {
				uint64_t o = ux.extu_fracl;
				ux.extu_fracl += 1llu << (LDBL_MANT_DIG - e - 1);
				if (ux.extu_fracl < o)	/* got a carry */
					INC_MANH(ux, 1);
			}
		}
		if (huge + x > 0.0)		/* raise inexact flag */
			ux.extu_fracl &= ~m;
	}
	return (ux.extu_ld);
}

#endif /* __HAVE_LONG_DOUBLE */