s_logbl.c revision 324006
151673Smdodd/*
251673Smdodd * From: @(#)s_ilogb.c 5.1 93/09/24
351673Smdodd * ====================================================
451673Smdodd * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
551673Smdodd *
651673Smdodd * Developed at SunPro, a Sun Microsystems, Inc. business.
751673Smdodd * Permission to use, copy, modify, and distribute this
851673Smdodd * software is freely granted, provided that this notice
951673Smdodd * is preserved.
1051673Smdodd * ====================================================
1151673Smdodd */
1251673Smdodd
1351673Smdodd#include <sys/cdefs.h>
1451673Smdodd__FBSDID("$FreeBSD: stable/11/lib/msun/src/s_logbl.c 324006 2017-09-26 09:01:56Z dim $");
1551673Smdodd
1651673Smdodd#include <float.h>
1751673Smdodd#include <limits.h>
1851673Smdodd#include <math.h>
1951673Smdodd
2051673Smdodd#include "fpmath.h"
2151673Smdodd
2251673Smdoddlong double
2351673Smdoddlogbl(long double x)
2451673Smdodd{
2551673Smdodd	union IEEEl2bits u;
2651673Smdodd	unsigned long m;
2751673Smdodd	int b;
2851673Smdodd
2951673Smdodd	u.e = x;
3051673Smdodd	if (u.bits.exp == 0) {
3151673Smdodd		if ((u.bits.manl | u.bits.manh) == 0) {	/* x == 0 */
3251673Smdodd			u.bits.sign = 1;
3351673Smdodd			return (1.0L / u.e);
3452549Smdodd		}
3551673Smdodd		/* denormalized */
3651673Smdodd		if (u.bits.manh == 0) {
3751673Smdodd			m = 1lu << (LDBL_MANL_SIZE - 1);
3852549Smdodd			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
3952549Smdodd				b++;
4052549Smdodd		} else {
4152549Smdodd			m = 1lu << (LDBL_MANH_SIZE - 1);
4252549Smdodd			for (b = 0; !(u.bits.manh & m); m >>= 1)
4352549Smdodd				b++;
4452549Smdodd		}
4551673Smdodd#ifdef LDBL_IMPLICIT_NBIT
4652549Smdodd		b++;
4752549Smdodd#endif
4851673Smdodd		return ((long double)(LDBL_MIN_EXP - b - 1));
4952549Smdodd	}
5051673Smdodd	if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)	/* normal */
5151673Smdodd		return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
5251673Smdodd	else						/* +/- inf or nan */
53110835Smdodd		return (x * x);
54110835Smdodd}
5551673Smdodd