blob: 7e88e36f6ab405768095c6d8c6fb99f1882adc0b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * From: @(#)s_ilogb.c 5.1 93/09/24
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
13#ifndef lint
Elliott Hughesa0ee0782013-01-30 19:06:37 -080014static char rcsid[] = "$FreeBSD$";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080015#endif
16
17#include <float.h>
18#include <limits.h>
19#include <math.h>
20
21#include "fpmath.h"
22
Elliott Hughesa0ee0782013-01-30 19:06:37 -080023long double
24logbl(long double x)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080025{
26 union IEEEl2bits u;
27 unsigned long m;
28 int b;
29
30 u.e = x;
31 if (u.bits.exp == 0) {
Elliott Hughesa0ee0782013-01-30 19:06:37 -080032 if ((u.bits.manl | u.bits.manh) == 0) { /* x == 0 */
33 u.bits.sign = 1;
34 return (1.0L / u.e);
35 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036 /* denormalized */
37 if (u.bits.manh == 0) {
38 m = 1lu << (LDBL_MANL_SIZE - 1);
39 for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
40 b++;
41 } else {
42 m = 1lu << (LDBL_MANH_SIZE - 1);
43 for (b = 0; !(u.bits.manh & m); m >>= 1)
44 b++;
45 }
46#ifdef LDBL_IMPLICIT_NBIT
47 b++;
48#endif
Elliott Hughesa0ee0782013-01-30 19:06:37 -080049 return ((long double)(LDBL_MIN_EXP - b - 1));
50 }
51 if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1) /* normal */
52 return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
53 else /* +/- inf or nan */
54 return (x * x);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055}