Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
math_legacy.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3
4#ifndef SYMBIAN_RUNTIME_MATH_LEGACY_H_
5#define SYMBIAN_RUNTIME_MATH_LEGACY_H_
6
7#include <bit>
8#include <cstdint>
9
10#include <absl/base/nullability.h>
11
13
14inline float Ceil(float value) {
15 const std::uint32_t bits = std::bit_cast<std::uint32_t>(value);
16 const int exponent = static_cast<int>((bits >> 23) & 0xff) - 127;
17 if (exponent >= 23) {
18 return value;
19 }
20 if (exponent < 0) {
21 if ((bits & 0x7fffffffU) == 0) {
22 return value;
23 }
24 return (bits & 0x80000000U) != 0 ? -0.0f : 1.0f;
25 }
26 const std::uint32_t fraction = (1U << (23 - exponent)) - 1;
27 if ((bits & fraction) == 0) {
28 return value;
29 }
30 std::uint32_t integral = bits & ~fraction;
31 if ((bits & 0x80000000U) == 0) {
32 integral += 1U << (23 - exponent);
33 }
34 return std::bit_cast<float>(integral);
35}
36
37inline double Frexp(double value, int* absl_nonnull exponent) {
38 std::uint64_t bits = std::bit_cast<std::uint64_t>(value);
39 int biased = static_cast<int>((bits >> 52) & 0x7ff);
40 *exponent = 0;
41 if (biased == 0x7ff || (bits & 0x7fffffffffffffffULL) == 0) {
42 return value;
43 }
44 if (biased == 0) {
45 value *= 0x1p54;
46 bits = std::bit_cast<std::uint64_t>(value);
47 biased = static_cast<int>((bits >> 52) & 0x7ff);
48 *exponent -= 54;
49 }
50 *exponent += biased - 1022;
51 bits = (bits & 0x800fffffffffffffULL) | (std::uint64_t{1022} << 52);
52 return std::bit_cast<double>(bits);
53}
54
55inline double Ldexp(double value, int exponent) {
56 std::uint64_t bits = std::bit_cast<std::uint64_t>(value);
57 if ((bits & 0x7fffffffffffffffULL) == 0 ||
58 (bits & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL ||
59 exponent == 0) {
60 return value;
61 }
62 int biased = static_cast<int>((bits >> 52) & 0x7ff);
63 if (biased == 0) {
64 value *= 0x1p54;
65 bits = std::bit_cast<std::uint64_t>(value);
66 biased = static_cast<int>((bits >> 52) & 0x7ff) - 54;
67 }
68 if (exponent > 50000) {
69 exponent = 50000;
70 } else if (exponent < -50000) {
71 exponent = -50000;
72 }
73 const int result_exponent = biased + exponent;
74 if (result_exponent >= 0x7ff) {
75 return std::bit_cast<double>((bits & 0x8000000000000000ULL) |
76 0x7ff0000000000000ULL);
77 }
78 if (result_exponent > 0) {
79 bits = (bits & 0x800fffffffffffffULL) |
80 (static_cast<std::uint64_t>(result_exponent) << 52);
81 return std::bit_cast<double>(bits);
82 }
83 if (result_exponent <= -53) {
84 return std::bit_cast<double>(bits & 0x8000000000000000ULL);
85 }
86 const int shift = 1 - result_exponent;
87 const std::uint64_t significand =
88 (bits & 0x000fffffffffffffULL) | 0x0010000000000000ULL;
89 const std::uint64_t quotient = significand >> shift;
90 const std::uint64_t remainder =
91 significand & ((std::uint64_t{1} << shift) - 1);
92 const std::uint64_t halfway = std::uint64_t{1} << (shift - 1);
93 const std::uint64_t rounded =
94 quotient +
95 (remainder > halfway || (remainder == halfway && (quotient & 1)));
96 return std::bit_cast<double>((bits & 0x8000000000000000ULL) | rounded);
97}
98
99inline float Scalbn(float value, int exponent) {
100 std::uint32_t bits = std::bit_cast<std::uint32_t>(value);
101 if ((bits & 0x7fffffffU) == 0 || (bits & 0x7f800000U) == 0x7f800000U ||
102 exponent == 0) {
103 return value;
104 }
105 int biased = static_cast<int>((bits >> 23) & 0xff);
106 if (biased == 0) {
107 value *= 0x1p25f;
108 bits = std::bit_cast<std::uint32_t>(value);
109 biased = static_cast<int>((bits >> 23) & 0xff) - 25;
110 }
111 if (exponent > 50000) {
112 exponent = 50000;
113 } else if (exponent < -50000) {
114 exponent = -50000;
115 }
116 const int result_exponent = biased + exponent;
117 if (result_exponent >= 0xff) {
118 return std::bit_cast<float>((bits & 0x80000000U) | 0x7f800000U);
119 }
120 if (result_exponent > 0) {
121 bits = (bits & 0x807fffffU) |
122 (static_cast<std::uint32_t>(result_exponent) << 23);
123 return std::bit_cast<float>(bits);
124 }
125 if (result_exponent <= -24) {
126 return std::bit_cast<float>(bits & 0x80000000U);
127 }
128 const int shift = 1 - result_exponent;
129 const std::uint32_t significand = (bits & 0x007fffffU) | 0x00800000U;
130 const std::uint32_t quotient = significand >> shift;
131 const std::uint32_t remainder =
132 significand & ((std::uint32_t{1} << shift) - 1);
133 const std::uint32_t halfway = std::uint32_t{1} << (shift - 1);
134 const std::uint32_t rounded =
135 quotient +
136 (remainder > halfway || (remainder == halfway && (quotient & 1)));
137 return std::bit_cast<float>((bits & 0x80000000U) | rounded);
138}
139
140inline double Modf(double value, double* absl_nonnull integral) {
141 const std::uint64_t bits = std::bit_cast<std::uint64_t>(value);
142 const std::uint64_t sign = bits & 0x8000000000000000ULL;
143 const int exponent = static_cast<int>((bits >> 52) & 0x7ff) - 1023;
144 if (exponent < 0) {
145 *integral = std::bit_cast<double>(sign);
146 return value;
147 }
148 if (exponent >= 52) {
149 *integral = value;
150 return (bits & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL
151 ? value
152 : std::bit_cast<double>(sign);
153 }
154 const std::uint64_t fraction = (std::uint64_t{1} << (52 - exponent)) - 1;
155 *integral = std::bit_cast<double>(bits & ~fraction);
156 if ((bits & fraction) == 0) {
157 return std::bit_cast<double>(sign);
158 }
159 return value - *integral;
160}
161
162inline float Nextafter(float value, float toward) {
163 std::uint32_t bits = std::bit_cast<std::uint32_t>(value);
164 const std::uint32_t target = std::bit_cast<std::uint32_t>(toward);
165 if ((bits & 0x7fffffffU) > 0x7f800000U ||
166 (target & 0x7fffffffU) > 0x7f800000U) {
167 return value + toward;
168 }
169 if (value == toward) {
170 return toward;
171 }
172 if ((bits & 0x7fffffffU) == 0) {
173 return std::bit_cast<float>((target & 0x80000000U) | 1U);
174 }
175 if ((value < toward) == (value > 0)) {
176 ++bits;
177 } else {
178 --bits;
179 }
180 return std::bit_cast<float>(bits);
181}
182
183inline double Round(double value) {
184 const std::uint64_t bits = std::bit_cast<std::uint64_t>(value);
185 const int exponent = static_cast<int>((bits >> 52) & 0x7ff) - 1023;
186 if (exponent >= 52) {
187 return value;
188 }
189 const std::uint64_t sign = bits & 0x8000000000000000ULL;
190 if (exponent < -1) {
191 return std::bit_cast<double>(sign);
192 }
193 if (exponent == -1) {
194 return std::bit_cast<double>(sign | 0x3ff0000000000000ULL);
195 }
196 const std::uint64_t fraction = (std::uint64_t{1} << (52 - exponent)) - 1;
197 const std::uint64_t half = std::uint64_t{1} << (51 - exponent);
198 std::uint64_t rounded = bits & ~fraction;
199 if ((bits & fraction) >= half) {
200 rounded += std::uint64_t{1} << (52 - exponent);
201 }
202 return std::bit_cast<double>(rounded);
203}
204
205} // namespace symbian::runtime::math
206
207#endif // SYMBIAN_RUNTIME_MATH_LEGACY_H_
unsigned fraction
Definition frame.cc:154
Definition math_legacy.h:12
double Ldexp(double value, int exponent)
Definition math_legacy.h:55
double Frexp(double value, int *absl_nonnull exponent)
Definition math_legacy.h:37
double Round(double value)
Definition math_legacy.h:183
float Ceil(float value)
Definition math_legacy.h:14
float Scalbn(float value, int exponent)
Definition math_legacy.h:99
double Modf(double value, double *absl_nonnull integral)
Definition math_legacy.h:140
float Nextafter(float value, float toward)
Definition math_legacy.h:162
std::string target
Definition sis.cc:296
libusb_context *absl_nullable value
Definition usb.cc:37