Branch data Line data Source code
1 : : /******************************************************************************
2 : : * This file is part of the cvc5 project.
3 : : *
4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 : : * in the top-level source directory and their institutional affiliations.
6 : : * All rights reserved. See the file COPYING in the top-level source
7 : : * directory for licensing information.
8 : : * ****************************************************************************
9 : : *
10 : : * Expand definitions for floating-point arithmetic.
11 : : */
12 : :
13 : : #include "theory/fp/fp_expand_defs.h"
14 : :
15 : : #include "expr/skolem_manager.h"
16 : : #include "expr/sort_to_term.h"
17 : : #include "util/floatingpoint.h"
18 : :
19 : : namespace cvc5::internal {
20 : : namespace theory {
21 : : namespace fp {
22 : :
23 : 12 : Node FpExpandDefs::minMaxUF(TNode node)
24 : : {
25 : 12 : Kind kind = node.getKind();
26 [ - + ][ - - ]: 12 : Assert(kind == Kind::FLOATINGPOINT_MIN || kind == Kind::FLOATINGPOINT_MAX);
[ - + ][ - + ]
[ - - ]
27 : :
28 : 12 : TypeNode type = node.getType();
29 [ - + ][ - + ]: 12 : Assert(type.getKind() == Kind::FLOATINGPOINT_TYPE);
[ - - ]
30 : :
31 : : return d_nm->mkNode(Kind::APPLY_UF,
32 [ - + ]: 24 : d_nm->getSkolemManager()->mkSkolemFunction(
33 : : kind == Kind::FLOATINGPOINT_MIN
34 [ - - ]: 0 : || kind == Kind::FLOATINGPOINT_MIN_TOTAL
35 : : ? SkolemId::FP_MIN_ZERO
36 : : : SkolemId::FP_MAX_ZERO,
37 : 24 : {d_nm->mkConst(SortToTerm(type))}),
38 : : node[0],
39 : 48 : node[1]);
40 : 12 : }
41 : :
42 : 29 : Node FpExpandDefs::toUbvSbvUF(TNode node)
43 : : {
44 : 29 : Kind kind = node.getKind();
45 [ + + ][ + - ]: 29 : Assert(kind == Kind::FLOATINGPOINT_TO_UBV
[ - + ][ - + ]
[ - - ]
46 : : || kind == Kind::FLOATINGPOINT_TO_SBV);
47 : :
48 : 29 : TypeNode type = node.getType();
49 [ - + ][ - + ]: 29 : Assert(type.getKind() == Kind::BITVECTOR_TYPE);
[ - - ]
50 : :
51 : : return d_nm->mkNode(
52 : : Kind::APPLY_UF,
53 [ + + ][ + + ]: 174 : d_nm->getSkolemManager()->mkSkolemFunction(
[ - - ]
54 : : kind == Kind::FLOATINGPOINT_TO_SBV ? SkolemId::FP_TO_SBV
55 : : : SkolemId::FP_TO_UBV,
56 : 58 : {d_nm->mkConst(SortToTerm(node[1].getType())),
57 : 58 : d_nm->mkConst(SortToTerm(type))}),
58 : : node[0],
59 : 116 : node[1]);
60 : 29 : }
61 : :
62 : 18 : Node FpExpandDefs::toRealUF(TNode node)
63 : : {
64 [ - + ][ - + ]: 18 : Assert(node.getKind() == Kind::FLOATINGPOINT_TO_REAL);
[ - - ]
65 : 18 : TypeNode type = node[0].getType();
66 [ - + ][ - + ]: 18 : Assert(type.getKind() == Kind::FLOATINGPOINT_TYPE);
[ - - ]
67 : :
68 : : return d_nm->mkNode(
69 : : Kind::APPLY_UF,
70 : 36 : d_nm->getSkolemManager()->mkSkolemFunction(
71 : 36 : SkolemId::FP_TO_REAL, {d_nm->mkConst(SortToTerm(type))}),
72 : 72 : node[0]);
73 : 18 : }
74 : :
75 : 2984 : Node FpExpandDefs::expandDefinition(Node node)
76 : : {
77 [ + - ]: 5968 : Trace("fp-expandDefinition")
78 : 2984 : << "FpExpandDefs::expandDefinition(): " << node << std::endl;
79 : :
80 : 2984 : Node res = node;
81 : 2984 : Kind kind = node.getKind();
82 [ + + ]: 2984 : if (kind == Kind::FLOATINGPOINT_MIN)
83 : : {
84 : 24 : res = d_nm->mkNode(
85 : 36 : Kind::FLOATINGPOINT_MIN_TOTAL, node[0], node[1], minMaxUF(node));
86 : : }
87 [ - + ]: 2972 : else if (kind == Kind::FLOATINGPOINT_MAX)
88 : : {
89 : 0 : res = d_nm->mkNode(
90 : 0 : Kind::FLOATINGPOINT_MAX_TOTAL, node[0], node[1], minMaxUF(node));
91 : : }
92 [ + + ]: 2972 : else if (kind == Kind::FLOATINGPOINT_TO_UBV)
93 : : {
94 : 72 : res = d_nm->mkNode( // Kind::FLOATINGPOINT_TO_UBV_TOTAL,
95 : 36 : d_nm->mkConst(FloatingPointToUBVTotal(
96 : 36 : node.getOperator().getConst<FloatingPointToUBV>())),
97 : : node[0],
98 : : node[1],
99 : 54 : toUbvSbvUF(node));
100 : : }
101 [ + + ]: 2954 : else if (kind == Kind::FLOATINGPOINT_TO_SBV)
102 : : {
103 : 44 : res = d_nm->mkNode( // Kind::FLOATINGPOINT_TO_SBV_TOTAL,
104 : 22 : d_nm->mkConst(FloatingPointToSBVTotal(
105 : 22 : node.getOperator().getConst<FloatingPointToSBV>())),
106 : : node[0],
107 : : node[1],
108 : 33 : toUbvSbvUF(node));
109 : : }
110 [ + + ]: 2943 : else if (kind == Kind::FLOATINGPOINT_TO_REAL)
111 : : {
112 : 36 : res = d_nm->mkNode(
113 : 54 : Kind::FLOATINGPOINT_TO_REAL_TOTAL, node[0], toRealUF(node));
114 : : }
115 : :
116 [ + + ]: 2984 : if (res != node)
117 : : {
118 [ + - ]: 118 : Trace("fp-expandDefinition") << "FpExpandDefs::expandDefinition(): " << node
119 : 59 : << " rewritten to " << res << std::endl;
120 : 59 : return res;
121 : : }
122 : 2925 : return Node::null();
123 : 2984 : }
124 : :
125 : : } // namespace fp
126 : : } // namespace theory
127 : : } // namespace cvc5::internal
|