File: GB_op_name_and_defn.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 (107 lines) | stat: -rw-r--r-- 3,835 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
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
//------------------------------------------------------------------------------
// GB_op_name_and_defn: construct name and defn of a user-defined op
//------------------------------------------------------------------------------

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

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

// This method constructs the name and defn of a user-defined op (unary,
// binary, or indexunary).  It also constructs the op->hash for the jit,
// which is never zero for this method.

#include "GB.h"
#include <ctype.h>
#include "jitifyer/GB_jitifyer.h"

GrB_Info GB_op_name_and_defn
(
    // output
    char *op_name,              // op->name of the GrB operator struct
    int32_t *op_name_len,       // op->name_len
    uint64_t *op_hash,          // op->hash
    char **op_defn,             // op->defn
    size_t *op_defn_size,       // op->defn_size
    // input
    const char *input_name,     // user-provided name, may be NULL
    const char *input_defn,     // user-provided name, may be NULL
    bool user_op,               // if true, a user-defined op
    bool jitable                // if true, the op can be JIT'd
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    ASSERT (op_name != NULL) ;
    ASSERT (op_name_len != NULL) ;
    ASSERT (op_hash != NULL) ;
    ASSERT (op_defn != NULL) ;
    ASSERT (op_defn_size != NULL) ;
    (*op_defn) = NULL ;
    (*op_defn_size) = 0 ;

    //--------------------------------------------------------------------------
    // get the name of the operator
    //--------------------------------------------------------------------------

    // note: this can get a mangled name; see the BF methods in LAGraph

    memset (op_name, 0, GxB_MAX_NAME_LEN) ;
    if (input_name != NULL)
    {
        // copy the input_name
        strncpy (op_name, input_name, GxB_MAX_NAME_LEN-1) ;
    }

    // ensure op_name is null-terminated
    op_name [GxB_MAX_NAME_LEN-1] = '\0' ;

    // get the operator name length (zero if no name given)
    (*op_name_len) = (int32_t) strlen (op_name) ;

    //--------------------------------------------------------------------------
    // get the definition of the operator, if present
    //--------------------------------------------------------------------------

    char *defn = NULL ;
    size_t defn_size = 0 ;
    size_t defn_len = 0 ;

    if (input_defn != NULL)
    { 
        // determine the string length of the definition
        defn_len = strlen (input_defn) ;

        // allocate space for the definition
        defn = GB_MALLOC_MEMORY (defn_len+1, sizeof (char), &defn_size) ;
        if (defn == NULL)
        { 
            // out of memory
            return (GrB_OUT_OF_MEMORY) ;
        }

        // copy the definition into the new operator
        memcpy (defn, input_defn, defn_len+1) ;
    }

    //--------------------------------------------------------------------------
    // compute the operator hash
    //--------------------------------------------------------------------------

    // a user-defined op can only be JIT'd if it has a name and defn.
    // a new builtin op (created by GB_reduce_to_vector) can always be JIT'd.
    (*op_hash) = GB_jitifyer_hash (op_name, (*op_name_len),
        jitable && (!user_op || ((*op_name_len) > 0 && defn_len > 0))) ;

    //--------------------------------------------------------------------------
    // return result
    //--------------------------------------------------------------------------

    (*op_defn) = defn ;
    (*op_defn_size) = defn_size ;
    return (GrB_SUCCESS) ;
}