1*38ffb667Sespie /* $OpenBSD: frexp.c,v 1.10 2013/07/03 04:46:36 espie Exp $ */
2aeb694e3Smartynas
3aeb694e3Smartynas /*-
4aeb694e3Smartynas * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
5aeb694e3Smartynas * All rights reserved.
6aeb694e3Smartynas *
7aeb694e3Smartynas * Redistribution and use in source and binary forms, with or without
8aeb694e3Smartynas * modification, are permitted provided that the following conditions
9aeb694e3Smartynas * are met:
10aeb694e3Smartynas * 1. Redistributions of source code must retain the above copyright
11aeb694e3Smartynas * notice, this list of conditions and the following disclaimer.
12aeb694e3Smartynas * 2. Redistributions in binary form must reproduce the above copyright
13aeb694e3Smartynas * notice, this list of conditions and the following disclaimer in the
14aeb694e3Smartynas * documentation and/or other materials provided with the distribution.
15aeb694e3Smartynas *
16aeb694e3Smartynas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17aeb694e3Smartynas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18aeb694e3Smartynas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19aeb694e3Smartynas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20aeb694e3Smartynas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21aeb694e3Smartynas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22aeb694e3Smartynas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23aeb694e3Smartynas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24aeb694e3Smartynas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25aeb694e3Smartynas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26aeb694e3Smartynas * SUCH DAMAGE.
27aeb694e3Smartynas *
28aeb694e3Smartynas * $FreeBSD: frexp.c,v 1.1 2004/07/18 21:23:39 das Exp $
29aeb694e3Smartynas */
30aeb694e3Smartynas
31aeb694e3Smartynas #include <sys/types.h>
32aeb694e3Smartynas #include <machine/ieee.h>
33aeb694e3Smartynas #include <float.h>
34aeb694e3Smartynas #include <math.h>
35aeb694e3Smartynas
36aeb694e3Smartynas double
frexp(double v,int * ex)37aeb694e3Smartynas frexp(double v, int *ex)
38aeb694e3Smartynas {
39aeb694e3Smartynas union {
40aeb694e3Smartynas double v;
41aeb694e3Smartynas struct ieee_double s;
42aeb694e3Smartynas } u;
43aeb694e3Smartynas
44aeb694e3Smartynas u.v = v;
45aeb694e3Smartynas switch (u.s.dbl_exp) {
46aeb694e3Smartynas case 0: /* 0 or subnormal */
47aeb694e3Smartynas if ((u.s.dbl_fracl | u.s.dbl_frach) == 0) {
48aeb694e3Smartynas *ex = 0;
49aeb694e3Smartynas } else {
50aeb694e3Smartynas /*
51aeb694e3Smartynas * The power of 2 is arbitrary, any value from 54 to
52aeb694e3Smartynas * 1024 will do.
53aeb694e3Smartynas */
54aeb694e3Smartynas u.v *= 0x1.0p514;
55aeb694e3Smartynas *ex = u.s.dbl_exp - (DBL_EXP_BIAS - 1 + 514);
56aeb694e3Smartynas u.s.dbl_exp = DBL_EXP_BIAS - 1;
57aeb694e3Smartynas }
58aeb694e3Smartynas break;
59aeb694e3Smartynas case DBL_EXP_INFNAN: /* Inf or NaN; value of *ex is unspecified */
60aeb694e3Smartynas break;
61aeb694e3Smartynas default: /* normal */
62aeb694e3Smartynas *ex = u.s.dbl_exp - (DBL_EXP_BIAS - 1);
63aeb694e3Smartynas u.s.dbl_exp = DBL_EXP_BIAS - 1;
64aeb694e3Smartynas break;
65aeb694e3Smartynas }
66aeb694e3Smartynas return (u.v);
67aeb694e3Smartynas }
68aeb694e3Smartynas
69*38ffb667Sespie #if LDBL_MANT_DIG == DBL_MANT_DIG
702fbf033eSmartynas __strong_alias(frexpl, frexp);
71*38ffb667Sespie #endif /* LDBL_MANT_DIG == DBL_MANT_DIG */
72