[newlib-cygwin/main] Cygwin: _endian.h: Add AArch64 implementations for `ntohl` and `ntohs`

Corinna Vinschen via Cygwin-cvs <[email protected]> Wed, 7 Jan 2026 11:17:19 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b14e2e7d15a8f4ef4829d797692b42541c61e0ed

commit b14e2e7d15a8f4ef4829d797692b42541c61e0ed
Author:     Thirumalai Nagalingam <[email protected]>
AuthorDate: Sat Dec 6 18:31:44 2025 +0530
Commit:     Corinna Vinschen <[email protected]>
CommitDate: Wed Jan 7 12:15:32 2026 +0100

    Cygwin: _endian.h: Add AArch64 implementations for `ntohl` and `ntohs`
    
    Add AArch64-specific implementations of __ntohl() and __ntohs() in
    `winsup/cygwin/include/machine/_endian.h`.
    For AArch64 targets, use the `REV` and `REV16` instructions to perform
    byte swapping, with explicit zero-extension for 16-bit values to ensure
    correct register semantics.
    
    Signed-off-by: Thirumalai Nagalingam <[email protected]>

Diff:
---
 winsup/cygwin/include/machine/_endian.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/winsup/cygwin/include/machine/_endian.h b/winsup/cygwin/include/machine/_endian.h
index dbd4429b8099..622d7a2e9ab2 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;
 }