@@ -51,7 +51,7 @@ def with_types(self, arg_id_to_dtype, callables_table):
5151 for id in arg_id_to_dtype :
5252 # since all the below functions are single arg.
5353 if not - 1 <= id <= 0 :
54- raise LoopyError ("%s can only take one argument." % name )
54+ raise LoopyError (f" { name } can only take one argument" )
5555
5656 if 0 not in arg_id_to_dtype or arg_id_to_dtype [0 ] is None :
5757 # the types provided aren't mature enough to specialize the
@@ -69,14 +69,23 @@ def with_types(self, arg_id_to_dtype, callables_table):
6969 elif dtype .numpy_dtype == np .complex128 :
7070 tpname = "cdouble"
7171 else :
72- raise LoopyTypeError ("unexpected complex type '%s'" % dtype )
72+ raise LoopyTypeError (f "unexpected complex type '{ dtype } '" )
7373
7474 return (
7575 self .copy (name_in_target = f"{ tpname } _{ name } " ,
7676 arg_id_to_dtype = {0 : dtype , - 1 : NumpyType (
7777 np .dtype (dtype .numpy_dtype .type (0 ).real ))}),
7878 callables_table )
7979
80+ if name in ["real" , "imag" ]:
81+ if not dtype .is_complex ():
82+ tpname = dtype .numpy_dtype .type .__name__
83+ return (
84+ self .copy (
85+ name_in_target = f"lpy_{ name } _{ tpname } " ,
86+ arg_id_to_dtype = {0 : dtype , - 1 : dtype }),
87+ callables_table )
88+
8089 if name in ["sqrt" , "exp" , "log" ,
8190 "sin" , "cos" , "tan" ,
8291 "sinh" , "cosh" , "tanh" ,
@@ -110,6 +119,23 @@ def with_types(self, arg_id_to_dtype, callables_table):
110119 self .copy (arg_id_to_dtype = arg_id_to_dtype ),
111120 callables_table )
112121
122+ def generate_preambles (self , target ):
123+ name = self .name_in_target
124+ if name .startswith ("lpy_real" ) or name .startswith ("lpy_imag" ):
125+ if name .startswith ("lpy_real" ):
126+ ret = "x"
127+ else :
128+ ret = "0"
129+
130+ dtype = self .arg_id_to_dtype [- 1 ]
131+ ctype = target .dtype_to_typename (dtype )
132+
133+ yield (f"40_{ name } " , f"""
134+ static inline { ctype } { name } ({ ctype } x) {{
135+ return { ret } ;
136+ }}
137+ """ )
138+
113139
114140def get_pyopencl_callables ():
115141 pyopencl_ids = ["sqrt" , "exp" , "log" , "sin" , "cos" , "tan" , "sinh" , "cosh" ,
0 commit comments