File: integral_limits.h

package info (click to toggle)
etlcpp 20.39.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,232 kB
  • sloc: cpp: 245,721; ansic: 10,254; sh: 1,481; asm: 301; python: 281; makefile: 24
file content (626 lines) | stat: -rw-r--r-- 20,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2014 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_INTEGRAL_LIMITS_INCLUDED
#define ETL_INTEGRAL_LIMITS_INCLUDED

#include "platform.h"
#include "type_traits.h"

#include <stddef.h>
#include <limits.h>

#include "private/minmax_push.h"

//*****************************************************************************
///\defgroup integral_limits integral_limits
/// A set of templated compile time constants that mirror some of std::numeric_limits functionality.
///\ingroup utilities
//*****************************************************************************

#ifndef LLONG_MAX
  #define LLONG_MAX	9223372036854775807LL
#endif

#ifndef LLONG_MIN
  #define LLONG_MIN	(-LLONG_MAX - 1LL)
#endif

#ifndef ULLONG_MAX
  #define ULLONG_MAX 18446744073709551615ULL
#endif

namespace etl
{
  namespace private_integral_limits
  {
    //*****************************************************************************
    /// The technique of using templated base classes to declare and define the statics
    /// is to get around the limits of the One Definition Rule (ODR) that occurs when
    /// the top level integral_limits classes are specialised. This issue currently seems
    /// to only be apparent for GCC/C++14.
    /// In C++17 and above this is not necessary, as the inline keyword may be used.
    /// At the time of writing, the ETL is generally aimed to be compatible with C++98 and above.
    //*****************************************************************************
    
    //*********************************
    // signed char
    template <typename T = void>
    struct statics_signed_char
    {
      typedef signed char value_type;

      static ETL_CONSTANT signed char min       = SCHAR_MIN;
      static ETL_CONSTANT signed char max       = SCHAR_MAX;
      static ETL_CONSTANT int         bits      = CHAR_BIT;
      static ETL_CONSTANT bool        is_signed = etl::is_signed<signed char>::value;
    };

    template <typename T>
    ETL_CONSTANT signed char statics_signed_char<T>::min;
    
    template <typename T>
    ETL_CONSTANT signed char statics_signed_char<T>::max;
    
    template <typename T>
    ETL_CONSTANT int statics_signed_char<T>::bits;
    
    template <typename T>
    ETL_CONSTANT bool statics_signed_char<T>::is_signed;

    //***********************************
    // unsigned char
    template <typename T = void>
    struct statics_unsigned_char
    {
      typedef unsigned char value_type;

      static ETL_CONSTANT unsigned char min       = 0;
      static ETL_CONSTANT unsigned char max       = UCHAR_MAX;
      static ETL_CONSTANT int           bits      = CHAR_BIT;
      static ETL_CONSTANT bool          is_signed = etl::is_signed<unsigned char>::value;
    };

    template <typename T>
    ETL_CONSTANT unsigned char statics_unsigned_char<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned char statics_unsigned_char<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned_char<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned_char<T>::is_signed;

    //***********************************
    // char
    template <typename T = void>
    struct statics_char
    {
      typedef char value_type;

      static ETL_CONSTANT char min       = (etl::is_signed<char>::value) ? SCHAR_MIN : 0;
      static ETL_CONSTANT char max       = (etl::is_signed<char>::value) ? SCHAR_MAX : static_cast<char>(UCHAR_MAX);
      static ETL_CONSTANT int  bits      = CHAR_BIT;
      static ETL_CONSTANT bool is_signed = etl::is_signed<char>::value;
    };

    template <typename T>
    ETL_CONSTANT char statics_char<T>::min;

    template <typename T>
    ETL_CONSTANT char statics_char<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_char<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_char<T>::is_signed;

    //***********************************
    // wchar_t
    template <typename T = void>
    struct statics_wchar_t
    {
      typedef wchar_t value_type;

      static ETL_CONSTANT wchar_t min       = WCHAR_MIN;
      static ETL_CONSTANT wchar_t max       = WCHAR_MAX;
      static ETL_CONSTANT int     bits      = CHAR_BIT * sizeof(wchar_t);
      static ETL_CONSTANT bool    is_signed = etl::is_signed<wchar_t>::value;
    };

    template <typename T>
    ETL_CONSTANT wchar_t statics_wchar_t<T>::min;

    template <typename T>
    ETL_CONSTANT wchar_t statics_wchar_t<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_wchar_t<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_wchar_t<T>::is_signed;

    //***********************************
    // short
#if defined(ETL_COMPILER_MICROSOFT)
  #pragma warning(push)
  #pragma warning(disable : 4309)
#endif

    template <typename T = void>
    struct statics_short
    {
      typedef short value_type;

      static ETL_CONSTANT short min       = SHRT_MIN;
      static ETL_CONSTANT short max       = SHRT_MAX;
      static ETL_CONSTANT int   bits      = CHAR_BIT * (sizeof(short) / sizeof(char));
      static ETL_CONSTANT bool  is_signed = etl::is_signed<short>::value;
    };

    template <typename T>
    ETL_CONSTANT short statics_short<T>::min;

    template <typename T>
    ETL_CONSTANT short statics_short<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_short<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_short<T>::is_signed;

#if defined(ETL_COMPILER_MICROSOFT)
  #pragma warning(pop)
#endif

    //***********************************
    // unsigned short
    template <typename T = void>
    struct statics_unsigned_short
    {
      typedef unsigned short value_type;

      static ETL_CONSTANT unsigned short min       = 0;
      static ETL_CONSTANT unsigned short max       = USHRT_MAX;
      static ETL_CONSTANT int            bits      = CHAR_BIT * (sizeof(unsigned short) / sizeof(char));
      static ETL_CONSTANT bool           is_signed = etl::is_signed<unsigned short>::value;
    };

    template <typename T>
    ETL_CONSTANT unsigned short statics_unsigned_short<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned short statics_unsigned_short<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned_short<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned_short<T>::is_signed;

    //***********************************
    // int
    template <typename T = void>
    struct statics_int
    {
      typedef int value_type;

      static ETL_CONSTANT int  min       = INT_MIN;
      static ETL_CONSTANT int  max       = INT_MAX;
      static ETL_CONSTANT int  bits      = CHAR_BIT * (sizeof(int) / sizeof(char));
      static ETL_CONSTANT bool is_signed = etl::is_signed<int>::value;
    };

    template <typename T>
    ETL_CONSTANT int statics_int<T>::min;

    template <typename T>
    ETL_CONSTANT int statics_int<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_int<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_int<T>::is_signed;

    //***********************************
    // unsigned int
    template <typename T = void>
    struct statics_unsigned_int
    {
      typedef unsigned int value_type;

      static ETL_CONSTANT unsigned int min       = 0;
      static ETL_CONSTANT unsigned int max       = UINT_MAX;
      static ETL_CONSTANT int          bits      = CHAR_BIT * (sizeof(unsigned int) / sizeof(char));
      static ETL_CONSTANT bool         is_signed = etl::is_signed<unsigned int>::value;
    };

    template <typename T>
    ETL_CONSTANT unsigned int statics_unsigned_int<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned int statics_unsigned_int<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned_int<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned_int<T>::is_signed;

    //***********************************
    // long
    template <typename T = void>
    struct statics_long
    {
      typedef long value_type;

      static ETL_CONSTANT long min       = LONG_MIN;
      static ETL_CONSTANT long max       = LONG_MAX;
      static ETL_CONSTANT int  bits      = CHAR_BIT * (sizeof(long) / sizeof(char));
      static ETL_CONSTANT bool is_signed = etl::is_signed<long>::value;
    };

    template <typename T>
    ETL_CONSTANT long statics_long<T>::min;

    template <typename T>
    ETL_CONSTANT long statics_long<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_long<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_long<T>::is_signed;

    //***********************************
    // unsigned long
    template <typename T = void>
    struct statics_unsigned_long
    {
      typedef unsigned long value_type;

      static ETL_CONSTANT unsigned long min       = 0;
      static ETL_CONSTANT unsigned long max       = ULONG_MAX;
      static ETL_CONSTANT int           bits      = CHAR_BIT * (sizeof(unsigned long) / sizeof(char));
      static ETL_CONSTANT bool          is_signed = etl::is_signed<unsigned long>::value;
    };

    template <typename T>
    ETL_CONSTANT unsigned long statics_unsigned_long<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned long statics_unsigned_long<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned_long<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned_long<T>::is_signed;

    //***********************************
    // long long
    template <typename T = void>
    struct statics_long_long
    {
      typedef long long value_type;

      static ETL_CONSTANT long long min       = LLONG_MIN;
      static ETL_CONSTANT long long max       = LLONG_MAX;
      static ETL_CONSTANT int       bits      = CHAR_BIT * (sizeof(long long) / sizeof(char));
      static ETL_CONSTANT bool      is_signed = etl::is_signed<long long>::value;
    };

    template <typename T>
    ETL_CONSTANT long long statics_long_long<T>::min;

    template <typename T>
    ETL_CONSTANT long long statics_long_long<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_long_long<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_long_long<T>::is_signed;

    //***********************************
    // unsigned long long
    template <typename T = void>
    struct statics_unsigned_long_long
    {
      typedef unsigned long value_type;

      static ETL_CONSTANT unsigned long long min       = 0;
      static ETL_CONSTANT unsigned long long max       = ULLONG_MAX;
      static ETL_CONSTANT int                bits      = CHAR_BIT * (sizeof(unsigned long long) / sizeof(char));
      static ETL_CONSTANT bool               is_signed = etl::is_signed<unsigned long long>::value;
    };

    template <typename T>
    ETL_CONSTANT unsigned long long statics_unsigned_long_long<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned long long statics_unsigned_long_long<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned_long_long<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned_long_long<T>::is_signed;

#if ETL_HAS_NATIVE_CHAR8_T
    //***********************************
    // char8_t
    template <typename T = void>
    struct statics_char8_t
    {
      typedef char8_t value_type;

      static ETL_CONSTANT char8_t min       = (etl::is_signed<char8_t>::value) ? SCHAR_MIN : 0;
      static ETL_CONSTANT char8_t max       = (etl::is_signed<char8_t>::value) ? SCHAR_MAX : static_cast<char8_t>(UCHAR_MAX);
      static ETL_CONSTANT int     bits      = CHAR_BIT;
      static ETL_CONSTANT bool    is_signed = etl::is_signed<char8_t>::value;
    };

    template <typename T>
    ETL_CONSTANT char8_t statics_char8_t<T>::min;

    template <typename T>
    ETL_CONSTANT char8_t statics_char8_t<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_char8_t<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_char8_t<T>::is_signed;
#endif

#if ETL_HAS_NATIVE_CHAR16_T
    //***********************************
    // char16_t
    template <typename T = void>
    struct statics_char16_t
    {
      typedef char16_t value_type;

      static ETL_CONSTANT char16_t min       = 0;
      static ETL_CONSTANT char16_t max       = 0xFFFFU;
      static ETL_CONSTANT int      bits      = 16;
      static ETL_CONSTANT bool     is_signed = false;
    };

    template <typename T>
    ETL_CONSTANT char16_t statics_char16_t<T>::min;

    template <typename T>
    ETL_CONSTANT char16_t statics_char16_t<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_char16_t<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_char16_t<T>::is_signed;
#endif

#if ETL_HAS_NATIVE_CHAR32_T
    //***********************************
    // char32_t
    template <typename T = void>
    struct statics_char32_t
    {
      typedef char32_t value_type;

      static ETL_CONSTANT char32_t min       = 0;
      static ETL_CONSTANT char32_t max       = 0xFFFFFFFFU;
      static ETL_CONSTANT int      bits      = 32;
      static ETL_CONSTANT bool     is_signed = false;
    };
     
    template <typename T>
    ETL_CONSTANT char32_t statics_char32_t<T>::min;

    template <typename T>
    ETL_CONSTANT char32_t statics_char32_t<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_char32_t<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_char32_t<T>::is_signed;
#endif

#if ETL_USING_20BIT_TYPES
template <typename T = void>
    struct statics___int20
    {
      typedef __int20 value_type;

      static ETL_CONSTANT __int20 min       = 0x80000;
      static ETL_CONSTANT __int20 max       = 0x7FFFF;
      static ETL_CONSTANT int     bits      = 20;
      static ETL_CONSTANT bool    is_signed = true;
    };

    template <typename T>
    ETL_CONSTANT __int20 statics___int20<T>::min;

    template <typename T>
    ETL_CONSTANT __int20 statics___int20<T>::max;

    template <typename T>
    ETL_CONSTANT int statics___int20<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics___int20<T>::is_signed;

    template <typename T = void>
    struct statics_unsigned___int20
    {
      typedef unsigned __int20 value_type;

      static ETL_CONSTANT unsigned __int20 min       = 0;
      static ETL_CONSTANT unsigned __int20 max       = 0xFFFFF;
      static ETL_CONSTANT int              bits      = 20;
      static ETL_CONSTANT bool             is_signed = false;
    };

    template <typename T>
    ETL_CONSTANT unsigned __int20 statics_unsigned___int20<T>::min;

    template <typename T>
    ETL_CONSTANT unsigned __int20 statics_unsigned___int20<T>::max;

    template <typename T>
    ETL_CONSTANT int statics_unsigned___int20<T>::bits;

    template <typename T>
    ETL_CONSTANT bool statics_unsigned___int20<T>::is_signed;
#endif
  }

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <typename T>
  struct integral_limits;

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<signed char> : public private_integral_limits::statics_signed_char<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<unsigned char> : public private_integral_limits::statics_unsigned_char<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************

  template <>
  struct integral_limits<char> : public private_integral_limits::statics_char<>
  {
    typedef char value_type;
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<short> : public private_integral_limits::statics_short<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<unsigned short> : public private_integral_limits::statics_unsigned_short<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<int> : public private_integral_limits::statics_int<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<unsigned int> : public private_integral_limits::statics_unsigned_int<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<long> : public private_integral_limits::statics_long<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<unsigned long> : public private_integral_limits::statics_unsigned_long<>
  {
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<long long> : public private_integral_limits::statics_long_long<>
  {    
  };

  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<unsigned long long> : public private_integral_limits::statics_unsigned_long_long<>
  {
  };

  #if ETL_USING_20BIT_TYPES
  //***************************************************************************
  ///\ingroup integral_limits
  //***************************************************************************
  template <>
  struct integral_limits<__int20> : public private_integral_limits::statics___int20<>
  {
  };

  template <>
  struct integral_limits<unsigned __int20> : public private_integral_limits::statics_unsigned___int20<>
  {
  };
  #endif
}

#include "private/minmax_pop.h"

#endif