[PATCH] Cygwin: _endian.h: Add AArch64 implementations for `ntohl` and `ntohs`
Corinna Vinschen
corinna-cygwin@cygwin.com
Wed Jan 7 11:17:56 GMT 2026
Hi Thirumalai,
On Jan 5 12:40, Thirumalai Nagalingam wrote:
> Hello Everyone,
>
> This patch adds AArch64-specific inline asm implementations of __ntohl()
> and __ntohs() in `winsup/cygwin/include/machine/_endian.h`.
>
> For AArch64 targets, the patch uses the REV and REV16 instructions
> to perform byte swapping, with explicit zero-extension for 16-bit
> values to ensure correct register semantics.
>
> Comments and reviews are welcome.
>
> Thanks & regards
> Thirumalai Nagalingam <thirumalai.nagalingam@multicorewareinc.com<mailto:thirumalai.nagalingam@multicorewareinc.com>>
>
> In-lined patch:
>
> diff --git a/winsup/cygwin/include/machine/_endian.h b/winsup/cygwin/include/machine/_endian.h
> index dbd4429b8..129cba66b 100644
> --- a/winsup/cygwin/include/machine/_endian.h
> +++ b/winsup/cygwin/include/machine/_endian.h
> @@ -26,16 +26,26 @@ _ELIDABLE_INLINE __uint16_t __ntohs(__uint16_t);
> _ELIDABLE_INLINE __uint32_t
> __ntohl(__uint32_t _x)
> {
> +#if defined(__x86_64__)
> __asm__("bswap %0" : "=r" (_x) : "0" (_x));
> +#elif defined(__aarch64__)
> + __asm__("rev %w0, %w0" : "=r" (_x) : "0" (_x));
> +#endif
> return _x;
> }
>
> _ELIDABLE_INLINE __uint16_t
> __ntohs(__uint16_t _x)
> {
> +#if defined(__x86_64__)
> __asm__("xchgb %b0,%h0" /* swap bytes */
> : "=Q" (_x)
> : "0" (_x));
> +#elif defined(__aarch64__)
> + __asm__("uxth %w0, %w0\n\t"
> + "rev16 %w0, %w0"
> + : "+r" (_x));
> +#endif
> return _x;
> }
This looks pretty obvious to me, pushed.
Thanks,
Corinna
More information about the Cygwin-patches
mailing list