ObjFW  Diff

Differences From Artifact [b827bb1ce0]:

To Artifact [e71e68e9ff]:


34
35
36
37
38
39
40


41
42
43
44
45
46
47
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/time.h>



#include "platform.h"

#ifdef OF_OBJFW_RUNTIME
# ifdef OF_COMPILING_OBJFW
#  include "ObjFWRT.h"
# else







>
>







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
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
	i = (uint64_t)_OFByteSwap32NonConst(
	    (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
	    _OFByteSwap32NonConst((uint32_t)(i >> 32));
#endif
	return i;
}

#ifdef __GNUC__






# define OFByteSwap16(i) \
    (__builtin_constant_p(i) ? _OFByteSwap16Const(i) : _OFByteSwap16NonConst(i))







# define OFByteSwap32(i) \
    (__builtin_constant_p(i) ? _OFByteSwap32Const(i) : _OFByteSwap32NonConst(i))







# define OFByteSwap64(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)
#endif







static OF_INLINE uint32_t
OFFloatToRawUInt32(float f)
{
	uint32_t ret;
	memcpy(&ret, &f, 4);
	return ret;
}







static OF_INLINE float
OFRawUInt32ToFloat(uint32_t uInt32)
{
	float ret;
	memcpy(&ret, &uInt32, 4);
	return ret;
}







static OF_INLINE uint64_t
OFDoubleToRawUInt64(double d)
{
	uint64_t ret;
	memcpy(&ret, &d, 8);
	return ret;
}







static OF_INLINE double
OFRawUInt64ToDouble(uint64_t uInt64)
{
	double ret;
	memcpy(&ret, &uInt64, 8);
	return ret;
}







static OF_INLINE float OF_CONST_FUNC
OFByteSwapFloat(float f)
{
	return OFRawUInt32ToFloat(OFByteSwap32(OFFloatToRawUInt32(f)));

}







static OF_INLINE double OF_CONST_FUNC
OFByteSwapDouble(double d)
{
	return OFRawUInt64ToDouble(OFByteSwap64(OFDoubleToRawUInt64(d)));

}

#ifdef OF_BIG_ENDIAN







# define OFFromBigEndian16(i) (i)








# define OFFromBigEndian32(i) (i)








# define OFFromBigEndian64(i) (i)








# define OFFromLittleEndian16(i) OFByteSwap16(i)








# define OFFromLittleEndian32(i) OFByteSwap32(i)








# define OFFromLittleEndian64(i) OFByteSwap64(i)








# define OFToBigEndian16(i) (i)








# define OFToBigEndian32(i) (i)








# define OFToBigEndian64(i) (i)








# define OFToLittleEndian16(i) OFByteSwap16(i)








# define OFToLittleEndian32(i) OFByteSwap32(i)








# 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






# define OFFromBigEndianFloat(f) (f)







# define OFFromBigEndianDouble(d) (d)







# define OFFromLittleEndianFloat(f) OFByteSwapFloat(f)







# define OFFromLittleEndianDouble(d) OFByteSwapDouble(d)







# define OFToBigEndianFloat(f) (f)







# define OFToBigEndianDouble(d) (d)







# define OFToLittleEndianFloat(f) OFByteSwapFloat(f)







# 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








#define OFRotateLeft(value, bits)					\
    (((bits) % (sizeof(value) * 8)) > 0					\
    ? ((value) << ((bits) % (sizeof(value) * 8))) |			\
    ((value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))	\
    : (value))








#define OFRotateRight(value, bits)					\
    (((bits) % (sizeof(value) * 8)) > 0					\
    ? ((value) >> ((bits) % (sizeof(value) * 8))) |			\
    ((value) << (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))	\
    : (value))








#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));







|
>
>
>
>
>
>


>
>
>
>
>
>
>


>
>
>
>
>
>
>








>
>
>
>
>
>
|
|






>
>
>
>
>
>
|
|






>
>
>
>
>
>
|
|






>
>
>
>
>
>
|
|






>
>
>
>
>
>



|
>


>
>
>
>
>
>



|
>


|
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
















|
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>

>
>
>
>
>
>
>












>
>
>
>
>
>
>





>
>
>
>
>
>
>
>






>
>
>
>
>
>
>







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
	i = (uint64_t)_OFByteSwap32NonConst(
	    (uint32_t)(i & UINT32_C(0xFFFFFFFF))) << 32 |
	    _OFByteSwap32NonConst((uint32_t)(i >> 32));
#endif
	return i;
}

#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))

/**
 * @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))

/**
 * @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))
#else
# 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 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 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 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 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 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 OFBitConvertUInt64ToDouble(OFByteSwap64(
	    OFBitConvertDoubleToUInt64(d)));
}

#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

#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));