change run command
[ieee754fpu.git] / fp16mul_test.smt2
1 ; SPDX-License-Identifier: LGPL-2.1-or-later
2
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.
8
9 ; run with: z3 -smt2 fp16mul_test.smt2
10
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))
22
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))
28 )
29 ; subtraction
30 (define-fun f16_sexp_sub ((a f16_sexp_t) (b f16_sexp_t)) f16_sexp_t
31 (bvadd a (bvneg b))
32 )
33 ; conversion
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))
52 ; count-leading-zeros
53 (define-fun bv1_clz ((v bv1)) bv1
54 (bvxor #b1 v)
55 )
56 (define-fun bv2_clz ((v bv2)) bv2
57 (let
58 ((shift (ite (bvult #b01 v) #b00 #b01)))
59 (bvadd shift (bv1_to_bv2 (bv1_clz ((_ extract 1 1) (bvshl v shift)))))
60 )
61 )
62 (define-fun bv4_clz ((v bv4)) bv4
63 (let
64 ((shift (ite (bvult #x3 v) #x0 #x2)))
65 (bvadd shift (bv2_to_bv4 (bv2_clz ((_ extract 3 2) (bvshl v shift)))))
66 )
67 )
68 (define-fun bv8_clz ((v bv8)) bv8
69 (let
70 ((shift (ite (bvult #x0F v) #x00 #x04)))
71 (bvadd shift (bv4_to_bv8 (bv4_clz ((_ extract 7 4) (bvshl v shift)))))
72 )
73 )
74 (define-fun bv16_clz ((v bv16)) bv16
75 (let
76 ((shift (ite (bvult #x00FF v) #x0000 #x0008)))
77 (bvadd shift (bv8_to_bv16 (bv8_clz
78 ((_ extract 15 8) (bvshl v shift)))))
79 )
80 )
81 (define-fun bv32_clz ((v bv32)) bv32
82 (let
83 ((shift (ite (bvult #x0000FFFF v) #x00000000 #x00000010)))
84 (bvadd shift (bv16_to_bv32 (bv16_clz
85 ((_ extract 31 16) (bvshl v shift)))))
86 )
87 )
88 (define-fun bv22_clz ((v bv22)) bv22
89 (bv32_to_bv22 (bv32_clz (concat v #b0000000000)))
90 )
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
96 (bvlshr v shift)
97 ; no. set lsb
98 (bvor (bvlshr v shift) #b0000000000000000000001)
99 )
100 )
101
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)
109 (exponent_field bv5)
110 (mantissa_field bv10)) bv16
111 (concat sign_field exponent_field mantissa_field)
112 )
113 ; handy constants
114 (define-fun f16_infinity ((sign_field bv1)) bv16
115 (f16_from_fields sign_field #b11111 #b0000000000)
116 )
117 (define-fun f16_zero ((sign_field bv1)) bv16
118 (f16_from_fields sign_field #b00000 #b0000000000)
119 )
120 ; conversion to quiet NaN
121 (define-fun f16_into_qnan ((v bv16)) bv16
122 (f16_from_fields
123 (f16_sign_field v)
124 #b11111
125 (bvor #b1000000000 (f16_mantissa_field v))
126 )
127 )
128 ; conversion
129 (define-fun f16_to_fp ((v bv16)) Float16 ((_ to_fp 5 11) v))
130 ; classification
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))
138 )
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))
144 )
145 )
146 ; f16 field values
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
153 (f16_sexp_sub
154 (bv5_to_f16_sexp (f16_exponent_field v))
155 f16_exponent_bias
156 )
157 )
158 )
159 ; f16 mul
160 (define-fun f16_round_product_final_step_rne ((sign bv1)
161 (product bv22)
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)))
168 (f16_from_fields
169 sign
170 #b00001
171 ((_ extract 20 11) product)
172 )
173 (f16_from_fields
174 sign
175 exponent_field
176 ((_ extract 20 11) product)
177 )
178 )
179 (f16_infinity sign)
180 )
181 )
182 (define-fun f16_round_product_rne ((sign bv1)
183 (product bv22)
184 (exponent f16_sexp_t)
185 (exponent_field bv5)) bv16
186 (let
187 (
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)))
191 )
192 (let
193 (
194 (round_up_overflows (bvult rounded_up product))
195 (do_round_up
196 (ite half_way
197 (not is_even)
198 (bvult #b10000000000 (bv22_to_bv11 product))
199 )
200 )
201 )
202 (ite do_round_up
203 (ite round_up_overflows
204 (f16_round_product_final_step_rne
205 sign
206 (bvor
207 (bvlshr rounded_up #b0000000000000000000001)
208 #b1000000000000000000000
209 )
210 (bvadd exponent #x01)
211 (bvadd exponent_field #b00001)
212 )
213 (f16_round_product_final_step_rne
214 sign rounded_up exponent exponent_field)
215 )
216 (f16_round_product_final_step_rne
217 sign product exponent exponent_field)
218 )
219 )
220 )
221 )
222 (define-fun f16_mul_nonzero_finite_rne ((a bv16) (b bv16)) bv16
223 (let
224 (
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)))
229 )
230 ; normalize product
231 (let
232 (
233 (norm_product (bvshl product (bv22_clz product)))
234 (norm_exponent
235 (bvadd
236 exponent
237
238 ; compensation for product changing from having two
239 ; integer-part bits to one by normalization
240 #x01
241
242 (bvneg (bv22_to_f16_sexp (bv22_clz product)))
243 )
244 )
245 )
246 (let
247 (
248 ; amount to shift norm_product right to de-normalize again
249 ; for subnormals
250 (subnormal_shift
251 (f16_sexp_sub f16_subnormal_exponent norm_exponent)
252 )
253 )
254 ; if subnormal_shift would not cause the mantissa to overflow
255 (ite (f16_sexp_lt #x00 subnormal_shift)
256 ; subnormals:
257 (f16_round_product_rne
258 sign
259 (bv22_lshr_merging
260 norm_product
261 (f16_sexp_to_bv22 subnormal_shift)
262 )
263 f16_subnormal_exponent
264 #b00000
265 )
266 ; normals:
267 (f16_round_product_rne
268 sign
269 norm_product
270 norm_exponent
271 (f16_sexp_to_bv5 (bvadd norm_exponent
272 f16_exponent_bias))
273 )
274 )
275 )
276 )
277 )
278 )
279
280 (define-fun f16_mul_rne ((a bv16) (b bv16)) bv16
281 (ite (f16_is_nan a)
282 (f16_into_qnan a)
283 (ite (f16_is_nan b)
284 (f16_into_qnan b)
285 (ite
286 (or
287 (and (f16_is_zero a) (f16_is_infinite b))
288 (and (f16_is_infinite a) (f16_is_zero b))
289 )
290 #x7E00
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)
296 )
297 )
298 )
299 )
300 )
301 )
302
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
316 (bvadd
317 exponent
318
319 ; compensation for product changing from having two
320 ; integer-part bits to one by normalization
321 #x01
322
323 (bvneg (bv22_to_f16_sexp (bv22_clz product)))
324 )
325 )
326 (define-const subnormal_shift f16_sexp_t
327 (f16_sexp_sub f16_subnormal_exponent norm_exponent)
328 )
329 ; intermediate values from f16_round_product_rne when the result is subnormal:
330 (define-const product_subnormal bv22
331 (bv22_lshr_merging
332 norm_product
333 (f16_sexp_to_bv22 subnormal_shift)
334 )
335 )
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))
348 )
349 )
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))
353 )
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))
358 )
359 (define-const round_up_overflows_normal Bool (bvult rounded_up_normal norm_product))
360 (define-const do_round_up_normal Bool
361 (ite half_way_normal
362 (not is_even_normal)
363 (bvult #b10000000000 (bv22_to_bv11 norm_product))
364 )
365 )
366
367
368
369 ; now look for a case where f16_mul_rne is broke:
370 (assert (not (=
371 (f16_to_fp (f16_mul_rne a b))
372 (fp.mul RNE (f16_to_fp a) (f16_to_fp b))
373 )))
374 ; should return unsat, meaning there aren't any broken cases
375 (echo "should return unsat:")
376 (check-sat)
377 (echo "dumping values in case it returned sat:")
378 (get-value (
379 a
380 b
381 p
382 (f16_to_fp a)
383 (f16_to_fp b)
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)
391 product
392 sign
393 exponent
394 (bv22_clz product)
395 norm_product
396 norm_exponent
397 subnormal_shift
398 product_subnormal
399 half_way_subnormal
400 is_even_subnormal
401 rounded_up_subnormal
402 round_up_overflows_subnormal
403 do_round_up_subnormal
404 exponent_field_normal
405 half_way_normal
406 is_even_normal
407 rounded_up_normal
408 round_up_overflows_normal
409 do_round_up_normal
410 ))