Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/23 16:50, David Laight wrote: From: 15330273...@189.cn <15330273...@189.cn> Sent: 23 May 2023 05:27 On 2023/5/22 19:29, Jani Nikula wrote: In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. Here I'm not sure about this, but there are plenty unsigned types arithmetic in the kernel. The real problem is (attempted) arithmetic on types smaller than int. Regardless of whether they are signed or unsigned. It is about sign extend. Yes, you may be right. I might create a wrong patch. But this will not happen in practice, because in general case: mode->crtc_htotal < 0x8fff; mode->crtc_vtotal < 0x8fff; u16 gets promoted to 'signed int' not 'unsigned int'. Sorry :/ David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
From: 15330273...@189.cn <15330273...@189.cn> > Sent: 23 May 2023 05:27 > > On 2023/5/22 19:29, Jani Nikula wrote: > > In general, do not use unsigned types in arithmethic to avoid negative > > values, because most people will be tripped over by integer promotion > > rules, and you'll get negative values anyway. > > > Here I'm sure about this, > > but there are plenty unsigned types arithmetic in the kernel. The real problem is (attempted) arithmetic on types smaller than int. Regardless of whether they are signed or unsigned. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/23 12:26, Sui Jingfeng wrote: Hi, On 2023/5/22 19:29, Jani Nikula wrote: In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. Here I'm sure about this, Here, I'm NOT sure about this but there are plenty unsigned types arithmetic in the kernel. take kmalloc_array() function as an example in /tools/virto/linux/kernel.h static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp) { return kmalloc(n * s, gfp); } NOTE that *size_t* is an unsigned integral data type.
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/22 19:29, Jani Nikula wrote: In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. Here I'm sure about this, but there are plenty unsigned types arithmetic in the kernel. take kmalloc_array() function as an example in /tools/virto/linux/kernel.h static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp) { return kmalloc(n * s, gfp); } NOTE that *size_t* is an unsigned integral data type.
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/22 23:01, Jani Nikula wrote: On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: Hi, On 2023/5/22 20:13, Jani Nikula wrote: On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: Hi, On 2023/5/22 19:29, Jani Nikula wrote: On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: On 2023/5/17 18:59, David Laight wrote: From: 15330273...@189.cn Sent: 16 May 2023 18:30 From: Sui Jingfeng Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. I believe that signed or unsigned is dependent on the declaration. I am talk about the math, while you are talking about compiler. I admit that u16 gets promoted to 'signed int' is true, but this is irrelevant, the point is how to understand the returned value. How does the compiler generate the code is one thing, how do we interpret the result is another How does the compiler generate the code is NOT determined by us, while how do we interpret the result is determined by us. I believe that using a u32 type to interpret the result(u16 * u16) is always true, it is true in the perspective of *math*. Integer promotions is the details of C program language. If the result of the multiply is signed, then there are risks that the result is negative, what's the benefit to present this risk to the programmer? What's the benefit to tell me(and others) that u16 * u16 yield a signed value? and can be negative? Using int type as the return type bring concerns to the programmer and the user of the function, even though this is not impossible in practice. In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. I'll bet most people will be surprised to see what this prints: #include #include int main(void) { uint16_t x = 0x; uint16_t y = 0x; uint64_t z = x * y; printf("0x%016lx\n", z); printf("%ld\n", z); Here, please replace the "%ld\n" with the "%lu\n", then you will see the difference. you are casting the variable 'z' to signed value, "%d" is for printing signed value, and "%u" is for printing unsigned value. Your simple code explained exactly why you are still in confusion, Am I? Take a look at the values, and explain the math. I meant the value itself is represent with 2's compliment, when you print a value with '%ld', then you will get the signed version, when you print a value with '%lu', then you will get the unsigned version. The result of a u16*u16 couldn't be negative in math. But when you using a '%ld' or '%d' to print a unsigned value, then is wrong. This is also the case which you shouldn't using a int type to store the result of u16*u16. because when I seen a int type, I will choose '%d' to print it, when I seen a unsigned int type, I will choose '%u' to print it. when using a int type as the return type, this could lead people to using '%d' to print such a value. Then, it generate the confusion as this little test program shows. Using 0x%016lx and %lu results in 0xfffe0001 and 18446744073709420545, respectively. They are equal. They are indeed not negative. However 0x * 0x = 0xfffe0001. Or 4294836225 in decimal. No matter what the math says, this is what actually happens in C. I don't know what more I could possibly tell you. Sorry, I realized something after rethink about it. Can we first assign the value to u32 first, then expend it to 64 bit then? Extend it to 64 bit from 32 bit explicitly, this enforce zero extend instead of sign extend. BR, Jani. BR, Jani. that is u16 * u16 can yield a negative value if you use the int as the return type. Because it overflowed. printf("%d\n", x * y); } And it's not that different from what you have below. Your patch doesn't change anything, and doesn't make it any less confusing. BR, Jani. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. Sorry about my expression, I think my sentence did not mention anything about 'because the original type is signed'. In the contrary, my patch eliminated the concerns to the reviewer. It say that the results of the multiply can't be negative. My intent is to tell the compiler we w
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: > Hi, > > On 2023/5/22 20:13, Jani Nikula wrote: >> On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: >>> Hi, >>> >>> On 2023/5/22 19:29, Jani Nikula wrote: On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: > On 2023/5/17 18:59, David Laight wrote: >> From: 15330273...@189.cn >>> Sent: 16 May 2023 18:30 >>> >>> From: Sui Jingfeng >>> >>> Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, >>> mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. >> Nope, u16 gets promoted to 'signed int' and the result of the >> multiply is also signed. > I believe that signed or unsigned is dependent on the declaration. > > I am talk about the math, while you are talking about compiler. > > I admit that u16 gets promoted to 'signed int' is true, but this is > irrelevant, > > the point is how to understand the returned value. > > > How does the compiler generate the code is one thing, how do we > interpret the result is another > > How does the compiler generate the code is NOT determined by us, while > how do we interpret the result is determined by us. > > > I believe that using a u32 type to interpret the result(u16 * u16) is > always true, it is true in the perspective of *math*. > > Integer promotions is the details of C program language. If the result > of the multiply is signed, then there are risks that > > the result is negative, what's the benefit to present this risk to the > programmer? > > What's the benefit to tell me(and others) that u16 * u16 yield a signed > value? and can be negative? > > Using int type as the return type bring concerns to the programmer and > the user of the function, > > even though this is not impossible in practice. In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. I'll bet most people will be surprised to see what this prints: #include #include int main(void) { uint16_t x = 0x; uint16_t y = 0x; uint64_t z = x * y; printf("0x%016lx\n", z); printf("%ld\n", z); >>> Here, please replace the "%ld\n" with the "%lu\n", then you will see the >>> difference. >>> >>> you are casting the variable 'z' to signed value, "%d" is for printing >>> signed value, and "%u" is for printing unsigned value. >>> >>> >>> Your simple code explained exactly why you are still in confusion, >> Am I? >> >> Take a look at the values, and explain the math. > > I meant the value itself is represent with 2's compliment, > > when you print a value with '%ld', then you will get the signed version, > > when you print a value with '%lu', then you will get the unsigned version. > > The result of a u16*u16 couldn't be negative in math. > > > But when you using a '%ld' or '%d' to print a unsigned value, then is wrong. > > This is also the case which you shouldn't using a int type to store the > result of u16*u16. > > because when I seen a int type, I will choose '%d' to print it, > > when I seen a unsigned int type, I will choose '%u' to print it. > > when using a int type as the return type, this could lead people to using > '%d' to print > > such a value. Then, it generate the confusion as this little test program > shows. Using 0x%016lx and %lu results in 0xfffe0001 and 18446744073709420545, respectively. They are equal. They are indeed not negative. However 0x * 0x = 0xfffe0001. Or 4294836225 in decimal. No matter what the math says, this is what actually happens in C. I don't know what more I could possibly tell you. BR, Jani. > >> >> BR, >> Jani. >> >>> that is u16 * u16 can yield a negative value if you use the int as the >>> return type. Because it overflowed. >>> printf("%d\n", x * y); } And it's not that different from what you have below. Your patch doesn't change anything, and doesn't make it any less confusing. BR, Jani. >>> Using a u32 is enough to store the result, but considering that the >>> result will be casted to u64 soon after. We use a u64 type directly. >>> So there no need to cast it to signed type and cast back then. >> >>> - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; >>> + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; >> ... >>> - framedur_ns = div_u64((u64) frame_size * 100, >>> dotclock); >>> + framedur_ns = div_u64(frame_size * 100, dotclock); >> The (u64) cast is there to extend the value to 64bits, not >> because the original type is signed. > So
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/22 20:13, Jani Nikula wrote: On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: Hi, On 2023/5/22 19:29, Jani Nikula wrote: On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: On 2023/5/17 18:59, David Laight wrote: From: 15330273...@189.cn Sent: 16 May 2023 18:30 From: Sui Jingfeng Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. I believe that signed or unsigned is dependent on the declaration. I am talk about the math, while you are talking about compiler. I admit that u16 gets promoted to 'signed int' is true, but this is irrelevant, the point is how to understand the returned value. How does the compiler generate the code is one thing, how do we interpret the result is another How does the compiler generate the code is NOT determined by us, while how do we interpret the result is determined by us. I believe that using a u32 type to interpret the result(u16 * u16) is always true, it is true in the perspective of *math*. Integer promotions is the details of C program language. If the result of the multiply is signed, then there are risks that the result is negative, what's the benefit to present this risk to the programmer? What's the benefit to tell me(and others) that u16 * u16 yield a signed value? and can be negative? Using int type as the return type bring concerns to the programmer and the user of the function, even though this is not impossible in practice. In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. I'll bet most people will be surprised to see what this prints: #include #include int main(void) { uint16_t x = 0x; uint16_t y = 0x; uint64_t z = x * y; printf("0x%016lx\n", z); printf("%ld\n", z); Here, please replace the "%ld\n" with the "%lu\n", then you will see the difference. you are casting the variable 'z' to signed value, "%d" is for printing signed value, and "%u" is for printing unsigned value. Your simple code explained exactly why you are still in confusion, Am I? Take a look at the values, and explain the math. I meant the value itself is represent with 2's compliment, when you print a value with '%ld', then you will get the signed version, when you print a value with '%lu', then you will get the unsigned version. The result of a u16*u16 couldn't be negative in math. But when you using a '%ld' or '%d' to print a unsigned value, then is wrong. This is also the case which you shouldn't using a int type to store the result of u16*u16. because when I seen a int type, I will choose '%d' to print it, when I seen a unsigned int type, I will choose '%u' to print it. when using a int type as the return type, this could lead people to using '%d' to print such a value. Then, it generate the confusion as this little test program shows. BR, Jani. that is u16 * u16 can yield a negative value if you use the int as the return type. Because it overflowed. printf("%d\n", x * y); } And it's not that different from what you have below. Your patch doesn't change anything, and doesn't make it any less confusing. BR, Jani. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. Sorry about my expression, I think my sentence did not mention anything about 'because the original type is signed'. In the contrary, my patch eliminated the concerns to the reviewer. It say that the results of the multiply can't be negative. My intent is to tell the compiler we want a unsigned return type, but GCC emit 'imul' instruction for the multiply.. I'm using u64 as the return type, because div_u64() function accept a u64 type value as its first argument. The compiler will detect that the old code is a 32x32 multiply where a 64bit result is needed, that may not be true for the changed code (it would need to track back as far as the u16s). I don't believe my code could be wrong. when you use the word 'may', you are saying that it could be wrong after apply my patch. Then you have to find at least one test example to prove you point, in which case my codes generate wrong results. Again I don't believe you could find one.
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
On Mon, 22 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: > Hi, > > On 2023/5/22 19:29, Jani Nikula wrote: >> On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: >>> On 2023/5/17 18:59, David Laight wrote: From: 15330273...@189.cn > Sent: 16 May 2023 18:30 > > From: Sui Jingfeng > > Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, > mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. >>> I believe that signed or unsigned is dependent on the declaration. >>> >>> I am talk about the math, while you are talking about compiler. >>> >>> I admit that u16 gets promoted to 'signed int' is true, but this is >>> irrelevant, >>> >>> the point is how to understand the returned value. >>> >>> >>> How does the compiler generate the code is one thing, how do we >>> interpret the result is another >>> >>> How does the compiler generate the code is NOT determined by us, while >>> how do we interpret the result is determined by us. >>> >>> >>> I believe that using a u32 type to interpret the result(u16 * u16) is >>> always true, it is true in the perspective of *math*. >>> >>> Integer promotions is the details of C program language. If the result >>> of the multiply is signed, then there are risks that >>> >>> the result is negative, what's the benefit to present this risk to the >>> programmer? >>> >>> What's the benefit to tell me(and others) that u16 * u16 yield a signed >>> value? and can be negative? >>> >>> Using int type as the return type bring concerns to the programmer and >>> the user of the function, >>> >>> even though this is not impossible in practice. >> In general, do not use unsigned types in arithmethic to avoid negative >> values, because most people will be tripped over by integer promotion >> rules, and you'll get negative values anyway. >> >> I'll bet most people will be surprised to see what this prints: >> >> #include >> #include >> >> int main(void) >> { >> uint16_t x = 0x; >> uint16_t y = 0x; >> uint64_t z = x * y; >> >> printf("0x%016lx\n", z); >> printf("%ld\n", z); > > Here, please replace the "%ld\n" with the "%lu\n", then you will see the > difference. > > you are casting the variable 'z' to signed value, "%d" is for printing > signed value, and "%u" is for printing unsigned value. > > > Your simple code explained exactly why you are still in confusion, Am I? Take a look at the values, and explain the math. BR, Jani. > > that is u16 * u16 can yield a negative value if you use the int as the > return type. Because it overflowed. > >> printf("%d\n", x * y); >> } >> >> And it's not that different from what you have below. Your patch doesn't >> change anything, and doesn't make it any less confusing. >> >> BR, >> Jani. >> >> > Using a u32 is enough to store the result, but considering that the > result will be casted to u64 soon after. We use a u64 type directly. > So there no need to cast it to signed type and cast back then. > - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; > + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... > - framedur_ns = div_u64((u64) frame_size * 100, dotclock); > + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. >>> Sorry about my expression, I think my sentence did not mention anything >>> about 'because the original type is signed'. >>> >>> In the contrary, my patch eliminated the concerns to the reviewer. It >>> say that the results of the multiply can't be negative. >>> >>> My intent is to tell the compiler we want a unsigned return type, but >>> GCC emit 'imul' instruction for the multiply.. >>> >>> I'm using u64 as the return type, because div_u64() function accept a >>> u64 type value as its first argument. >>> The compiler will detect that the old code is a 32x32 multiply where a 64bit result is needed, that may not be true for the changed code (it would need to track back as far as the u16s). >>> I don't believe my code could be wrong. >>> >>> when you use the word 'may', you are saying that it could be wrong after >>> apply my patch. >>> >>> Then you have to find at least one test example to prove you point, in >>> which case my codes generate wrong results. >>> >>> Again I don't believe you could find one. >>> It is not uncommon to force a 64bit result from a multiply by making the constant 64bit. As in: div_u64(frame_size * 100ULL, dotclock); >>> In fact, After apply this patch, the ASM code generated is same with before. >>> >>> This may because the GCC is smart enough to generate optimized code in >>> either case, >>> >>> I think It could be different with a different optimization-level. >>> >>
RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
From: 15330273...@189.cn <15330273...@189.cn> > Sent: 22 May 2023 12:56 ... > > I'll bet most people will be surprised to see what this prints: > > > > #include > > #include > > > > int main(void) > > { > > uint16_t x = 0x; > > uint16_t y = 0x; > > uint64_t z = x * y; > > > > printf("0x%016lx\n", z); > > printf("%ld\n", z); > > Here, please replace the "%ld\n" with the "%lu\n", then you will see the > difference. > > you are casting the variable 'z' to signed value, "%d" is for printing > signed value, and "%u" is for printing unsigned value. That makes very little difference on 2's compliment systems. They both display the contents of the variable. > Your simple code explained exactly why you are still in confusion, > > that is u16 * u16 can yield a negative value if you use the int as the > return type. Because it overflowed. There is no 'return type', the type of 'u16 * u16' is signed int. When 'signed int' is promoted/cast to u64 it is first sign extended to 64 bits. You can get what you want/expect by either forcing an unsigned multiply or by explicitly casting the result of the multiply to u32. So the product in 'z = (x + 0u) * y' is 'unsigned int' it gets promoted to int64_t (ie a signed type) and then converted to unsigned. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/22 19:29, Jani Nikula wrote: On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: On 2023/5/17 18:59, David Laight wrote: From: 15330273...@189.cn Sent: 16 May 2023 18:30 From: Sui Jingfeng Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. I believe that signed or unsigned is dependent on the declaration. I am talk about the math, while you are talking about compiler. I admit that u16 gets promoted to 'signed int' is true, but this is irrelevant, the point is how to understand the returned value. How does the compiler generate the code is one thing, how do we interpret the result is another How does the compiler generate the code is NOT determined by us, while how do we interpret the result is determined by us. I believe that using a u32 type to interpret the result(u16 * u16) is always true, it is true in the perspective of *math*. Integer promotions is the details of C program language. If the result of the multiply is signed, then there are risks that the result is negative, what's the benefit to present this risk to the programmer? What's the benefit to tell me(and others) that u16 * u16 yield a signed value? and can be negative? Using int type as the return type bring concerns to the programmer and the user of the function, even though this is not impossible in practice. In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. I'll bet most people will be surprised to see what this prints: #include #include int main(void) { uint16_t x = 0x; uint16_t y = 0x; uint64_t z = x * y; printf("0x%016lx\n", z); printf("%ld\n", z); Here, please replace the "%ld\n" with the "%lu\n", then you will see the difference. you are casting the variable 'z' to signed value, "%d" is for printing signed value, and "%u" is for printing unsigned value. Your simple code explained exactly why you are still in confusion, that is u16 * u16 can yield a negative value if you use the int as the return type. Because it overflowed. printf("%d\n", x * y); } And it's not that different from what you have below. Your patch doesn't change anything, and doesn't make it any less confusing. BR, Jani. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. Sorry about my expression, I think my sentence did not mention anything about 'because the original type is signed'. In the contrary, my patch eliminated the concerns to the reviewer. It say that the results of the multiply can't be negative. My intent is to tell the compiler we want a unsigned return type, but GCC emit 'imul' instruction for the multiply.. I'm using u64 as the return type, because div_u64() function accept a u64 type value as its first argument. The compiler will detect that the old code is a 32x32 multiply where a 64bit result is needed, that may not be true for the changed code (it would need to track back as far as the u16s). I don't believe my code could be wrong. when you use the word 'may', you are saying that it could be wrong after apply my patch. Then you have to find at least one test example to prove you point, in which case my codes generate wrong results. Again I don't believe you could find one. It is not uncommon to force a 64bit result from a multiply by making the constant 64bit. As in: div_u64(frame_size * 100ULL, dotclock); In fact, After apply this patch, the ASM code generated is same with before. This may because the GCC is smart enough to generate optimized code in either case, I think It could be different with a different optimization-level. I have tested this patch on three different architecture, I can not find error still. Below is the assembly extract on x86-64: because GCC generate the same code in either case, so I pasted only one copy here. 0530 : 530: f3 0f 1e fa endbr64 534: e8 00 00 00 00 callq 539 539: 55 push %rbp 53a: 48 89 e5 mov %rsp,%rbp 53d: 41 57 push %r15 53f: 41 56
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
On Thu, 18 May 2023, Sui Jingfeng <15330273...@189.cn> wrote: > On 2023/5/17 18:59, David Laight wrote: >> From: 15330273...@189.cn >>> Sent: 16 May 2023 18:30 >>> >>> From: Sui Jingfeng >>> >>> Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, >>> mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. >> Nope, u16 gets promoted to 'signed int' and the result of the >> multiply is also signed. > > I believe that signed or unsigned is dependent on the declaration. > > I am talk about the math, while you are talking about compiler. > > I admit that u16 gets promoted to 'signed int' is true, but this is > irrelevant, > > the point is how to understand the returned value. > > > How does the compiler generate the code is one thing, how do we > interpret the result is another > > How does the compiler generate the code is NOT determined by us, while > how do we interpret the result is determined by us. > > > I believe that using a u32 type to interpret the result(u16 * u16) is > always true, it is true in the perspective of *math*. > > Integer promotions is the details of C program language. If the result > of the multiply is signed, then there are risks that > > the result is negative, what's the benefit to present this risk to the > programmer? > > What's the benefit to tell me(and others) that u16 * u16 yield a signed > value? and can be negative? > > Using int type as the return type bring concerns to the programmer and > the user of the function, > > even though this is not impossible in practice. In general, do not use unsigned types in arithmethic to avoid negative values, because most people will be tripped over by integer promotion rules, and you'll get negative values anyway. I'll bet most people will be surprised to see what this prints: #include #include int main(void) { uint16_t x = 0x; uint16_t y = 0x; uint64_t z = x * y; printf("0x%016lx\n", z); printf("%ld\n", z); printf("%d\n", x * y); } And it's not that different from what you have below. Your patch doesn't change anything, and doesn't make it any less confusing. BR, Jani. > >>> Using a u32 is enough to store the result, but considering that the >>> result will be casted to u64 soon after. We use a u64 type directly. >>> So there no need to cast it to signed type and cast back then. >> >>> - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; >>> + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; >> ... >>> - framedur_ns = div_u64((u64) frame_size * 100, dotclock); >>> + framedur_ns = div_u64(frame_size * 100, dotclock); >> The (u64) cast is there to extend the value to 64bits, not >> because the original type is signed. > > Sorry about my expression, I think my sentence did not mention anything > about 'because the original type is signed'. > > In the contrary, my patch eliminated the concerns to the reviewer. It > say that the results of the multiply can't be negative. > > My intent is to tell the compiler we want a unsigned return type, but > GCC emit 'imul' instruction for the multiply.. > > I'm using u64 as the return type, because div_u64() function accept a > u64 type value as its first argument. > >> The compiler will detect that the old code is a 32x32 multiply >> where a 64bit result is needed, that may not be true for the >> changed code (it would need to track back as far as the u16s). > > I don't believe my code could be wrong. > > when you use the word 'may', you are saying that it could be wrong after > apply my patch. > > Then you have to find at least one test example to prove you point, in > which case my codes generate wrong results. > > Again I don't believe you could find one. > >> It is not uncommon to force a 64bit result from a multiply >> by making the constant 64bit. As in: >> div_u64(frame_size * 100ULL, dotclock); > > In fact, After apply this patch, the ASM code generated is same with before. > > This may because the GCC is smart enough to generate optimized code in > either case, > > I think It could be different with a different optimization-level. > > I have tested this patch on three different architecture, I can not > find error still. > > Below is the assembly extract on x86-64: because GCC generate the same > code in either case, > > so I pasted only one copy here. > > > 0530 : > 530: f3 0f 1e fa endbr64 > 534: e8 00 00 00 00 callq 539 > > 539: 55 push %rbp > 53a: 48 89 e5 mov %rsp,%rbp > 53d: 41 57 push %r15 > 53f: 41 56 push %r14 > 541: 41 55 push %r13 > 543: 41 54 push %r12 > 545: 53 push %rbx > 546: 48 83 ec 18 sub $0x18,%rsp >
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
On 2023/5/17 18:59, David Laight wrote: From: 15330273...@189.cn Sent: 16 May 2023 18:30 From: Sui Jingfeng Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. I believe that signed or unsigned is dependent on the declaration. I am talk about the math, while you are talking about compiler. I admit that u16 gets promoted to 'signed int' is true, but this is irrelevant, the point is how to understand the returned value. How does the compiler generate the code is one thing, how do we interpret the result is another How does the compiler generate the code is NOT determined by us, while how do we interpret the result is determined by us. I believe that using a u32 type to interpret the result(u16 * u16) is always true, it is true in the perspective of *math*. Integer promotions is the details of C program language. If the result of the multiply is signed, then there are risks that the result is negative, what's the benefit to present this risk to the programmer? What's the benefit to tell me(and others) that u16 * u16 yield a signed value? and can be negative? Using int type as the return type bring concerns to the programmer and the user of the function, even though this is not impossible in practice. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. Sorry about my expression, I think my sentence did not mention anything about 'because the original type is signed'. In the contrary, my patch eliminated the concerns to the reviewer. It say that the results of the multiply can't be negative. My intent is to tell the compiler we want a unsigned return type, but GCC emit 'imul' instruction for the multiply.. I'm using u64 as the return type, because div_u64() function accept a u64 type value as its first argument. The compiler will detect that the old code is a 32x32 multiply where a 64bit result is needed, that may not be true for the changed code (it would need to track back as far as the u16s). I don't believe my code could be wrong. when you use the word 'may', you are saying that it could be wrong after apply my patch. Then you have to find at least one test example to prove you point, in which case my codes generate wrong results. Again I don't believe you could find one. It is not uncommon to force a 64bit result from a multiply by making the constant 64bit. As in: div_u64(frame_size * 100ULL, dotclock); In fact, After apply this patch, the ASM code generated is same with before. This may because the GCC is smart enough to generate optimized code in either case, I think It could be different with a different optimization-level. I have tested this patch on three different architecture, I can not find error still. Below is the assembly extract on x86-64: because GCC generate the same code in either case, so I pasted only one copy here. 0530 : 530: f3 0f 1e fa endbr64 534: e8 00 00 00 00 callq 539 539: 55 push %rbp 53a: 48 89 e5 mov %rsp,%rbp 53d: 41 57 push %r15 53f: 41 56 push %r14 541: 41 55 push %r13 543: 41 54 push %r12 545: 53 push %rbx 546: 48 83 ec 18 sub $0x18,%rsp 54a: 4c 8b 3f mov (%rdi),%r15 54d: 41 8b 87 6c 01 00 00 mov 0x16c(%r15),%eax 554: 85 c0 test %eax,%eax 556: 0f 84 ec 00 00 00 je 648 55c: 44 8b 87 90 00 00 00 mov 0x90(%rdi),%r8d 563: 49 89 fc mov %rdi,%r12 566: 44 39 c0 cmp %r8d,%eax 569: 0f 86 40 01 00 00 jbe 6af 56f: 44 8b 76 1c mov 0x1c(%rsi),%r14d 573: 49 8b 8f 40 01 00 00 mov 0x140(%r15),%rcx 57a: 48 89 f3 mov %rsi,%rbx 57d: 45 85 f6 test %r14d,%r14d 580: 0f 8e d5 00 00 00 jle 65b 586: 0f b7 43 2a movzwl 0x2a(%rbx),%eax 58a: 49 63 f6 movslq %r14d,%rsi 58d: 31 d2
RE: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
From: 15330273...@189.cn > Sent: 16 May 2023 18:30 > > From: Sui Jingfeng > > Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, > mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Nope, u16 gets promoted to 'signed int' and the result of the multiply is also signed. > Using a u32 is enough to store the result, but considering that the > result will be casted to u64 soon after. We use a u64 type directly. > So there no need to cast it to signed type and cast back then. > - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; > + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; ... > - framedur_ns = div_u64((u64) frame_size * 100, dotclock); > + framedur_ns = div_u64(frame_size * 100, dotclock); The (u64) cast is there to extend the value to 64bits, not because the original type is signed. The compiler will detect that the old code is a 32x32 multiply where a 64bit result is needed, that may not be true for the changed code (it would need to track back as far as the u16s). It is not uncommon to force a 64bit result from a multiply by making the constant 64bit. As in: div_u64(frame_size * 100ULL, dotclock); David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
[PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
From: Sui Jingfeng Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. Signed-off-by: Sui Jingfeng Reviewed-by: Thomas Zimmermann Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-ker...@vger.kernel.org Cc: loongson-ker...@lists.loongnix.cn --- drivers/gpu/drm/drm_vblank.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 877e2067534f..d99c404b181b 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -622,7 +622,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, /* Valid dotclock? */ if (dotclock > 0) { - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; /* * Convert scanline length in pixels and video @@ -630,7 +630,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, * in nanoseconds: */ linedur_ns = div_u64((u64) mode->crtc_htotal * 100, dotclock); - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); /* * Fields of interlaced scanout modes are only half a frame duration. -- 2.25.1
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Hi, On 2023/5/17 01:06, Thomas Zimmermann wrote: Am 16.05.23 um 18:59 schrieb Sui Jingfeng: Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. Signed-off-by: Sui Jingfeng Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-ker...@vger.kernel.org Reviewed-by: Thomas Zimmermann Thanks a lot, thanks for the response time also. --- drivers/gpu/drm/drm_vblank.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 877e2067534f..d99c404b181b 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -622,7 +622,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, /* Valid dotclock? */ if (dotclock > 0) { - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; /* * Convert scanline length in pixels and video @@ -630,7 +630,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, * in nanoseconds: */ linedur_ns = div_u64((u64) mode->crtc_htotal * 100, dotclock); - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); /* * Fields of interlaced scanout modes are only half a frame duration.
Re: [PATCH] drm/drm_vblank.c: avoid unsigned int to signed int cast
Am 16.05.23 um 18:59 schrieb Sui Jingfeng: Both mode->crtc_htotal and mode->crtc_vtotal are u16 type, mode->crtc_htotal * mode->crtc_vtotal will results a unsigned type. Using a u32 is enough to store the result, but considering that the result will be casted to u64 soon after. We use a u64 type directly. So there no need to cast it to signed type and cast back then. Signed-off-by: Sui Jingfeng Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter Cc: dri-devel@lists.freedesktop.org Cc: linux-ker...@vger.kernel.org Reviewed-by: Thomas Zimmermann --- drivers/gpu/drm/drm_vblank.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_vblank.c b/drivers/gpu/drm/drm_vblank.c index 877e2067534f..d99c404b181b 100644 --- a/drivers/gpu/drm/drm_vblank.c +++ b/drivers/gpu/drm/drm_vblank.c @@ -622,7 +622,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, /* Valid dotclock? */ if (dotclock > 0) { - int frame_size = mode->crtc_htotal * mode->crtc_vtotal; + u64 frame_size = mode->crtc_htotal * mode->crtc_vtotal; /* * Convert scanline length in pixels and video @@ -630,7 +630,7 @@ void drm_calc_timestamping_constants(struct drm_crtc *crtc, * in nanoseconds: */ linedur_ns = div_u64((u64) mode->crtc_htotal * 100, dotclock); - framedur_ns = div_u64((u64) frame_size * 100, dotclock); + framedur_ns = div_u64(frame_size * 100, dotclock); /* * Fields of interlaced scanout modes are only half a frame duration. -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg) OpenPGP_signature Description: OpenPGP digital signature