Treelite
typeinfo_ctypes.h
Go to the documentation of this file.
1 
9 #ifndef TREELITE_COMPILER_NATIVE_TYPEINFO_CTYPES_H_
10 #define TREELITE_COMPILER_NATIVE_TYPEINFO_CTYPES_H_
11 
12 #include <treelite/base.h>
13 #include <treelite/error.h>
14 #include <string>
15 
16 namespace treelite {
17 namespace compiler {
18 namespace native {
19 
25 inline std::string TypeInfoToCTypeString(TypeInfo type) {
26  switch (type) {
27  case TypeInfo::kInvalid:
28  throw Error("Invalid type");
29  return "";
30  case TypeInfo::kUInt32:
31  return "uint32_t";
32  case TypeInfo::kFloat32:
33  return "float";
34  case TypeInfo::kFloat64:
35  return "double";
36  default:
37  throw Error(std::string("Unrecognized type: ") + std::to_string(static_cast<int>(type)));
38  return "";
39  }
40 }
41 
47 inline std::string CExpForTypeInfo(TypeInfo type) {
48  switch (type) {
49  case TypeInfo::kInvalid:
50  case TypeInfo::kUInt32:
51  throw Error(std::string("Invalid type: ") + TypeInfoToString(type));
52  return "";
53  case TypeInfo::kFloat32:
54  return "expf";
55  case TypeInfo::kFloat64:
56  return "exp";
57  default:
58  throw Error(std::string("Unrecognized type: ") + std::to_string(static_cast<int>(type)));
59  return "";
60  }
61 }
62 
68 inline std::string CExp2ForTypeInfo(TypeInfo type) {
69  switch (type) {
70  case TypeInfo::kInvalid:
71  case TypeInfo::kUInt32:
72  throw Error(std::string("Invalid type: ") + TypeInfoToString(type));
73  return "";
74  case TypeInfo::kFloat32:
75  return "exp2f";
76  case TypeInfo::kFloat64:
77  return "exp2";
78  default:
79  throw Error(std::string("Unrecognized type: ") + std::to_string(static_cast<int>(type)));
80  return "";
81  }
82 }
83 
89 inline std::string CCopySignForTypeInfo(TypeInfo type) {
90  switch (type) {
91  case TypeInfo::kInvalid:
92  case TypeInfo::kUInt32:
93  throw Error(std::string("Invalid type: ") + TypeInfoToString(type));
94  return "";
95  case TypeInfo::kFloat32:
96  return "copysignf";
97  case TypeInfo::kFloat64:
98  return "copysign";
99  default:
100  throw Error(std::string("Unrecognized type: ") + std::to_string(static_cast<int>(type)));
101  return "";
102  }
103 }
104 
110 inline std::string CLog1PForTypeInfo(TypeInfo type) {
111  switch (type) {
112  case TypeInfo::kInvalid:
113  case TypeInfo::kUInt32:
114  throw Error(std::string("Invalid type: ") + TypeInfoToString(type));
115  return "";
116  case TypeInfo::kFloat32:
117  return "log1pf";
118  case TypeInfo::kFloat64:
119  return "log1p";
120  default:
121  throw Error(std::string("Unrecognized type: ") + std::to_string(static_cast<int>(type)));
122  return "";
123  }
124 }
125 
126 } // namespace native
127 } // namespace compiler
128 } // namespace treelite
129 #endif // TREELITE_COMPILER_NATIVE_TYPEINFO_CTYPES_H_
std::string CCopySignForTypeInfo(TypeInfo type)
Look up the correct variant of copysign() in C that should be used with a given type.
std::string CExp2ForTypeInfo(TypeInfo type)
Look up the correct variant of exp2() in C that should be used with a given type. ...
Exception class that will be thrown by Treelite.
Definition: error.h:18
Exception class used throughout the Treelite codebase.
TypeInfo
Types used by thresholds and leaf outputs.
Definition: typeinfo.h:23
std::string TypeInfoToString(treelite::TypeInfo type)
Get string representation of type info.
Definition: typeinfo.h:40
std::string CExpForTypeInfo(TypeInfo type)
Look up the correct variant of exp() in C that should be used with a given type.
defines configuration macros of Treelite
std::string TypeInfoToCTypeString(TypeInfo type)
Get string representation of the C type that&#39;s equivalent to the given type info. ...
std::string CLog1PForTypeInfo(TypeInfo type)
Look up the correct variant of log1p() in C that should be used with a given type.