@@ -550,6 +550,107 @@ typedef int64_t INT64;
550550 #define WOLFTPM2_WRAP_ECC_KEY_BITS (MAX_ECC_KEY_BITS*8)
551551#endif
552552
553+
554+ /* ---------------------------------------------------------------------------*/
555+ /* ENDIANESS HELPERS */
556+ /* ---------------------------------------------------------------------------*/
557+
558+ #ifdef __ICCARM__
559+ #include "intrinsics.h"
560+ #endif
561+
562+ #ifdef INTEL_INTRINSICS
563+ /* for non visual studio probably need no long version, 32 bit only
564+ * i.e., _rotl and _rotr */
565+ #include <stdlib.h> /* get intrinsic definitions */
566+ #pragma intrinsic(_lrotl, _lrotr)
567+ static inline word32 rotlFixed (word32 x , word32 y ) {
568+ return y ? _lrotl (x , y ) : x ;
569+ }
570+ static inline word32 rotrFixed (word32 x , word32 y ) {
571+ return y ? _lrotr (x , y ) : x ;
572+ }
573+ #elif defined(__CCRX__ )
574+ #include <builtin.h> /* get intrinsic definitions */
575+ static inline word32 rotlFixed (word32 x , word32 y ) {
576+ return _builtin_rotl (x , y );
577+ }
578+ static inline word32 rotrFixed (word32 x , word32 y ) {
579+ return _builtin_rotr (x , y );
580+ }
581+ #else /* generic */
582+ /* This routine performs a left circular arithmetic shift of <x> by <y> value. */
583+ static inline word32 rotlFixed (word32 x , word32 y ) {
584+ return (x << y ) | (x >> (sizeof (y ) * 8 - y ));
585+ }
586+ /* This routine performs a right circular arithmetic shift of <x> by <y> value. */
587+ static inline word32 rotrFixed (word32 x , word32 y )
588+ {
589+ return (x >> y ) | (x << (sizeof (y ) * 8 - y ));
590+ }
591+ #endif
592+
593+ static inline word16 ByteReverseWord16 (word16 value )
594+ {
595+ #if defined(__ICCARM__ )
596+ return (word16 )__REV16 (value );
597+ #elif defined(KEIL_INTRINSICS )
598+ return (word16 )__rev16 (value );
599+ #elif defined(__GNUC_PREREQ ) && __GNUC_PREREQ (4 , 3 )
600+ return (word16 )__builtin_bswap16 (value );
601+ #else
602+ return (value >> 8 ) | (value << 8 );
603+ #endif
604+ }
605+
606+ static inline word32 ByteReverseWord32 (word32 value )
607+ {
608+ #ifdef PPC_INTRINSICS
609+ /* PPC: load reverse indexed instruction */
610+ return (word32 )__lwbrx (& value ,0 );
611+ #elif defined(__ICCARM__ )
612+ return (word32 )__REV (value );
613+ #elif defined(KEIL_INTRINSICS )
614+ return (word32 )__rev (value );
615+ #elif defined(__CCRX__ )
616+ return (word32 )_builtin_revl (value );
617+ #elif defined(WOLF_ALLOW_BUILTIN ) && \
618+ defined(__GNUC_PREREQ ) && __GNUC_PREREQ (4 , 3 )
619+ return (word32 )__builtin_bswap32 (value );
620+ #elif defined(WOLFSSL_BYTESWAP32_ASM ) && defined(__GNUC__ ) && \
621+ defined(__aarch64__ )
622+ __asm__ volatile (
623+ "REV32 %0, %0 \n"
624+ : "+r" (value )
625+ :
626+ );
627+ return value ;
628+ #elif defined(WOLFSSL_BYTESWAP32_ASM ) && defined(__GNUC__ ) && \
629+ (defined(__thumb__ ) || defined(__arm__ ))
630+ __asm__ volatile (
631+ "REV %0, %0 \n"
632+ : "+r" (value )
633+ :
634+ );
635+ return value ;
636+ #elif defined(FAST_ROTATE )
637+ /* 5 instructions with rotate instruction, 9 without */
638+ return (rotrFixed (value , 8U ) & 0xff00ff00 ) |
639+ (rotlFixed (value , 8U ) & 0x00ff00ff );
640+ #else
641+ /* 6 instructions with rotate instruction, 8 without */
642+ value = ((value & 0xFF00FF00 ) >> 8 ) | ((value & 0x00FF00FF ) << 8 );
643+ return rotlFixed (value , 16U );
644+ #endif
645+ }
646+
647+ static inline word64 ByteReverseWord64 (word64 value )
648+ {
649+ return (word64 )((word64 )ByteReverseWord32 ((word32 )value )) << 32 |
650+ (word64 )ByteReverseWord32 ((word32 )(value >> 32 ));
651+ }
652+
653+
553654#ifdef __cplusplus
554655 } /* extern "C" */
555656#endif
0 commit comments