source: mainline/uspace/lib/cpp/include/__bits/limits.hpp@ 8fd0675f

Last change on this file since 8fd0675f was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

  • Property mode set to 100644
File size: 15.4 KB
RevLine 
[a3067af]1/*
[b57ba05]2 * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
[a3067af]3 *
[b57ba05]4 * SPDX-License-Identifier: BSD-3-Clause
[a3067af]5 */
6
[b57a3ee]7#ifndef LIBCPP_BITS_LIMITS
8#define LIBCPP_BITS_LIMITS
[a3067af]9
10#include <cstdint>
[092a436]11#include <climits>
[a3067af]12
13namespace std
14{
15
16 /**
17 * 18.3.2.5, type float_round_style:
18 */
19
20 enum float_round_style
21 {
22 round_indeterminate = -1,
23 round_toward_zero = 0,
24 round_to_nearest = 1,
25 round_toward_infinity = 2,
26 round_toward_neg_infinity = 3
27 };
28
29 /**
30 * 18.3.2.6, type float_denorm_style:
31 */
32
33 enum float_denorm_style
34 {
35 denorm_indeterminate = -1,
36 denorm_absent = 0,
37 denorm_present = 1
38 };
39
40 /**
41 * 18.3.2.3, class template numeric_limits:
42 */
43
44 namespace aux
45 {
46 template<class T>
47 class numeric_limits
48 {
49 public:
50 static constexpr bool is_specialized = false;
51
52 static constexpr T min() noexcept
53 {
54 return T{};
55 }
56
57 static constexpr T max() noexcept
58 {
59 return T{};
60 }
61
62 static constexpr T lowest() noexcept
63 {
64 return T{};
65 }
66
67 static constexpr int digits = 0;
68 static constexpr int digits10 = 0;
69 static constexpr int max_digits10 = 0;
70
71 static constexpr bool is_signed = false;
72 static constexpr bool is_integer = false;
73 static constexpr bool is_exact = false;
74
75 static constexpr int radix = 0;
76
77 static constexpr T epsilon() noexcept
78 {
79 return T{};
80 }
81
82 static constexpr T round_error() noexcept
83 {
84 return T{};
85 }
86
87 static constexpr int min_exponent = 0;
88 static constexpr int min_exponent10 = 0;
89 static constexpr int max_exponent = 0;
90 static constexpr int max_exponent10 = 0;
91
92 static constexpr bool has_infinity = false;
93 static constexpr bool has_quiet_NaN = false;
94 static constexpr bool has_signaling_NaN = false;
95
96 static constexpr float_denorm_style has_denorm = denorm_absent;
97 static constexpr bool has_denorm_loss = false;
98
99 static constexpr T infinity() noexcept
100 {
101 return T{};
102 }
103
104 static constexpr T quiet_NaN() noexcept
105 {
106 return T{};
107 }
108
109 static constexpr T signaling_NaN() noexcept
110 {
111 return T{};
112 }
113
114 static constexpr T denorm_min() noexcept
115 {
116 return T{};
117 }
118
119 static constexpr bool is_iec559 = false;
120 static constexpr bool is_bounded = false;
121 static constexpr bool is_modulo = false;
122
123 static constexpr bool traps = false;
124 static constexpr bool tinyness_before = false;
125
126 static constexpr float_round_style round_style = round_toward_zero;
127 };
128 }
129
130 template<class T>
131 class numeric_limits: public aux::numeric_limits<T>
132 { /* DUMMY BODY */ };
133
134 template<class T>
135 class numeric_limits<const T>: public numeric_limits<T>
136 { /* DUMMY BODY */ };
137
138 template<class T>
139 class numeric_limits<volatile T>: public numeric_limits<T>
140 { /* DUMMY BODY */ };
141
142 template<class T>
143 class numeric_limits<const volatile T>: public numeric_limits<T>
144 { /* DUMMY BODY */ };
145
146 /**
147 * 18.3.2.3, class template numeric_limits:
148 */
149
150 template<>
151 class numeric_limits<float>
152 {
153 public:
154 static constexpr bool is_specialized = true;
155
156 static constexpr float min() noexcept
157 {
158 return 1.17549435e-38f;
159 }
160
161 static constexpr float max() noexcept
162 {
163 return 3.40282347e+38f;
164 }
165
166 static constexpr float lowest() noexcept
167 {
168 return -3.40282347e+38f;
169 }
170
171 static constexpr int digits = 24;
172 static constexpr int digits10 = 6;
173 static constexpr int max_digits10 = 9;
174
175 static constexpr bool is_signed = true;
176 static constexpr bool is_integer = false;
177 static constexpr bool is_exact = false;
178
179 static constexpr int radix = 2;
180
181 static constexpr float epsilon() noexcept
182 {
183 return 1.19209290e-07f;
184 }
185
186 static constexpr float round_error() noexcept
187 {
188 return 0.5f;
189 }
190
191 static constexpr int min_exponent = -127;
192 static constexpr int min_exponent10 = -37;
193 static constexpr int max_exponent = 128;
194 static constexpr int max_exponent10 = 38;
195
196 static constexpr bool has_infinity = true;
197 static constexpr bool has_quiet_NaN = true;
198 static constexpr bool has_signaling_NaN = true;
199
200 static constexpr float_denorm_style has_denorm = denorm_absent;
201 static constexpr bool has_denorm_loss = false;
202
203 static constexpr float infinity() noexcept
204 {
205 // TODO: implement
206 return 0.f;
207 }
208
209 static constexpr float quiet_NaN() noexcept
210 {
211 // TODO: implement
212 return 0.f;
213 }
214
215 static constexpr float signaling_NaN() noexcept
216 {
217 // TODO: implement
218 return 0.f;
219 }
220
221 static constexpr float denorm_min() noexcept
222 {
223 return min();
224 }
225
226 static constexpr bool is_iec559 = true;
227 static constexpr bool is_bounded = true;
228 static constexpr bool is_modulo = false;
229
230 static constexpr bool traps = true;
231 static constexpr bool tinyness_before = true;
232
233 static constexpr float_round_style round_style = round_to_nearest;
234 };
235
236 template<>
237 class numeric_limits<bool>
238 {
239 public:
240 static constexpr bool is_specialized = true;
241
242 static constexpr bool min() noexcept
243 {
244 return false;
245 }
246
247 static constexpr bool max() noexcept
248 {
249 return true;
250 }
251
252 static constexpr bool lowest() noexcept
253 {
254 return false;
255 }
256
257 static constexpr int digits = 1;
258 static constexpr int digits10 = 0;
259 static constexpr int max_digits10 = 0;
260
261 static constexpr bool is_signed = false;
262 static constexpr bool is_integer = true;
263 static constexpr bool is_exact = true;
264
265 static constexpr int radix = 2;
266
267 static constexpr bool epsilon() noexcept
268 {
269 return 0;
270 }
271
272 static constexpr bool round_error() noexcept
273 {
274 return 0;
275 }
276
277 static constexpr int min_exponent = 0;
278 static constexpr int min_exponent10 = 0;
279 static constexpr int max_exponent = 0;
280 static constexpr int max_exponent10 = 0;
281
282 static constexpr bool has_infinity = false;
283 static constexpr bool has_quiet_NaN = false;
284 static constexpr bool has_signaling_NaN = false;
285
286 static constexpr float_denorm_style has_denorm = denorm_absent;
287 static constexpr bool has_denorm_loss = false;
288
289 static constexpr bool infinity() noexcept
290 {
291 return 0;
292 }
293
294 static constexpr bool quiet_NaN() noexcept
295 {
296 return 0;
297 }
298
299 static constexpr bool signaling_NaN() noexcept
300 {
301 return 0;
302 }
303
304 static constexpr bool denorm_min() noexcept
305 {
306 return 0;
307 }
308
309 static constexpr bool is_iec559 = false;
310 static constexpr bool is_bounded = true;
311 static constexpr bool is_modulo = false;
312
313 static constexpr bool traps = false;
314 static constexpr bool tinyness_before = false;
315
316 static constexpr float_round_style round_style = round_toward_zero;
317 };
318
319 /**
320 * Note: Because of the limited state of limit.h, we define only
321 * the most basic properties of the most basic types (that are likely
322 * to be used in a program that is being ported to HelenOS).
323 */
324
325 template<>
[092a436]326 class numeric_limits<char>: public aux::numeric_limits<char>
[a3067af]327 {
328 public:
329 static constexpr bool is_specialized = true;
330
[980ad03]331 static constexpr int digits = sizeof(char) * 8;
332
[092a436]333 static constexpr char max()
[a3067af]334 {
[092a436]335 return CHAR_MAX;
[a3067af]336 }
337
[092a436]338 static constexpr char min()
[a3067af]339 {
[092a436]340 return CHAR_MIN;
[a3067af]341 }
[4e68727]342
[092a436]343 static constexpr char lowest()
[4e68727]344 {
345 return min();
346 }
[a3067af]347 };
348
349 template<>
[092a436]350 class numeric_limits<signed char>: public aux::numeric_limits<signed char>
[a3067af]351 {
352 public:
353 static constexpr bool is_specialized = true;
354
[980ad03]355 static constexpr int digits = sizeof(signed char) * 8 - 1;
356
[092a436]357 static constexpr signed char max()
[a3067af]358 {
[092a436]359 return SCHAR_MAX;
[a3067af]360 }
361
[092a436]362 static constexpr signed char min()
[a3067af]363 {
[092a436]364 return SCHAR_MIN;
[a3067af]365 }
[4e68727]366
[092a436]367 static constexpr signed char lowest()
[4e68727]368 {
369 return min();
370 }
[a3067af]371 };
372
373 template<>
[092a436]374 class numeric_limits<short>: public aux::numeric_limits<short>
[a3067af]375 {
376 public:
377 static constexpr bool is_specialized = true;
378
[980ad03]379 static constexpr int digits = sizeof(short) * 8 - 1;
380
[092a436]381 static constexpr short max()
[a3067af]382 {
[092a436]383 return SHRT_MAX;
[a3067af]384 }
385
[092a436]386 static constexpr short min()
[a3067af]387 {
[092a436]388 return SHRT_MIN;
[a3067af]389 }
[4e68727]390
[092a436]391 static constexpr short lowest()
[4e68727]392 {
393 return min();
394 }
[a3067af]395 };
396
397 template<>
[092a436]398 class numeric_limits<int>: public aux::numeric_limits<int>
[a3067af]399 {
400 public:
401 static constexpr bool is_specialized = true;
402
[980ad03]403 static constexpr int digits = sizeof(int) * 8 - 1;
404
[092a436]405 static constexpr int max()
[a3067af]406 {
[092a436]407 return INT_MAX;
[a3067af]408 }
409
[092a436]410 static constexpr int min()
[a3067af]411 {
[092a436]412 return INT_MIN;
[a3067af]413 }
[4e68727]414
[092a436]415 static constexpr int lowest()
[4e68727]416 {
417 return min();
418 }
[a3067af]419 };
420
421 template<>
[092a436]422 class numeric_limits<long>: public aux::numeric_limits<long>
[a3067af]423 {
424 public:
425 static constexpr bool is_specialized = true;
426
[980ad03]427 static constexpr int digits = sizeof(long) * 8 - 1;
428
[092a436]429 static constexpr long max()
[a3067af]430 {
[092a436]431 return LONG_MAX;
[a3067af]432 }
433
[092a436]434 static constexpr long min()
[a3067af]435 {
[092a436]436 return LONG_MIN;
[a3067af]437 }
[4e68727]438
[092a436]439 static constexpr long lowest()
[4e68727]440 {
441 return min();
442 }
[a3067af]443 };
444
445 template<>
[092a436]446 class numeric_limits<long long>: public aux::numeric_limits<long long>
[a3067af]447 {
448 public:
449 static constexpr bool is_specialized = true;
450
[980ad03]451 static constexpr int digits = sizeof(long long) * 8 - 1;
452
[092a436]453 static constexpr long long max()
[a3067af]454 {
[092a436]455 return LLONG_MAX;
[a3067af]456 }
457
[092a436]458 static constexpr long long min()
[a3067af]459 {
[092a436]460 return LLONG_MIN;
[a3067af]461 }
[4e68727]462
[092a436]463 static constexpr long long lowest()
[4e68727]464 {
465 return min();
466 }
[a3067af]467 };
468
469 template<>
[092a436]470 class numeric_limits<unsigned char>: public aux::numeric_limits<unsigned char>
[a3067af]471 {
472 public:
473 static constexpr bool is_specialized = true;
474
[980ad03]475 static constexpr int digits = sizeof(unsigned char) * 8;
476
[092a436]477 static constexpr unsigned char max()
[a3067af]478 {
[092a436]479 return SCHAR_MAX;
[a3067af]480 }
481
[092a436]482 static constexpr unsigned char min()
[a3067af]483 {
[092a436]484 return SCHAR_MIN;
[a3067af]485 }
[4e68727]486
[092a436]487 static constexpr unsigned char lowest()
[4e68727]488 {
489 return min();
490 }
[a3067af]491 };
492
493 template<>
[092a436]494 class numeric_limits<unsigned short>: public aux::numeric_limits<unsigned short>
[a3067af]495 {
496 public:
497 static constexpr bool is_specialized = true;
498
[980ad03]499 static constexpr int digits = sizeof(unsigned short) * 8;
500
[092a436]501 static constexpr unsigned short max()
[a3067af]502 {
[092a436]503 return USHRT_MAX;
[a3067af]504 }
505
[092a436]506 static constexpr unsigned short min()
[a3067af]507 {
[bc56f30]508 return 0;
[a3067af]509 }
[4e68727]510
[092a436]511 static constexpr unsigned short lowest()
512 {
513 return min();
514 }
515 };
516
517 template<>
518 class numeric_limits<unsigned int>: public aux::numeric_limits<unsigned int>
519 {
520 public:
521 static constexpr bool is_specialized = true;
522
[980ad03]523 static constexpr int digits = sizeof(unsigned int) * 8;
524
[092a436]525 static constexpr unsigned int max()
526 {
527 return UINT_MAX;
528 }
529
530 static constexpr unsigned int min()
531 {
[bc56f30]532 return 0;
[092a436]533 }
534
535 static constexpr unsigned int lowest()
536 {
537 return min();
538 }
539 };
540
541 template<>
542 class numeric_limits<unsigned long>: public aux::numeric_limits<unsigned long>
543 {
544 public:
545 static constexpr bool is_specialized = true;
546
[980ad03]547 static constexpr int digits = sizeof(unsigned long) * 8;
548
[092a436]549 static constexpr unsigned long max()
550 {
551 return ULONG_MAX;
552 }
553
554 static constexpr unsigned long min()
555 {
[bc56f30]556 return 0;
[092a436]557 }
558
559 static constexpr unsigned long lowest()
560 {
561 return min();
562 }
563 };
564
565 template<>
566 class numeric_limits<unsigned long long>: public aux::numeric_limits<unsigned long long>
567 {
568 public:
569 static constexpr bool is_specialized = true;
570
[980ad03]571 static constexpr int digits = sizeof(unsigned long long) * 8;
572
[092a436]573 static constexpr unsigned long long max()
574 {
575 return ULLONG_MAX;
576 }
577
578 static constexpr unsigned long long min()
579 {
[bc56f30]580 return 0;
[092a436]581 }
582
583 static constexpr unsigned long long lowest()
[4e68727]584 {
585 return min();
586 }
[a3067af]587 };
588
589 template<>
590 class numeric_limits<double>: public aux::numeric_limits<double>
591 {
592 public:
593 // TODO: implement
[980ad03]594 static constexpr int digits = sizeof(short) * 8 - 1;
[a3067af]595 };
596
597 template<>
598 class numeric_limits<long double>: public aux::numeric_limits<long double>
599 {
600 public:
601 // TODO: implement
602 };
603}
604
605#endif
Note: See TracBrowser for help on using the repository browser.