acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
_.h
1#pragma once
2
3
4#include <limits>
5
6
7#include "_constant.h"
8
9
10
11
12typedef void GENERATE_RANDOM_BYTES(void * p, memsize s);
13using PFN_GENERATE_RANDOM_BYTES = GENERATE_RANDOM_BYTES *;
14
15
16//CLASS_DECL_ACME void set_generate_random_bytes(PFN_GENERATE_RANDOM_BYTES pfngeneraterandombytes);
17//CLASS_DECL_ACME void generate_random_bytes(void * p, memsize s);
18
19
20inline ::std::strong_ordering strong_order(::f64 d1, ::f64 d2)
21{
22
23 auto d = d1 - d2;
24
25 if(d > std::numeric_limits<::f64>::epsilon())
26 {
27
28 return ::std::strong_ordering::greater;
29
30 }
31 else if(d < -std::numeric_limits<::f64>::epsilon())
32 {
33
34 return ::std::strong_ordering::less;
35
36 }
37 else
38 {
39
40 return ::std::strong_ordering::equal;
41
42 }
43
44}
45
46template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
47inline constexpr bool __lt(const SIGNED1 & a, const SIGNED2 & b) { return a < b; }
48template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
49inline constexpr bool __le(const SIGNED1 & a, const SIGNED2 & b) { return a <= b; }
50template < prototype_unsigned UNSIGNED1, prototype_signed SIGNED2 >
51inline constexpr bool __lt(const UNSIGNED1 & a, const SIGNED2 & b) { return b < 0 ? false : a < static_cast < ::u32 > (b); }
52template < prototype_unsigned UNSIGNED1, prototype_signed SIGNED2 >
53inline constexpr bool __le(const UNSIGNED1 & a, const SIGNED2 & b) { return b < 0 ? false : a <= static_cast <::u32> (b); }
54
55
56template < prototype_signed SIGNED1, prototype_unsigned UNSIGNED2 >
57inline constexpr bool __lt(const SIGNED1 & a, const UNSIGNED2 & b) { return a < 0 ? true : static_cast <::u32> (a) < b; }
58template < prototype_signed SIGNED1, prototype_unsigned UNSIGNED2 >
59inline constexpr bool __le(const SIGNED1 & a, const UNSIGNED2 & b) { return a < 0 ? false : static_cast <::u64> (a) <= b; }
60
61
62template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
63inline constexpr bool __lt(const UNSIGNED1 & a, const UNSIGNED2 & b) { return a < b; }
64template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
65inline constexpr bool __le(const UNSIGNED1 & a, const UNSIGNED2 & b) { return a <= b; }
66
67
68template < prototype_integral INTEGRAL1, prototype_floating FLOATING2 >
69inline constexpr bool __lt(const INTEGRAL1 & a, const FLOATING2 & b) { return a < b; }
70template < prototype_integral INTEGRAL1, prototype_floating FLOATING2 >
71inline constexpr bool __le(const INTEGRAL1 & a, const FLOATING2 & b) { return a <= b; }
72
73
74template < prototype_floating FLOATING1, prototype_integral INTEGRAL2 >
75inline constexpr bool __lt(const FLOATING1 & a, const INTEGRAL2 & b) { return a < b; }
76template < prototype_floating FLOATING1, prototype_integral INTEGRAL2 >
77inline constexpr bool __le(const FLOATING1 & a, const INTEGRAL2 & b) { return a <= b; }
78
79
80template < prototype_floating FLOATING1, prototype_floating FLOATING2 >
81inline constexpr bool __lt(const FLOATING1 & a, const FLOATING2 & b) { return a < b; }
82template < prototype_floating FLOATING1, prototype_floating FLOATING2 >
83inline constexpr bool __le(const FLOATING1 & a, const FLOATING2 & b) { return a <= b; }
84
85
86//template < typename TYPE1, typename TYPE2 >
87//inline constexpr bool __lt(const TYPE1 & a, const TYPE2 & b) { return a < b; }
88//template < typename TYPE1, typename TYPE2 >
89//inline constexpr bool __le(const TYPE1 & a, const TYPE2 & b) { return a <= b; }
90//
91//
92//template < typename T1, typename T2 >
93//inline constexpr auto __ge(const T1 & a, const T2 & b) { return __le(b, a); }
94//template < typename T1, typename T2 >
95//inline constexpr auto __gt(const T1 & a, const T2 & b) { return __lt(b, a); }
96
97
98//template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
99//constexpr typename largest_type < SIGNED1, SIGNED2 >::type minimum(const SIGNED1 & a, const SIGNED2 & b) { return __lt(a, b) ? ((typename largest_type < SIGNED1, SIGNED2 >::type)a ): ((typename largest_type < SIGNED1, SIGNED2 >::type)b); }
100//
101//template < prototype_unsigned UNSIGNED1, prototype_signed SIGNED2 >
102//constexpr SIGNED2 minimum(const UNSIGNED1 & a, const SIGNED2 & b) { return __lt(a, b) ? ((SIGNED2) a) : b; }
103//
104//template < prototype_signed SIGNED1, prototype_unsigned UNSIGNED2 >
105//constexpr SIGNED1 minimum(const SIGNED1 & a, const UNSIGNED2 & b) { return __lt(a, b) ? a : ((SIGNED1) b); }
106//
107//template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
108//constexpr typename largest_type < UNSIGNED1, UNSIGNED2 >::type minimum(const UNSIGNED1 & a, const UNSIGNED2 & b) { return __lt(a, b) ? ((typename largest_type < UNSIGNED1, UNSIGNED2 >::type)a) : ((typename largest_type < UNSIGNED1, UNSIGNED2 >::type)b); }
109//
110//
111//template < prototype_integral INTEGRAL1, prototype_floating FLOATING2 >
112//constexpr FLOATING2 minimum(const INTEGRAL1 & a, const FLOATING2 & b) { return __lt(a, b) ? ((FLOATING2)a) : b; }
113//
114//template < prototype_floating FLOATING1, prototype_integral INTEGRAL2 >
115//constexpr FLOATING1 minimum(const FLOATING1 & a, const INTEGRAL2 & b) { return __lt(a, b) ? a : ((FLOATING1)b); }
116//
117//template < prototype_floating FLOATING1, prototype_floating FLOATING2 >
118//constexpr typename largest_type < FLOATING1, FLOATING2 >::type minimum(const FLOATING1 & a, const FLOATING2 & b) { return __lt(a, b) ? ((typename largest_type < FLOATING1, FLOATING2 >::type)a) : ((typename largest_type < FLOATING1, FLOATING2 >::type)b); }
119//
120//
121//template < typename TYPE1, typename TYPE2 >
122//constexpr TYPE1 minimum(const TYPE1 & a, const TYPE2 & b) { return __lt(a, b) ? a : ((TYPE1)b); }
123//
124
125
126//template < prototype_signed SIGNED1, prototype_signed SIGNED2 >
127//constexpr typename largest_type < SIGNED1, SIGNED2 >::type maximum(const SIGNED1 & a, const SIGNED2 & b) { return __lt(b, a) ? ((typename largest_type < SIGNED1, SIGNED2 >::type)a) : ((typename largest_type < SIGNED1, SIGNED2 >::type)b); }
128//
129//template < prototype_unsigned UNSIGNED1, prototype_signed SIGNED2 >
130//constexpr SIGNED2 maximum(const UNSIGNED1 & a, const SIGNED2 & b) { return __lt(b, a) ? ((SIGNED2)a) : b; }
131//
132//template < prototype_signed SIGNED1, prototype_unsigned UNSIGNED2 >
133//constexpr SIGNED1 maximum(const SIGNED1 & a, const UNSIGNED2 & b) { return __lt(b, a) ? a : ((SIGNED1)b); }
134//
135//template < prototype_unsigned UNSIGNED1, prototype_unsigned UNSIGNED2 >
136//constexpr typename largest_type < UNSIGNED1, UNSIGNED2 >::type maximum(const UNSIGNED1 & a, const UNSIGNED2 & b) { return __lt(b, a) ? ((typename largest_type < UNSIGNED1, UNSIGNED2 >::type)a) : ((typename largest_type < UNSIGNED1, UNSIGNED2 >::type)b); }
137//
138//template < prototype_integral INTEGRAL1, prototype_floating FLOATING2 >
139//constexpr FLOATING2 maximum(const INTEGRAL1 & a, const FLOATING2 & b) { return __lt(b, a) ? ((FLOATING2)a) : b; }
140//
141//template < prototype_floating FLOATING1, prototype_integral INTEGRAL2 >
142//constexpr FLOATING1 maximum(const FLOATING1 & a, const INTEGRAL2 & b) { return __lt(b, a) ? a : ((FLOATING1)b); }
143
144//template < prototype_floating FLOATING1, prototype_floating FLOATING2 >
145//constexpr largest_type < FLOATING1, FLOATING2 > maximum(const FLOATING1 & a, const FLOATING2 & b) { return __lt(b, a) ? ((typename largest_type < FLOATING1, FLOATING2 >::type)a) : ((typename largest_type < FLOATING1, FLOATING2 >::type)b); }
146
147
148namespace comparison
149{
150
151// template < typename T1, typename T2 >
152//
153//constexpr ::std::strong_ordering order(const T1 & t1, const T2 & t2)
154//{
155//
156// return t1 <=> t2;
157//
158//}
159
160 template < typename T1, typename T2 >
162 {
163 public:
164
165
166 constexpr ::std::strong_ordering order(const T1 & t1, const T2 & t2) const
167 {
168
169 return t1 <=> t2;
170
171 }
172
173
174 };
175
176
177 template < prototype_signed S1, prototype_signed S2 >
178 constexpr ::std::strong_ordering order(S1 s1, S2 s2)
179 {
180
181 return s1 <=> s2;
182
183 }
184
185
186 template < prototype_signed S1, prototype_signed S2 >
187 class comparison2 < S1, S2 >
188 {
189 public:
190
191 constexpr ::std::strong_ordering order(S1 s1, S2 s2) const
192 {
193
194 return s1 <=> s2;
195
196 }
197
198 };
199
200template < prototype_unsigned U1, prototype_unsigned U2 >
201constexpr ::std::strong_ordering order(U1 u1, U2 u2)
202{
203
204 return u1 <=> u2;
205
206}
207 template < prototype_unsigned U1, prototype_unsigned U2 >
208 class comparison2 < U1, U2 >
209 {
210 public:
211
212 constexpr ::std::strong_ordering order(U1 u1, U2 u2) const
213 {
214
215 return u1 <=> u2;
216
217 }
218
219 };
220
221template < prototype_signed S, prototype_unsigned U >
222constexpr ::std::strong_ordering order(S s, U u)
223{
224
225 return s < 0 ? ::std::strong_ordering::less : ::std::make_unsigned_t<S>(s) <=> u;
226
227}
228 template < prototype_signed S, prototype_unsigned U >
229 class comparison2 < S, U >
230 {
231 public:
232
233 constexpr ::std::strong_ordering order(S s, U u) const
234 {
235
236 return s < 0 ? ::std::strong_ordering::less : ::std::make_unsigned_t<S>(s) <=> u;
237
238 }
239
240 };
241template < prototype_unsigned U, prototype_signed S >
242constexpr ::std::strong_ordering order(U u, S s)
243{
244
245 return s < 0 ? ::std::strong_ordering::greater : u <=> ::std::make_unsigned_t<S>(s);
246
247}
248 template < prototype_unsigned U, prototype_signed S >
249 class comparison2 < U, S >
250 {
251 public:
252
253
254
255 constexpr ::std::strong_ordering order(U u, S s) const
256 {
257
258 return s < 0 ? ::std::strong_ordering::greater : u <=> ::std::make_unsigned_t<S>(s);
259
260 }
261
262 };
263template < prototype_floating F1, prototype_floating F2 >
264
265constexpr ::std::strong_ordering order(F1 f1, F2 f2)
266{
267
268 return ::strong_order(f1, f2);
269
270}
271 template < prototype_floating F1, prototype_floating F2 >
272 class comparison2 < F1, F2 >
273 {
274 public:
275
276 constexpr ::std::strong_ordering order(F1 f1, F2 f2) const
277 {
278
279 return ::strong_order(f1, f2);
280
281 }
282
283 };
284
285template < prototype_floating F, prototype_integral T >
286
287constexpr ::std::strong_ordering order(F f, T t)
288{
289
290#if defined(__GNUC__)
291
292 if(f > t)
293 {
294
295 return ::std::strong_ordering::greater;
296
297 }
298 else if(f < t)
299 {
300
301 return ::std::strong_ordering::less;
302
303 }
304 else
305 {
306
307 return ::std::strong_ordering::equal;
308
309 }
310
311#else
312
313 return ::std::strong_order(f, (F)t);
314
315#endif
316
317
318
319}
320 template < prototype_floating F, prototype_integral T >
321 class comparison2 < F, T >
322 {
323 public:
324
325 constexpr ::std::strong_ordering order(F f, T t) const
326 {
327
328#if defined(__GNUC__)
329
330 if(f > t)
331 {
332
333 return ::std::strong_ordering::greater;
334
335 }
336 else if(f < t)
337 {
338
339 return ::std::strong_ordering::less;
340
341 }
342 else
343 {
344
345 return ::std::strong_ordering::equal;
346
347 }
348
349#else
350
351 return ::std::strong_order(f, (F)t);
352
353#endif
354
355
356
357 }
358
359 };
360
361template < prototype_integral T, prototype_floating F >
362constexpr ::std::strong_ordering order(T t, F f)
363{
364
365#if defined(__GNUC__)
366
367 if(t > f)
368 {
369
370 return ::std::strong_ordering::greater;
371
372 }
373 else if(t < f)
374 {
375
376 return ::std::strong_ordering::less;
377
378 }
379 else
380 {
381
382 return ::std::strong_ordering::equal;
383
384 }
385
386#else
387
388 return ::std::strong_order((F) t, f);
389
390#endif
391
392}
393
394 template < prototype_integral T, prototype_floating F >
395 class comparison2 < T, F >
396 {
397 public:
398
399 constexpr ::std::strong_ordering order(T t, F f) const
400 {
401
402#if defined(__GNUC__)
403
404 if(t > f)
405 {
406
407 return ::std::strong_ordering::greater;
408
409 }
410 else if(t < f)
411 {
412
413 return ::std::strong_ordering::less;
414
415 }
416 else
417 {
418
419 return ::std::strong_ordering::equal;
420
421 }
422
423#else
424
425 return ::std::strong_order((F) t, f);
426
427#endif
428
429 }
430
431 };
432
433
434
435} //namespace comparison
436
437
438//template < typename TYPE >
439//constexpr TYPE minimum(const TYPE & a, const TYPE & b)
440//{
441//
442// return a <= b ? a : b;
443//
444//}
445
446
447template < typename TYPE >
448constexpr TYPE maximum(const TYPE & a, const TYPE & b)
449{
450
451 return a < b ? b : a;
452
453}
454
455
456template< typename TYPE, typename COMPARE >
457constexpr TYPE compare_reduce(std::initializer_list<TYPE> list_base, COMPARE compare)
458{
459
460 auto p = list_base.begin();
461
462 if (!p)
463 {
464
465 return {};
466
467 }
468
469 TYPE t = *p;
470
471 p++;
472
473 while(p != list_base.end())
474 {
475
476 if (compare(*p, t))
477 {
478
479 t = *p;
480
481 }
482
483 p++;
484
485 }
486
487 return t;
488
489}
490
491
492template< typename TYPE >
493constexpr TYPE maximum(std::initializer_list<TYPE> list_base)
494{
495
496 return compare_reduce(list_base, [](auto a, auto b) {return a > b; });
497
498}
499
500
501template< typename TYPE >
502constexpr TYPE minimum(std::initializer_list<TYPE> list_base)
503{
504
505 return compare_reduce(list_base, [](auto a, auto b) {return a < b; });
506
507}
508
509
510template < typename TYPE1, typename TYPE2 >
511constexpr largest_type<TYPE1, TYPE2> maximum(const TYPE1 & a, const TYPE2 & b)
512{
513
514 return ::comparison::comparison2<TYPE1, TYPE2>().order(a, b) >= 0 ?
515 (largest_type<TYPE1, TYPE2>) a : (largest_type<TYPE1, TYPE2>)b;
516
517}
518
519
520template < prototype_number TYPE1, prototype_number TYPE2 >
521constexpr smallest_type<TYPE1, TYPE2> natural_minimum(const TYPE1 & a, const TYPE2 & b)
522{
523
524 return ::comparison::comparison2<TYPE1, TYPE2>().order(a, b) <= 0 ?
525 (smallest_type<TYPE1, TYPE2>) maximum(0, a) : (smallest_type<TYPE1, TYPE2>)maximum(0, b);
526
527}
528
529
530template < prototype_unsigned TYPE1, prototype_unsigned TYPE2 >
531constexpr smallest_type<TYPE1, TYPE2> minimum(const TYPE1 & a, const TYPE2 & b)
532{
533
534 return ::comparison::comparison2<TYPE1, TYPE2>().order(a, b) <= 0 ?
535 (smallest_type<TYPE1, TYPE2>) a : (smallest_type<TYPE1, TYPE2>)b;
536
537}
538
539
540template < typename TYPE1, typename TYPE2 >
541constexpr largest_type<TYPE1, TYPE2> minimum(const TYPE1 & a, const TYPE2 & b)
542{
543
544 return ::comparison::comparison2<TYPE1, TYPE2>().order(a, b) <= 0 ?
545 (largest_type<TYPE1, TYPE2>) a : (largest_type<TYPE1, TYPE2>)b;
546
547}
548
549
550CLASS_DECL_ACME ::i64 ceil_div(::i64 num, ::i64 den);
551
552
553CLASS_DECL_ACME ::f64 tri(::f64 angle);
554
555
556#ifdef __cplusplus
557
558
559#include "natural.h"
560
561
562#endif
563
564
565
566CLASS_DECL_ACME ::i32 ccvt_dup(char_pointer buf,::i32 nchar, ::f64 arg, ::i32 ndigits,::i32 *decpt,::i32 *sign,::i32 eflag);
567CLASS_DECL_ACME ::i32 ecvt_dup(char_pointer buf,::i32 nchar, ::f64 arg, ::i32 ndigits,::i32 *decpt,::i32 *sign);
568CLASS_DECL_ACME ::i32 fcvt_dup(char_pointer buf,::i32 nchar, ::f64 arg, ::i32 ndigits,::i32 *decpt,::i32 *sign);
569CLASS_DECL_ACME ::i32 gcvt_dup(char_pointer buf,::i32 nchar, ::f64 arg, ::i32 ndigits);
570CLASS_DECL_ACME ::i32 max_cvt_dup(char_pointer buf,::i32 nchar,::f64 arg,::i32 ndigits,::i32 *decpt,::i32 *sign, ::i32 * pi);
571
572
573
574
575#define MATH_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844
576#define MATH_HALF_PI 1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412671058533991074043256641153323546922
577#define DEGREE_RADIAN 0.01745329251994329576923690768488612713442871888541725456097191440171009114603449443682241569634509482212304492507379059248385468
578#define RADIAN_DEGREE 57.29577951308232087679815481410517033240547246656432154916024386120284714832155263244096899585111094418622338163286489328144827
579
580#include <math.h>
581
582
583namespace degree
584{
585
586 inline CLASS_DECL_ACME ::f64 to_radian(::f64 dAngleDegrees)
587 {
588
589 return dAngleDegrees * DEGREE_RADIAN;
590
591 }
592
593 inline CLASS_DECL_ACME ::f64 sin(::f64 dAngleDegrees)
594 {
595
596 return ::sin(to_radian(dAngleDegrees));
597
598 }
599
600 inline CLASS_DECL_ACME ::f64 cos(::f64 dAngleDegrees)
601 {
602
603 return ::cos(to_radian(dAngleDegrees));
604
605 }
606
607 inline CLASS_DECL_ACME ::f64 tan(::f64 dAngleDegrees)
608 {
609
610 return ::tan(to_radian(dAngleDegrees));
611
612 }
613
614} // namespace degree
615
616
617
618
619
620inline ::u8 __byte_rate(::f64 dRate, ::f64 dMin, ::f64 dMax)
621{
622
623 return (::u8)(dRate * (dMax - dMin) + dMin);
624
625}
626
627
628template < typename TYPE >
629inline TYPE range_rate(TYPE iMin, TYPE iMax, ::f64 dRate)
630{
631 return (TYPE)((iMax - iMin) * dRate) + iMin;
632}
633
634
635
636#include "number_limits.h"
637
638
639
640template < prototype_integral_number INTEGRAL_NUMBER >
641constexpr ::i16 __loword(INTEGRAL_NUMBER i)
642{
643 return i & 0xffff;
644}
645
646
647template<prototype_integral_number INTEGRAL_NUMBER>
648constexpr ::i16 __hiword(INTEGRAL_NUMBER i)
649{
650 return (i >> 16) & 0xffff;
651}
652
653
654//inline ::i64 as_i64(const_char_pointer psz, ::i32 iBase = 10)
655//{
656//
657// const_char_pointer pszEnd = nullptr;
658//
659// return ansi_to_i64(scopedstr, &pszEnd, iBase);
660//
661//}
662
663
664CLASS_DECL_ACME ::i32 type_maximum_digits(enum_integer_type etype, ::i32 iBase = 10);
665
666
667template < prototype_signed SIGNED >
668inline ::i32 maximum_digits(::i32 iBase)
669{
670
671 if constexpr (sizeof(SIGNED) == 1)
672 {
673
674 return type_maximum_digits(e_integer_type_char, iBase);
675
676 }
677 else if constexpr (sizeof(SIGNED) == 2)
678 {
679
680 return type_maximum_digits(e_integer_type_short, iBase);
681
682 }
683 else if constexpr (sizeof(SIGNED) == 4)
684 {
685
686 return type_maximum_digits(e_integer_type_int, iBase);
687
688 }
689 else if constexpr (sizeof(SIGNED) == 8)
690 {
691
692 return type_maximum_digits(e_integer_type_i64, iBase);
693
694 }
695 else
696 {
697
698 throw_exception(error_unexpected);
699
700 return -1;
701
702 }
703
704}
705
706
707template < prototype_unsigned UNSIGNED >
708inline ::i32 maximum_digits(::i32 iBase)
709{
710
711 if constexpr (sizeof(UNSIGNED) == 1)
712 {
713
714 return type_maximum_digits(e_integer_type_unsigned_char, iBase);
715
716 }
717 else if constexpr (sizeof(UNSIGNED) == 2)
718 {
719
720 return type_maximum_digits(e_integer_type_unsigned_short, iBase);
721
722 }
723 else if constexpr (sizeof(UNSIGNED) == 4)
724 {
725
726 return type_maximum_digits(e_integer_type_u32, iBase);
727
728 }
729 else if constexpr (sizeof(UNSIGNED) == 8)
730 {
731
732 return type_maximum_digits(e_integer_type_u64, iBase);
733
734 }
735 else
736 {
737
738 throw_exception(error_unexpected);
739
740 return -1;
741
742 }
743
744}
745
746
747//CLASS_DECL_ACME ::i32 i8_maximum_digits(::i32 iBase);
748//CLASS_DECL_ACME ::i32 byte_maximum_digits(::i32 iBase);
749//
750//CLASS_DECL_ACME ::i32 i16_maximum_digits(::i32 iBase);
751//CLASS_DECL_ACME ::i32 u16_maximum_digits(::i32 iBase);
752//
753//CLASS_DECL_ACME ::i32 i32_maximum_digits(::i32 iBase);
754//CLASS_DECL_ACME ::i32 u32_maximum_digits(::i32 iBase);
755//
756//CLASS_DECL_ACME ::i32 i64_maximum_digits(::i32 iBase);
757//CLASS_DECL_ACME ::i32 u64_maximum_digits(::i32 iBase);
758
759
760template <class T>
761inline T square_power(T v)
762{
763
764 return v * v;
765
766}
767
768
769namespace mathematics
770{
771
772
773 class mathematics;
774
775
776} // namespace mathematics
777
778
779
Definition _.h:162
Definition list.h:33
Definition mathematics.h:29