1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
//------------------------------------------------------------------------------
// GB_macrofy_cast_expression: construct a typecasting string
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Return a typecast expression to cast from xtype to ztype.
#include "GB.h"
#include "math/GB_math.h"
#include "jitifyer/GB_stringify.h"
const char *GB_macrofy_cast_expression // return cast expression
(
FILE *fp,
// input:
GrB_Type ztype, // output type
GrB_Type xtype, // input type
// output
int *nargs // # of string arguments in output format
)
{
//--------------------------------------------------------------------------
// get inputs
//--------------------------------------------------------------------------
(*nargs) = 2 ;
const char *f = NULL ;
ASSERT (ztype != NULL) ;
ASSERT (xtype != NULL) ;
const GB_Type_code zcode = ztype->code ;
const GB_Type_code xcode = xtype->code ;
if (zcode == xcode)
{
//----------------------------------------------------------------------
// no typecasting
//----------------------------------------------------------------------
// user-defined types come here, and require type->defn and type->name
// to both be defined. If the user-defined type has no name or defn,
// then no JIT kernel can be created for it.
ASSERT (GB_IMPLIES (zcode == GB_UDT_code, (ztype == xtype) &&
ztype->name != NULL && ztype->defn != NULL)) ;
f = "%s = %s" ;
}
else if (zcode == GB_BOOL_code)
{
//----------------------------------------------------------------------
// typecast to boolean
//----------------------------------------------------------------------
if (xcode == GB_FC32_code)
{
f = "%s = (GB_crealf (%s) != 0 || GB_cimagf (%s) != 0)" ;
(*nargs) = 3 ;
}
else if (xcode == GB_FC64_code)
{
f = "%s = (GB_creal (%s) != 0 || GB_cimag (%s) != 0)" ;
(*nargs) = 3 ;
}
else
{
f = "%s = ((%s) != 0)" ;
}
}
else if ((zcode >= GB_INT8_code && zcode <= GB_UINT64_code)
&& (xcode >= GB_FP32_code && zcode <= GB_FC64_code))
{
//----------------------------------------------------------------------
// typecast to integer from floating-point
//----------------------------------------------------------------------
switch (zcode)
{
case GB_INT8_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_int8 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_int8 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_int8 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_int8 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_int8",
GJ_cast_to_int8_DEFN) ;
break ;
case GB_INT16_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_int16 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_int16 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_int16 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_int16 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_int16",
GJ_cast_to_int16_DEFN) ;
break ;
case GB_INT32_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_int32 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_int32 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_int32 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_int32 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_int32",
GJ_cast_to_int32_DEFN) ;
break ;
case GB_INT64_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_int64 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_int64 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_int64 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_int64 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_int64",
GJ_cast_to_int64_DEFN) ;
break ;
case GB_UINT8_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_uint8 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_uint8 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_uint8 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_uint8 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_uint8",
GJ_cast_to_uint8_DEFN) ;
break ;
case GB_UINT16_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_uint16 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_uint16 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_uint16 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_uint16 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_uint16",
GJ_cast_to_uint16_DEFN) ;
break ;
case GB_UINT32_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_uint32 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_uint32 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_uint32 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_uint32 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_uint32",
GJ_cast_to_uint32_DEFN) ;
break ;
case GB_UINT64_code :
switch (xcode)
{
case GB_FP32_code :
f = "%s = GJ_cast_to_uint64 ((double) (%s))" ;
break ;
case GB_FP64_code :
f = "%s = GJ_cast_to_uint64 (%s)" ;
break ;
case GB_FC32_code :
f = "%s = GJ_cast_to_uint64 ((double) GB_crealf (%s))" ;
break ;
case GB_FC64_code :
f = "%s = GJ_cast_to_uint64 (GB_creal (%s))" ;
break ;
default:;
}
GB_macrofy_defn (fp, 0, "GJ_cast_to_uint64",
GJ_cast_to_uint64_DEFN) ;
break ;
default:;
}
}
else
{
//----------------------------------------------------------------------
// all other cases: use C11 typecasting rules
//----------------------------------------------------------------------
f = NULL ;
(*nargs) = 0 ;
}
return (f) ;
}
|