1f9f848faSopenharmony_ci/*-
2f9f848faSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
3f9f848faSopenharmony_ci *
4f9f848faSopenharmony_ci * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5f9f848faSopenharmony_ci * All rights reserved.
6f9f848faSopenharmony_ci *
7f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without
8f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions
9f9f848faSopenharmony_ci * are met:
10f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
11f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
12f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
13f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
14f9f848faSopenharmony_ci *    documentation and/or other materials provided with the distribution.
15f9f848faSopenharmony_ci *
16f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f9f848faSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f9f848faSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f9f848faSopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f9f848faSopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f9f848faSopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f9f848faSopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f9f848faSopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f9f848faSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f9f848faSopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f9f848faSopenharmony_ci * SUCH DAMAGE.
27f9f848faSopenharmony_ci */
28f9f848faSopenharmony_ci
29f9f848faSopenharmony_ci#include <float.h>
30f9f848faSopenharmony_ci#include <math.h>
31f9f848faSopenharmony_ci
32f9f848faSopenharmony_ci#include "fpmath.h"
33f9f848faSopenharmony_ci
34f9f848faSopenharmony_ci#if LDBL_MAX_EXP != 0x4000
35f9f848faSopenharmony_ci#error "Unsupported long double format"
36f9f848faSopenharmony_ci#endif
37f9f848faSopenharmony_ci
38f9f848faSopenharmony_cilong double
39f9f848faSopenharmony_cifrexpl(long double x, int *ex)
40f9f848faSopenharmony_ci{
41f9f848faSopenharmony_ci	union IEEEl2bits u;
42f9f848faSopenharmony_ci
43f9f848faSopenharmony_ci	u.e = x;
44f9f848faSopenharmony_ci	switch (u.bits.exp) {
45f9f848faSopenharmony_ci	case 0:		/* 0 or subnormal */
46f9f848faSopenharmony_ci		if ((u.bits.manl | u.bits.manh) == 0) {
47f9f848faSopenharmony_ci			*ex = 0;
48f9f848faSopenharmony_ci		} else {
49f9f848faSopenharmony_ci			u.e *= 0x1.0p514;
50f9f848faSopenharmony_ci			*ex = u.bits.exp - 0x4200;
51f9f848faSopenharmony_ci			u.bits.exp = 0x3ffe;
52f9f848faSopenharmony_ci		}
53f9f848faSopenharmony_ci		break;
54f9f848faSopenharmony_ci	case 0x7fff:	/* infinity or NaN; value of *ex is unspecified */
55f9f848faSopenharmony_ci		break;
56f9f848faSopenharmony_ci	default:	/* normal */
57f9f848faSopenharmony_ci		*ex = u.bits.exp - 0x3ffe;
58f9f848faSopenharmony_ci		u.bits.exp = 0x3ffe;
59f9f848faSopenharmony_ci		break;
60f9f848faSopenharmony_ci	}
61f9f848faSopenharmony_ci	return (u.e);
62f9f848faSopenharmony_ci}
63