1 //------------------------------------------------------------------------------
2 // GB_stringify_identity: return string or enum for identity value
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 #include "GB.h"
11 #include "GB_stringify.h"
12 
13 //------------------------------------------------------------------------------
14 // GB_stringify_identity: macro for identity value
15 //------------------------------------------------------------------------------
16 
GB_stringify_identity(char * identity_macro,GB_Opcode opcode,GB_Type_code zcode)17 void GB_stringify_identity     // return string for identity value
18 (
19     // output:
20     char *identity_macro,   // string with the #define macro
21     // input:
22     GB_Opcode opcode,       // must be a built-in binary operator from a monoid
23     GB_Type_code zcode      // type code of the binary operator
24 )
25 {
26 
27     char *identity_value ;
28     int ecode ;
29 
30     // get ecode from the opcode and zcode
31     GB_enumify_identity (&ecode, opcode, zcode) ;
32 
33     // convert ecode to string
34     GB_charify_identity_or_terminal (&identity_value, ecode) ;
35 
36     // convert string to macro
37     GB_macrofy_identity (identity_macro, identity_value) ;
38 }
39 
40 //------------------------------------------------------------------------------
41 // GB_enumify_identity: return ecode for identity value of an operator
42 //------------------------------------------------------------------------------
43 
GB_enumify_identity(int * ecode,GB_Opcode opcode,GB_Type_code zcode)44 void GB_enumify_identity       // return enum of identity value
45 (
46     // output:
47     int *ecode,             // enumerated identity, 0 to 17 (-1 if fail)
48     // inputs:
49     GB_Opcode opcode,       // built-in binary opcode of a monoid
50     GB_Type_code zcode      // type code used in the opcode we want
51 )
52 {
53 
54     int e = -1 ;
55 
56     switch (opcode)
57     {
58 
59         case GB_PLUS_opcode     : e = 0 ; break ; // 0
60 
61         case GB_TIMES_opcode    : e = 1 ; break ; // 1
62 
63         case GB_LAND_opcode     :
64         // case GB_LXNOR_opcode :
65         case GB_EQ_opcode       :
66             e = (zcode == GB_BOOL_code) ? 2 : (-1) ; break ; // true
67 
68         case GB_LOR_opcode      :
69         case GB_LXOR_opcode     :
70             e = (zcode == GB_BOOL_code) ? 3 : (-1) ; break ; // false
71 
72         case GB_MIN_opcode :
73 
74             switch (zcode)
75             {
76                 case GB_BOOL_code   : e =  2 ; break ; // true
77                 case GB_INT8_code   : e =  4 ; break ; // INT8_MAX
78                 case GB_INT16_code  : e =  5 ; break ; // INT16_MAX
79                 case GB_INT32_code  : e =  6 ; break ; // INT32_MAX
80                 case GB_INT64_code  : e =  7 ; break ; // INT64_MAX
81                 case GB_UINT8_code  : e =  8 ; break ; // UINT8_MAX
82                 case GB_UINT16_code : e =  9 ; break ; // UINT16_MAX
83                 case GB_UINT32_code : e = 10 ; break ; // UINT32_MAX
84                 case GB_UINT64_code : e = 11 ; break ; // UINT64_MAX
85                 case GB_FP32_code   :
86                 case GB_FP64_code   : e = 12 ; break ; // INFINITY
87                 default             : e = -1 ; break ;
88             }
89             break ;
90 
91         case GB_MAX_opcode :
92 
93             switch (zcode)
94             {
95                 case GB_BOOL_code   : e =  3 ; break ; // false
96                 case GB_INT8_code   : e = 13 ; break ; // INT8_MIN
97                 case GB_INT16_code  : e = 14 ; break ; // INT16_MIN
98                 case GB_INT32_code  : e = 15 ; break ; // INT32_MIN
99                 case GB_INT64_code  : e = 16 ; break ; // INT64_MIN
100                 case GB_UINT8_code  :
101                 case GB_UINT16_code :
102                 case GB_UINT32_code :
103                 case GB_UINT64_code : e =  0 ; break ; // 0
104                 case GB_FP32_code   :
105                 case GB_FP64_code   : e = 17 ; break ; // -INFINITY
106                 default             : e = -1 ; break ;
107             }
108             break ;
109 
110         case GB_ANY_opcode   : e = 0 ; break ; // 0
111 
112         // identity/terminal values for user-defined monoids must be provided
113         // by an additional string.  This value is a place-holder to indicate
114         // that the additional user-provided string must be used.
115         case GB_USER_opcode  : e = 31 ; break ;
116 
117         default              : e = -1 ; break ; // invalid operator or type
118     }
119 
120     (*ecode) = e ;
121 }
122 
123 //------------------------------------------------------------------------------
124 // GB_charify_identity_or_terminal: string for identity/terminal value
125 //------------------------------------------------------------------------------
126 
GB_charify_identity_or_terminal(char ** value_string,int ecode)127 void GB_charify_identity_or_terminal
128 (
129     // output:
130     char **value_string,        // string encoding the value
131     // input:
132     int ecode                   // enumerated identity/terminal value
133 )
134 {
135 
136     const char *f ;
137 
138     switch (ecode)
139     {
140 
141         //----------------------------------------------------------------------
142         // for identity values and terminal values for terminal monoids
143         //----------------------------------------------------------------------
144 
145         case  0 : f = "0"           ; break ;
146         case  1 : f = "1"           ; break ;
147         case  2 : f = "true"        ; break ;
148         case  3 : f = "false"       ; break ;
149         case  4 : f = "INT8_MAX"    ; break ;
150         case  5 : f = "INT16_MAX"   ; break ;
151         case  6 : f = "INT32_MAX"   ; break ;
152         case  7 : f = "INT64_MAX"   ; break ;
153         case  8 : f = "UINT8_MAX"   ; break ;
154         case  9 : f = "UINT16_MAX"  ; break ;
155         case 10 : f = "UINT32_MAX"  ; break ;
156         case 11 : f = "UINT64_MAX"  ; break ;
157         case 12 : f = "INFINITY"    ; break ;
158         case 13 ; f = "INT8_MIN"    ; break ;
159         case 14 ; f = "INT16_MIN"   ; break ;
160         case 15 ; f = "INT32_MIN"   ; break ;
161         case 16 ; f = "INT64_MIN"   ; break ;
162         case 17 : f = "-INFINITY"   ; break ;
163         case 18 : f = "0"           ; break ;       // for the ANY monoid only
164 
165         // ecodes 19 to 28 are reserved for future use, for terminal values
166 
167         // user-defined terminal monoid
168         case 29 : f = "(user provided, terminal)" ; break ;
169 
170         //----------------------------------------------------------------------
171         // for non-terminal monoids
172         //----------------------------------------------------------------------
173 
174         // user-defined non-terminal monoids
175         case 30 : f = "(user provided, not terminal)" ; break ;
176 
177         // built-in non-terminal monoids
178         case 31 : f = "(built in, not terminal)" ; break ;
179 
180         default : f = NULL ;        ; break ;
181     }
182 
183     (*value_string) = f ;
184 }
185 
186 //------------------------------------------------------------------------------
187 // GB_macrofy_identity: convert a value string to a macro
188 //------------------------------------------------------------------------------
189 
GB_macrofy_identity(char * identity_macro,const char * value_string)190 void GB_macrofy_identity
191 (
192     // output:
193     char *identity_macro,        // string with #define macro
194     // input:
195     const char *value_string    // string defining the identity value
196 )
197 {
198     snprintf (identity_macro, GB_CUDA_STRLEN, "#define GB_IDENTITY (%s)",
199         value_string) ;
200 }
201 
202