From: Dmitry Antipov
Sent: 28 June 2023 11:45
On 6/28/23 11:24, David Laight wrote:
What are the (u16) casts for?
Well, this is a kind of a C language purism intended to silence warning: conversion from ‘int’ to ‘u16’ {aka ‘short unsigned int’} may change value [-Wconversion] observed with W=123.
The problem is that the casts can hide more than just a value being truncated. In this case the compiler even knows the values don't overflow.
FWIW I've seen generated code for: *cp++ = (unsigned char)(value & 0xff); that masked the value with 0xff once for the & a second time for the cast and then stored the low byte. I don't think modern gcc will do that, but a dumb compiler will.
If -Wconversion bleats about these sort of assignments it just needs permanently disabling (or deleting from the compiler!).
David
- Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)