/ src / hal / components / abs_s32.comp
abs_s32.comp
 1  //   This is a component for EMC2 HAL
 2  //   Copyright 2011 Sebastian Kuzminsky <seb@highlab.com>
 3  //
 4  //   This program is free software; you can redistribute it and/or
 5  //   modify it under the terms of version 2 of the GNU General
 6  //   Public License as published by the Free Software Foundation.
 7  //
 8  //   This program is distributed in the hope that it will be useful,
 9  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  //   GNU General Public License for more details.
12  //
13  //   You should have received a copy of the GNU General Public License
14  //   along with this program; if not, write to the Free Software
15  //   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  component abs_s32 "Compute the absolute value and sign of the input signal";
17  
18  pin in s32 in "input value" ;
19  pin out s32 out "output value, always non-negative";
20  pin out bit sign "Sign of input, false for positive, true for negative" ;
21  pin out bit is_positive "TRUE if input is positive, FALSE if input is 0 or negative";
22  pin out bit is_negative "TRUE if input is negative, FALSE if input is 0 or positive";
23  
24  function _ nofp;
25  license "GPL";
26  ;;
27  FUNCTION(_) {
28      rtapi_s32 tmp = in;
29      if ( tmp >= 0 ) {
30  	sign = 0;
31  	out = tmp;
32      } else {
33  	sign = 1;
34  	out = -tmp;
35      }
36  
37      if (tmp > 0) is_positive = 1;
38      else is_positive = 0;
39  
40      if (tmp < 0) is_negative = 1;
41      else is_negative = 0;
42  }