VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/math/frexpl.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: frexpl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - No-CRT - frexpl().
4 */
5
6/*
7 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define IPRT_NO_CRT_FOR_3RD_PARTY
42#include "internal/nocrt.h"
43#include <iprt/nocrt/math.h>
44#include <iprt/assertcompile.h>
45#include <iprt/nocrt/limits.h>
46#ifdef RT_COMPILER_WITH_128BIT_LONG_DOUBLE
47# include <iprt/uint128.h>
48#endif
49
50
51/* Similar to the fxtract instruction. */
52#undef frexpl
53long double RT_NOCRT(frexpl)(long double lrdValue, int *piExp)
54{
55#ifdef RT_COMPILER_WITH_64BIT_LONG_DOUBLE
56 RTFLOAT64U Value;
57 AssertCompile(sizeof(Value) == sizeof(lrdValue));
58 Value.lrd = lrdValue;
59
60 if (RTFLOAT64U_IS_NORMAL(&Value))
61 {
62 *piExp = (int)Value.s.uExponent - RTFLOAT64U_EXP_BIAS + 1;
63 Value.s.uExponent = RTFLOAT64U_EXP_BIAS - 1;
64 }
65 else if (RTFLOAT64U_IS_ZERO(&Value))
66 {
67 *piExp = 0;
68 return lrdValue;
69 }
70 else if (RTFLOAT64U_IS_SUBNORMAL(&Value))
71 {
72 int iExp = -RTFLOAT64U_EXP_BIAS + 1;
73 uint64_t uFraction = Value.s64.uFraction;
74 while (!(uFraction & RT_BIT_64(RTFLOAT64U_FRACTION_BITS)))
75 {
76 iExp--;
77 uFraction <<= 1;
78 }
79 Value.s64.uFraction = uFraction;
80 Value.s64.uExponent = RTFLOAT64U_EXP_BIAS - 1;
81 *piExp = iExp + 1;
82 }
83 else
84 {
85 /* NaN, Inf */
86 *piExp = Value.s.fSign ? INT_MIN : INT_MAX;
87 return lrdValue;
88 }
89 return Value.lrd;
90
91#elif defined(RT_COMPILER_WITH_80BIT_LONG_DOUBLE)
92 RTFLOAT80U2 Value;
93 Value.r = lrdValue;
94
95 if (RTFLOAT80U_IS_NORMAL(&Value))
96 {
97 *piExp = (int)Value.s.uExponent - RTFLOAT80U_EXP_BIAS + 1;
98 Value.s.uExponent = RTFLOAT80U_EXP_BIAS - 1;
99 }
100 else if (RTFLOAT80U_IS_ZERO(&Value))
101 {
102 *piExp = 0;
103 return lrdValue;
104 }
105 else if (RTFLOAT80U_IS_DENORMAL_OR_PSEUDO_DENORMAL(&Value))
106 {
107 int iExp = -RTFLOAT80U_EXP_BIAS + 1;
108 while (!(Value.s.uMantissa & RT_BIT_64(RTFLOAT80U_FRACTION_BITS)))
109 {
110 iExp--;
111 Value.s.uMantissa <<= 1;
112 }
113 Value.s.uExponent = RTFLOAT80U_EXP_BIAS - 1;
114 *piExp = iExp + 1;
115 }
116 else /* NaN, Inf */
117 {
118 *piExp = Value.s.fSign ? INT_MIN : INT_MAX;
119 return lrdValue;
120 }
121 return Value.r;
122
123
124#elif defined(RT_COMPILER_WITH_128BIT_LONG_DOUBLE)
125 RTFLOAT128U Value;
126 AssertCompile(sizeof(Value) == sizeof(lrdValue));
127 Value.r = lrdValue;
128
129 if (RTFLOAT128U_IS_NORMAL(&Value))
130 {
131 *piExp = (int)Value.s.uExponent - RTFLOAT128U_EXP_BIAS + 1;
132 Value.s.uExponent = RTFLOAT128U_EXP_BIAS - 1;
133 }
134 else if (RTFLOAT128U_IS_ZERO(&Value))
135 {
136 *piExp = 0;
137 return lrdValue;
138 }
139 else if (RTFLOAT128U_IS_SUBNORMAL(&Value))
140 {
141 int iExp = -RTFLOAT128U_EXP_BIAS + 1;
142 RTUINT128U uFraction;
143 uFraction.s.Hi = Value.s64.uFractionHi;
144 uFraction.s.Lo = Value.s64.uFractionLo;
145 while (!(uFraction.s.Hi & RT_BIT_64(RTFLOAT128U_FRACTION_BITS - 64)))
146 {
147 iExp--;
148 RTUInt128AssignShiftLeft(&uFraction, 1);
149 }
150 Value.s64.uFractionHi = uFraction.s.Hi;
151 Value.s64.uFractionLo = uFraction.s.Lo;
152 Value.s64.uExponent = RTFLOAT64U_EXP_BIAS - 1;
153 *piExp = iExp + 1;
154 }
155 else
156 {
157 /* NaN, Inf */
158 *piExp = Value.s.fSign ? INT_MIN : INT_MAX;
159 return lrdValue;
160 }
161 return Value.r;
162#else
163# error "Port ME!"
164#endif
165}
166RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(frexpl);
167
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use