Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/typecast.cc @ 9064:7c02ec148a3c
Check grammar on all .cc files
Same check as previously done on .m files
Attempt to enforce some conformity in documentation text for rules
such as two spaces after a period, commas around latin abbreviations, etc.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sat, 28 Mar 2009 13:57:22 -0700 |
parents | 58604c45ca74 |
children | e4b383a0effa |
rev | line source |
---|---|
6869 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2007, 2008, 2009 David Bateman |
6869 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
6869 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
6869 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
6948 | 27 #include <algorithm> |
6869 | 28 #include <string> |
29 | |
30 #include "oct.h" | |
31 | |
32 template <class LT, class RT> | |
7229 | 33 /* static */ void |
6869 | 34 typecast (const Array<RT>& x, Array<LT>& y) |
35 { | |
36 octave_idx_type n = x.length (); | |
37 size_t ns = sizeof (RT); | |
38 size_t ms = sizeof (LT); | |
39 | |
40 if (n * ns % ms != 0) | |
7229 | 41 error ("typecast: incorrect number of input values to make output value"); |
6869 | 42 else |
43 { | |
44 octave_idx_type m = n * ns / ms; | |
45 dim_vector dv = x.dims (); | |
46 for (octave_idx_type i = 0; i < dv.length(); i++) | |
47 if (dv(i) == n) | |
48 { | |
49 dv(i) = m; | |
50 break; | |
51 } | |
52 y.resize (dv); | |
7229 | 53 const unsigned char *xp |
54 = reinterpret_cast<const unsigned char *> (x.fortran_vec ()); | |
6869 | 55 unsigned char *yp = reinterpret_cast<unsigned char *>(y.fortran_vec ()); |
56 for (octave_idx_type i = 0; | |
57 i < n * static_cast<octave_idx_type>(ns); i++) | |
58 *yp++ = *xp++; | |
59 } | |
60 } | |
61 | |
62 template <class T> | |
63 static octave_value | |
64 typecast (const T& x, std::string type) | |
65 { | |
66 octave_value retval; | |
67 if (type == "uint8") | |
68 { | |
69 uint8NDArray y; | |
70 typecast (x, y); | |
71 retval = octave_value (y); | |
72 } | |
73 else if (type == "uint16") | |
74 { | |
75 uint16NDArray y; | |
76 typecast (x, y); | |
77 retval = octave_value (y); | |
78 } | |
79 else if (type == "uint32") | |
80 { | |
81 uint32NDArray y; | |
82 typecast (x, y); | |
83 retval = octave_value (y); | |
84 } | |
85 else if (type == "uint64") | |
86 { | |
87 uint64NDArray y; | |
88 typecast (x, y); | |
89 retval = octave_value (y); | |
90 } | |
91 else if (type == "int8") | |
92 { | |
93 int8NDArray y; | |
94 typecast (x, y); | |
95 retval = octave_value (y); | |
96 } | |
97 else if (type == "int16") | |
98 { | |
99 int16NDArray y; | |
100 typecast (x, y); | |
101 retval = octave_value (y); | |
102 } | |
103 else if (type == "int32") | |
104 { | |
105 int32NDArray y; | |
106 typecast (x, y); | |
107 retval = octave_value (y); | |
108 } | |
109 else if (type == "int64") | |
110 { | |
111 int64NDArray y; | |
112 typecast (x, y); | |
113 retval = octave_value (y); | |
114 } | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
115 else if (type == "single") |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
116 { |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
117 FloatNDArray y; |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
118 typecast (x, y); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
119 retval = octave_value (y); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
120 } |
6869 | 121 else |
122 { | |
123 NDArray y; | |
124 typecast (x, y); | |
125 retval = octave_value (y); | |
126 } | |
127 | |
128 return retval; | |
129 } | |
130 | |
131 DEFUN_DLD (typecast, args, , | |
8715 | 132 "-*- texinfo -*-\n\ |
6869 | 133 @deftypefn {Loadable Function} {} typecast (@var{x}, @var{type})\n\ |
8100
da2fbd22d672
First sentence of a doc string should read like a command
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7789
diff
changeset
|
134 Convert from one datatype to another without changing the underlying\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
135 data. The argument @var{type} defines the type of the return argument\n\ |
6869 | 136 and must be one of 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16',\n\ |
137 'int32', 'int64', 'single' or 'double'.\n\ | |
138 \n\ | |
139 An example of the use of typecast on a little-endian machine is\n\ | |
140 \n\ | |
141 @example\n\ | |
142 @group\n\ | |
143 @var{x} = uint16 ([1, 65535]);\n\ | |
144 typecast (@var{x}, 'uint8')\n\ | |
145 @result{} [ 0, 1, 255, 255]\n\ | |
146 @end group\n\ | |
147 @end example\n\ | |
148 @seealso{cast, swapbytes}\n\ | |
149 @end deftypefn") | |
150 { | |
151 int nargin = args.length (); | |
152 octave_value retval; | |
153 | |
154 if (nargin != 2) | |
155 print_usage (); | |
156 else | |
157 { | |
158 std::string type = args (1).string_value (); | |
159 | |
160 if (! error_state) | |
161 { | |
162 std::transform (type.begin (), type.end (), type.begin (), tolower); | |
163 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
164 if (type != "uint8" && type != "uint16" |
7229 | 165 && type != "uint32" && type != "uint64" |
166 && type != "int8" && type != "int16" | |
167 && type != "int32" && type != "int64" | |
168 && type != "single" && type != "double") | |
6869 | 169 error ("typecast: unrecognized or invalid type"); |
170 else if (args(0).is_sparse_type () || args(0).is_complex_type ()) | |
171 error ("typecast: sparse and complex types are invalid"); | |
172 else | |
173 { | |
174 dim_vector dv = args(0).dims (); | |
175 bool seen = false; | |
176 | |
177 for (octave_idx_type i = 0; i < dv.length(); i++) | |
178 if (dv (i) != 1) | |
179 { | |
180 if (seen) | |
181 { | |
182 error ("typecast: argument must be a vector"); | |
183 break; | |
184 } | |
185 else | |
186 seen = true; | |
187 } | |
188 | |
189 if (!error_state) | |
190 { | |
191 if (args(0).is_uint8_type ()) | |
192 retval = typecast (args(0).uint8_array_value (), type); | |
193 else if (args(0).is_uint16_type ()) | |
194 retval = typecast (args(0).uint16_array_value (), type); | |
195 else if (args(0).is_uint32_type ()) | |
196 retval = typecast (args(0).uint32_array_value (), type); | |
197 else if (args(0).is_uint64_type ()) | |
198 retval = typecast (args(0).uint64_array_value (), type); | |
199 else if (args(0).is_int8_type ()) | |
200 retval = typecast (args(0).int8_array_value (), type); | |
201 else if (args(0).is_int16_type ()) | |
202 retval = typecast (args(0).int16_array_value (), type); | |
203 else if (args(0).is_int32_type ()) | |
204 retval = typecast (args(0).int32_array_value (), type); | |
205 else if (args(0).is_int64_type ()) | |
206 retval = typecast (args(0).int64_array_value (), type); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
207 else if (args(0).is_single_type ()) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7229
diff
changeset
|
208 retval = typecast (args(0).float_array_value (), type); |
6869 | 209 else |
210 retval = typecast (args(0).array_value (), type); | |
211 } | |
212 } | |
213 } | |
214 } | |
215 | |
216 return retval; | |
217 } |