-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix clang warnings about values not fitting the type and implicit con…
…version. Redo the generation of type conversion components to use C standard min/max values and constructs without the preprocessor.
- Loading branch information
Showing
3 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
component conv_@IN@_@OUT@ "Convert a value from @IN@ to @OUT@"; | ||
pin in @IN@ in_; | ||
pin in @IN@ in; | ||
pin out @OUT@ out; | ||
@CC@ pin out bit out_of_range "TRUE when 'in' is not in the range of @OUT@"; | ||
@CC@ param rw bit clamp """If TRUE, then clamp to the range of @OUT@. If FALSE, then allow the value to "wrap around"."""; | ||
@CC@pin out bit out_of_range "TRUE when 'in' is not in the range of @OUT@"; | ||
@CC@param rw bit clamp """If TRUE, then clamp to the range of @OUT@. If FALSE, then allow the value to "wrap around"."""; | ||
function _ @FP@ "Update 'out' based on 'in'"; | ||
license "GPL"; | ||
author "Jeff Epler"; | ||
|
||
;; | ||
#include <stdint.h> | ||
FUNCTION(_) { | ||
hal_@IN@_t in = in_; | ||
@CC@ if(clamp) { | ||
#if @MAX@ != 0 | ||
@CC@ if(in > @MAX@) { out = @MAX@; out_of_range = 1; return; } | ||
@TYPI@ val = in; | ||
@CC@ if(clamp) { | ||
#if @MAXEN@ == 0 | ||
@CC@ if(val > (@TYPI@)@MAX@) { out = @MAX@; out_of_range = 1; return; } | ||
#endif | ||
#if @MIN@ != -1 | ||
@CC@ if(in < @MIN@) { out = @MIN@; out_of_range = 1; return; } | ||
#if @MINEN@ == 0 | ||
@CC@ if(val < (@TYPI@)@MIN@) { out = @MIN@; out_of_range = 1; return; } | ||
#endif | ||
@CC@ out = in; out_of_range = 0; | ||
@CC@ } else { | ||
out = in; | ||
@CC@ if(out != in) out_of_range = 1; | ||
@CC@ } | ||
@CC@ out = (@TYPO@)val; | ||
@CC@ out_of_range = 0; | ||
@CC@ } else { | ||
out = (@TYPO@)val; | ||
@CC@ if((@TYPI@)out != val) out_of_range = 1; | ||
@CC@ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,107 @@ | ||
#!/bin/bash | ||
if [ "$1" = "float" -o "$2" = "float" ]; then F=""; else F="nofp"; fi | ||
sed -e "s,@IN@,$1,g; s,@OUT@,$2,g; s,@CC@,$3,g; s,@MIN@,${4-0},g; s,@MAX@,${5-0},g; s,@FP@,$F,g;" | ||
|
||
if [ $# -lt 2 ]; then | ||
echo "Too few arguments to $(basename "$0")." >&2 | ||
echo "Usage: $(basename "$0") <from_type> <to_type>." >&2 | ||
exit 1 | ||
fi | ||
|
||
# Convert the hal type into the underlying type | ||
utype() { | ||
case "$1" in | ||
"bit") echo "bool" ;; | ||
"s32") echo "rtapi_s32" ;; | ||
"u32") echo "rtapi_u32" ;; | ||
"s64") echo "rtapi_s64" ;; | ||
"u64") echo "rtapi_u64" ;; | ||
"float") echo "real_t" ;; | ||
*) echo "This_Will_Generate_An_Error" ;; | ||
esac | ||
} | ||
|
||
# Return the maximum value supported by a type | ||
maxval() { | ||
case "$1" in | ||
"bit") echo "1" ;; | ||
"s32") echo "INT32_MAX" ;; | ||
"u32") echo "UINT32_MAX" ;; | ||
"s64") echo "INT64_MAX" ;; | ||
"u64") echo "UINT64_MAX" ;; | ||
"float") echo "Never_Used" ;; | ||
*) echo "This_Will_Generate_An_Error" ;; | ||
esac | ||
} | ||
|
||
# Return the minimum value supported by a type | ||
minval() { | ||
case "$1" in | ||
"bit") echo "0" ;; | ||
"s32") echo "INT32_MIN" ;; | ||
"u32") echo "0" ;; | ||
"s64") echo "INT64_MIN" ;; | ||
"u64") echo "0" ;; | ||
"float") echo "Never_Used" ;; | ||
*) echo "This_Will_Generate_An_Error" ;; | ||
esac | ||
} | ||
|
||
# | ||
# Conversions | ||
# xxx = unsupported conversion | ||
# o = no bounds checks or clamp needed | ||
# + = max side bound needed | ||
# - = min side bound needed | ||
# (table: vertical=from ($1); horizontal=to ($2)) | ||
# | flt | u32 | s32 | u64 | s64 | bit | ||
# ----+-----+-----+-----+-----+-----+----- | ||
# flt | xxx | +- | +- | +- | +- | xxx | ||
# u32 | o | xxx | + | o | o | + | ||
# s32 | o | - | xxx | - | o | +- | ||
# u64 | o | + | + | xxx | + | + | ||
# s64 | o | +- | +- | - | xxx | +- | ||
# bit | o | o | o | o | o | xxx | ||
# | ||
# Boolean implementation of the above table: | ||
# | flt | u32 | s32 | u64 | s64 | bit | ||
# ----+-----+-----+-----+-----+-----+----- | ||
# flt | o+- | +- | +- | +- | +- | x+- | ||
# u32 | o | xxx | + | o | o | + | ||
# s32 | o | - | x+x | - | o | +- | ||
# u64 | o | + | + | x+x | + | + | ||
# s64 | o | +- | +- | - | xx- | +- | ||
# bit | o | o | o | o | o | o+x | ||
# | ||
|
||
# Enable (val > MAX) test | ||
MAXEN="s,@MAXEN@,$([[ \ | ||
"$1" == "float" || \ | ||
"$2" == "bit" || \ | ||
( "$2" == "s32" && "$1" != "bit" ) || \ | ||
( "$1" == "u64" && "$2" != "float" ) || \ | ||
( "$1" == "s64" && "$2" == "u32" ) \ | ||
]]; echo $?),g" | ||
|
||
# Enable (val < MIN) test | ||
MINEN="s,@MINEN@,$([[ \ | ||
"$1" == "float" || \ | ||
( "$1" == "s64" && "$2" != "float" ) || \ | ||
( "$1" == "s32" && ( "$2" == "u32" || "$2" == "u64" || "$2" == "bit" ) ) \ | ||
]]; echo $?),g" | ||
|
||
# Disable clamp code | ||
CC="s,@CC@,$([[ \ | ||
"$2" == "float" || \ | ||
"$1" == "bit" || \ | ||
( "$1" == "u32" && ( "$2" == "u64" || "$2" == "s64" ) ) || \ | ||
( "$1" == "s32" && "$2" == "s64" ) \ | ||
]] && echo "//"),g" | ||
|
||
FP="s,@FP@,$([[ "$1" == "float" || "$2" == "float" ]] || echo "nofp"),g" | ||
IN="s,@IN@,$1,g" | ||
OUT="s,@OUT@,$2,g" | ||
MIN="s,@MIN@,$(minval "$2"),g" | ||
MAX="s,@MAX@,$(maxval "$2"),g" | ||
TYPI="s,@TYPI@,$(utype "$1"),g" | ||
TYPO="s,@TYPO@,$(utype "$2"),g" | ||
|
||
exec sed -e "$IN; $OUT; $CC; $MIN; $MAX; $FP; $TYPI; $TYPO; $MINEN; $MAXEN;" |