ObjFW  Diff

Differences From Artifact [7e3d0a019b]:

To Artifact [cdc40153f2]:


34
35
36
37
38
39
40


41
42
43
44
45
46
47
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49







+
+







#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/time.h>

/** @file */

#include "platform.h"

#ifdef OF_OBJFW_RUNTIME
# ifdef OF_COMPILING_OBJFW
#  include "ObjFWRT.h"
# else
101
102
103
104
105
106
107






108
109
110
111
112
113
114
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122







+
+
+
+
+
+







# define OF_INLINE inline
# define OF_LIKELY(cond) (cond)
# define OF_UNLIKELY(cond) (cond)
# define OF_CONST_FUNC
# define OF_NO_RETURN_FUNC
# define OF_WEAK_REF(sym)
#endif

#ifndef OF_DJGPP
# define OF_VISIBILITY_HIDDEN __attribute__((__visibility__("hidden")))
#else
# define OF_VISIBILITY_HIDDEN
#endif

#if __STDC_VERSION__ >= 201112L
# define OF_ALIGN(size) _Alignas(size)
# define OF_ALIGNOF(type) _Alignof(type)
# define OF_ALIGNAS(type) _Alignas(type)
#else
# define OF_ALIGN(size) __attribute__((__aligned__(size)))
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
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







-
+





-
+








-
+












-
+







	static void __attribute__((__constructor__(prio)))	\
	OF_PREPROCESSOR_CONCAT(constructor, __LINE__)(void)
#define OF_DESTRUCTOR(prio)					\
	static void __attribute__((__destructor__(prio)))	\
	OF_PREPROCESSOR_CONCAT(destructor, __LINE__)(void)

static OF_INLINE uint16_t OF_CONST_FUNC
OFByteSwap16Const(uint16_t i)
_OFByteSwap16Const(uint16_t i)
{
	return (i & UINT16_C(0xFF00)) >> 8 | (i & UINT16_C(0x00FF)) << 8;
}

static OF_INLINE uint32_t OF_CONST_FUNC
OFByteSwap32Const(uint32_t i)
_OFByteSwap32Const(uint32_t i)
{
	return (i & UINT32_C(0xFF000000)) >> 24 |
	    (i & UINT32_C(0x00FF0000)) >> 8 |
	    (i & UINT32_C(0x0000FF00)) << 8 |
	    (i & UINT32_C(0x000000FF)) << 24;
}

static OF_INLINE uint64_t OF_CONST_FUNC
OFByteSwap64Const(uint64_t i)
_OFByteSwap64Const(uint64_t i)
{
	return (i & UINT64_C(0xFF00000000000000)) >> 56 |
	    (i & UINT64_C(0x00FF000000000000)) >> 40 |
	    (i & UINT64_C(0x0000FF0000000000)) >> 24 |
	    (i & UINT64_C(0x000000FF00000000)) >> 8 |
	    (i & UINT64_C(0x00000000FF000000)) << 8 |
	    (i & UINT64_C(0x0000000000FF0000)) << 24 |
	    (i & UINT64_C(0x000000000000FF00)) << 40 |
	    (i & UINT64_C(0x00000000000000FF)) << 56;
}

static OF_INLINE uint16_t OF_CONST_FUNC
OFByteSwap16NonConst(uint16_t i)
_OFByteSwap16NonConst(uint16_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP16)
	return __builtin_bswap16(i);
#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
	__asm__ (
	    "xchg{b}	{ %h0, %b0 | %b0, %h0 }"
	    : "=Q" (i)
524
525
526
527
528
529
530
531

532
533
534
535
536
537
538
532
533
534
535
536
537
538

539
540
541
542
543
544
545
546







-
+







	i = (i & UINT16_C(0xFF00)) >> 8 |
	    (i & UINT16_C(0x00FF)) << 8;
#endif
	return i;
}

static OF_INLINE uint32_t OF_CONST_FUNC
OFByteSwap32NonConst(uint32_t i)
_OFByteSwap32NonConst(uint32_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP32)
	return __builtin_bswap32(i);
#elif (defined(OF_AMD64) || defined(OF_X86)) && defined(__GNUC__)
	__asm__ (
	    "bswap	%0"
	    : "=q" (i)
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






627
628


629
630
631
632
633
634






635
636
637
638


639
640






641
642
643
644


645
646
647








648








649








650








651








652








653








654








655








656








657








658








659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675







676







677







678







679







680







681







682







683
684
685
686
687
688
689
690
691
692
693
694







695
696
697
698
699








700
701
702
703
704
705







706
707
708
709
710
711
712
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
627
628
629
630
631
632
633
634
635
636


637
638
639
640
641
642
643
644
645
646
647
648
649
650


651
652
653
654
655
656
657
658
659
660
661
662
663
664


665
666
667
668
669
670
671
672
673
674
675
676
677
678


679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

696
697
698
699
700
701
702
703
704
705
706
707
708

709
710
711
712

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950







-
+


















-
+

-
+




-
+
+
+
+
+
+
+

-
+
+
+
+
+
+
+
+

-
+
+
+
+
+
+
+
+

-
+

-
-
-
+
+
+


+
+
+
+
+
+
-
-
+
+






+
+
+
+
+
+
-
-
+
+






+
+
+
+
+
+
-
-
+
+






+
+
+
+
+
+
-
-
+
+






+
+
+
+
+
+



-
+
+


+
+
+
+
+
+



-
+
+


-
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
















-
+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+

+
+
+
+
+
+
+












+
+
+
+
+
+
+





+
+
+
+
+
+
+
+






+
+
+
+
+
+
+







	    (i & UINT32_C(0x0000FF00)) <<  8 |
	    (i & UINT32_C(0x000000FF)) << 24;
#endif
	return i;
}

static OF_INLINE uint64_t OF_CONST_FUNC
OFByteSwap64NonConst(uint64_t i)
_OFByteSwap64NonConst(uint64_t i)
{
#if defined(OF_HAVE_BUILTIN_BSWAP64)
	return __builtin_bswap64(i);
#elif defined(OF_AMD64) && defined(__GNUC__)
	__asm__ (
	    "bswap	%0"
	    : "=r" (i)
	    : "0" (i)
	);
#elif defined(OF_X86) && defined(__GNUC__)
	__asm__ (
	    "bswap	{%%}eax\n\t"
	    "bswap	{%%}edx\n\t"
	    "xchg{l}	{ %%eax, %%edx | edx, eax }"
	    : "=A" (i)
	    : "0" (i)
	);
#else
	i = (uint64_t)OFByteSwap32NonConst(
	i = (uint64_t)_OFByteSwap32NonConst(
	    (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
	    OFByteSwap32NonConst((uint32_t)(i >> 32));
	    _OFByteSwap32NonConst((uint32_t)(i >> 32));
#endif
	return i;
}

#ifdef __GNUC__
#if defined(__GNUC__) || defined(DOXYGEN)
/**
 * @brief Byte swaps the specified 16 bit integer.
 *
 * @param i The integer to byte swap
 * @return The byte swapped integer
 */
# define OFByteSwap16(i) \
    (__builtin_constant_p(i) ? OFByteSwap16Const(i) : OFByteSwap16NonConst(i))
    (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))

/**
 * @brief Byte swaps the specified 32 bit integer.
 *
 * @param i The integer to byte swap
 * @return The byte swapped integer
 */
# define OFByteSwap32(i) \
    (__builtin_constant_p(i) ? OFByteSwap32Const(i) : OFByteSwap32NonConst(i))
    (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))

/**
 * @brief Byte swaps the specified 64 bit integer.
 *
 * @param i The integer to byte swap
 * @return The byte swapped integer
 */
# define OFByteSwap64(i) \
    (__builtin_constant_p(i) ? OFByteSwap64Const(i) : OFByteSwap64NonConst(i))
    (__builtin_constant_p(i) ? _OFByteSwap64Const(i) : _OFByteSwap64NonConst(i))
#else
# define OFByteSwap16(i) OFByteSwap16Const(i)
# define OFByteSwap32(i) OFByteSwap32Const(i)
# define OFByteSwap64(i) OFByteSwap64Const(i)
# define OFByteSwap16(i) _OFByteSwap16Const(i)
# define OFByteSwap32(i) _OFByteSwap32Const(i)
# define OFByteSwap64(i) _OFByteSwap64Const(i)
#endif

/**
 * @brief Bit-converts the specified float to a uint32_t.
 *
 * @param f The float to bit-convert
 * @return The float bit-converted to a uint32_t
 */
static OF_INLINE uint32_t
OFFloatToRawUInt32(float f)
static OF_INLINE uint32_t OF_CONST_FUNC
OFBitConvertFloatToUInt32(float f)
{
	uint32_t ret;
	memcpy(&ret, &f, 4);
	return ret;
}

/**
 * @brief Bit-converts the specified uint32_t to a float.
 *
 * @param uInt32 The uint32_t to bit-convert
 * @return The uint32_t bit-converted to a float
 */
static OF_INLINE float
OFRawUInt32ToFloat(uint32_t uInt32)
static OF_INLINE float OF_CONST_FUNC
OFBitConvertUInt32ToFloat(uint32_t uInt32)
{
	float ret;
	memcpy(&ret, &uInt32, 4);
	return ret;
}

/**
 * @brief Bit-converts the specified double to a uint64_t.
 *
 * @param d The double to bit-convert
 * @return The double bit-converted to a uint64_t
 */
static OF_INLINE uint64_t
OFDoubleToRawUInt64(double d)
static OF_INLINE uint64_t OF_CONST_FUNC
OFBitConvertDoubleToUInt64(double d)
{
	uint64_t ret;
	memcpy(&ret, &d, 8);
	return ret;
}

/**
 * @brief Bit-converts the specified uint64_t to a double.
 *
 * @param uInt64 The uint64_t to bit-convert
 * @return The uint64_t bit-converted to a double
 */
static OF_INLINE double
OFRawUInt64ToDouble(uint64_t uInt64)
static OF_INLINE double OF_CONST_FUNC
OFBitConvertUInt64ToDouble(uint64_t uInt64)
{
	double ret;
	memcpy(&ret, &uInt64, 8);
	return ret;
}

/**
 * @brief Byte swaps the specified float.
 *
 * @param f The float to byte swap
 * @return The byte swapped float
 */
static OF_INLINE float OF_CONST_FUNC
OFByteSwapFloat(float f)
{
	return OFRawUInt32ToFloat(OFByteSwap32(OFFloatToRawUInt32(f)));
	return OFBitConvertUInt32ToFloat(OFByteSwap32(
	    OFBitConvertFloatToUInt32(f)));
}

/**
 * @brief Byte swaps the specified double.
 *
 * @param d The double to byte swap
 * @return The byte swapped double
 */
static OF_INLINE double OF_CONST_FUNC
OFByteSwapDouble(double d)
{
	return OFRawUInt64ToDouble(OFByteSwap64(OFDoubleToRawUInt64(d)));
	return OFBitConvertUInt64ToDouble(OFByteSwap64(
	    OFBitConvertDoubleToUInt64(d)));
}

#ifdef OF_BIG_ENDIAN
#if defined(OF_BIG_ENDIAN) || defined(DOXYGEN)
/**
 * @brief Converts the specified 16 bit integer from big endian to native
 *	  endian.
 *
 * @param i The 16 bit integer to convert
 * @return The 16 bit integer converted to native endian
 */
# define OFFromBigEndian16(i) (i)

/**
 * @brief Converts the specified 32 bit integer from big endian to native
 *	  endian.
 *
 * @param i The 32 bit integer to convert
 * @return The 32 bit integer converted to native endian
 */
# define OFFromBigEndian32(i) (i)

/**
 * @brief Converts the specified 64 bit integer from big endian to native
 *	  endian.
 *
 * @param i The 64 bit integer to convert
 * @return The 64 bit integer converted to native endian
 */
# define OFFromBigEndian64(i) (i)

/**
 * @brief Converts the specified 16 bit integer from little endian to native
 *	  endian.
 *
 * @param i The 16 bit integer to convert
 * @return The 16 bit integer converted to native endian
 */
# define OFFromLittleEndian16(i) OFByteSwap16(i)

/**
 * @brief Converts the specified 32 bit integer from little endian to native
 *	  endian.
 *
 * @param i The 32 bit integer to convert
 * @return The 32 bit integer converted to native endian
 */
# define OFFromLittleEndian32(i) OFByteSwap32(i)

/**
 * @brief Converts the specified 64 bit integer from little endian to native
 *	  endian.
 *
 * @param i The 64 bit integer to convert
 * @return The 64 bit integer converted to native endian
 */
# define OFFromLittleEndian64(i) OFByteSwap64(i)

/**
 * @brief Converts the specified 16 bit integer from native endian to big
 *	  endian.
 *
 * @param i The 16 bit integer to convert
 * @return The 16 bit integer converted to big endian
 */
# define OFToBigEndian16(i) (i)

/**
 * @brief Converts the specified 32 bit integer from native endian to big
 *	  endian.
 *
 * @param i The 32 bit integer to convert
 * @return The 32 bit integer converted to big endian
 */
# define OFToBigEndian32(i) (i)

/**
 * @brief Converts the specified 64 bit integer from native endian to big
 *	  endian.
 *
 * @param i The 64 bit integer to convert
 * @return The 64 bit integer converted to big endian
 */
# define OFToBigEndian64(i) (i)

/**
 * @brief Converts the specified 16 bit integer from native endian to little
 *	  endian.
 *
 * @param i The 16 bit integer to convert
 * @return The 16 bit integer converted to little endian
 */
# define OFToLittleEndian16(i) OFByteSwap16(i)

/**
 * @brief Converts the specified 32 bit integer from native endian to little
 *	  endian.
 *
 * @param i The 32 bit integer to convert
 * @return The 32 bit integer converted to little endian
 */
# define OFToLittleEndian32(i) OFByteSwap32(i)

/**
 * @brief Converts the specified 64 bit integer from native endian to little
 *	  endian.
 *
 * @param i The 64 bit integer to convert
 * @return The 64 bit integer converted to little endian
 */
# define OFToLittleEndian64(i) OFByteSwap64(i)
#else
# define OFFromBigEndian16(i) OFByteSwap16(i)
# define OFFromBigEndian32(i) OFByteSwap32(i)
# define OFFromBigEndian64(i) OFByteSwap64(i)
# define OFFromLittleEndian16(i) (i)
# define OFFromLittleEndian32(i) (i)
# define OFFromLittleEndian64(i) (i)
# define OFToBigEndian16(i) OFByteSwap16(i)
# define OFToBigEndian32(i) OFByteSwap32(i)
# define OFToBigEndian64(i) OFByteSwap64(i)
# define OFToLittleEndian16(i) (i)
# define OFToLittleEndian32(i) (i)
# define OFToLittleEndian64(i) (i)
#endif

#ifdef OF_FLOAT_BIG_ENDIAN
#if defined(OF_FLOAT_BIG_ENDIAN) || defined(DOXYGEN)
/**
 * @brief Converts the specified float from big endian to native endian.
 *
 * @param f The float to convert
 * @return The float converted to native endian
 */
# define OFFromBigEndianFloat(f) (f)

/**
 * @brief Converts the specified double from big endian to native endian.
 *
 * @param d The double to convert
 * @return The double converted to native endian
 */
# define OFFromBigEndianDouble(d) (d)

/**
 * @brief Converts the specified float from little endian to native endian.
 *
 * @param f The float to convert
 * @return The float converted to native endian
 */
# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)

/**
 * @brief Converts the specified double from little endian to native endian.
 *
 * @param d The double to convert
 * @return The double converted to native endian
 */
# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)

/**
 * @brief Converts the specified float from native endian to big endian.
 *
 * @param f The float to convert
 * @return The float converted to big endian
 */
# define OFToBigEndianFloat(f) (f)

/**
 * @brief Converts the specified double from native endian to big endian.
 *
 * @param d The double to convert
 * @return The double converted to big endian
 */
# define OFToBigEndianDouble(d) (d)

/**
 * @brief Converts the specified float from native endian to little endian.
 *
 * @param f The float to convert
 * @return The float converted to little endian
 */
# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)

/**
 * @brief Converts the specified double from native endian to little endian.
 *
 * @param d The double to convert
 * @return The double converted to little endian
 */
# define OFToLittleEndianDouble(d) OFByteSwapDouble(d)
#else
# define OFFromBigEndianFloat(f) OFByteSwapFloat(f)
# define OFFromBigEndianDouble(d) OFByteSwapDouble(d)
# define OFFromLittleEndianFloat(f) (f)
# define OFFromLittleEndianDouble(d) (d)
# define OFToBigEndianFloat(f) OFByteSwapFloat(f)
# define OFToBigEndianDouble(d) OFByteSwapDouble(d)
# define OFToLittleEndianFloat(f) (f)
# define OFToLittleEndianDouble(d) (d)
#endif

/**
 * @brief Rotates the specified value left by the specified amount of bits.
 *
 * @param value The value to rotate
 * @param bits The number of bits to rotate left the value by
 * @return The value rotated left by the specified amount of bits
 */
#define OFRotateLeft(value, bits)					\
    (((bits) % (sizeof(value) * 8)) > 0					\
    ? ((value) << ((bits) % (sizeof(value) * 8))) |			\
    ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))	\
    : (value))

/**
 * @brief Rotates the specified value right by the specified amount of bits.
 *
 * @param value The value to rotate
 * @param bits The number of bits to rotate right the value by
 * @return The value rotated right by the specified amount of bits
 */
#define OFRotateRight(value, bits)					\
    (((bits) % (sizeof(value) * 8)) > 0					\
    ? ((value) >> ((bits) % (sizeof(value) * 8))) |			\
    ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))	\
    : (value))

/**
 * @brief Rounds up the specified value to the specified power of two.
 *
 * @param pow2 The power of 2 to round up to
 * @param value The value to round up to the specified power of two
 * @return The specified value rounded up to the specified power of two
 */
#define OFRoundUpToPowerOf2(pow2, value)	\
    (((value) + (pow2) - 1) & ~((pow2) - 1))

static OF_INLINE bool
OFBitsetIsSet(unsigned char *_Nonnull storage, size_t idx)
{
	return storage[idx / CHAR_BIT] & (1u << (idx % CHAR_BIT));