/**
 * Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package algorithms.bits;


/**
 * Compute the sign of an integer
 */
public class Signum
{
£<for T_S in byte_7 short_15 int_31 long_63; do
  T=${T_S%_*}
£>S=${T_S#*_}
    /**
     * Compute the sign of an integer
     * 
     * @param   value  The integer
     * @return         {@code -1} if negative, otherwise {@code 0}
     */
    public static int minus_zero(£{T} value)
    {
	return (int)(value >> £{S});
	/* In C, you can also do either of:
	 *     -(value < 0)
	 *     -(£{T})((unsigned £{T})((£{T})value) >> (sizeof(£{T}) * CHAR_BITS - 1));
	 * The method used here is not portable in C
	 * CHAR_BITS should be defined as 8 if not already defined
	 */
    }
    
    /**
     * Compute the sign of an integer
     * 
     * @param   value  The integer
     * @return         {@code -1} if negative, otherwise {@code 1}
     */
    public static int minus_plus(£{T} value)
    {
	return 1 | (int)(value >> £{S});
    }
    
    /**
     * Compute the sign of an integer
     * 
     * @param   value  The integer
     * @return         {@code -1} if negative, {@code 1} if positive, and {@code 0} if zero
     */
    public static int minus_zero_plus(£{T} value)
    {
	return (int)(value >> £{S}) - (int)(-value >> £{S});
	/* In C, you can also do either of:
	 *     (value != 0) | -(£{T})((unsigned int)((£{T})value) >> (sizeof(£{T}) * CHAR_BITS - 1))
	 *     (value != 0) | (value >> (sizeof(£{T}) * CHAR_BITS - 1))
	 *     (value > 0) - (value < 0)
	 */
    }
    
    /**
     * Compute the sign of an integer
     * 
     * @param   value  The integer
     * @return         {@code 0} if negative, otherwise {@code 1}
     */
    public static int zero_plus(£{T} value)
    {
	return 1 ^ (1 & (int)(value >> £{S}));
	/* In C, you can also do:
	 *     1 ^ ((unsigned £{T})value >> (sizeof(£{T}) * CHAR_BITS - 1))
	 */
    }
    
    /**
     * Detect if two integers have opposite signs
     * 
     * @param   a  One of the integers
     * @param   b  The other of the integers
     * @return     {@code true} iff either {@code a} or {@code b} is negative and the other is positive
     */
    public static boolean haveOpposite(£{T} a, £{T} b)
    {
	return (a ^ b) < 0;
    }
£>done
}


@@ -770,37 +770,35 @@
 %0A/**%0A * 
-Compute the sign of a
+Signum operations o
 n intege
@@ -798,16 +798,17 @@
  integer
+s
 %0A */%0Apub
@@ -2909,16 +2909,22 @@
 %09return 
+(%C2%A3%7BT%7D)
 (a %5E b) 
@@ -2934,16 +2934,694 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Negates a value of a flag is set to 1%0A     * %0A     * @param   value  The value%0A     * @param   flag   The flag, must be either 1 or 0%0A     * @return         %7B@code flag == 1 ? value : -value%7D%0A     */%0A    public static %C2%A3%7BT%7D negateOn1(%C2%A3%7BT%7D value, %C2%A3%7BT%7D flag)%0A    %7B%0A%09return (%C2%A3%7BT%7D)((%C2%A3%7BT%7D)(flag %5E (flag - 1)) * value);%0A    %7D%0A    %0A    /**%0A     * Negates a value of a flag is set to 1%0A     * %0A     * @param   value  The value%0A     * @param   flag   The flag, must be either 1 or 0%0A     * @return         %7B@code flag == 0 ? value : -value%7D%0A     */%0A    public static %C2%A3%7BT%7D negateOn0(%C2%A3%7BT%7D value, %C2%A3%7BT%7D flag)%0A    %7B%0A%09return (%C2%A3%7BT%7D)((%C2%A3%7BT%7D)(value %5E -flag) + flag);%0A    %7D%0A    %0A
 %C2%A3%3Edone%0A%7D

add conditional negation