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

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

cpp: added a very basic implementation of numeric_limits

  • Property mode set to 100644
File size: 13.5 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
363 template<>
364 class numeric_limits<int16_t>: public aux::numeric_limits<int16_t>
365 {
366 public:
367 static constexpr bool is_specialized = true;
368
369 static constexpr int16_t max()
370 {
371 return INT16_MAX;
372 }
373
374 static constexpr int16_t min()
375 {
376 return INT16_MIN;
377 }
378 };
379
380 template<>
381 class numeric_limits<int32_t>: public aux::numeric_limits<int32_t>
382 {
383 public:
384 static constexpr bool is_specialized = true;
385
386 static constexpr int32_t max()
387 {
388 return INT32_MAX;
389 }
390
391 static constexpr int32_t min()
392 {
393 return INT32_MIN;
394 }
395 };
396
397 template<>
398 class numeric_limits<int64_t>: public aux::numeric_limits<int64_t>
399 {
400 public:
401 static constexpr bool is_specialized = true;
402
403 static constexpr int64_t max()
404 {
405 return INT64_MAX;
406 }
407
408 static constexpr int64_t min()
409 {
410 return INT64_MIN;
411 }
412 };
413
414 template<>
415 class numeric_limits<uint8_t>: public aux::numeric_limits<uint8_t>
416 {
417 public:
418 static constexpr bool is_specialized = true;
419
420 static constexpr uint8_t max()
421 {
422 return UINT8_MAX;
423 }
424
425 static constexpr uint8_t min()
426 {
427 return UINT8_MIN;
428 }
429 };
430
431 template<>
432 class numeric_limits<uint16_t>: public aux::numeric_limits<uint16_t>
433 {
434 public:
435 static constexpr bool is_specialized = true;
436
437 static constexpr uint16_t max()
438 {
439 return UINT16_MAX;
440 }
441
442 static constexpr uint16_t min()
443 {
444 return UINT16_MIN;
445 }
446 };
447
448 template<>
449 class numeric_limits<uint32_t>: public aux::numeric_limits<uint32_t>
450 {
451 public:
452 static constexpr bool is_specialized = true;
453
454 static constexpr uint32_t max()
455 {
456 return UINT32_MAX;
457 }
458
459 static constexpr uint32_t min()
460 {
461 return UINT32_MIN;
462 }
463 };
464
465 template<>
466 class numeric_limits<uint64_t>: public aux::numeric_limits<uint64_t>
467 {
468 public:
469 static constexpr bool is_specialized = true;
470
471 static constexpr uint64_t max()
472 {
473 return UINT64_MAX;
474 }
475
476 static constexpr uint64_t min()
477 {
478 return UINT64_MIN;
479 }
480 };
481
482 template<>
483 class numeric_limits<double>: public aux::numeric_limits<double>
484 {
485 public:
486 // TODO: implement
487 };
488
489 template<>
490 class numeric_limits<long double>: public aux::numeric_limits<long double>
491 {
492 public:
493 // TODO: implement
494 };
495}
496
497#endif
Note: See TracBrowser for help on using the repository browser.