mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Zhangjin Wu' <falcon@tinylab.org>, "w@1wt.eu" <w@1wt.eu>
Cc: "arnd@arndb.de" <arnd@arndb.de>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>,
	"thomas@t-8ch.de" <thomas@t-8ch.de>
Subject: RE: [PATCH v3] tools/nolibc: fix up size inflate regression
Date: Mon, 7 Aug 2023 08:40:27 +0000	[thread overview]
Message-ID: <f51e54bcf470451ea36f24640f000e61@AcuMS.aculab.com> (raw)
In-Reply-To: <8eaab5da2dcbba42e3f3efc2ae686a22c95f84f0.1691386601.git.falcon@tinylab.org>

From: Zhangjin Wu
> Sent: 07 August 2023 06:58
...
> +/* __auto_type is used instead of __typeof__ to workaround the build error
> + * 'error: assignment of read-only variable' when the argument has 'const' in
> + * the type, but __auto_type is a new feature from newer gcc version and it
> + * only works with 'const' from gcc 11.0 (__GXX_ABI_VERSION = 1016)
> + * https://gcc.gnu.org/legacy-ml/gcc-patches/2013-11/msg01378.html
> + */

You can use typeof((x) + 0) to lose the 'const' flag.
The only downside is that char/short become int.

> +
> +#if __GXX_ABI_VERSION >= 1016
> +#define __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT
> +#endif
> +
> +#ifdef __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT
> +#define __sysret(arg)                                                    \
> +({                                                                       \
> +	__auto_type __ret = (arg);                                       \
> +	if (__is_signed_type(__typeof__(arg))) {                         \
> +		if (__ret < 0) {                                         \
> +			SET_ERRNO(-(long)__ret);                         \
> +			__ret = (__typeof__(arg))(-1L);                  \
> +		}                                                        \
> +	} else {                                                         \
> +		if ((unsigned long)__ret >= (unsigned long)-MAX_ERRNO) { \
> +			SET_ERRNO(-(long)__ret);                         \
> +			__ret = (__typeof__(arg))(-1L);                  \
> +		}                                                        \
> +	}                                                                \
> +	__ret;                                                           \
> +})
> +
> +#else  /* ! __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT */
> +#define __sysret(arg)                                                    \
> +({                                                                       \
> +	long __ret = (long)(arg);                                        \
> +	if (__is_signed_type(__typeof__(arg))) {                         \
> +		if (__ret < 0) {                                         \
> +			SET_ERRNO(-__ret);                               \
> +			__ret = -1L;                                     \
> +		}                                                        \
> +	} else {                                                         \
> +		if ((unsigned long)__ret >= (unsigned long)-MAX_ERRNO) { \
> +			SET_ERRNO(-__ret);                               \
> +			__ret = -1L;                                     \
> +		}                                                        \
> +	}                                                                \
> +	(__typeof__(arg))__ret;                                          \
> +})
> +#endif /* ! __GXX_HAS_AUTO_TYPE_WITH_CONST_SUPPORT */

with (retyped so it may be wrong):
#define is_constexpr(x) sizeof(*(0 ? (void *)((long)(x) * 0) : (int *)0)) == 1)
and (because even (void *)0 isn't completely constant):
#define is_pointer(x) (!is_constexpr((typeof(x))0))

You can probably do:
#define __sysret(arg) \
({ \
	typeof((arg) + 0) __ret = arg; \
	if (__built_choose_expr(is_pointer(arg), (unsigned long)-(MAX_ERRNO+1), __ret) \
			< (__built_choose_expr(is_pointer(arg), (unsigned long)__ret, 0)) { \
		SET_ERRNO(-__ret); \
		__reg = typeof(ret)-1L; \
	} \
	__ret; \
})

Apart from the annoyance of having to reverse the conditional
that only has one copy of the check.

Using two __builtin_choose_expr() saves you having to write two
comparisons that are valid for both pointer and integer.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  reply	other threads:[~2023-08-07  8:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07  5:57 Zhangjin Wu
2023-08-07  8:40 ` David Laight [this message]
2023-08-07  9:53   ` David Laight
2023-08-07 14:12   ` Zhangjin Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f51e54bcf470451ea36f24640f000e61@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=arnd@arndb.de \
    --cc=falcon@tinylab.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=thomas@t-8ch.de \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®