[Groonga-commit] groonga/grnxx at a72dc89 [new_data_types] Add typecast functions, Int::to_float() and Float::to_int(). (#105)

アーカイブの一覧に戻る

susumu.yata null+****@clear*****
Thu Nov 13 11:33:21 JST 2014


susumu.yata	2014-11-13 11:33:21 +0900 (Thu, 13 Nov 2014)

  New Revision: a72dc899837d017efe97ba38b9307c8c5b5521ca
  https://github.com/groonga/grnxx/commit/a72dc899837d017efe97ba38b9307c8c5b5521ca

  Message:
    Add typecast functions, Int::to_float() and Float::to_int(). (#105)

  Added files:
    include/grnxx/data_types/typecast.hpp
  Modified files:
    include/grnxx/data_types.hpp
    include/grnxx/data_types/Makefile.am
    include/grnxx/data_types/scalar/float.hpp
    include/grnxx/data_types/scalar/int.hpp

  Modified: include/grnxx/data_types.hpp (+1 -0)
===================================================================
--- include/grnxx/data_types.hpp    2014-11-12 19:00:24 +0900 (3f5b701)
+++ include/grnxx/data_types.hpp    2014-11-13 11:33:21 +0900 (335bcf6)
@@ -6,6 +6,7 @@
 #include "grnxx/data_types/na.hpp"
 #include "grnxx/data_types/record.hpp"
 #include "grnxx/data_types/scalar.hpp"
+#include "grnxx/data_types/typecast.hpp"
 #include "grnxx/data_types/vector.hpp"
 
 #endif  // GRNXX_DATA_TYPES_HPP

  Modified: include/grnxx/data_types/Makefile.am (+1 -0)
===================================================================
--- include/grnxx/data_types/Makefile.am    2014-11-12 19:00:24 +0900 (392ca96)
+++ include/grnxx/data_types/Makefile.am    2014-11-13 11:33:21 +0900 (114d5d4)
@@ -9,4 +9,5 @@ pkginclude_HEADERS =	\
 	na.hpp		\
 	record.hpp	\
 	scalar.hpp	\
+	typecast.hpp	\
 	vector.hpp

  Modified: include/grnxx/data_types/scalar/float.hpp (+6 -0)
===================================================================
--- include/grnxx/data_types/scalar/float.hpp    2014-11-12 19:00:24 +0900 (3935ba9)
+++ include/grnxx/data_types/scalar/float.hpp    2014-11-13 11:33:21 +0900 (afd4a76)
@@ -9,6 +9,8 @@
 
 namespace grnxx {
 
+class Int;
+
 // NOTE: This implementation assumes IEEE 754.
 class Float {
  public:
@@ -119,6 +121,10 @@ class Float {
     return Float(std::nextafter(value_, to.value_));
   }
 
+  // -- Typecast (grnxx/data_types/typecast.hpp) --
+
+  constexpr Int to_int() const;
+
   static constexpr DataType type() {
     return FLOAT_DATA;
   }

  Modified: include/grnxx/data_types/scalar/int.hpp (+7 -1)
===================================================================
--- include/grnxx/data_types/scalar/int.hpp    2014-11-12 19:00:24 +0900 (3d86d7f)
+++ include/grnxx/data_types/scalar/int.hpp    2014-11-13 11:33:21 +0900 (8d066fa)
@@ -4,12 +4,14 @@
 #include <cstdint>
 #include <limits>
 
-#include "grnxx/features.hpp"
 #include "grnxx/data_types/data_type.hpp"
 #include "grnxx/data_types/na.hpp"
+#include "grnxx/features.hpp"
 
 namespace grnxx {
 
+class Float;
+
 // NOTE: This implementation assumes two's complement.
 class Int {
  public:
@@ -219,6 +221,10 @@ class Int {
     return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ >= rhs.value_);
   }
 
+  // -- Typecast (grnxx/data_types/typecast.hpp) --
+
+  constexpr Float to_float() const;
+
   static constexpr DataType type() {
     return INT_DATA;
   }

  Added: include/grnxx/data_types/typecast.hpp (+25 -0) 100644
===================================================================
--- /dev/null
+++ include/grnxx/data_types/typecast.hpp    2014-11-13 11:33:21 +0900 (468017b)
@@ -0,0 +1,25 @@
+#ifndef GRNXX_DATA_TYPES_TYPECAST_HPP
+#define GRNXX_DATA_TYPES_TYPECAST_HPP
+
+#include "grnxx/data_types/data_type.hpp"
+#include "grnxx/data_types/na.hpp"
+#include "grnxx/data_types/scalar.hpp"
+#include "grnxx/data_types/vector.hpp"
+
+namespace grnxx {
+
+inline constexpr Float Int::to_float() const {
+  return is_na() ? Float::na() : Float(static_cast<double>(value_));
+}
+
+// NOTE: This implementation assumes that an integer overflow in conversion
+//       from double-precision floating-point number to 64-bit signed integer
+//       results in the value associated with N/A (0x80...).
+//       The CVTTSD2SI instruction of x86_64 works as mentioned.
+inline constexpr Int Float::to_int() const {
+  return Int(static_cast<int64_t>(value_));
+}
+
+}  // namespace grnxx
+
+#endif  // GRNXX_DATA_TYPES_TYPECAST_HPP
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る