Skip to contentSkip to conversation

include/linux/make_type.h: Helpers for making u16/u32/u64 values

RFC In review Michal Wajdeczko 2 participants 4 messages Feb 14, 2024

lore
Share & tools
lore
#0 lore raw

RFC 1/1 include/linux/make_type.h: Helpers for making u16/u32/u64 values

It is quite common practice to make u16, u32 or u64 values from smaller words. Add simple helpers for that.

Cc Andy Shevchenko <[email protected]>
Cc Rodrigo Vivi <[email protected]>
Cc Jani Nikula <[email protected]>
---
 include/linux/make_type.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 include/linux/make_type.h
include/linux/make_type.h new file +29 -0
new file mode 100644
index 000000000000..60e2e091ea3c
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _LINUX_MAKE_TYPE_H
+#define _LINUX_MAKE_TYPE_H
+
+#include <linux/types.h>
+
+/**
+ * make_u16 - make u16 value from two u8 values
+ * @hi__: value representing upper 8 bits
+ * @lo__: value representing lower 8 bits
+ */
+#define make_u16(hi__, lo__)  ((u16)(u8)(hi__) << 8 | (u8)(lo__))
+
+/**
+ * make_u32 - make u32 value from two u16 values
+ * @hi__: value representing upper 16 bits
+ * @lo__: value representing lower 16 bits
+ */
+#define make_u32(hi__, lo__)  ((u32)(u16)(hi__) << 16 | (u16)(lo__))
+
+/**
+ * make_u64 - make u64 value from two u32 values
+ * @hi__: value representing upper 32 bits
+ * @lo__: value representing lower 32 bits
+ */
+#define make_u64(hi__, lo__)  ((u64)(hi__) << 32 | (u32)(lo__))
+
+#endif
Signature · 1 line
2.43.0
#1 lore raw
Quoted 2 lines · Michal Wajdeczko

It is quite common practice to make u16, u32 or u64 values from smaller words. Add simple helpers for that.

...

Quoted 1 line

+++ b/include/linux/make_type.h

If we go with this, please make better name so we can combine this with upper/lower_*_bits() helpers which seems related semantically to this.

Also _type suffix is a bit confusing as we use _types in the headers that provide just data type(s).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

It is quite common practice to make u16, u32 or u64 values from smaller words. Add simple helpers for that.

...

Quoted 1 line

+++ b/include/linux/make_type.h

If we go with this, please make better name so we can combine this with upper/lower_*_bits() helpers which seems related semantically to this.

what about "include/linux/uintops.h" like we have bitops.h ?

#3 lore raw
Quoted 2 lines · Michal Wajdeczko
Quoted 1 line · Andy Shevchenko

On Wed, Feb 14, 2024 at 04:54:08PM +0100, Michal Wajdeczko wrote:

...

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+++ b/include/linux/make_type.h

If we go with this, please make better name so we can combine this with upper/lower_*_bits() helpers which seems related semantically to this.

what about "include/linux/uintops.h" like we have bitops.h ?

We have wordpart.h, would it work?

Signature · 2 lines
With Best Regards,
Andy Shevchenko