aboutsummaryrefslogtreecommitdiffstats
path: root/src/algorithms/bits/Signum.java
blob: 3744be7d4bca6505c2dbd84355fb0eed7e11311b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
 * 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 (x ^ y) < 0;
    }
£>done
}