commit 7767739713aa27943816b7c846a406a3c2c8a370 parent eee87050e1b4fcd2524c8e4658a893636a60b6a2 Author: Michael Savage <mikejsavage@gmail.com> Date: Wed Sep 14 20:37:12 +0100 Add script to generate int_conversions.h Diffstat:
manualgen/int_conversions.lua | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
diff --git a/manualgen/int_conversions.lua b/manualgen/int_conversions.lua @@ -0,0 +1,55 @@ +#! /usr/bin/lua + +local s = { "s64", "s32", "s16", "s8" } +local u = { "u64", "u32", "u16", "u8" } + +local function printf( form, ... ) + print( form:format( ... ) ) +end + +local function codes( t ) + printf( + "template< typename T >\n" .. + "%s check_%s( T x ) {\n" .. + "\tif( IS_SIGNED( T ) ) assert( x >= %s_MIN );\n" .. + "\tassert( x <= %s_MAX );\n" .. + "\treturn ( %s ) x;\n" .. + "}\n" .. + "\n" .. + "template< typename T >\n" .. + "%s check_%s( T x ) {\n" .. + "#pragma GCC diagnostic push // GCC warns even though the optimiser removes the branch\n" .. + "#pragma GCC diagnostic ignored \"-Wsign-compare\"\n" .. + "\tif( IS_SIGNED( T ) ) assert( x >= %s_MIN );\n" .. + "#pragma GCC diagnostic pop\n" .. + "\tassert( x <= %s_MAX );\n" .. + "\treturn ( %s ) x;\n" .. + "}", + t, t, t:upper(), t:upper(), t, + t, t, t:upper(), t:upper(), t ) +end + +local function codeu( t ) + printf( + "template< typename T >\n" .. + "%s check_%s( T x ) {\n" .. + "\tassert( x >= 0 && x <= %s_MAX );\n" .. + "\treturn ( %s ) x;\n" .. + "}\n" .. + "\n" .. + "template< typename T >\n" .. + "%s clamp_%s( T x ) {\n" .. + "\tif( x < 0 ) return 0;\n" .. + "\tif( x > %s_MAX ) return %s_MAX;\n" .. + "\treturn ( %s ) x;\n" .. + "}\n", + t, t, t:upper(), t, + t, t, t:upper(), t:upper(), t ) +end + +for _, t in ipairs( s ) do + codes( t ) +end +for _, t in ipairs( u ) do + codeu( t ) +end