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

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

cpp: added lowest to the integral numeric_limits

  • Property mode set to 100644
File size: 14.4 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
34namespace std
35{
36
37 /**
38 * 18.3.2.5, type float_round_style:
39 */
40
41 enum float_round_style
42 {
43 round_indeterminate = -1,
44 round_toward_zero = 0,
45 round_to_nearest = 1,
46 round_toward_infinity = 2,
47 round_toward_neg_infinity = 3
48 };
49
50 /**
51 * 18.3.2.6, type float_denorm_style:
52 */
53
54 enum float_denorm_style
55 {
56 denorm_indeterminate = -1,
57 denorm_absent = 0,
58 denorm_present = 1
59 };
60
61 /**
62 * 18.3.2.3, class template numeric_limits:
63 */
64
65 namespace aux
66 {
67 template<class T>
68 class numeric_limits
69 {
70 public:
71 static constexpr bool is_specialized = false;
72
73 static constexpr T min() noexcept
74 {
75 return T{};
76 }
77
78 static constexpr T max() noexcept
79 {
80 return T{};
81 }
82
83 static constexpr T lowest() noexcept
84 {
85 return T{};
86 }
87
88 static constexpr int digits = 0;
89 static constexpr int digits10 = 0;
90 static constexpr int max_digits10 = 0;
91
92 static constexpr bool is_signed = false;
93 static constexpr bool is_integer = false;
94 static constexpr bool is_exact = false;
95
96 static constexpr int radix = 0;
97
98 static constexpr T epsilon() noexcept
99 {
100 return T{};
101 }
102
103 static constexpr T round_error() noexcept
104 {
105 return T{};
106 }
107
108 static constexpr int min_exponent = 0;
109 static constexpr int min_exponent10 = 0;
110 static constexpr int max_exponent = 0;
111 static constexpr int max_exponent10 = 0;
112
113 static constexpr bool has_infinity = false;
114 static constexpr bool has_quiet_NaN = false;
115 static constexpr bool has_signaling_NaN = false;
116
117 static constexpr float_denorm_style has_denorm = denorm_absent;
118 static constexpr bool has_denorm_loss = false;
119
120 static constexpr T infinity() noexcept
121 {
122 return T{};
123 }
124
125 static constexpr T quiet_NaN() noexcept
126 {
127 return T{};
128 }
129
130 static constexpr T signaling_NaN() noexcept
131 {
132 return T{};
133 }
134
135 static constexpr T denorm_min() noexcept
136 {
137 return T{};
138 }
139
140 static constexpr bool is_iec559 = false;
141 static constexpr bool is_bounded = false;
142 static constexpr bool is_modulo = false;
143
144 static constexpr bool traps = false;
145 static constexpr bool tinyness_before = false;
146
147 static constexpr float_round_style round_style = round_toward_zero;
148 };
149 }
150
151 template<class T>
152 class numeric_limits: public aux::numeric_limits<T>
153 { /* DUMMY BODY */ };
154
155 template<class T>
156 class numeric_limits<const T>: public numeric_limits<T>
157 { /* DUMMY BODY */ };
158
159 template<class T>
160 class numeric_limits<volatile T>: public numeric_limits<T>
161 { /* DUMMY BODY */ };
162
163 template<class T>
164 class numeric_limits<const volatile T>: public numeric_limits<T>
165 { /* DUMMY BODY */ };
166
167 /**
168 * 18.3.2.3, class template numeric_limits:
169 */
170
171 template<>
172 class numeric_limits<float>
173 {
174 public:
175 static constexpr bool is_specialized = true;
176
177 static constexpr float min() noexcept
178 {
179 return 1.17549435e-38f;
180 }
181
182 static constexpr float max() noexcept
183 {
184 return 3.40282347e+38f;
185 }
186
187 static constexpr float lowest() noexcept
188 {
189 return -3.40282347e+38f;
190 }
191
192 static constexpr int digits = 24;
193 static constexpr int digits10 = 6;
194 static constexpr int max_digits10 = 9;
195
196 static constexpr bool is_signed = true;
197 static constexpr bool is_integer = false;
198 static constexpr bool is_exact = false;
199
200 static constexpr int radix = 2;
201
202 static constexpr float epsilon() noexcept
203 {
204 return 1.19209290e-07f;
205 }
206
207 static constexpr float round_error() noexcept
208 {
209 return 0.5f;
210 }
211
212 static constexpr int min_exponent = -127;
213 static constexpr int min_exponent10 = -37;
214 static constexpr int max_exponent = 128;
215 static constexpr int max_exponent10 = 38;
216
217 static constexpr bool has_infinity = true;
218 static constexpr bool has_quiet_NaN = true;
219 static constexpr bool has_signaling_NaN = true;
220
221 static constexpr float_denorm_style has_denorm = denorm_absent;
222 static constexpr bool has_denorm_loss = false;
223
224 static constexpr float infinity() noexcept
225 {
226 // TODO: implement
227 return 0.f;
228 }
229
230 static constexpr float quiet_NaN() noexcept
231 {
232 // TODO: implement
233 return 0.f;
234 }
235
236 static constexpr float signaling_NaN() noexcept
237 {
238 // TODO: implement
239 return 0.f;
240 }
241
242 static constexpr float denorm_min() noexcept
243 {
244 return min();
245 }
246
247 static constexpr bool is_iec559 = true;
248 static constexpr bool is_bounded = true;
249 static constexpr bool is_modulo = false;
250
251 static constexpr bool traps = true;
252 static constexpr bool tinyness_before = true;
253
254 static constexpr float_round_style round_style = round_to_nearest;
255 };
256
257 template<>
258 class numeric_limits<bool>
259 {
260 public:
261 static constexpr bool is_specialized = true;
262
263 static constexpr bool min() noexcept
264 {
265 return false;
266 }
267
268 static constexpr bool max() noexcept
269 {
270 return true;
271 }
272
273 static constexpr bool lowest() noexcept
274 {
275 return false;
276 }
277
278 static constexpr int digits = 1;
279 static constexpr int digits10 = 0;
280 static constexpr int max_digits10 = 0;
281
282 static constexpr bool is_signed = false;
283 static constexpr bool is_integer = true;
284 static constexpr bool is_exact = true;
285
286 static constexpr int radix = 2;
287
288 static constexpr bool epsilon() noexcept
289 {
290 return 0;
291 }
292
293 static constexpr bool round_error() noexcept
294 {
295 return 0;
296 }
297
298 static constexpr int min_exponent = 0;
299 static constexpr int min_exponent10 = 0;
300 static constexpr int max_exponent = 0;
301 static constexpr int max_exponent10 = 0;
302
303 static constexpr bool has_infinity = false;
304 static constexpr bool has_quiet_NaN = false;
305 static constexpr bool has_signaling_NaN = false;
306
307 static constexpr float_denorm_style has_denorm = denorm_absent;
308 static constexpr bool has_denorm_loss = false;
309
310 static constexpr bool infinity() noexcept
311 {
312 return 0;
313 }
314
315 static constexpr bool quiet_NaN() noexcept
316 {
317 return 0;
318 }
319
320 static constexpr bool signaling_NaN() noexcept
321 {
322 return 0;
323 }
324
325 static constexpr bool denorm_min() noexcept
326 {
327 return 0;
328 }
329
330 static constexpr bool is_iec559 = false;
331 static constexpr bool is_bounded = true;
332 static constexpr bool is_modulo = false;
333
334 static constexpr bool traps = false;
335 static constexpr bool tinyness_before = false;
336
337 static constexpr float_round_style round_style = round_toward_zero;
338 };
339
340 /**
341 * Note: Because of the limited state of limit.h, we define only
342 * the most basic properties of the most basic types (that are likely
343 * to be used in a program that is being ported to HelenOS).
344 */
345
346 template<>
347 class numeric_limits<int8_t>: public aux::numeric_limits<int8_t>
348 {
349 public:
350 static constexpr bool is_specialized = true;
351
352 static constexpr int8_t max()
353 {
354 return INT8_MAX;
355 }
356
357 static constexpr int8_t min()
358 {
359 return INT8_MIN;
360 }
361
362 static constexpr int8_t lowest()
363 {
364 return min();
365 }
366 };
367
368 template<>
369 class numeric_limits<int16_t>: public aux::numeric_limits<int16_t>
370 {
371 public:
372 static constexpr bool is_specialized = true;
373
374 static constexpr int16_t max()
375 {
376 return INT16_MAX;
377 }
378
379 static constexpr int16_t min()
380 {
381 return INT16_MIN;
382 }
383
384 static constexpr int16_t lowest()
385 {
386 return min();
387 }
388 };
389
390 template<>
391 class numeric_limits<int32_t>: public aux::numeric_limits<int32_t>
392 {
393 public:
394 static constexpr bool is_specialized = true;
395
396 static constexpr int32_t max()
397 {
398 return INT32_MAX;
399 }
400
401 static constexpr int32_t min()
402 {
403 return INT32_MIN;
404 }
405
406 static constexpr int32_t lowest()
407 {
408 return min();
409 }
410 };
411
412 template<>
413 class numeric_limits<int64_t>: public aux::numeric_limits<int64_t>
414 {
415 public:
416 static constexpr bool is_specialized = true;
417
418 static constexpr int64_t max()
419 {
420 return INT64_MAX;
421 }
422
423 static constexpr int64_t min()
424 {
425 return INT64_MIN;
426 }
427
428 static constexpr int64_t lowest()
429 {
430 return min();
431 }
432 };
433
434 template<>
435 class numeric_limits<uint8_t>: public aux::numeric_limits<uint8_t>
436 {
437 public:
438 static constexpr bool is_specialized = true;
439
440 static constexpr uint8_t max()
441 {
442 return UINT8_MAX;
443 }
444
445 static constexpr uint8_t min()
446 {
447 return UINT8_MIN;
448 }
449
450 static constexpr uint8_t lowest()
451 {
452 return min();
453 }
454 };
455
456 template<>
457 class numeric_limits<uint16_t>: public aux::numeric_limits<uint16_t>
458 {
459 public:
460 static constexpr bool is_specialized = true;
461
462 static constexpr uint16_t max()
463 {
464 return UINT16_MAX;
465 }
466
467 static constexpr uint16_t min()
468 {
469 return UINT16_MIN;
470 }
471
472 static constexpr uint16_t lowest()
473 {
474 return min();
475 }
476 };
477
478 template<>
479 class numeric_limits<uint32_t>: public aux::numeric_limits<uint32_t>
480 {
481 public:
482 static constexpr bool is_specialized = true;
483
484 static constexpr uint32_t max()
485 {
486 return UINT32_MAX;
487 }
488
489 static constexpr uint32_t min()
490 {
491 return UINT32_MIN;
492 }
493
494 static constexpr uint32_t lowest()
495 {
496 return min();
497 }
498 };
499
500 template<>
501 class numeric_limits<uint64_t>: public aux::numeric_limits<uint64_t>
502 {
503 public:
504 static constexpr bool is_specialized = true;
505
506 static constexpr uint64_t max()
507 {
508 return UINT64_MAX;
509 }
510
511 static constexpr uint64_t min()
512 {
513 return UINT64_MIN;
514 }
515
516 static constexpr uint64_t lowest()
517 {
518 return min();
519 }
520 };
521
522 template<>
523 class numeric_limits<double>: public aux::numeric_limits<double>
524 {
525 public:
526 // TODO: implement
527 };
528
529 template<>
530 class numeric_limits<long double>: public aux::numeric_limits<long double>
531 {
532 public:
533 // TODO: implement
534 };
535}
536
537#endif
Note: See TracBrowser for help on using the repository browser.