The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Elliott Hughes | 8da8ca4 | 2018-05-08 13:35:33 -0700 | [diff] [blame] | 13 | #include <sys/cdefs.h> |
| 14 | __FBSDID("$FreeBSD: head/lib/msun/src/s_logbl.c 306409 2016-09-28 14:48:34Z emaste $"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 15 | |
| 16 | #include <float.h> |
| 17 | #include <limits.h> |
| 18 | #include <math.h> |
| 19 | |
| 20 | #include "fpmath.h" |
| 21 | |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 22 | long double |
| 23 | logbl(long double x) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 24 | { |
| 25 | union IEEEl2bits u; |
| 26 | unsigned long m; |
| 27 | int b; |
| 28 | |
| 29 | u.e = x; |
| 30 | if (u.bits.exp == 0) { |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 31 | if ((u.bits.manl | u.bits.manh) == 0) { /* x == 0 */ |
| 32 | u.bits.sign = 1; |
| 33 | return (1.0L / u.e); |
| 34 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | /* denormalized */ |
| 36 | if (u.bits.manh == 0) { |
| 37 | m = 1lu << (LDBL_MANL_SIZE - 1); |
| 38 | for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1) |
| 39 | b++; |
| 40 | } else { |
| 41 | m = 1lu << (LDBL_MANH_SIZE - 1); |
| 42 | for (b = 0; !(u.bits.manh & m); m >>= 1) |
| 43 | b++; |
| 44 | } |
| 45 | #ifdef LDBL_IMPLICIT_NBIT |
| 46 | b++; |
| 47 | #endif |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 48 | return ((long double)(LDBL_MIN_EXP - b - 1)); |
| 49 | } |
| 50 | if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1) /* normal */ |
| 51 | return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1)); |
| 52 | else /* +/- inf or nan */ |
| 53 | return (x * x); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | } |