From 777ab888d5032d7630827f91fd26c388a9a09db2 Mon Sep 17 00:00:00 2001
From: zy69guqi <richard.angersbach@fau.de>
Date: Wed, 22 Jan 2025 16:01:51 +0100
Subject: [PATCH] Use literals for C macros used for the numeric limits

---
 .../backend/platforms/generic_cpu.py          | 26 ++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/pystencils/backend/platforms/generic_cpu.py b/src/pystencils/backend/platforms/generic_cpu.py
index 2b4309627..58b9c7946 100644
--- a/src/pystencils/backend/platforms/generic_cpu.py
+++ b/src/pystencils/backend/platforms/generic_cpu.py
@@ -4,6 +4,7 @@ from typing import Sequence
 from pystencils.backend.ast.expressions import PsCall
 
 from ..functions import CFunction, PsMathFunction, MathFunctions, NumericLimitsFunctions
+from ..literals import PsLiteral
 from ...types import PsIntegerType, PsIeeeFloatType, PsScalarType
 
 from .platform import Platform
@@ -25,7 +26,7 @@ from ..ast.expressions import (
     PsLookup,
     PsGe,
     PsLe,
-    PsTernary,
+    PsTernary, PsLiteralExpr,
 )
 from ..ast.vector import PsVecMemAcc
 from ...types import PsVectorType, PsCustomType
@@ -43,7 +44,7 @@ class GenericCpu(Platform):
 
     @property
     def required_headers(self) -> set[str]:
-        return {"<math.h>", "<limits.h>"}
+        return {"<math.h>", "<limits.h>", "<float.h>"}
 
     def materialize_iteration_space(
         self, body: PsBlock, ispace: IterationSpace
@@ -63,12 +64,25 @@ class GenericCpu(Platform):
         arg_types = (dtype,) * func.num_args
 
         if isinstance(dtype, PsScalarType) and func in (NumericLimitsFunctions.Min, NumericLimitsFunctions.Max):
-            cfunc: CFunction
-            cfunc = CFunction(f"{dtype.c_string()}_{func.function_name}".capitalize(), arg_types, dtype)
-            call.function = cfunc
-            return call
+            # get type prefix for macro
+            # TODO: there must be a better way...
+            tpe = ""
+            match dtype:
+                case PsIeeeFloatType():
+                    match dtype.width:
+                        case 32:
+                            tpe = "FLT"
+                        case 64:
+                            tpe = "DBL"
+                case _:
+                    raise MaterializationError(
+                        f"No implementation available for function {func} on data type {dtype}"
+                    )
+
+            return PsLiteralExpr(PsLiteral(f"{tpe}_{func.function_name}".upper(), dtype))
 
         if isinstance(dtype, PsIeeeFloatType) and dtype.width in (32, 64):
+            cfunc: CFunction
             match func:
                 case (
                     MathFunctions.Exp
-- 
GitLab