public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
@ 2024-02-14 15:54 Michal Wajdeczko
  2024-02-14 16:04 ` Andy Shevchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Michal Wajdeczko @ 2024-02-14 15:54 UTC (permalink / raw)
  To: linux-kernel; +Cc: Michal Wajdeczko, Andy Shevchenko, Rodrigo Vivi, Jani Nikula

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

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Jani Nikula <jani.nikula@intel.com>
---
 include/linux/make_type.h | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 include/linux/make_type.h

diff --git a/include/linux/make_type.h b/include/linux/make_type.h
new file mode 100644
index 000000000000..60e2e091ea3c
--- /dev/null
+++ b/include/linux/make_type.h
@@ -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
-- 
2.43.0


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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 15:54 Michal Wajdeczko
@ 2024-02-14 16:04 ` Andy Shevchenko
  2024-02-14 17:02   ` Michal Wajdeczko
  0 siblings, 1 reply; 10+ messages in thread
From: Andy Shevchenko @ 2024-02-14 16:04 UTC (permalink / raw)
  To: Michal Wajdeczko; +Cc: linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 04:54:08PM +0100, Michal Wajdeczko wrote:
> It is quite common practice to make u16, u32 or u64 values from
> smaller words.  Add simple helpers for that.

...

> +++ 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).


-- 
With Best Regards,
Andy Shevchenko



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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 16:04 ` Andy Shevchenko
@ 2024-02-14 17:02   ` Michal Wajdeczko
  2024-02-14 17:15     ` Andy Shevchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Michal Wajdeczko @ 2024-02-14 17:02 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-kernel, Rodrigo Vivi, Jani Nikula



On 14.02.2024 17:04, Andy Shevchenko wrote:
> On Wed, Feb 14, 2024 at 04:54:08PM +0100, Michal Wajdeczko wrote:
>> It is quite common practice to make u16, u32 or u64 values from
>> smaller words.  Add simple helpers for that.
> 
> ...
> 
>> +++ 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 ?

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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:02   ` Michal Wajdeczko
@ 2024-02-14 17:15     ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-02-14 17:15 UTC (permalink / raw)
  To: Michal Wajdeczko; +Cc: linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 06:02:01PM +0100, Michal Wajdeczko wrote:
> On 14.02.2024 17:04, Andy Shevchenko wrote:
> > On Wed, Feb 14, 2024 at 04:54:08PM +0100, Michal Wajdeczko wrote:

...

> >> +++ 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?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
@ 2024-02-14 17:20 Alexey Dobriyan
  2024-02-14 17:32 ` Andy Shevchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Alexey Dobriyan @ 2024-02-14 17:20 UTC (permalink / raw)
  To: Michal Wajdeczko; +Cc: linux-kernel, Andy Shevchenko, Rodrigo Vivi, Jani Nikula

> +#define make_u16(hi__, lo__)  ((u16)(u8)(hi__) << 8 | (u8)(lo__))

	Public Service Announcement

Identifiers representing macro arguments generally don't need to be
undescored. They are local to the macro, they don't leak outside.

	End of Public Service Announcement



Firstly, make_u16() doesn't return u16.

Secondly,

	#define make_u64(hi__, lo__)  ((u64)(hi__) << 32 | (u32)(lo__))

doesn't truncate hi, why?

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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:20 [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values Alexey Dobriyan
@ 2024-02-14 17:32 ` Andy Shevchenko
  2024-02-14 17:39   ` Alexey Dobriyan
  0 siblings, 1 reply; 10+ messages in thread
From: Andy Shevchenko @ 2024-02-14 17:32 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Michal Wajdeczko, linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 08:20:55PM +0300, Alexey Dobriyan wrote:

...

> Secondly,
> 
> 	#define make_u64(hi__, lo__)  ((u64)(hi__) << 32 | (u32)(lo__))
> 
> doesn't truncate hi, why?

Because it's not needed (the whole point AFAIU is to override promotion
to a _signed_ type (int) and here it makes no difference)?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:32 ` Andy Shevchenko
@ 2024-02-14 17:39   ` Alexey Dobriyan
  2024-02-14 17:42     ` Andy Shevchenko
  2024-02-14 17:55     ` Michal Wajdeczko
  0 siblings, 2 replies; 10+ messages in thread
From: Alexey Dobriyan @ 2024-02-14 17:39 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Michal Wajdeczko, linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 07:32:10PM +0200, Andy Shevchenko wrote:
> On Wed, Feb 14, 2024 at 08:20:55PM +0300, Alexey Dobriyan wrote:
> 
> ...
> 
> > Secondly,
> > 
> > 	#define make_u64(hi__, lo__)  ((u64)(hi__) << 32 | (u32)(lo__))
> > 
> > doesn't truncate hi, why?
> 
> Because it's not needed (the whole point AFAIU is to override promotion
> to a _signed_ type (int) and here it makes no difference)?

Well,

	make_u64((u64)-1, 0)

will return (u64)-1 unlike

	make_u16((u64)-1, 0)

which will return 0xff00.
BTW, I'm for truncation, but it should be done everywhere.

Thirdly, there were no users posted.

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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:39   ` Alexey Dobriyan
@ 2024-02-14 17:42     ` Andy Shevchenko
  2024-02-14 17:55     ` Michal Wajdeczko
  1 sibling, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-02-14 17:42 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Michal Wajdeczko, linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 08:39:35PM +0300, Alexey Dobriyan wrote:
> On Wed, Feb 14, 2024 at 07:32:10PM +0200, Andy Shevchenko wrote:
> > On Wed, Feb 14, 2024 at 08:20:55PM +0300, Alexey Dobriyan wrote:

...

> > > Secondly,
> > > 
> > > 	#define make_u64(hi__, lo__)  ((u64)(hi__) << 32 | (u32)(lo__))
> > > 
> > > doesn't truncate hi, why?
> > 
> > Because it's not needed (the whole point AFAIU is to override promotion
> > to a _signed_ type (int) and here it makes no difference)?
> 
> Well,
> 
> 	make_u64((u64)-1, 0)
> 
> will return (u64)-1 unlike

How? It's shifted by 32 bits. Am I missing something?

> BTW, I'm for truncation, but it should be done everywhere.

I'm not against this.

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:39   ` Alexey Dobriyan
  2024-02-14 17:42     ` Andy Shevchenko
@ 2024-02-14 17:55     ` Michal Wajdeczko
  2024-02-16  6:23       ` Alexey Dobriyan
  1 sibling, 1 reply; 10+ messages in thread
From: Michal Wajdeczko @ 2024-02-14 17:55 UTC (permalink / raw)
  To: Alexey Dobriyan, Andy Shevchenko; +Cc: linux-kernel, Rodrigo Vivi, Jani Nikula



On 14.02.2024 18:39, Alexey Dobriyan wrote:
> On Wed, Feb 14, 2024 at 07:32:10PM +0200, Andy Shevchenko wrote:
>> On Wed, Feb 14, 2024 at 08:20:55PM +0300, Alexey Dobriyan wrote:

...

> 
> Thirdly, there were no users posted.

for make_u64() there is already one at [1]
and Jani pointed other potential candidates [2]


[1]
https://elixir.bootlin.com/linux/v6.8-rc4/source/drivers/gpu/drm/xe/xe_gt_pagefault.c#L555

[2]
https://lore.kernel.org/intel-xe/87cysz6ud7.fsf@intel.com/T/#md6577722f9226258c8eb99119707e12db4dd3b79

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

* Re: [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values
  2024-02-14 17:55     ` Michal Wajdeczko
@ 2024-02-16  6:23       ` Alexey Dobriyan
  0 siblings, 0 replies; 10+ messages in thread
From: Alexey Dobriyan @ 2024-02-16  6:23 UTC (permalink / raw)
  To: Michal Wajdeczko; +Cc: Andy Shevchenko, linux-kernel, Rodrigo Vivi, Jani Nikula

On Wed, Feb 14, 2024 at 06:55:38PM +0100, Michal Wajdeczko wrote:
> 
> 
> On 14.02.2024 18:39, Alexey Dobriyan wrote:
> > On Wed, Feb 14, 2024 at 07:32:10PM +0200, Andy Shevchenko wrote:
> >> On Wed, Feb 14, 2024 at 08:20:55PM +0300, Alexey Dobriyan wrote:
> 
> ...
> 
> > 
> > Thirdly, there were no users posted.
> 
> for make_u64() there is already one at [1]
> [1]
> https://elixir.bootlin.com/linux/v6.8-rc4/source/drivers/gpu/drm/xe/xe_gt_pagefault.c#L555

OK, this one doesn't truncate too.

Honestly wordpath.h _sucks_ as a name.

I'd go with stdint.h, at least this name is known from userspace.

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

end of thread, other threads:[~2024-02-16  6:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-14 17:20 [RFC] include/linux/make_type.h: Helpers for making u16/u32/u64 values Alexey Dobriyan
2024-02-14 17:32 ` Andy Shevchenko
2024-02-14 17:39   ` Alexey Dobriyan
2024-02-14 17:42     ` Andy Shevchenko
2024-02-14 17:55     ` Michal Wajdeczko
2024-02-16  6:23       ` Alexey Dobriyan
  -- strict thread matches above, loose matches on Subject: below --
2024-02-14 15:54 Michal Wajdeczko
2024-02-14 16:04 ` Andy Shevchenko
2024-02-14 17:02   ` Michal Wajdeczko
2024-02-14 17:15     ` Andy Shevchenko

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