File: GB_macrofy_cast_output.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (70 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download | duplicates (2)
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
//------------------------------------------------------------------------------
// GB_macrofy_cast_output: construct a macro and defn for typecasting to output
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// constructs a macro of the form:

//      #define macro(z,x...) ... = z

// The name of the macro is given by macro_name.
// The name of the variable z is given by zarg.
// The outputs x,... are given by xargs (which may contain a comma).
// xexpr is an expression using the xargs that produce a value of type xtype.
// z has type ztype.

// This method is very similar to GB_macrofy_cast_input.

#include "GB.h"
#include "jitifyer/GB_stringify.h"

void GB_macrofy_cast_output
(
    FILE *fp,
    // input:
    const char *macro_name,     // name of the macro: #define macro(z,x...)
    const char *zarg,           // name of the z argument of the macro
    const char *xargs,          // one or more x arguments
    const char *xexpr,          // an expression based on xargs
    const GrB_Type ztype,       // the type of the z input
    const GrB_Type xtype        // the type of the x output
)
{

    if (ztype == NULL || xtype == NULL)
    { 
        // empty macro if xtype or ztype are NULL (value not needed)
        fprintf (fp, "#define %s(%s,%s)\n", macro_name, zarg, xargs) ;
        return ;
    }

    int nargs ;
    const char *f = GB_macrofy_cast_expression (fp, xtype, ztype, &nargs) ;

    if (f == NULL)
    { 
        // C11 typecasting
        ASSERT (ztype != xtype) ;
        fprintf (fp, "#define %s(%s,%s) %s = (%s) (%s)\n",
            macro_name, zarg, xargs, xexpr, xtype->name, zarg) ;
    }
    else
    {
        // GraphBLAS typecasting, or no typecasting
        fprintf (fp, "#define %s(%s,%s) ", macro_name, zarg, xargs) ;
        if (nargs == 3)
        { 
            fprintf (fp, f, xexpr, zarg, zarg) ;
        }
        else
        { 
            fprintf (fp, f, xexpr, zarg) ;
        }
        fprintf (fp, "\n") ;
    }
}