On 12/16/2016 05:12 PM, Stefan Hajnoczi wrote: >> You are not the first one complaining, so the sentence is definitively >> bad. What disturbs me regarding your formulation is that we do not use >> uint16_t to represent neither the ring size nor inuse. >> >> How about "Since max ring size < UINT16_MAX it's safe to use modulo >> UINT16_MAX + 1 subtraction."? > That doesn't mention "representing the size of the ring" so it's > unclear what "safe" means. > > Stefan >
IMHO it is not about representation but about correct arithmetic. We introduce the cast, not because representing the ring size as int is necessarily an issue, but because we ended up with a wrong result. In my opinion how can 'inuse' be represented correctly and efficiently concerns the member of struct VirtQueue. Here the important point is how conversions between signed unsigned integer types work in C. """ 6.3.1.3 Signed and unsigned integers 1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. """ That is we get mod UINT16_MAX + 1 subtraction which is what we need if we want to calculate the difference between the two counters under the assumption that the actual conceptual difference (that is if the counters where of type arbitrary natural) is less or equal that queue size which is less than UINT16_MAX. But no strong opinion here. If I did not manage to convince you now, just tell, and I will be happy to just go with your formulation. Thanks, Halil