1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
4 *
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 *
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
13 */
14 /*--------------------------------------------------------------------------*/
15 #include <limits>
16
17 #include "int.hxx"
18 #include "double.hxx"
19 #include "bool.hxx"
20 #include "function.hxx"
21 #include "integer_gw.hxx"
22 extern "C"
23 {
24 #include "Scierror.h"
25 }
26
27 template <class T, class U>
convert_int(U * _pIn,int _iSize,T * _pOut)28 void convert_int(U* _pIn, int _iSize, T* _pOut)
29 {
30 static T minval = std::numeric_limits<T>::min();
31 static T maxval = std::numeric_limits<T>::max();
32
33 for (int i = 0 ; i < _iSize ; i++)
34 {
35 if (std::isnan((double)_pIn[i]))
36 {
37 _pOut[i] = 0;
38 }
39 else if (std::isinf((double)_pIn[i]))
40 {
41 if ((double)_pIn[i] > 0)
42 {
43 _pOut[i] = maxval;
44 }
45 else
46 {
47 _pOut[i] = minval;
48 }
49 }
50 else
51 {
52 _pOut[i] = (T)_pIn[i];
53 }
54 }
55 }
56
57 template <class T>
convertInt(types::InternalType * _pIn,T * _pOut)58 void convertInt(types::InternalType* _pIn, T* _pOut)
59 {
60 switch (_pIn->getType())
61 {
62 case types::InternalType::ScilabBool :
63 {
64 types::Bool* pBool = _pIn->getAs<types::Bool>();
65 convert_int(pBool->get(), pBool->getSize(), _pOut->get());
66 break;
67 }
68 case types::InternalType::ScilabDouble :
69 {
70 types::Double* pD = _pIn->getAs<types::Double>();
71 convert_int(pD->get(), pD->getSize(), _pOut->get());
72 break;
73 }
74 case types::InternalType::ScilabInt8 :
75 {
76 types::Int8* pD = _pIn->getAs<types::Int8>();
77 convert_int(pD->get(), pD->getSize(), _pOut->get());
78 break;
79 }
80 case types::InternalType::ScilabUInt8 :
81 {
82 types::UInt8* pD = _pIn->getAs<types::UInt8>();
83 convert_int(pD->get(), pD->getSize(), _pOut->get());
84 break;
85 }
86 case types::InternalType::ScilabInt16 :
87 {
88 types::Int16* pD = _pIn->getAs<types::Int16>();
89 convert_int(pD->get(), pD->getSize(), _pOut->get());
90 break;
91 }
92 case types::InternalType::ScilabUInt16 :
93 {
94 types::UInt16* pD = _pIn->getAs<types::UInt16>();
95 convert_int(pD->get(), pD->getSize(), _pOut->get());
96 break;
97 }
98 case types::InternalType::ScilabInt32 :
99 {
100 types::Int32* pD = _pIn->getAs<types::Int32>();
101 convert_int(pD->get(), pD->getSize(), _pOut->get());
102 break;
103 }
104 case types::InternalType::ScilabUInt32 :
105 {
106 types::UInt32* pD = _pIn->getAs<types::UInt32>();
107 convert_int(pD->get(), pD->getSize(), _pOut->get());
108 break;
109 }
110 case types::InternalType::ScilabInt64 :
111 {
112 types::Int64* pD = _pIn->getAs<types::Int64>();
113 convert_int(pD->get(), pD->getSize(), _pOut->get());
114 break;
115 }
116 case types::InternalType::ScilabUInt64 :
117 {
118 types::UInt64* pD = _pIn->getAs<types::UInt64>();
119 convert_int(pD->get(), pD->getSize(), _pOut->get());
120 break;
121 }
122 default:
123 return;
124 }
125 }
126
127 template< class T>
commonInt(types::typed_list & in,int _iRetCount,types::typed_list & out,std::string _stName)128 types::Callable::ReturnValue commonInt(types::typed_list &in, int _iRetCount, types::typed_list &out, std::string _stName)
129 {
130 if (in.size() != 1)
131 {
132 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), _stName.c_str(), 1);
133 return types::Function::Error;
134 }
135
136 if (in[0]->isDouble() == false && in[0]->isInt() == false && in[0]->isBool() == false)
137 {
138 Scierror(999, _("%s: Wrong type for input argument #%d: %s, %s or %s expected.\n"), _stName.c_str(), 1, "integer", "boolean", "double");
139 return types::Function::Error;
140 }
141
142 types::GenericType* pGT = in[0]->getAs<types::GenericType>();
143 if (pGT->getDims() == 2 && pGT->getRows() == 0 && pGT->getCols() == 0)
144 {
145 out.push_back(types::Double::Empty());
146 return types::Function::OK;
147 }
148
149 T* pOut = new T(pGT->getDims(), pGT->getDimsArray());
150
151 convertInt(in[0], pOut);
152 out.push_back(pOut);
153 return types::Function::OK;
154 }
155 /*--------------------------------------------------------------------------*/
sci_integer8(types::typed_list & in,int _iRetCount,types::typed_list & out)156 types::Callable::ReturnValue sci_integer8(types::typed_list &in, int _iRetCount, types::typed_list &out)
157 {
158 return commonInt<types::Int8>(in, _iRetCount, out, "int8");
159 }
160
sci_uinteger8(types::typed_list & in,int _iRetCount,types::typed_list & out)161 types::Callable::ReturnValue sci_uinteger8(types::typed_list &in, int _iRetCount, types::typed_list &out)
162 {
163 return commonInt<types::UInt8>(in, _iRetCount, out, "uint8");
164 }
165
sci_integer16(types::typed_list & in,int _iRetCount,types::typed_list & out)166 types::Callable::ReturnValue sci_integer16(types::typed_list &in, int _iRetCount, types::typed_list &out)
167 {
168 return commonInt<types::Int16>(in, _iRetCount, out, "int16");
169 }
170
sci_uinteger16(types::typed_list & in,int _iRetCount,types::typed_list & out)171 types::Callable::ReturnValue sci_uinteger16(types::typed_list &in, int _iRetCount, types::typed_list &out)
172 {
173 return commonInt<types::UInt16>(in, _iRetCount, out, "uint16");
174 }
175
sci_integer32(types::typed_list & in,int _iRetCount,types::typed_list & out)176 types::Callable::ReturnValue sci_integer32(types::typed_list &in, int _iRetCount, types::typed_list &out)
177 {
178 return commonInt<types::Int32>(in, _iRetCount, out, "int32");
179 }
180
sci_uinteger32(types::typed_list & in,int _iRetCount,types::typed_list & out)181 types::Callable::ReturnValue sci_uinteger32(types::typed_list &in, int _iRetCount, types::typed_list &out)
182 {
183 return commonInt<types::UInt32>(in, _iRetCount, out, "uint32");
184 }
185
sci_integer64(types::typed_list & in,int _iRetCount,types::typed_list & out)186 types::Callable::ReturnValue sci_integer64(types::typed_list &in, int _iRetCount, types::typed_list &out)
187 {
188 return commonInt<types::Int64>(in, _iRetCount, out, "int64");
189 }
190
sci_uinteger64(types::typed_list & in,int _iRetCount,types::typed_list & out)191 types::Callable::ReturnValue sci_uinteger64(types::typed_list &in, int _iRetCount, types::typed_list &out)
192 {
193 return commonInt<types::UInt64>(in, _iRetCount, out, "uint64");
194 }
195 /*--------------------------------------------------------------------------*/
196