1 ; SPDX-License-Identifier: LGPL-2.1-or-later
3 ; Test to see if using smt-lib2's floating-point support for checking fpu hw
4 ; is feasible by implementing fp multiplication with bit-vectors and seeing if
5 ; the smt checkers work. The idea is we can run this test before putting in
6 ; all the effort to add support in yosys and nmigen for smtlib2 reals and
7 ; floating-point numbers.
9 ; run with: z3 -smt2 fpmul_test.smt2
11 ; create some handy type aliases
12 (define-sort bv1 () (_ BitVec 1))
13 (define-sort bv2 () (_ BitVec 2))
14 (define-sort bv4 () (_ BitVec 4))
15 (define-sort bv5 () (_ BitVec 5))
16 (define-sort bv8 () (_ BitVec 8))
17 (define-sort bv10 () (_ BitVec 10))
18 (define-sort bv11 () (_ BitVec 11))
19 (define-sort bv16 () (_ BitVec 16))
20 (define-sort bv22 () (_ BitVec 22))
21 (define-sort bv32 () (_ BitVec 32))
23 ; type for signed f16 exponents
24 (define-sort f16_sexp_t () bv8)
25 ; signed less-than comparison
26 (define-fun f16_sexp_lt ((a f16_sexp_t) (b f16_sexp_t)) Bool
27 (bvult (bvxor #x80 a) (bvxor #x80 b))
30 (define-fun f16_sexp_sub ((a f16_sexp_t) (b f16_sexp_t)) f16_sexp_t
34 (define-fun f16_sexp_to_bv5 ((v f16_sexp_t)) bv5 ((_ extract 4 0) v))
35 (define-fun bv5_to_f16_sexp ((v bv5)) f16_sexp_t (concat #b000 v))
36 (define-fun f16_sexp_to_bv22 ((v f16_sexp_t)) bv22 (concat #b00000000000000 v))
37 (define-fun bv22_to_f16_sexp ((v bv22)) f16_sexp_t ((_ extract 7 0) v))
38 (define-fun bv11_to_bv22 ((v bv11)) bv22 (concat #b00000000000 v))
39 (define-fun bv22_to_bv11 ((v bv22)) bv11 ((_ extract 10 0) v))
40 (define-fun bv22_to_bv32 ((v bv22)) bv32 (concat #b0000000000 v))
41 (define-fun bv32_to_bv22 ((v bv32)) bv22 ((_ extract 21 0) v))
42 (define-fun bv16_to_bv32 ((v bv16)) bv32 (concat #x0000 v))
43 (define-fun bv32_to_bv16 ((v bv32)) bv16 ((_ extract 15 0) v))
44 (define-fun bv8_to_bv16 ((v bv8)) bv16 (concat #x00 v))
45 (define-fun bv16_to_bv8 ((v bv16)) bv8 ((_ extract 7 0) v))
46 (define-fun bv4_to_bv8 ((v bv4)) bv8 (concat #x0 v))
47 (define-fun bv8_to_bv4 ((v bv8)) bv4 ((_ extract 3 0) v))
48 (define-fun bv2_to_bv4 ((v bv2)) bv4 (concat #b00 v))
49 (define-fun bv4_to_bv2 ((v bv4)) bv2 ((_ extract 1 0) v))
50 (define-fun bv1_to_bv2 ((v bv1)) bv2 (concat #b0 v))
51 (define-fun bv2_to_bv1 ((v bv2)) bv1 ((_ extract 0 0) v))
53 (define-fun bv1_clz ((v bv1)) bv1
56 (define-fun bv2_clz ((v bv2)) bv2
58 ((shift (ite (bvult #b01 v) #b00 #b01)))
59 (bvadd shift (bv1_to_bv2 (bv1_clz ((_ extract 1 1) (bvshl v shift)))))
62 (define-fun bv4_clz ((v bv4)) bv4
64 ((shift (ite (bvult #x3 v) #x0 #x2)))
65 (bvadd shift (bv2_to_bv4 (bv2_clz ((_ extract 3 2) (bvshl v shift)))))
68 (define-fun bv8_clz ((v bv8)) bv8
70 ((shift (ite (bvult #x0F v) #x00 #x04)))
71 (bvadd shift (bv4_to_bv8 (bv4_clz ((_ extract 7 4) (bvshl v shift)))))
74 (define-fun bv16_clz ((v bv16)) bv16
76 ((shift (ite (bvult #x00FF v) #x0000 #x0008)))
77 (bvadd shift (bv8_to_bv16 (bv8_clz
78 ((_ extract 15 8) (bvshl v shift)))))
81 (define-fun bv32_clz ((v bv32)) bv32
83 ((shift (ite (bvult #x0000FFFF v) #x00000000 #x00000010)))
84 (bvadd shift (bv16_to_bv32 (bv16_clz
85 ((_ extract 31 16) (bvshl v shift)))))
88 (define-fun bv22_clz ((v bv22)) bv22
89 (bv32_to_bv22 (bv32_clz (concat v #b0000000000)))
91 ; shift right merging shifted out bits into the result's lsb
92 (define-fun bv22_lshr_merging ((v bv22) (shift bv22)) bv22
93 ; did we shift out only zeros?
94 (ite (= v (bvshl (bvlshr v shift) shift))
95 ; yes. no adjustment needed
98 (bvor (bvlshr v shift) #b0000000000000000000001)
102 ; field extraction functions
103 (define-fun f16_sign_field ((v bv16)) bv1 ((_ extract 15 15) v))
104 (define-fun f16_exponent_field ((v bv16)) bv5 ((_ extract 14 10) v))
105 (define-fun f16_mantissa_field ((v bv16)) bv10 ((_ extract 9 0) v))
106 (define-fun f16_mantissa_field_msb ((v bv16)) bv1 ((_ extract 9 9) v))
107 ; construction from fields
108 (define-fun f16_from_fields ((sign_field bv1)
110 (mantissa_field bv10)) bv16
111 (concat sign_field exponent_field mantissa_field)
114 (define-fun f16_infinity ((sign_field bv1)) bv16
115 (f16_from_fields sign_field #b11111 #b0000000000)
117 (define-fun f16_zero ((sign_field bv1)) bv16
118 (f16_from_fields sign_field #b00000 #b0000000000)
120 ; conversion to quiet NaN
121 (define-fun f16_into_qnan ((v bv16)) bv16
125 (bvor #b1000000000 (f16_mantissa_field v))
129 (define-fun f16_to_fp ((v bv16)) Float16 ((_ to_fp 5 11) v))
131 (define-fun f16_is_nan ((v bv16)) Bool (fp.isNaN (f16_to_fp v)))
132 (define-fun f16_is_infinite ((v bv16)) Bool (fp.isInfinite (f16_to_fp v)))
133 (define-fun f16_is_normal ((v bv16)) Bool (fp.isNormal (f16_to_fp v)))
134 (define-fun f16_is_subnormal ((v bv16)) Bool (fp.isSubnormal (f16_to_fp v)))
135 (define-fun f16_is_zero ((v bv16)) Bool (fp.isZero (f16_to_fp v)))
136 (define-fun f16_is_qnan ((v bv16)) Bool
137 (and (f16_is_nan v) (= (f16_mantissa_field_msb v) #b1))
139 ; get mantissa value -- only correct for finite values
140 (define-fun f16_mantissa_value ((v bv16)) bv11
141 (ite (f16_is_subnormal v)
142 (concat #b0 (f16_mantissa_field v))
143 (concat #b1 (f16_mantissa_field v))
147 (define-const f16_exponent_bias f16_sexp_t #x0F)
148 (define-const f16_max_exponent f16_sexp_t #x10)
149 (define-const f16_subnormal_exponent f16_sexp_t #xF2) ; -14
150 (define-fun f16_exponent_value ((v bv16)) f16_sexp_t
151 (ite (= (f16_exponent_field v) #b00000)
152 f16_subnormal_exponent
154 (bv5_to_f16_sexp (f16_exponent_field v))
160 (define-fun f16_round_product_final_step_rne ((sign bv1)
162 (exponent f16_sexp_t)
163 (exponent_field bv5)) bv16
164 ; if the exponent doesn't overflow
165 (ite (f16_sexp_lt exponent f16_max_exponent)
166 ; if we rounded a subnormal up to a normal
167 (ite (and (= exponent_field #b00000) (not (bvult product #b1000000000000000000000)))
171 ((_ extract 20 11) product)
176 ((_ extract 20 11) product)
182 (define-fun f16_round_product_rne ((sign bv1)
184 (exponent f16_sexp_t)
185 (exponent_field bv5)) bv16
188 (half_way (= (bv22_to_bv11 product) #b10000000000))
189 (is_even (= ((_ extract 11 11) product) #b0))
190 (rounded_up (bvadd product (bv11_to_bv22 #b10000000000)))
194 (round_up_overflows (bvult rounded_up product))
198 (bvult #b10000000000 (bv22_to_bv11 product))
203 (ite round_up_overflows
204 (f16_round_product_final_step_rne
207 (bvlshr rounded_up #b0000000000000000000001)
208 #b1000000000000000000000
210 (bvadd exponent #x01)
211 (bvadd exponent_field #b00001)
213 (f16_round_product_final_step_rne
214 sign rounded_up exponent exponent_field)
216 (f16_round_product_final_step_rne
217 sign product exponent exponent_field)
222 (define-fun f16_mul_nonzero_finite_rne ((a bv16) (b bv16)) bv16
225 (product (bvmul (bv11_to_bv22 (f16_mantissa_value a))
226 (bv11_to_bv22 (f16_mantissa_value b))))
227 (sign (bvxor (f16_sign_field a) (f16_sign_field b)))
228 (exponent (bvadd (f16_exponent_value a) (f16_exponent_value b)))
233 (norm_product (bvshl product (bv22_clz product)))
238 ; compensation for product changing from having two
239 ; integer-part bits to one by normalization
242 (bvneg (bv22_to_f16_sexp (bv22_clz product)))
248 ; amount to shift norm_product right to de-normalize again
251 (f16_sexp_sub f16_subnormal_exponent norm_exponent)
254 ; if subnormal_shift would not cause the mantissa to overflow
255 (ite (f16_sexp_lt #x00 subnormal_shift)
257 (f16_round_product_rne
261 (f16_sexp_to_bv22 subnormal_shift)
263 f16_subnormal_exponent
267 (f16_round_product_rne
271 (f16_sexp_to_bv5 (bvadd norm_exponent
280 (define-fun f16_mul_rne ((a bv16) (b bv16)) bv16
287 (and (f16_is_zero a) (f16_is_infinite b))
288 (and (f16_is_infinite a) (f16_is_zero b))
291 (ite (or (f16_is_infinite a) (f16_is_infinite b))
292 (f16_infinity (bvxor (f16_sign_field a) (f16_sign_field b)))
293 (ite (or (f16_is_zero a) (f16_is_zero b))
294 (f16_zero (bvxor (f16_sign_field a) (f16_sign_field b)))
295 (f16_mul_nonzero_finite_rne a b)
303 ; input values in ieee754 f16 format as bit-vectors
304 (declare-const a bv16)
305 (declare-const b bv16)
306 ; product for debugging
307 (declare-const p bv16)
308 (assert (= (f16_to_fp p) (fp.mul RNE (f16_to_fp a) (f16_to_fp b))))
309 ; intermediate values from f16_mul_nonzero_finite_rne for debugging
310 (define-const product bv22 (bvmul (bv11_to_bv22 (f16_mantissa_value a))
311 (bv11_to_bv22 (f16_mantissa_value b))))
312 (define-const sign bv1 (bvxor (f16_sign_field a) (f16_sign_field b)))
313 (define-const exponent f16_sexp_t (bvadd (f16_exponent_value a) (f16_exponent_value b)))
314 (define-const norm_product bv22 (bvshl product (bv22_clz product)))
315 (define-const norm_exponent f16_sexp_t
319 ; compensation for product changing from having two
320 ; integer-part bits to one by normalization
323 (bvneg (bv22_to_f16_sexp (bv22_clz product)))
326 (define-const subnormal_shift f16_sexp_t
327 (f16_sexp_sub f16_subnormal_exponent norm_exponent)
329 ; intermediate values from f16_round_product_rne when the result is subnormal:
330 (define-const product_subnormal bv22
333 (f16_sexp_to_bv22 subnormal_shift)
336 (define-const half_way_subnormal Bool
337 (= (bv22_to_bv11 product_subnormal) #b10000000000))
338 (define-const is_even_subnormal Bool
339 (= ((_ extract 11 11) product_subnormal) #b0))
340 (define-const rounded_up_subnormal bv22
341 (bvadd product_subnormal (bv11_to_bv22 #b10000000000)))
342 (define-const round_up_overflows_subnormal Bool
343 (bvult rounded_up_subnormal product_subnormal))
344 (define-const do_round_up_subnormal Bool
345 (ite half_way_subnormal
346 (not is_even_subnormal)
347 (bvult #b10000000000 (bv22_to_bv11 product_subnormal))
350 ; intermediate values from f16_round_product_rne when the result is normal:
351 (define-const exponent_field_normal bv5
352 (f16_sexp_to_bv5 (bvadd norm_exponent f16_exponent_bias))
354 (define-const half_way_normal Bool (= (bv22_to_bv11 norm_product) #b10000000000))
355 (define-const is_even_normal Bool (= ((_ extract 11 11) norm_product) #b0))
356 (define-const rounded_up_normal bv22
357 (bvadd norm_product (bv11_to_bv22 #b10000000000))
359 (define-const round_up_overflows_normal Bool (bvult rounded_up_normal norm_product))
360 (define-const do_round_up_normal Bool
363 (bvult #b10000000000 (bv22_to_bv11 norm_product))
369 ; now look for a case where f16_mul_rne is broke:
371 (f16_to_fp (f16_mul_rne a b))
372 (fp.mul RNE (f16_to_fp a) (f16_to_fp b))
374 ; should return unsat, meaning there aren't any broken cases
375 (echo "should return unsat:")
377 (echo "dumping values in case it returned sat:")
384 (fp.mul RNE (f16_to_fp a) (f16_to_fp b))
385 (f16_to_fp (f16_mul_rne a b))
386 (f16_mul_nonzero_finite_rne a b)
387 (f16_mantissa_field a)
388 (f16_mantissa_value a)
389 (f16_mantissa_field b)
390 (f16_mantissa_value b)
402 round_up_overflows_subnormal
403 do_round_up_subnormal
404 exponent_field_normal
408 round_up_overflows_normal