ext/nmatrix/data/data.cpp in nmatrix-0.2.0 vs ext/nmatrix/data/data.cpp in nmatrix-0.2.1
- old
+ new
@@ -89,39 +89,86 @@
"log10", "sqrt", "erf",
"erfc", "cbrt", "gamma",
"negate", "floor", "ceil", "round"
};
+
+ /*
+ * Create a RubyObject from a regular C value (given a dtype). Does not return a VALUE! To get a VALUE, you need to
+ * look at the rval property of what this function returns.
+ */
+ nm::RubyObject rubyobj_from_cval(void* val, nm::dtype_t dtype) {
+ using namespace nm;
+ switch (dtype) {
+ case BYTE:
+ return RubyObject(*reinterpret_cast<uint8_t*>(val));
+
+ case INT8:
+ return RubyObject(*reinterpret_cast<int8_t*>(val));
+
+ case INT16:
+ return RubyObject(*reinterpret_cast<int16_t*>(val));
+
+ case INT32:
+ return RubyObject(*reinterpret_cast<int32_t*>(val));
+
+ case INT64:
+ return RubyObject(*reinterpret_cast<int64_t*>(val));
+
+ case FLOAT32:
+ return RubyObject(*reinterpret_cast<float32_t*>(val));
+
+ case FLOAT64:
+ return RubyObject(*reinterpret_cast<float64_t*>(val));
+
+ case COMPLEX64:
+ return RubyObject(*reinterpret_cast<Complex64*>(val));
+
+ case COMPLEX128:
+ return RubyObject(*reinterpret_cast<Complex128*>(val));
+
+ default:
+ try {
+ throw std::logic_error("Cannot create ruby object");
+ }
+ catch (std::logic_error err) {
+ printf("%s\n", err.what());
+ }
+
+ rb_raise(nm_eDataTypeError, "Conversion to RubyObject requested from unknown/invalid data type (did you try to convert from a VALUE?)");
+ }
+ return Qnil;
+ }
} // end of namespace nm
extern "C" {
const char* const DTYPE_NAMES[nm::NUM_DTYPES] = {
- "byte",
- "int8",
- "int16",
- "int32",
- "int64",
- "float32",
- "float64",
- "complex64",
- "complex128",
- "object"
+ "byte",
+ "int8",
+ "int16",
+ "int32",
+ "int64",
+ "float32",
+ "float64",
+ "complex64",
+ "complex128",
+ "object"
};
const size_t DTYPE_SIZES[nm::NUM_DTYPES] = {
- sizeof(uint8_t),
- sizeof(int8_t),
- sizeof(int16_t),
- sizeof(int32_t),
- sizeof(int64_t),
- sizeof(float32_t),
- sizeof(float64_t),
- sizeof(nm::Complex64),
- sizeof(nm::Complex128),
- sizeof(nm::RubyObject)
+ sizeof(uint8_t),
+ sizeof(int8_t),
+ sizeof(int16_t),
+ sizeof(int32_t),
+ sizeof(int64_t),
+ sizeof(float32_t),
+ sizeof(float64_t),
+ sizeof(nm::Complex64),
+ sizeof(nm::Complex128),
+ sizeof(nm::RubyObject)
};
const nm::dtype_t Upcast[nm::NUM_DTYPES][nm::NUM_DTYPES] = {
{ nm::BYTE, nm::INT16, nm::INT16, nm::INT32, nm::INT64, nm::FLOAT32, nm::FLOAT64, nm::COMPLEX64, nm::COMPLEX128, nm::RUBYOBJ},
@@ -148,103 +195,57 @@
/*
* Converts a RubyObject
*/
void rubyval_to_cval(VALUE val, nm::dtype_t dtype, void* loc) {
using namespace nm;
- switch (dtype) {
- case BYTE:
- *reinterpret_cast<uint8_t*>(loc) = static_cast<uint8_t>(RubyObject(val));
- break;
+ switch (dtype) {
+ case BYTE:
+ *reinterpret_cast<uint8_t*>(loc) = static_cast<uint8_t>(RubyObject(val));
+ break;
- case INT8:
- *reinterpret_cast<int8_t*>(loc) = static_cast<int8_t>(RubyObject(val));
- break;
+ case INT8:
+ *reinterpret_cast<int8_t*>(loc) = static_cast<int8_t>(RubyObject(val));
+ break;
- case INT16:
- *reinterpret_cast<int16_t*>(loc) = static_cast<int16_t>(RubyObject(val));
- break;
+ case INT16:
+ *reinterpret_cast<int16_t*>(loc) = static_cast<int16_t>(RubyObject(val));
+ break;
- case INT32:
- *reinterpret_cast<int32_t*>(loc) = static_cast<int32_t>(RubyObject(val));
- break;
+ case INT32:
+ *reinterpret_cast<int32_t*>(loc) = static_cast<int32_t>(RubyObject(val));
+ break;
- case INT64:
- *reinterpret_cast<int64_t*>(loc) = static_cast<int64_t>(RubyObject(val));
- break;
+ case INT64:
+ *reinterpret_cast<int64_t*>(loc) = static_cast<int64_t>(RubyObject(val));
+ break;
- case FLOAT32:
- *reinterpret_cast<float32_t*>(loc) = static_cast<float32_t>(RubyObject(val));
- break;
+ case FLOAT32:
+ *reinterpret_cast<float32_t*>(loc) = static_cast<float32_t>(RubyObject(val));
+ break;
- case FLOAT64:
- *reinterpret_cast<float64_t*>(loc) = static_cast<float64_t>(RubyObject(val));
- break;
+ case FLOAT64:
+ *reinterpret_cast<float64_t*>(loc) = static_cast<float64_t>(RubyObject(val));
+ break;
- case COMPLEX64:
- *reinterpret_cast<Complex64*>(loc) = RubyObject(val).to<Complex64>();
- break;
+ case COMPLEX64:
+ *reinterpret_cast<Complex64*>(loc) = RubyObject(val).to<Complex64>();
+ break;
- case COMPLEX128:
- *reinterpret_cast<Complex128*>(loc) = RubyObject(val).to<Complex128>();
- break;
+ case COMPLEX128:
+ *reinterpret_cast<Complex128*>(loc) = RubyObject(val).to<Complex128>();
+ break;
- case RUBYOBJ:
- *reinterpret_cast<VALUE*>(loc) = val;
- //rb_raise(rb_eTypeError, "Attempting a bad conversion from a Ruby value.");
- break;
+ case RUBYOBJ:
+ *reinterpret_cast<VALUE*>(loc) = val;
+ //rb_raise(rb_eTypeError, "Attempting a bad conversion from a Ruby value.");
+ break;
- default:
- rb_raise(rb_eTypeError, "Attempting a bad conversion.");
- break;
- }
+ default:
+ rb_raise(rb_eTypeError, "Attempting a bad conversion.");
+ break;
+ }
}
-/*
- * Create a RubyObject from a regular C value (given a dtype). Does not return a VALUE! To get a VALUE, you need to
- * look at the rval property of what this function returns.
- */
-nm::RubyObject rubyobj_from_cval(void* val, nm::dtype_t dtype) {
- using namespace nm;
- switch (dtype) {
- case BYTE:
- return RubyObject(*reinterpret_cast<uint8_t*>(val));
-
- case INT8:
- return RubyObject(*reinterpret_cast<int8_t*>(val));
-
- case INT16:
- return RubyObject(*reinterpret_cast<int16_t*>(val));
-
- case INT32:
- return RubyObject(*reinterpret_cast<int32_t*>(val));
-
- case INT64:
- return RubyObject(*reinterpret_cast<int64_t*>(val));
-
- case FLOAT32:
- return RubyObject(*reinterpret_cast<float32_t*>(val));
-
- case FLOAT64:
- return RubyObject(*reinterpret_cast<float64_t*>(val));
-
- case COMPLEX64:
- return RubyObject(*reinterpret_cast<Complex64*>(val));
-
- case COMPLEX128:
- return RubyObject(*reinterpret_cast<Complex128*>(val));
-
- default:
- try {
- throw std::logic_error("Cannot create ruby object");
- }
- catch (std::logic_error err) {
- printf("%s\n", err.what());
- }
-
- rb_raise(nm_eDataTypeError, "Conversion to RubyObject requested from unknown/invalid data type (did you try to convert from a VALUE?)");
- }
- return Qnil;
-}
/*
* Allocate and return a piece of data of the correct dtype, converted from a