source: mainline/uspace/lib/cpp/include/impl/limits.hpp@ f9823e2

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

cpp: changed limits to the primitive types so that we can be sure we cover them all

  • Property mode set to 100644
File size: 15.9 KB
Line 
1/*
2 * Copyright (c) 2017 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_LIMITS
30#define LIBCPP_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 char max()
354 {
355 return CHAR_MAX;
356 }
357
358 static constexpr char min()
359 {
360 return CHAR_MIN;
361 }
362
363 static constexpr char lowest()
364 {
365 return min();
366 }
367 };
368
369 template<>
370 class numeric_limits<signed char>: public aux::numeric_limits<signed char>
371 {
372 public:
373 static constexpr bool is_specialized = true;
374
375 static constexpr signed char max()
376 {
377 return SCHAR_MAX;
378 }
379
380 static constexpr signed char min()
381 {
382 return SCHAR_MIN;
383 }
384
385 static constexpr signed char lowest()
386 {
387 return min();
388 }
389 };
390
391 template<>
392 class numeric_limits<short>: public aux::numeric_limits<short>
393 {
394 public:
395 static constexpr bool is_specialized = true;
396
397 static constexpr short max()
398 {
399 return SHRT_MAX;
400 }
401
402 static constexpr short min()
403 {
404 return SHRT_MIN;
405 }
406
407 static constexpr short lowest()
408 {
409 return min();
410 }
411 };
412
413 template<>
414 class numeric_limits<int>: public aux::numeric_limits<int>
415 {
416 public:
417 static constexpr bool is_specialized = true;
418
419 static constexpr int max()
420 {
421 return INT_MAX;
422 }
423
424 static constexpr int min()
425 {
426 return INT_MIN;
427 }
428
429 static constexpr int lowest()
430 {
431 return min();
432 }
433 };
434
435 template<>
436 class numeric_limits<long>: public aux::numeric_limits<long>
437 {
438 public:
439 static constexpr bool is_specialized = true;
440
441 static constexpr long max()
442 {
443 return LONG_MAX;
444 }
445
446 static constexpr long min()
447 {
448 return LONG_MIN;
449 }
450
451 static constexpr long lowest()
452 {
453 return min();
454 }
455 };
456
457 template<>
458 class numeric_limits<long long>: public aux::numeric_limits<long long>
459 {
460 public:
461 static constexpr bool is_specialized = true;
462
463 static constexpr long long max()
464 {
465 return LLONG_MAX;
466 }
467
468 static constexpr long long min()
469 {
470 return LLONG_MIN;
471 }
472
473 static constexpr long long lowest()
474 {
475 return min();
476 }
477 };
478
479 template<>
480 class numeric_limits<unsigned char>: public aux::numeric_limits<unsigned char>
481 {
482 public:
483 static constexpr bool is_specialized = true;
484
485 static constexpr unsigned char max()
486 {
487 return SCHAR_MAX;
488 }
489
490 static constexpr unsigned char min()
491 {
492 return SCHAR_MIN;
493 }
494
495 static constexpr unsigned char lowest()
496 {
497 return min();
498 }
499 };
500
501 template<>
502 class numeric_limits<unsigned short>: public aux::numeric_limits<unsigned short>
503 {
504 public:
505 static constexpr bool is_specialized = true;
506
507 static constexpr unsigned short max()
508 {
509 return USHRT_MAX;
510 }
511
512 static constexpr unsigned short min()
513 {
514 return USHRT_MIN;
515 }
516
517 static constexpr unsigned short lowest()
518 {
519 return min();
520 }
521 };
522
523 template<>
524 class numeric_limits<unsigned int>: public aux::numeric_limits<unsigned int>
525 {
526 public:
527 static constexpr bool is_specialized = true;
528
529 static constexpr unsigned int max()
530 {
531 return UINT_MAX;
532 }
533
534 static constexpr unsigned int min()
535 {
536 return UINT_MIN;
537 }
538
539 static constexpr unsigned int lowest()
540 {
541 return min();
542 }
543 };
544
545 template<>
546 class numeric_limits<unsigned long>: public aux::numeric_limits<unsigned long>
547 {
548 public:
549 static constexpr bool is_specialized = true;
550
551 static constexpr unsigned long max()
552 {
553 return ULONG_MAX;
554 }
555
556 static constexpr unsigned long min()
557 {
558 return ULONG_MIN;
559 }
560
561 static constexpr unsigned long lowest()
562 {
563 return min();
564 }
565 };
566
567 template<>
568 class numeric_limits<unsigned long long>: public aux::numeric_limits<unsigned long long>
569 {
570 public:
571 static constexpr bool is_specialized = true;
572
573 static constexpr unsigned long long max()
574 {
575 return ULLONG_MAX;
576 }
577
578 static constexpr unsigned long long min()
579 {
580 return ULLONG_MIN;
581 }
582
583 static constexpr unsigned long long lowest()
584 {
585 return min();
586 }
587 };
588
589 template<>
590 class numeric_limits<double>: public aux::numeric_limits<double>
591 {
592 public:
593 // TODO: implement
594 };
595
596 template<>
597 class numeric_limits<long double>: public aux::numeric_limits<long double>
598 {
599 public:
600 // TODO: implement
601 };
602}
603
604#endif
Note: See TracBrowser for help on using the repository browser.