* [QUESTION] Usage of negative number notations on unsigned numbers.
@ 2023-11-24 9:36 Karthik B K
2023-11-24 11:08 ` Andreas Schwab
0 siblings, 1 reply; 8+ messages in thread
From: Karthik B K @ 2023-11-24 9:36 UTC (permalink / raw)
To: opensbi
Hi. The pmp_get function in lib/sbi/riscv_asm.c uses a negative number notation on an unsigned integer.
pmpaddr = -1UL;
Just curious why it is this way. I believe that the intention here is to represent the largest unsigned long number.
If so, isn't it safer to use (~0UL) ? This could be a very silent bug. Please correct me if I'm wrong.
Best,
karthik.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-24 9:36 [QUESTION] Usage of negative number notations on unsigned numbers Karthik B K
@ 2023-11-24 11:08 ` Andreas Schwab
2023-11-27 11:59 ` Karthik B K
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2023-11-24 11:08 UTC (permalink / raw)
To: opensbi
On Nov 24 2023, Karthik B K wrote:
> If so, isn't it safer to use (~0UL) ? This could be a very silent bug. Please correct me if I'm wrong.
The safest way is to write -1 without any suffix. This is guaranteed to
evaluate to the maximum value when converted to any unsigned type.
--
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-24 11:08 ` Andreas Schwab
@ 2023-11-27 11:59 ` Karthik B K
2023-11-27 12:09 ` Andreas Schwab
0 siblings, 1 reply; 8+ messages in thread
From: Karthik B K @ 2023-11-27 11:59 UTC (permalink / raw)
To: opensbi
From:?Andreas Schwab <schwab@linux-m68k.org>
Sent:?Friday, November 24, 2023 4:38 PM
To:?Karthik B K <karthik.bk@incoresemi.com>
Cc:?opensbi at lists.infradead.org <opensbi@lists.infradead.org>
Subject:?Re: [QUESTION] Usage of negative number notations on unsigned numbers.
?
On Nov 24 2023, Karthik B K wrote:
> If so, isn't it safer to use (~0UL) ? This could be a very silent bug. Please correct me if I'm wrong.
The safest way is to write -1 without any suffix.? This is guaranteed to
evaluate to the maximum value when converted to any unsigned type.
This is with the assumption that the encoding for -1 == 2^32-1.
I'm still trying to understand what arithmetic negation mean to an unsigned quantity and its representation in UL.
While 0 has a defined encoding in a UL type, -1 does not!
karthik.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-27 11:59 ` Karthik B K
@ 2023-11-27 12:09 ` Andreas Schwab
2023-11-27 12:55 ` Karthik B K
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2023-11-27 12:09 UTC (permalink / raw)
To: opensbi
On Nov 27 2023, Karthik B K wrote:
> This is with the assumption that the encoding for -1 == 2^32-1.
No, the representation has nothing to do with that. It's a value
conversion.
--
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-27 12:09 ` Andreas Schwab
@ 2023-11-27 12:55 ` Karthik B K
2023-11-27 13:46 ` Andreas Schwab
0 siblings, 1 reply; 8+ messages in thread
From: Karthik B K @ 2023-11-27 12:55 UTC (permalink / raw)
To: opensbi
On 27/11/23 17:39, Andreas Schwab wrote:
> On Nov 27 2023, Karthik B K wrote:
>
>> This is with the assumption that the encoding for -1 == 2^32-1.
> No, the representation has nothing to do with that. It's a value
> conversion.
What's the encoding of (2^32-1)UL and (2^64-1)UL ?
karthik.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-27 12:55 ` Karthik B K
@ 2023-11-27 13:46 ` Andreas Schwab
2023-11-28 5:28 ` Karthik B K
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Schwab @ 2023-11-27 13:46 UTC (permalink / raw)
To: opensbi
On Nov 27 2023, Karthik B K wrote:
> What's the encoding of (2^32-1)UL and (2^64-1)UL ?
A value does not have an encoding. Only a memory location does.
--
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-27 13:46 ` Andreas Schwab
@ 2023-11-28 5:28 ` Karthik B K
2023-11-28 6:01 ` Jessica Clarke
0 siblings, 1 reply; 8+ messages in thread
From: Karthik B K @ 2023-11-28 5:28 UTC (permalink / raw)
To: opensbi
On 27/11/23 19:16, Andreas Schwab wrote:
> On Nov 27 2023, Karthik B K wrote:
>
>> What's the encoding of (2^32-1)UL and (2^64-1)UL ?
> A value does not have an encoding. Only a memory location does.
>
So does a register (container of bits), and a type (signed, unsigned,
float).
The "encoding" is dependent on the "container" (reg or mem) and "type"
(signed, unsigned, float).
The same bits (11...11) would reflect very different values (-1, 2^32-1,
quiteNaN).
The container of (2^32-1) is UInt. 32b unsigned integer. -1 cannot be
represented in a 32b unsigned int.
I'm trying to say, that the unary operator (-) in the expression (-1UL)
is an integer arithmetic operator.
The unary operator (-) when applied to an unsigned type, results in the
_negative_ of it's (promoted as per _usual arithmetic conversions_)
operand, which in this case, is a signed integer.
The result will retain this type. Are we assuming an implicit cast ?
This looks like an illegal set of operations to create the bit pattern
we desire (11...11) for an unsigned int.
Simply put, a -1 in a UL is an oxymoron. Don't you think ?
Please correct me if I'm wrong.
karthik.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature.asc
Type: application/pgp-signature
Size: 665 bytes
Desc: OpenPGP digital signature
URL: <http://lists.infradead.org/pipermail/opensbi/attachments/20231128/41bd0218/attachment.sig>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [QUESTION] Usage of negative number notations on unsigned numbers.
2023-11-28 5:28 ` Karthik B K
@ 2023-11-28 6:01 ` Jessica Clarke
0 siblings, 0 replies; 8+ messages in thread
From: Jessica Clarke @ 2023-11-28 6:01 UTC (permalink / raw)
To: opensbi
On 28 Nov 2023, at 05:28, Karthik B K <karthik.bk@incoresemi.com> wrote:
>
> On 27/11/23 19:16, Andreas Schwab wrote:
>> On Nov 27 2023, Karthik B K wrote:
>>
>>> What's the encoding of (2^32-1)UL and (2^64-1)UL ?
>> A value does not have an encoding. Only a memory location does.
>>
> So does a register (container of bits), and a type (signed, unsigned, float).
>
> The "encoding" is dependent on the "container" (reg or mem) and "type" (signed, unsigned, float).
>
> The same bits (11...11) would reflect very different values (-1, 2^32-1, quiteNaN).
>
> The container of (2^32-1) is UInt. 32b unsigned integer. -1 cannot be represented in a 32b unsigned int.
>
>
> I'm trying to say, that the unary operator (-) in the expression (-1UL) is an integer arithmetic operator.
>
> The unary operator (-) when applied to an unsigned type, results in the _negative_ of it's (promoted as per _usual arithmetic conversions_) operand, which in this case, is a signed integer.
>
> The result will retain this type. Are we assuming an implicit cast ?
>
> This looks like an illegal set of operations to create the bit pattern we desire (11...11) for an unsigned int.
>
>
> Simply put, a -1 in a UL is an oxymoron. Don't you think ?
>
> Please correct me if I'm wrong.
C99 6.2.5p9:
"The range of nonnegative values of a signed integer type is a subrange
of the corresponding unsigned integer type, and the representation of
the same value in each type is the same.31) A computation involving
unsigned operands can never overflow, because a result that cannot be
represented by the resulting unsigned integer type is reduced modulo
the number that is one greater than the largest value that can be
represented by the resulting type.?
-1UL (parsed as -(1UL); integer constants start with a digit in the
grammar, the - (or leading +) is always a normal unary operator) is
thus well-defined.
If you want to learn C, I?d rather not see it happen on this list.
Jess
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2023-11-28 6:01 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-24 9:36 [QUESTION] Usage of negative number notations on unsigned numbers Karthik B K
2023-11-24 11:08 ` Andreas Schwab
2023-11-27 11:59 ` Karthik B K
2023-11-27 12:09 ` Andreas Schwab
2023-11-27 12:55 ` Karthik B K
2023-11-27 13:46 ` Andreas Schwab
2023-11-28 5:28 ` Karthik B K
2023-11-28 6:01 ` Jessica Clarke
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.