xref: /qemu/include/tcg/tcg-cond.h (revision d48097d0)
1 /*
2  * Tiny Code Generator for QEMU
3  *
4  * Copyright (c) 2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TCG_COND_H
26 #define TCG_COND_H
27 
28 /*
29  * Conditions.  Note that these are laid out for easy manipulation by
30  * the functions below:
31  *    bit 0 is used for inverting;
32  *    bit 1 is used for conditions that need swapping (signed/unsigned).
33  *    bit 2 is used with bit 1 for swapping.
34  *    bit 3 is used for unsigned conditions.
35  */
36 typedef enum {
37     /* non-signed */
38     TCG_COND_NEVER  = 0 | 0 | 0 | 0,
39     TCG_COND_ALWAYS = 0 | 0 | 0 | 1,
40 
41     /* equality */
42     TCG_COND_EQ     = 8 | 0 | 0 | 0,
43     TCG_COND_NE     = 8 | 0 | 0 | 1,
44 
45     /* "test" i.e. and then compare vs 0 */
46     TCG_COND_TSTEQ  = 8 | 4 | 0 | 0,
47     TCG_COND_TSTNE  = 8 | 4 | 0 | 1,
48 
49     /* signed */
50     TCG_COND_LT     = 0 | 0 | 2 | 0,
51     TCG_COND_GE     = 0 | 0 | 2 | 1,
52     TCG_COND_GT     = 0 | 4 | 2 | 0,
53     TCG_COND_LE     = 0 | 4 | 2 | 1,
54 
55     /* unsigned */
56     TCG_COND_LTU    = 8 | 0 | 2 | 0,
57     TCG_COND_GEU    = 8 | 0 | 2 | 1,
58     TCG_COND_GTU    = 8 | 4 | 2 | 0,
59     TCG_COND_LEU    = 8 | 4 | 2 | 1,
60 } TCGCond;
61 
62 /* Invert the sense of the comparison.  */
tcg_invert_cond(TCGCond c)63 static inline TCGCond tcg_invert_cond(TCGCond c)
64 {
65     return (TCGCond)(c ^ 1);
66 }
67 
68 /* Swap the operands in a comparison.  */
tcg_swap_cond(TCGCond c)69 static inline TCGCond tcg_swap_cond(TCGCond c)
70 {
71     return (TCGCond)(c ^ ((c & 2) << 1));
72 }
73 
74 /* Must a comparison be considered signed?  */
is_signed_cond(TCGCond c)75 static inline bool is_signed_cond(TCGCond c)
76 {
77     return (c & (8 | 2)) == 2;
78 }
79 
80 /* Must a comparison be considered unsigned?  */
is_unsigned_cond(TCGCond c)81 static inline bool is_unsigned_cond(TCGCond c)
82 {
83     return (c & (8 | 2)) == (8 | 2);
84 }
85 
86 /* Must a comparison be considered a test?  */
is_tst_cond(TCGCond c)87 static inline bool is_tst_cond(TCGCond c)
88 {
89     return (c | 1) == TCG_COND_TSTNE;
90 }
91 
92 /* Create an "unsigned" version of a "signed" comparison.  */
tcg_unsigned_cond(TCGCond c)93 static inline TCGCond tcg_unsigned_cond(TCGCond c)
94 {
95     return is_signed_cond(c) ? (TCGCond)(c + 8) : c;
96 }
97 
98 /* Create a "signed" version of an "unsigned" comparison.  */
tcg_signed_cond(TCGCond c)99 static inline TCGCond tcg_signed_cond(TCGCond c)
100 {
101     return is_unsigned_cond(c) ? (TCGCond)(c - 8) : c;
102 }
103 
104 /* Create the eq/ne version of a tsteq/tstne comparison.  */
tcg_tst_eqne_cond(TCGCond c)105 static inline TCGCond tcg_tst_eqne_cond(TCGCond c)
106 {
107     return is_tst_cond(c) ? (TCGCond)(c - 4) : c;
108 }
109 
110 /* Create the lt/ge version of a tstne/tsteq comparison of the sign.  */
tcg_tst_ltge_cond(TCGCond c)111 static inline TCGCond tcg_tst_ltge_cond(TCGCond c)
112 {
113     return is_tst_cond(c) ? (TCGCond)(c ^ 0xf) : c;
114 }
115 
116 /*
117  * Create a "high" version of a double-word comparison.
118  * This removes equality from a LTE or GTE comparison.
119  */
tcg_high_cond(TCGCond c)120 static inline TCGCond tcg_high_cond(TCGCond c)
121 {
122     switch (c) {
123     case TCG_COND_GE:
124     case TCG_COND_LE:
125     case TCG_COND_GEU:
126     case TCG_COND_LEU:
127         return (TCGCond)(c ^ (4 | 1));
128     default:
129         return c;
130     }
131 }
132 
133 #endif /* TCG_COND_H */
134