1132366Sdas/*-
2132366Sdas * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
3132366Sdas * All rights reserved.
4132366Sdas *
5132366Sdas * Redistribution and use in source and binary forms, with or without
6132366Sdas * modification, are permitted provided that the following conditions
7132366Sdas * are met:
8132366Sdas * 1. Redistributions of source code must retain the above copyright
9132366Sdas *    notice, this list of conditions and the following disclaimer.
10132366Sdas * 2. Redistributions in binary form must reproduce the above copyright
11132366Sdas *    notice, this list of conditions and the following disclaimer in the
12132366Sdas *    documentation and/or other materials provided with the distribution.
13132366Sdas *
14132366Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15132366Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132366Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132366Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18132366Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132366Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132366Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132366Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132366Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132366Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132366Sdas * SUCH DAMAGE.
25132366Sdas *
26132366Sdas * $FreeBSD$
27132366Sdas */
28132366Sdas
29132366Sdas#include <math.h>
30132366Sdas
31132366Sdas#include "fpmath.h"
32132366Sdas
33132366Sdasdouble
34132366Sdasfrexp(double d, int *ex)
35132366Sdas{
36132366Sdas	union IEEEd2bits u;
37132366Sdas
38132366Sdas	u.d = d;
39132366Sdas	switch (u.bits.exp) {
40132366Sdas	case 0:		/* 0 or subnormal */
41132366Sdas		if ((u.bits.manl | u.bits.manh) == 0) {
42132366Sdas			*ex = 0;
43132366Sdas		} else {
44132366Sdas			u.d *= 0x1.0p514;
45132366Sdas			*ex = u.bits.exp - 1536;
46132366Sdas			u.bits.exp = 1022;
47132366Sdas		}
48132366Sdas		break;
49132366Sdas	case 2047:	/* infinity or NaN; value of *ex is unspecified */
50132366Sdas		break;
51132366Sdas	default:	/* normal */
52132366Sdas		*ex = u.bits.exp - 1022;
53132366Sdas		u.bits.exp = 1022;
54132366Sdas		break;
55132366Sdas	}
56132366Sdas	return (u.d);
57132366Sdas}
58