[Zlib-devel] [PATCH] deflate.c: identify slide_Pos() for later optimization

John Reiser jreiser at bitwagon.com
Tue Jul 24 09:54:36 EDT 2012


On 07/24/2012 Jan Seiffert wrote:

> I was talking about C compiler. They can't get this because of C type promotion
> rules in this construct (or they have to cut through a lot of red tape).

My view is that ordinary range analysis can infer the saturating subtract,
even in C.  I was careful to use 'Pos' everywhere, particularly for the
'slide' parameter, and not use 'unsigned' for any of the data declarations
['n' is a "control" variable here]:

+    Pos *const ptr; unsigned const n; Pos const slide;

+        Pos const m = ptr[j];
+        ptr[j] = (Pos)(m >= slide ? m-slide : NIL);

Type promotion [extend with high-order bits which are zero] from 'Pos'
[unsigned short] to 'unsigned', for either inputs or outputs of any
_operation_ above, does not change the value of any result.  Thus
performing all operations in 16-bit arithmetic does conform to the
rules for evaluation of C expressions, and so saturating subtract
of unsigned 16-bit quantities may be inferred.

-- 





More information about the Zlib-devel mailing list