xref: /dflybsd-src/lib/libc/gen/frexp.c (revision 6ff43c949ec457c91648dc9aef9dbf805e4083d6)
1037fad31SPeter Avalos /*-
2037fad31SPeter Avalos  * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
3037fad31SPeter Avalos  * All rights reserved.
4037fad31SPeter Avalos  *
5037fad31SPeter Avalos  * Redistribution and use in source and binary forms, with or without
6037fad31SPeter Avalos  * modification, are permitted provided that the following conditions
7037fad31SPeter Avalos  * are met:
8037fad31SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
9037fad31SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
10037fad31SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
11037fad31SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
12037fad31SPeter Avalos  *    documentation and/or other materials provided with the distribution.
13037fad31SPeter Avalos  *
14037fad31SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15037fad31SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16037fad31SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17037fad31SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18037fad31SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19037fad31SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20037fad31SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21037fad31SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22037fad31SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23037fad31SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24037fad31SPeter Avalos  * SUCH DAMAGE.
25037fad31SPeter Avalos  *
26*6ff43c94SPeter Avalos  * $FreeBSD: head/lib/libc/gen/frexp.c 132366 2004-07-18 21:23:39Z das $
27037fad31SPeter Avalos  */
28037fad31SPeter Avalos 
29037fad31SPeter Avalos #include <math.h>
30037fad31SPeter Avalos 
31037fad31SPeter Avalos #include "fpmath.h"
32037fad31SPeter Avalos 
33037fad31SPeter Avalos double
frexp(double d,int * ex)34037fad31SPeter Avalos frexp(double d, int *ex)
35037fad31SPeter Avalos {
36037fad31SPeter Avalos 	union IEEEd2bits u;
37037fad31SPeter Avalos 
38037fad31SPeter Avalos 	u.d = d;
39037fad31SPeter Avalos 	switch (u.bits.exp) {
40037fad31SPeter Avalos 	case 0:		/* 0 or subnormal */
41037fad31SPeter Avalos 		if ((u.bits.manl | u.bits.manh) == 0) {
42037fad31SPeter Avalos 			*ex = 0;
43037fad31SPeter Avalos 		} else {
44037fad31SPeter Avalos 			u.d *= 0x1.0p514;
45037fad31SPeter Avalos 			*ex = u.bits.exp - 1536;
46037fad31SPeter Avalos 			u.bits.exp = 1022;
47037fad31SPeter Avalos 		}
48037fad31SPeter Avalos 		break;
49037fad31SPeter Avalos 	case 2047:	/* infinity or NaN; value of *ex is unspecified */
50037fad31SPeter Avalos 		break;
51037fad31SPeter Avalos 	default:	/* normal */
52037fad31SPeter Avalos 		*ex = u.bits.exp - 1022;
53037fad31SPeter Avalos 		u.bits.exp = 1022;
54037fad31SPeter Avalos 		break;
55037fad31SPeter Avalos 	}
56037fad31SPeter Avalos 	return (u.d);
57037fad31SPeter Avalos }
58