xref: /qemu/include/tcg/tcg-cond.h (revision f9734d5d)
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 signed,
33  *    bit 2 is unsigned,
34  *    bit 3 is used with bit 0 for swapping signed/unsigned.
35  */
36 typedef enum {
37     /* non-signed */
38     TCG_COND_NEVER  = 0 | 0 | 0 | 0,
39     TCG_COND_ALWAYS = 0 | 0 | 0 | 1,
40     TCG_COND_EQ     = 8 | 0 | 0 | 0,
41     TCG_COND_NE     = 8 | 0 | 0 | 1,
42     /* signed */
43     TCG_COND_LT     = 0 | 0 | 2 | 0,
44     TCG_COND_GE     = 0 | 0 | 2 | 1,
45     TCG_COND_LE     = 8 | 0 | 2 | 0,
46     TCG_COND_GT     = 8 | 0 | 2 | 1,
47     /* unsigned */
48     TCG_COND_LTU    = 0 | 4 | 0 | 0,
49     TCG_COND_GEU    = 0 | 4 | 0 | 1,
50     TCG_COND_LEU    = 8 | 4 | 0 | 0,
51     TCG_COND_GTU    = 8 | 4 | 0 | 1,
52 } TCGCond;
53 
54 /* Invert the sense of the comparison.  */
55 static inline TCGCond tcg_invert_cond(TCGCond c)
56 {
57     return (TCGCond)(c ^ 1);
58 }
59 
60 /* Swap the operands in a comparison.  */
61 static inline TCGCond tcg_swap_cond(TCGCond c)
62 {
63     return c & 6 ? (TCGCond)(c ^ 9) : c;
64 }
65 
66 /* Create an "unsigned" version of a "signed" comparison.  */
67 static inline TCGCond tcg_unsigned_cond(TCGCond c)
68 {
69     return c & 2 ? (TCGCond)(c ^ 6) : c;
70 }
71 
72 /* Create a "signed" version of an "unsigned" comparison.  */
73 static inline TCGCond tcg_signed_cond(TCGCond c)
74 {
75     return c & 4 ? (TCGCond)(c ^ 6) : c;
76 }
77 
78 /* Must a comparison be considered unsigned?  */
79 static inline bool is_unsigned_cond(TCGCond c)
80 {
81     return (c & 4) != 0;
82 }
83 
84 /*
85  * Create a "high" version of a double-word comparison.
86  * This removes equality from a LTE or GTE comparison.
87  */
88 static inline TCGCond tcg_high_cond(TCGCond c)
89 {
90     switch (c) {
91     case TCG_COND_GE:
92     case TCG_COND_LE:
93     case TCG_COND_GEU:
94     case TCG_COND_LEU:
95         return (TCGCond)(c ^ 8);
96     default:
97         return c;
98     }
99 }
100 
101 #endif /* TCG_COND_H */
102