1 //------------------------------------------------------------------------------
2 // GB_casting.c: unary typecasting functions
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 #include "GB.h"
11 
12 //------------------------------------------------------------------------------
13 // typecasting from double to integer
14 //------------------------------------------------------------------------------
15 
16 extern int8_t   GB_cast_to_int8_t   (double x) ;
17 extern int16_t  GB_cast_to_int16_t  (double x) ;
18 extern int32_t  GB_cast_to_int32_t  (double x) ;
19 extern int64_t  GB_cast_to_int64_t  (double x) ;
20 extern uint8_t  GB_cast_to_uint8_t  (double x) ;
21 extern uint16_t GB_cast_to_uint16_t (double x) ;
22 extern uint32_t GB_cast_to_uint32_t (double x) ;
23 extern uint64_t GB_cast_to_uint64_t (double x) ;
24 
25 //------------------------------------------------------------------------------
26 // unary typecast operators, used in GB_cast_factory.c.
27 //------------------------------------------------------------------------------
28 
29 #define GB_CAST_FUNCTION(ztype,xtype)                                   \
30 extern void GB (_cast_ ## ztype ## _ ## xtype)                          \
31 (                                                                       \
32     void *z,            /* typecasted output, of type ztype */          \
33     const void *x,      /* input value to typecast, of type xtype */    \
34     size_t s            /* size of type, for GB_copy_user_user only */  \
35 ) ;
36 
37 //------------------------------------------------------------------------------
38 // typecast to boolean
39 //------------------------------------------------------------------------------
40 
41 GB_CAST_FUNCTION (bool      , bool      )
42 GB_CAST_FUNCTION (bool      , int8_t    )
43 GB_CAST_FUNCTION (bool      , int16_t   )
44 GB_CAST_FUNCTION (bool      , int32_t   )
45 GB_CAST_FUNCTION (bool      , int64_t   )
46 GB_CAST_FUNCTION (bool      , uint8_t   )
47 GB_CAST_FUNCTION (bool      , uint16_t  )
48 GB_CAST_FUNCTION (bool      , uint32_t  )
49 GB_CAST_FUNCTION (bool      , uint64_t  )
50 GB_CAST_FUNCTION (bool      , float     )
51 GB_CAST_FUNCTION (bool      , double    )
52 GB_CAST_FUNCTION (bool      , GxB_FC32_t)
53 GB_CAST_FUNCTION (bool      , GxB_FC64_t)
54 
55 //------------------------------------------------------------------------------
56 // typecast to int8_t
57 //------------------------------------------------------------------------------
58 
59 GB_CAST_FUNCTION (int8_t    , bool      )
60 GB_CAST_FUNCTION (int8_t    , int8_t    )
61 GB_CAST_FUNCTION (int8_t    , int16_t   )
62 GB_CAST_FUNCTION (int8_t    , int32_t   )
63 GB_CAST_FUNCTION (int8_t    , int64_t   )
64 GB_CAST_FUNCTION (int8_t    , uint8_t   )
65 GB_CAST_FUNCTION (int8_t    , uint16_t  )
66 GB_CAST_FUNCTION (int8_t    , uint32_t  )
67 GB_CAST_FUNCTION (int8_t    , uint64_t  )
68 GB_CAST_FUNCTION (int8_t    , float     )
69 GB_CAST_FUNCTION (int8_t    , double    )
70 GB_CAST_FUNCTION (int8_t    , GxB_FC32_t)
71 GB_CAST_FUNCTION (int8_t    , GxB_FC64_t)
72 
73 //------------------------------------------------------------------------------
74 // typecast to int16_t
75 //------------------------------------------------------------------------------
76 
77 GB_CAST_FUNCTION (int16_t   , bool      )
78 GB_CAST_FUNCTION (int16_t   , int8_t    )
79 GB_CAST_FUNCTION (int16_t   , int16_t   )
80 GB_CAST_FUNCTION (int16_t   , int32_t   )
81 GB_CAST_FUNCTION (int16_t   , int64_t   )
82 GB_CAST_FUNCTION (int16_t   , uint8_t   )
83 GB_CAST_FUNCTION (int16_t   , uint16_t  )
84 GB_CAST_FUNCTION (int16_t   , uint32_t  )
85 GB_CAST_FUNCTION (int16_t   , uint64_t  )
86 GB_CAST_FUNCTION (int16_t   , float     )
87 GB_CAST_FUNCTION (int16_t   , double    )
88 GB_CAST_FUNCTION (int16_t   , GxB_FC32_t)
89 GB_CAST_FUNCTION (int16_t   , GxB_FC64_t)
90 
91 //------------------------------------------------------------------------------
92 // typecast to int32_t
93 //------------------------------------------------------------------------------
94 
95 GB_CAST_FUNCTION (int32_t   , bool      )
96 GB_CAST_FUNCTION (int32_t   , int8_t    )
97 GB_CAST_FUNCTION (int32_t   , int16_t   )
98 GB_CAST_FUNCTION (int32_t   , int32_t   )
99 GB_CAST_FUNCTION (int32_t   , int64_t   )
100 GB_CAST_FUNCTION (int32_t   , uint8_t   )
101 GB_CAST_FUNCTION (int32_t   , uint16_t  )
102 GB_CAST_FUNCTION (int32_t   , uint32_t  )
103 GB_CAST_FUNCTION (int32_t   , uint64_t  )
104 GB_CAST_FUNCTION (int32_t   , float     )
105 GB_CAST_FUNCTION (int32_t   , double    )
106 GB_CAST_FUNCTION (int32_t   , GxB_FC32_t)
107 GB_CAST_FUNCTION (int32_t   , GxB_FC64_t)
108 
109 //------------------------------------------------------------------------------
110 // typecast to int64_t
111 //------------------------------------------------------------------------------
112 
113 GB_CAST_FUNCTION (int64_t   , bool      )
114 GB_CAST_FUNCTION (int64_t   , int8_t    )
115 GB_CAST_FUNCTION (int64_t   , int16_t   )
116 GB_CAST_FUNCTION (int64_t   , int32_t   )
117 GB_CAST_FUNCTION (int64_t   , int64_t   )
118 GB_CAST_FUNCTION (int64_t   , uint8_t   )
119 GB_CAST_FUNCTION (int64_t   , uint16_t  )
120 GB_CAST_FUNCTION (int64_t   , uint32_t  )
121 GB_CAST_FUNCTION (int64_t   , uint64_t  )
122 GB_CAST_FUNCTION (int64_t   , float     )
123 GB_CAST_FUNCTION (int64_t   , double    )
124 GB_CAST_FUNCTION (int64_t   , GxB_FC32_t)
125 GB_CAST_FUNCTION (int64_t   , GxB_FC64_t)
126 
127 //------------------------------------------------------------------------------
128 // typecast to uint8_t
129 //------------------------------------------------------------------------------
130 
131 GB_CAST_FUNCTION (uint8_t   , bool      )
132 GB_CAST_FUNCTION (uint8_t   , int8_t    )
133 GB_CAST_FUNCTION (uint8_t   , int16_t   )
134 GB_CAST_FUNCTION (uint8_t   , int32_t   )
135 GB_CAST_FUNCTION (uint8_t   , int64_t   )
136 GB_CAST_FUNCTION (uint8_t   , uint8_t   )
137 GB_CAST_FUNCTION (uint8_t   , uint16_t  )
138 GB_CAST_FUNCTION (uint8_t   , uint32_t  )
139 GB_CAST_FUNCTION (uint8_t   , uint64_t  )
140 GB_CAST_FUNCTION (uint8_t   , float     )
141 GB_CAST_FUNCTION (uint8_t   , double    )
142 GB_CAST_FUNCTION (uint8_t   , GxB_FC32_t)
143 GB_CAST_FUNCTION (uint8_t   , GxB_FC64_t)
144 
145 //------------------------------------------------------------------------------
146 // typecast to uint16_t
147 //------------------------------------------------------------------------------
148 
149 GB_CAST_FUNCTION (uint16_t  , bool      )
150 GB_CAST_FUNCTION (uint16_t  , int8_t    )
151 GB_CAST_FUNCTION (uint16_t  , int16_t   )
152 GB_CAST_FUNCTION (uint16_t  , int32_t   )
153 GB_CAST_FUNCTION (uint16_t  , int64_t   )
154 GB_CAST_FUNCTION (uint16_t  , uint8_t   )
155 GB_CAST_FUNCTION (uint16_t  , uint16_t  )
156 GB_CAST_FUNCTION (uint16_t  , uint32_t  )
157 GB_CAST_FUNCTION (uint16_t  , uint64_t  )
158 GB_CAST_FUNCTION (uint16_t  , float     )
159 GB_CAST_FUNCTION (uint16_t  , double    )
160 GB_CAST_FUNCTION (uint16_t  , GxB_FC32_t)
161 GB_CAST_FUNCTION (uint16_t  , GxB_FC64_t)
162 
163 //------------------------------------------------------------------------------
164 // typecast to uint32_t
165 //------------------------------------------------------------------------------
166 
167 GB_CAST_FUNCTION (uint32_t  , bool      )
168 GB_CAST_FUNCTION (uint32_t  , int8_t    )
169 GB_CAST_FUNCTION (uint32_t  , int16_t   )
170 GB_CAST_FUNCTION (uint32_t  , int32_t   )
171 GB_CAST_FUNCTION (uint32_t  , int64_t   )
172 GB_CAST_FUNCTION (uint32_t  , uint8_t   )
173 GB_CAST_FUNCTION (uint32_t  , uint16_t  )
174 GB_CAST_FUNCTION (uint32_t  , uint32_t  )
175 GB_CAST_FUNCTION (uint32_t  , uint64_t  )
176 GB_CAST_FUNCTION (uint32_t  , float     )
177 GB_CAST_FUNCTION (uint32_t  , double    )
178 GB_CAST_FUNCTION (uint32_t  , GxB_FC32_t)
179 GB_CAST_FUNCTION (uint32_t  , GxB_FC64_t)
180 
181 //------------------------------------------------------------------------------
182 // typecast to uint64_t
183 //------------------------------------------------------------------------------
184 
185 GB_CAST_FUNCTION (uint64_t  , bool      )
186 GB_CAST_FUNCTION (uint64_t  , int8_t    )
187 GB_CAST_FUNCTION (uint64_t  , int16_t   )
188 GB_CAST_FUNCTION (uint64_t  , int32_t   )
189 GB_CAST_FUNCTION (uint64_t  , int64_t   )
190 GB_CAST_FUNCTION (uint64_t  , uint8_t   )
191 GB_CAST_FUNCTION (uint64_t  , uint16_t  )
192 GB_CAST_FUNCTION (uint64_t  , uint32_t  )
193 GB_CAST_FUNCTION (uint64_t  , uint64_t  )
194 GB_CAST_FUNCTION (uint64_t  , float     )
195 GB_CAST_FUNCTION (uint64_t  , double    )
196 GB_CAST_FUNCTION (uint64_t  , GxB_FC32_t)
197 GB_CAST_FUNCTION (uint64_t  , GxB_FC64_t)
198 
199 //------------------------------------------------------------------------------
200 // typecast to float
201 //------------------------------------------------------------------------------
202 
203 GB_CAST_FUNCTION (float     , bool      )
204 GB_CAST_FUNCTION (float     , int8_t    )
205 GB_CAST_FUNCTION (float     , int16_t   )
206 GB_CAST_FUNCTION (float     , int32_t   )
207 GB_CAST_FUNCTION (float     , int64_t   )
208 GB_CAST_FUNCTION (float     , uint8_t   )
209 GB_CAST_FUNCTION (float     , uint16_t  )
210 GB_CAST_FUNCTION (float     , uint32_t  )
211 GB_CAST_FUNCTION (float     , uint64_t  )
212 GB_CAST_FUNCTION (float     , float     )
213 GB_CAST_FUNCTION (float     , double    )
214 GB_CAST_FUNCTION (float     , GxB_FC32_t)
215 GB_CAST_FUNCTION (float     , GxB_FC64_t)
216 
217 //------------------------------------------------------------------------------
218 // typecast to double
219 //------------------------------------------------------------------------------
220 
221 GB_CAST_FUNCTION (double    , bool      )
222 GB_CAST_FUNCTION (double    , int8_t    )
223 GB_CAST_FUNCTION (double    , int16_t   )
224 GB_CAST_FUNCTION (double    , int32_t   )
225 GB_CAST_FUNCTION (double    , int64_t   )
226 GB_CAST_FUNCTION (double    , uint8_t   )
227 GB_CAST_FUNCTION (double    , uint16_t  )
228 GB_CAST_FUNCTION (double    , uint32_t  )
229 GB_CAST_FUNCTION (double    , uint64_t  )
230 GB_CAST_FUNCTION (double    , float     )
231 GB_CAST_FUNCTION (double    , double    )
232 GB_CAST_FUNCTION (double    , GxB_FC32_t)
233 GB_CAST_FUNCTION (double    , GxB_FC64_t)
234 
235 //------------------------------------------------------------------------------
236 // typecast to float complex
237 //------------------------------------------------------------------------------
238 
239 GB_CAST_FUNCTION (GxB_FC32_t, bool      )
240 GB_CAST_FUNCTION (GxB_FC32_t, int8_t    )
241 GB_CAST_FUNCTION (GxB_FC32_t, int16_t   )
242 GB_CAST_FUNCTION (GxB_FC32_t, int32_t   )
243 GB_CAST_FUNCTION (GxB_FC32_t, int64_t   )
244 GB_CAST_FUNCTION (GxB_FC32_t, uint8_t   )
245 GB_CAST_FUNCTION (GxB_FC32_t, uint16_t  )
246 GB_CAST_FUNCTION (GxB_FC32_t, uint32_t  )
247 GB_CAST_FUNCTION (GxB_FC32_t, uint64_t  )
248 GB_CAST_FUNCTION (GxB_FC32_t, float     )
249 GB_CAST_FUNCTION (GxB_FC32_t, double    )
250 GB_CAST_FUNCTION (GxB_FC32_t, GxB_FC32_t)
251 GB_CAST_FUNCTION (GxB_FC32_t, GxB_FC64_t)
252 
253 //------------------------------------------------------------------------------
254 // typecast to double complex
255 //------------------------------------------------------------------------------
256 
257 GB_CAST_FUNCTION (GxB_FC64_t, bool      )
258 GB_CAST_FUNCTION (GxB_FC64_t, int8_t    )
259 GB_CAST_FUNCTION (GxB_FC64_t, int16_t   )
260 GB_CAST_FUNCTION (GxB_FC64_t, int32_t   )
261 GB_CAST_FUNCTION (GxB_FC64_t, int64_t   )
262 GB_CAST_FUNCTION (GxB_FC64_t, uint8_t   )
263 GB_CAST_FUNCTION (GxB_FC64_t, uint16_t  )
264 GB_CAST_FUNCTION (GxB_FC64_t, uint32_t  )
265 GB_CAST_FUNCTION (GxB_FC64_t, uint64_t  )
266 GB_CAST_FUNCTION (GxB_FC64_t, float     )
267 GB_CAST_FUNCTION (GxB_FC64_t, double    )
268 GB_CAST_FUNCTION (GxB_FC64_t, GxB_FC32_t)
269 GB_CAST_FUNCTION (GxB_FC64_t, GxB_FC64_t)
270 
271