public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] include/bitops.h: Use the operating system byteswapping functions
@ 2012-12-26 17:38 Cristian Rodríguez
  2012-12-26 18:48 ` Mike Frysinger
  2013-01-08 13:17 ` Karel Zak
  0 siblings, 2 replies; 4+ messages in thread
From: Cristian Rodríguez @ 2012-12-26 17:38 UTC (permalink / raw)
  To: util-linux; +Cc: Cristian Rodríguez

There is no need to reinvent the wheel.
---
 include/bitops.h | 69 +++++++++++++++-----------------------------------------
 1 file changed, 18 insertions(+), 51 deletions(-)

diff --git a/include/bitops.h b/include/bitops.h
index 81375d0..89b418c 100644
--- a/include/bitops.h
+++ b/include/bitops.h
@@ -8,6 +8,9 @@
  */
 #include <sys/param.h>
 
+#include <byteswap.h>
+#include <endian.h>
+
 #ifndef NBBY
 # define NBBY            CHAR_BIT
 #endif
@@ -22,63 +25,27 @@
 /*
  * Byte swab macros (based on linux/byteorder/swab.h)
  */
-#define swab16(x) \
-	((uint16_t)( \
-		(((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
-		(((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
-
-#define swab32(x) \
-	((uint32_t)( \
-		(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
-		(((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) | \
-		(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) | \
-		(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
-
-#define swab64(x) \
-	((uint64_t)( \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
-	        (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
-		(uint64_t)(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
-
-
-#ifdef WORDS_BIGENDIAN
+#define swab16(x) bswap_16(x)
 
-#define cpu_to_le16(x) swab16(x)
-#define cpu_to_le32(x) swab32(x)
-#define cpu_to_le64(x) swab64(x)
-#define cpu_to_be16(x) ((uint16_t)(x))
-#define cpu_to_be32(x) ((uint32_t)(x))
-#define cpu_to_be64(x) ((uint64_t)(x))
+#define swab32(x) bswap_32(x)
 
-#define le16_to_cpu(x) swab16(x)
-#define le32_to_cpu(x) swab32(x)
-#define le64_to_cpu(x) swab64(x)
-#define be16_to_cpu(x) ((uint16_t)(x))
-#define be32_to_cpu(x) ((uint32_t)(x))
-#define be64_to_cpu(x) ((uint64_t)(x))
+#define swab64(x) bswap_64(x)
 
-#else /* !WORDS_BIGENDIAN */
+#define cpu_to_le16(x) htole16(x)
+#define cpu_to_le32(x) htole32(x)
+#define cpu_to_le64(x) htole64(x)
 
-#define cpu_to_le16(x) ((uint16_t)(x))
-#define cpu_to_le32(x) ((uint32_t)(x))
-#define cpu_to_le64(x) ((uint64_t)(x))
-#define cpu_to_be16(x) swab16(x)
-#define cpu_to_be32(x) swab32(x)
-#define cpu_to_be64(x) swab64(x)
+#define cpu_to_be16(x) htobe16(x)
+#define cpu_to_be32(x) htobe32(x)
+#define cpu_to_be64(x) htobe64(x)
 
-#define le16_to_cpu(x) ((uint16_t)(x))
-#define le32_to_cpu(x) ((uint32_t)(x))
-#define le64_to_cpu(x) ((uint64_t)(x))
-#define be16_to_cpu(x) swab16(x)
-#define be32_to_cpu(x) swab32(x)
-#define be64_to_cpu(x) swab64(x)
+#define le16_to_cpu(x) le16toh(x)
+#define le32_to_cpu(x) le32toh(x)
+#define le64_to_cpu(x) le64toh(x)
 
-#endif /* WORDS_BIGENDIAN */
+#define be16_to_cpu(x) be16toh(x)
+#define be32_to_cpu(x) be32toh(x)
+#define be64_to_cpu(x) be64toh(x)
 
 #endif /* BITOPS_H */
 
-- 
1.8.0.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] include/bitops.h: Use the operating system byteswapping functions
  2012-12-26 17:38 [PATCH] include/bitops.h: Use the operating system byteswapping functions Cristian Rodríguez
@ 2012-12-26 18:48 ` Mike Frysinger
  2012-12-26 19:01   ` Cristian Rodríguez
  2013-01-08 13:17 ` Karel Zak
  1 sibling, 1 reply; 4+ messages in thread
From: Mike Frysinger @ 2012-12-26 18:48 UTC (permalink / raw)
  To: Cristian Rodríguez; +Cc: util-linux

[-- Attachment #1: Type: Text/Plain, Size: 258 bytes --]

On Wednesday 26 December 2012 12:38:21 Cristian Rodríguez wrote:
> There is no need to reinvent the wheel.

the trouble is that util-linux supports more than linux, and the api of these 
funcs isn't the same (or even available) on other systems
-mike

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] include/bitops.h: Use the operating system byteswapping functions
  2012-12-26 18:48 ` Mike Frysinger
@ 2012-12-26 19:01   ` Cristian Rodríguez
  0 siblings, 0 replies; 4+ messages in thread
From: Cristian Rodríguez @ 2012-12-26 19:01 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: util-linux

On Wed 26 Dec 2012 03:48:01 PM CLST, Mike Frysinger wrote:
> On Wednesday 26 December 2012 12:38:21 Cristian Rodríguez wrote:
>> There is no need to reinvent the wheel.
>
> the trouble is that util-linux supports more than linux, and the api of these
> funcs isn't the same (or even available) on other systems
> -mike

That's easy to fix. I will send a follow up patch later.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] include/bitops.h: Use the operating system byteswapping functions
  2012-12-26 17:38 [PATCH] include/bitops.h: Use the operating system byteswapping functions Cristian Rodríguez
  2012-12-26 18:48 ` Mike Frysinger
@ 2013-01-08 13:17 ` Karel Zak
  1 sibling, 0 replies; 4+ messages in thread
From: Karel Zak @ 2013-01-08 13:17 UTC (permalink / raw)
  To: Cristian Rodríguez; +Cc: util-linux

On Wed, Dec 26, 2012 at 02:38:21PM -0300, Cristian Rodríguez wrote:
> There is no need to reinvent the wheel.

 Well, htoXX function were added to glibc in version 2.9 (2009?), our
 bitops.h is probably older.

 Anyway, you're right that use libc implementation is good idea. I
 have merged both patches and applied with some changes.
 
 Please, use Signed-off-by: next time.

 Thanks!

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-01-08 13:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-26 17:38 [PATCH] include/bitops.h: Use the operating system byteswapping functions Cristian Rodríguez
2012-12-26 18:48 ` Mike Frysinger
2012-12-26 19:01   ` Cristian Rodríguez
2013-01-08 13:17 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox