1. Arlen Cox
  2. z3

Source

z3 / src / ast / rewriter / float_rewriter.cpp

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*++
Copyright (c) 2012 Microsoft Corporation

Module Name:

    float_rewriter.cpp

Abstract:

    Basic rewriting rules for floating point numbers.

Author:

    Leonardo (leonardo) 2012-02-02

Notes:

--*/
#include"float_rewriter.h"

float_rewriter::float_rewriter(ast_manager & m, params_ref const & p):
    m_util(m) {
    updt_params(p);
}

float_rewriter::~float_rewriter() {
}

void float_rewriter::updt_params(params_ref const & p) {
}
 
void float_rewriter::get_param_descrs(param_descrs & r) {
}

br_status float_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
    br_status st = BR_FAILED;
    SASSERT(f->get_family_id() == get_fid());
    switch (f->get_decl_kind()) {
    case OP_TO_FLOAT:        st = mk_to_float(f, num_args, args, result); break;
    case OP_FLOAT_ADD:       SASSERT(num_args == 3); st = mk_add(args[0], args[1], args[2], result); break;
    case OP_FLOAT_SUB:       SASSERT(num_args == 3); st = mk_sub(args[0], args[1], args[2], result); break;
    case OP_FLOAT_UMINUS:    SASSERT(num_args == 1); st = mk_uminus(args[0], result); break;
    case OP_FLOAT_MUL:       SASSERT(num_args == 3); st = mk_mul(args[0], args[1], args[2], result); break;
    case OP_FLOAT_DIV:       SASSERT(num_args == 3); st = mk_div(args[0], args[1], args[2], result); break;
    case OP_FLOAT_REM:       SASSERT(num_args == 2); st = mk_rem(args[0], args[1], result); break;
    case OP_FLOAT_ABS:       SASSERT(num_args == 1); st = mk_abs(args[0], result); break;
    case OP_FLOAT_MIN:       SASSERT(num_args == 2); st = mk_min(args[0], args[1], result); break;
    case OP_FLOAT_MAX:       SASSERT(num_args == 2); st = mk_max(args[0], args[1], result); break;
    case OP_FLOAT_FUSED_MA:  SASSERT(num_args == 4); st = mk_fused_ma(args[0], args[1], args[2], args[3], result); break;
    case OP_FLOAT_SQRT:      SASSERT(num_args == 2); st = mk_sqrt(args[0], args[1], result); break;
    case OP_FLOAT_ROUND_TO_INTEGRAL: SASSERT(num_args == 2); st = mk_round(args[0], args[1], result); break;

    case OP_FLOAT_EQ:        SASSERT(num_args == 2); st = mk_float_eq(args[0], args[1], result); break; 
    case OP_FLOAT_LT:        SASSERT(num_args == 2); st = mk_lt(args[0], args[1], result); break;
    case OP_FLOAT_GT:        SASSERT(num_args == 2); st = mk_gt(args[0], args[1], result); break;
    case OP_FLOAT_LE:        SASSERT(num_args == 2); st = mk_le(args[0], args[1], result); break;
    case OP_FLOAT_GE:        SASSERT(num_args == 2); st = mk_ge(args[0], args[1], result); break;
    case OP_FLOAT_IS_ZERO:   SASSERT(num_args == 1); st = mk_is_zero(args[0], result); break;
    case OP_FLOAT_IS_NZERO:  SASSERT(num_args == 1); st = mk_is_nzero(args[0], result); break;
    case OP_FLOAT_IS_PZERO:  SASSERT(num_args == 1); st = mk_is_pzero(args[0], result); break;
    case OP_FLOAT_IS_SIGN_MINUS: SASSERT(num_args == 1); st = mk_is_sign_minus(args[0], result); break;
    }
    return st;
}

br_status float_rewriter::mk_to_float(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
    SASSERT(f->get_num_parameters() == 2);
    SASSERT(f->get_parameter(0).is_int());
    SASSERT(f->get_parameter(1).is_int());
    unsigned ebits = f->get_parameter(0).get_int();
    unsigned sbits = f->get_parameter(1).get_int();

    if (num_args == 2) {
        mpf_rounding_mode rm;
        if (!m_util.is_rm(args[0], rm))
            return BR_FAILED;
        
        rational q;
        if (!m_util.au().is_numeral(args[1], q))
            return BR_FAILED;
        
        mpf v;
        m_util.fm().set(v, ebits, sbits, rm, q.to_mpq());
        result = m_util.mk_value(v);
        m_util.fm().del(v);
        return BR_DONE;
    }
    else if (num_args == 3 && 
             m_util.is_rm(m().get_sort(args[0])) && 
             m_util.au().is_real(args[1]) &&
             m_util.au().is_int(args[2])) {

        mpf_rounding_mode rm;
        if (!m_util.is_rm(args[0], rm))
            return BR_FAILED;

        rational q;
        if (!m_util.au().is_numeral(args[1], q))
            return BR_FAILED;

        rational e;
        if (!m_util.au().is_numeral(args[2], e))
            return BR_FAILED;

        TRACE("fp_rewriter", tout << "q: " << q << ", e: " << e << "\n";);

        mpf v;
	    m_util.fm().set(v, ebits, sbits, rm, q.to_mpq(), e.to_mpq().numerator());
        result = m_util.mk_value(v);
        m_util.fm().del(v);
        return BR_DONE;
    }
    else {
        return BR_FAILED;
    }
}

br_status float_rewriter::mk_add(expr * arg1, expr * arg2, expr * arg3, expr_ref & result) {    
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm()), v3(m_util.fm());
        if (m_util.is_value(arg2, v2) && m_util.is_value(arg3, v3)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().add(rm, v2, v3, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_sub(expr * arg1, expr * arg2, expr * arg3, expr_ref & result) {
    // a - b = a + (-b)
    result = m_util.mk_add(arg1, arg2, m_util.mk_uminus(arg3));
    return BR_REWRITE2;
}

br_status float_rewriter::mk_mul(expr * arg1, expr * arg2, expr * arg3, expr_ref & result) {    
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm()), v3(m_util.fm());
        if (m_util.is_value(arg2, v2) && m_util.is_value(arg3, v3)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().mul(rm, v2, v3, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_div(expr * arg1, expr * arg2, expr * arg3, expr_ref & result) {
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm()), v3(m_util.fm());
        if (m_util.is_value(arg2, v2) && m_util.is_value(arg3, v3)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().div(rm, v2, v3, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_uminus(expr * arg1, expr_ref & result) {
    if (m_util.is_nan(arg1)) {
        // -nan --> nan
        result = arg1;
        return BR_DONE;
    }
    if (m_util.is_plus_inf(arg1)) {
        // - +oo --> -oo
        result = m_util.mk_minus_inf(m().get_sort(arg1));
        return BR_DONE;
    }
    if (m_util.is_minus_inf(arg1)) {
        // - -oo -> +oo
        result = m_util.mk_plus_inf(m().get_sort(arg1));
        return BR_DONE;
    }
    if (m_util.is_uminus(arg1)) {
        // - - a --> a
        result = to_app(arg1)->get_arg(0);
        return BR_DONE;
    }
    
    scoped_mpf v1(m_util.fm());
    if (m_util.is_value(arg1, v1)) {
        m_util.fm().neg(v1);
        result = m_util.mk_value(v1);
        return BR_DONE;
    }

    // TODO: more simplifications
    return BR_FAILED;
}

br_status float_rewriter::mk_rem(expr * arg1, expr * arg2, expr_ref & result) {
    scoped_mpf v1(m_util.fm()), v2(m_util.fm());
    if (m_util.is_value(arg1, v1) && m_util.is_value(arg2, v2)) {
        scoped_mpf t(m_util.fm());
        m_util.fm().rem(v1, v2, t);
        result = m_util.mk_value(t);
        return BR_DONE;
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_abs(expr * arg1, expr_ref & result) {
    if (m_util.is_nan(arg1)) {
        result = arg1;
        return BR_DONE;
    }
    sort * s = m().get_sort(arg1);
    result = m().mk_ite(m_util.mk_lt(arg1, m_util.mk_pzero(s)),
                        m_util.mk_uminus(arg1),
                        arg1);
    return BR_REWRITE2;
}

br_status float_rewriter::mk_min(expr * arg1, expr * arg2, expr_ref & result) {
    if (m_util.is_nan(arg1)) {
        result = arg2;
        return BR_DONE;
    }
    if (m_util.is_nan(arg2)) {
        result = arg1;
        return BR_DONE;
    }
    // expand as using ite's
    result = m().mk_ite(mk_eq_nan(arg1),
                        arg2,
                        m().mk_ite(mk_eq_nan(arg2), 
                                   arg1,
                                   m().mk_ite(m_util.mk_lt(arg1, arg2),
                                              arg1,
                                              arg2)));
    return BR_REWRITE_FULL;
}

br_status float_rewriter::mk_max(expr * arg1, expr * arg2, expr_ref & result) {
    if (m_util.is_nan(arg1)) {
        result = arg2;
        return BR_DONE;
    }
    if (m_util.is_nan(arg2)) {
        result = arg1;
        return BR_DONE;
    }
    // expand as using ite's
    result = m().mk_ite(mk_eq_nan(arg1), 
                        arg2,
                        m().mk_ite(mk_eq_nan(arg2), 
                                   arg1,
                                   m().mk_ite(m_util.mk_gt(arg1, arg2),
                                              arg1,
                                              arg2)));
    return BR_REWRITE_FULL;
}

br_status float_rewriter::mk_fused_ma(expr * arg1, expr * arg2, expr * arg3, expr * arg4, expr_ref & result) {
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm()), v3(m_util.fm()), v4(m_util.fm());
        if (m_util.is_value(arg2, v2) && m_util.is_value(arg3, v3) && m_util.is_value(arg4, v4)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().fused_mul_add(rm, v2, v3, v4, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_sqrt(expr * arg1, expr * arg2, expr_ref & result) {
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm());
        if (m_util.is_value(arg2, v2)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().sqrt(rm, v2, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_round(expr * arg1, expr * arg2, expr_ref & result) {
    mpf_rounding_mode rm;
    if (m_util.is_rm(arg1, rm)) {
        scoped_mpf v2(m_util.fm());
        if (m_util.is_value(arg2, v2)) {
            scoped_mpf t(m_util.fm());
            m_util.fm().round_to_integral(rm, v2, t);
            result = m_util.mk_value(t);
            return BR_DONE;
        }
    }

    return BR_FAILED;
}

// This the floating point theory ==
br_status float_rewriter::mk_float_eq(expr * arg1, expr * arg2, expr_ref & result) {
    scoped_mpf v1(m_util.fm()), v2(m_util.fm());
    if (m_util.is_value(arg1, v1) && m_util.is_value(arg2, v2)) {
        result = (m_util.fm().eq(v1, v2)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

// Return (= arg NaN)
app * float_rewriter::mk_eq_nan(expr * arg) {
    return m().mk_eq(arg, m_util.mk_nan(m().get_sort(arg)));
}

// Return (not (= arg NaN))
app * float_rewriter::mk_neq_nan(expr * arg) {
    return m().mk_not(mk_eq_nan(arg));
}

br_status float_rewriter::mk_lt(expr * arg1, expr * arg2, expr_ref & result) {
    if (m_util.is_nan(arg1) || m_util.is_nan(arg2)) {
        result = m().mk_false();
        return BR_DONE;
    }
    if (m_util.is_minus_inf(arg1)) {
        // -oo < arg2 -->  not(arg2 = -oo) and not(arg2 = NaN)
        result = m().mk_and(m().mk_not(m().mk_eq(arg2, arg1)), mk_neq_nan(arg2));
        return BR_REWRITE2;
    }
    if (m_util.is_minus_inf(arg2)) {
        // arg1 < -oo  --> false
        result = m().mk_false();
        return BR_DONE;
    }
    if (m_util.is_plus_inf(arg1)) {
        // +oo < arg2 --> false
        result = m().mk_false();
        return BR_DONE;
    }
    if (m_util.is_plus_inf(arg2)) {
        // arg1 < +oo --> not(arg1 = +oo) and not(arg1 = NaN)
        result = m().mk_and(m().mk_not(m().mk_eq(arg1, arg2)), mk_neq_nan(arg1));
        return BR_REWRITE2;
    }

    scoped_mpf v1(m_util.fm()), v2(m_util.fm());
    if (m_util.is_value(arg1, v1) && m_util.is_value(arg2, v2)) {
        result = (m_util.fm().lt(v1, v2)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    // TODO: more simplifications
    return BR_FAILED;
}

br_status float_rewriter::mk_gt(expr * arg1, expr * arg2, expr_ref & result) {
    result = m_util.mk_lt(arg2, arg1);
    return BR_REWRITE1;
}

br_status float_rewriter::mk_le(expr * arg1, expr * arg2, expr_ref & result) {
    if (m_util.is_nan(arg1) || m_util.is_nan(arg2)) {
        result = m().mk_false();
        return BR_DONE;
    }
    scoped_mpf v1(m_util.fm()), v2(m_util.fm());
    if (m_util.is_value(arg1, v1) && m_util.is_value(arg2, v2)) {
        result = (m_util.fm().le(v1, v2)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_ge(expr * arg1, expr * arg2, expr_ref & result) {
    result = m_util.mk_le(arg2, arg1);
    return BR_REWRITE1;
}

br_status float_rewriter::mk_is_zero(expr * arg1, expr_ref & result) {
    scoped_mpf v(m_util.fm());
    if (m_util.is_value(arg1, v)) {
        result = (m_util.fm().is_zero(v)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_is_nzero(expr * arg1, expr_ref & result) {
    scoped_mpf v(m_util.fm());
    if (m_util.is_value(arg1, v)) {
        result = (m_util.fm().is_nzero(v)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_is_pzero(expr * arg1, expr_ref & result) {
    scoped_mpf v(m_util.fm());
    if (m_util.is_value(arg1, v)) {
        result = (m_util.fm().is_pzero(v)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

br_status float_rewriter::mk_is_sign_minus(expr * arg1, expr_ref & result) {
    scoped_mpf v(m_util.fm());
    if (m_util.is_value(arg1, v)) {
        result = (m_util.fm().is_neg(v)) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}

// This the SMT =
br_status float_rewriter::mk_eq_core(expr * arg1, expr * arg2, expr_ref & result) {
    scoped_mpf v1(m_util.fm()), v2(m_util.fm());
    if (m_util.is_value(arg1, v1) && m_util.is_value(arg2, v2)) {
        result = (v1 == v2) ? m().mk_true() : m().mk_false();
        return BR_DONE;
    }

    return BR_FAILED;
}