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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b57a3ee was b57a3ee, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: refactored the library layout, everything from the impl directory was moved to the bits directory for the sake of consistency, updated copyright notices and include guards

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