Skip to content

Commit 04e9cc5

Browse files
committed
compiler: Keep a real literal at a half-precision Operator's precision
`_prec` floors an untyped real literal at `float32` so that an integer default doesn't degrade the arithmetic around it. That floor also caught `float16`, which is never a fallback but an explicit request, so every literal in a half-precision Operator printed one type too wide. Only apply the floor when the default is not already a real type.
1 parent 32b54e9 commit 04e9cc5

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎devito/ir/cgen/printer.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def _prec(self, expr):
7979
dtype = sympy_dtype(expr, default=self.dtype)
8080
if dtype is None or np.issubdtype(dtype, np.integer):
8181
if any(isinstance(i, Float) for i in expr.atoms()):
82+
# A real literal in an otherwise integer (or untyped)
83+
# expression is emitted at the Operator's precision, floored at
84+
# `float32` so that an integer default doesn't degrade it.
85+
# A `float16` default is a deliberate choice though, so leave
86+
# it alone rather than silently widening the arithmetic
87+
if np.issubdtype(self.dtype, np.floating):
88+
return self.dtype
8289
try:
8390
return np.promote_types(self.dtype, np.float32).type
8491
except np.exceptions.DTypePromotionError:

0 commit comments

Comments
 (0)