From cd973b8483af21d4a3a03497aa223b82b5c1a9a2 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 15 Feb 2009 09:10:00 +0000 Subject: [PATCH] integrate machine-dependent integer serialization routine to msgpack/pack_template.h git-svn-id: file:///Users/frsyuki/project/msgpack-git/svn/x@90 5a5092ae-2292-43ba-b2d5-dcab9c1a2731 --- Makefile.am | 1 + c/pack.c | 4 + c/pack.h | 6 + cpp/pack.hpp | 74 +++++-- cpp/type/integer.hpp | 116 +--------- msgpack/pack_define.h | 26 +++ msgpack/pack_template.h | 481 ++++++++++++++++++++++++++++++---------- ruby/gem/Manifest.txt | 1 + ruby/gengem.sh | 1 + ruby/pack.c | 30 +-- 10 files changed, 468 insertions(+), 272 deletions(-) create mode 100644 msgpack/pack_define.h diff --git a/Makefile.am b/Makefile.am index d79f7eb3..c23320ae 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,7 @@ SUBDIRS = c cpp nobase_include_HEADERS = \ + msgpack/pack_define.h \ msgpack/pack_template.h \ msgpack/unpack_define.h \ msgpack/unpack_template.h diff --git a/c/pack.c b/c/pack.c index 766a9d17..f4787c03 100644 --- a/c/pack.c +++ b/c/pack.c @@ -16,12 +16,16 @@ * limitations under the License. */ #include "msgpack/pack.h" +#include "msgpack/pack_define.h" #include #define msgpack_pack_inline_func(name) \ void msgpack_pack_##name +#define msgpack_pack_inline_func_cint(name) \ + void msgpack_pack_##name + #define msgpack_pack_user msgpack_pack_t* #define msgpack_pack_append_buffer(user, buf, len) \ diff --git a/c/pack.h b/c/pack.h index fac642ff..ecf675c1 100644 --- a/c/pack.h +++ b/c/pack.h @@ -41,6 +41,7 @@ void msgpack_pack_int(msgpack_pack_t* ctx, int d); void msgpack_pack_unsigned_int(msgpack_pack_t* ctx, unsigned int d); void msgpack_pack_long(msgpack_pack_t* ctx, long d); void msgpack_pack_unsigned_long(msgpack_pack_t* ctx, unsigned long d); + void msgpack_pack_uint8(msgpack_pack_t* ctx, uint8_t d); void msgpack_pack_uint16(msgpack_pack_t* ctx, uint16_t d); void msgpack_pack_uint32(msgpack_pack_t* ctx, uint32_t d); @@ -49,13 +50,18 @@ void msgpack_pack_int8(msgpack_pack_t* ctx, int8_t d); void msgpack_pack_int16(msgpack_pack_t* ctx, int16_t d); void msgpack_pack_int32(msgpack_pack_t* ctx, int32_t d); void msgpack_pack_int64(msgpack_pack_t* ctx, int64_t d); + void msgpack_pack_float(msgpack_pack_t* ctx, float d); void msgpack_pack_double(msgpack_pack_t* ctx, double d); + void msgpack_pack_nil(msgpack_pack_t* ctx); void msgpack_pack_true(msgpack_pack_t* ctx); void msgpack_pack_false(msgpack_pack_t* ctx); + void msgpack_pack_array(msgpack_pack_t* ctx, unsigned int n); + void msgpack_pack_map(msgpack_pack_t* ctx, unsigned int n); + void msgpack_pack_raw(msgpack_pack_t* ctx, size_t l); void msgpack_pack_raw_body(msgpack_pack_t* ctx, const void* b, size_t l); diff --git a/cpp/pack.hpp b/cpp/pack.hpp index aee4b873..e860f4c3 100644 --- a/cpp/pack.hpp +++ b/cpp/pack.hpp @@ -20,6 +20,8 @@ #include // __BYTE_ORDER #include +#include +#include "msgpack/pack_define.h" namespace msgpack { @@ -30,33 +32,38 @@ public: packer(Stream& s); public: - void pack_int(int d) { pack_int_impl(m_stream, d); } - void pack_long(long d) { pack_long_impl(m_stream, d); } - void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } - void pack_unsigned_long(unsigned long d) { pack_unsigned_long_impl(m_stream, d); } - void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); } - void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); } - void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); } - void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); } - void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); } - void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); } - void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); } - void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); } - void pack_float(float d) { pack_float_impl(m_stream, d); } - void pack_double(double d) { pack_double_impl(m_stream, d); } - void pack_nil() { pack_nil_impl(m_stream); } - void pack_true() { pack_true_impl(m_stream); } - void pack_false() { pack_false_impl(m_stream); } - void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } - void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } + void pack_uint8(uint8_t d) { pack_uint8_impl(m_stream, d); } + void pack_uint16(uint16_t d) { pack_uint16_impl(m_stream, d); } + void pack_uint32(uint32_t d) { pack_uint32_impl(m_stream, d); } + void pack_uint64(uint64_t d) { pack_uint64_impl(m_stream, d); } + void pack_int8(uint8_t d) { pack_int8_impl(m_stream, d); } + void pack_int16(uint16_t d) { pack_int16_impl(m_stream, d); } + void pack_int32(uint32_t d) { pack_int32_impl(m_stream, d); } + void pack_int64(uint64_t d) { pack_int64_impl(m_stream, d); } + + void pack_short(int d) { pack_short_impl(m_stream, d); } + void pack_int(int d) { pack_int_impl(m_stream, d); } + void pack_long(long d) { pack_long_impl(m_stream, d); } + void pack_long_long(long long d) { pack_long_long_impl(m_stream, d); } + void pack_unsigned_short(unsigned short d) { pack_unsigned_short_impl(m_stream, d); } + void pack_unsigned_int(unsigned int d) { pack_unsigned_int_impl(m_stream, d); } + void pack_unsigned_long_long(unsigned long long d) { pack_unsigned_long_long_impl(m_stream, d); } + + void pack_float(float d) { pack_float_impl(m_stream, d); } + void pack_double(double d) { pack_double_impl(m_stream, d); } + + void pack_nil() { pack_nil_impl(m_stream); } + void pack_true() { pack_true_impl(m_stream); } + void pack_false() { pack_false_impl(m_stream); } + + void pack_array(unsigned int n) { pack_array_impl(m_stream, n); } + + void pack_map(unsigned int n) { pack_map_impl(m_stream, n); } + void pack_raw(size_t l) { pack_raw_impl(m_stream, l); } void pack_raw_body(const char* b, size_t l) { pack_raw_body_impl(m_stream, b, l); } private: - static void pack_int_impl(Stream& x, int d); - static void pack_long_impl(Stream& x, long d); - static void pack_unsigned_int_impl(Stream& x, unsigned int d); - static void pack_unsigned_long_impl(Stream& x, unsigned long d); static void pack_uint8_impl(Stream& x, uint8_t d); static void pack_uint16_impl(Stream& x, uint16_t d); static void pack_uint32_impl(Stream& x, uint32_t d); @@ -65,13 +72,27 @@ private: static void pack_int16_impl(Stream& x, int16_t d); static void pack_int32_impl(Stream& x, int32_t d); static void pack_int64_impl(Stream& x, int64_t d); + + static void pack_short_impl(Stream& x, short d); + static void pack_int_impl(Stream& x, int d); + static void pack_long_impl(Stream& x, long d); + static void pack_long_long_impl(Stream& x, long long d); + static void pack_unsigned_short_impl(Stream& x, unsigned short d); + static void pack_unsigned_int_impl(Stream& x, unsigned int d); + static void pack_unsigned_long_impl(Stream& x, unsigned long d); + static void pack_unsigned_long_long_impl(Stream& x, unsigned long long d); + static void pack_float_impl(Stream& x, float d); static void pack_double_impl(Stream& x, double d); + static void pack_nil_impl(Stream& x); static void pack_true_impl(Stream& x); static void pack_false_impl(Stream& x); + static void pack_array_impl(Stream& x, unsigned int n); + static void pack_map_impl(Stream& x, unsigned int n); + static void pack_raw_impl(Stream& x, size_t l); static void pack_raw_body_impl(Stream& x, const void* b, size_t l); @@ -88,8 +109,15 @@ private: #define msgpack_pack_inline_func(name) \ template \ inline void packer::pack_ ## name ## _impl + +#define msgpack_pack_inline_func_cint(name) \ + template \ + inline void packer::pack_ ## name ## _impl + #define msgpack_pack_user Stream& + #define msgpack_pack_append_buffer append_buffer + #include "msgpack/pack_template.h" diff --git a/cpp/type/integer.hpp b/cpp/type/integer.hpp index 3fd57b41..5cd10f6a 100644 --- a/cpp/type/integer.hpp +++ b/cpp/type/integer.hpp @@ -63,102 +63,6 @@ namespace detail { return detail::convert_integer_sign::is_signed>::convert(o); } - - - template - struct pack_integer_size_sign; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) - { o.pack_int8(v); } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) - { o.pack_uint8(v); } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (int16_t)v <= (int16_t)std::numeric_limits::max() && - (int16_t)v >= (int16_t)std::numeric_limits::min()) - { o.pack_int8(v); } - else { o.pack_int16(v); } - } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (uint16_t)v <= (uint16_t)std::numeric_limits::max()) - { o.pack_uint8(v); } - else { o.pack_uint16(v); } - } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (int32_t)v <= (int32_t)std::numeric_limits::max() && - (int32_t)v >= (int32_t)std::numeric_limits::min()) - { o.pack_int8(v); } - else if( (int32_t)v <= (int32_t)std::numeric_limits::max() && - (int32_t)v >= (int32_t)std::numeric_limits::min()) - { o.pack_int16(v); } - else { o.pack_int32(v); } - } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) - { o.pack_uint8(v); } - else if( (uint32_t)v <= (uint32_t)std::numeric_limits::max()) - { o.pack_uint16(v); } - else { o.pack_uint32(v); } - } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (int64_t)v <= (int64_t)std::numeric_limits::max() && - (int64_t)v >= (int64_t)std::numeric_limits::min()) - { o.pack_int8(v); } - else if( (int64_t)v <= (int64_t)std::numeric_limits::max() && - (int64_t)v >= (int64_t)std::numeric_limits::min()) - { o.pack_int16(v); } - else if( (int64_t)v <= (int64_t)std::numeric_limits::max() && - (int64_t)v >= (int64_t)std::numeric_limits::min()) - { o.pack_int32(v); } - else { o.pack_int64(v); } - } - }; - - template - struct pack_integer_size_sign { - static inline void pack(packer& o, T v) { - if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) - { o.pack_uint8(v); } - else if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) - { o.pack_uint16(v); } - else if( (uint64_t)v <= (uint64_t)std::numeric_limits::max()) - { o.pack_uint32(v); } - else { o.pack_uint64(v); } - } - }; - - - template - static inline void pack_integer(T v, packer& o) - { - pack_integer_size_sign::is_signed>::pack(o, v); - } - } // namespace detail } // namespace type @@ -197,44 +101,44 @@ inline unsigned long long& operator>> (object o, unsigned long long& v) template inline packer& operator<< (packer& o, const signed char& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_int8(v); return o; } template inline packer& operator<< (packer& o, const signed short& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_short(v); return o; } template inline packer& operator<< (packer& o, const signed int& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_int(v); return o; } template inline packer& operator<< (packer& o, const signed long& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_long(v); return o; } template inline packer& operator<< (packer& o, const signed long long& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_long_long(v); return o; } template inline packer& operator<< (packer& o, const unsigned char& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_uint8(v); return o; } template inline packer& operator<< (packer& o, const unsigned short& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_unsigned_short(v); return o; } template inline packer& operator<< (packer& o, const unsigned int& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_unsigned_int(v); return o; } template inline packer& operator<< (packer& o, const unsigned long& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_unsigned_long(v); return o; } template inline packer& operator<< (packer& o, const unsigned long long& v) - { type::detail::pack_integer(v, o); return o; } + { o.pack_unsigned_long_long(v); return o; } } // namespace msgpack diff --git a/msgpack/pack_define.h b/msgpack/pack_define.h new file mode 100644 index 00000000..6512d77e --- /dev/null +++ b/msgpack/pack_define.h @@ -0,0 +1,26 @@ +/* + * MessagePack unpacking routine template + * + * Copyright (C) 2008 FURUHASHI Sadayuki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MSGPACK_PACK_DEFINE_H__ +#define MSGPACK_PACK_DEFINE_H__ + +#include +#include +#include + +#endif /* msgpack/pack_define.h */ + diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h index 94fbfd47..dafb0b1c 100644 --- a/msgpack/pack_template.h +++ b/msgpack/pack_template.h @@ -50,7 +50,6 @@ #endif - #ifndef msgpack_pack_inline_func #error msgpack_pack_inline_func template is not defined #endif @@ -68,7 +67,127 @@ * Integer */ -#define msgpack_pack_compress_int32(x, d) \ +#define msgpack_pack_real_uint8(x, d) \ +do { \ + if(d < (1<<7)) { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } else { \ + /* unsigned 8 */ \ + const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } \ +} while(0) + +#define msgpack_pack_real_uint16(x, d) \ +do { \ + if(d < (1<<7)) { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } else if(d < (1<<8)) { \ + /* unsigned 8 */ \ + const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } else { \ + /* unsigned 16 */ \ + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ + msgpack_pack_append_buffer(x, buf, 3); \ + } \ +} while(0) + +#define msgpack_pack_real_uint32(x, d) \ +do { \ + if(d < (1<<8)) { \ + if(d < (1<<7)) { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } else { \ + /* unsigned 8 */ \ + const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } \ + } else { \ + if(d < (1<<16)) { \ + /* unsigned 16 */ \ + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ + msgpack_pack_append_buffer(x, buf, 3); \ + } else { \ + /* unsigned 32 */ \ + const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \ + msgpack_pack_append_buffer(x, buf, 5); \ + } \ + } \ +} while(0) + +#define msgpack_pack_real_uint64(x, d) \ +do { \ + if(d < (1ULL<<8)) { \ + if(d < (1<<7)) { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } else { \ + /* unsigned 8 */ \ + const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } \ + } else { \ + if(d < (1ULL<<16)) { \ + /* signed 16 */ \ + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ + msgpack_pack_append_buffer(x, buf, 3); \ + } else if(d < (1ULL<<32)) { \ + /* signed 32 */ \ + const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \ + msgpack_pack_append_buffer(x, buf, 5); \ + } else { \ + /* signed 64 */ \ + const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \ + msgpack_pack_append_buffer(x, buf, 9); \ + } \ + } \ +} while(0) + +#define msgpack_pack_real_int8(x, d) \ +do { \ + if(d < -(1<<5)) { \ + /* signed 8 */ \ + const unsigned char buf[2] = {0xd0, d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } else { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } \ +} while(0) + +#define msgpack_pack_real_int16(x, d) \ +do { \ + if(d < -(1<<5)) { \ + if(d < -(1<<7)) { \ + /* signed 16 */ \ + const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; \ + msgpack_pack_append_buffer(x, buf, 3); \ + } else { \ + /* signed 8 */ \ + const unsigned char buf[2] = {0xd0, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } \ + } else if(d < (1<<7)) { \ + /* fixnum */ \ + msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); \ + } else { \ + if(d < (1<<8)) { \ + /* unsigned 8 */ \ + const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ + msgpack_pack_append_buffer(x, buf, 2); \ + } else { \ + /* unsigned 16 */ \ + const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ + msgpack_pack_append_buffer(x, buf, 3); \ + } \ + } \ +} while(0) + +#define msgpack_pack_real_int32(x, d) \ do { \ if(d < -(1<<5)) { \ if(d < -(1<<15)) { \ @@ -104,31 +223,7 @@ do { \ } \ } while(0) -#define msgpack_pack_compress_uint32(x, d) \ -do { \ - if(d < (1<<8)) { \ - if(d < (1<<7)) { \ - /* fixnum */ \ - msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \ - } else { \ - /* unsigned 8 */ \ - const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ - msgpack_pack_append_buffer(x, buf, 2); \ - } \ - } else { \ - if(d < (1<<16)) { \ - /* unsigned 16 */ \ - const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ - msgpack_pack_append_buffer(x, buf, 3); \ - } else { \ - /* unsigned 32 */ \ - const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \ - msgpack_pack_append_buffer(x, buf, 5); \ - } \ - } \ -} while(0) - -#define msgpack_pack_compress_int64(x, d) \ +#define msgpack_pack_real_int64(x, d) \ do { \ if(d < -(1LL<<5)) { \ if(d < -(1LL<<15)) { \ @@ -180,133 +275,276 @@ do { \ } \ } while(0) -#define msgpack_pack_compress_uint64(x, d) \ -do { \ - if(d < (1ULL<<8)) { \ - if(d < (1<<7)) { \ - /* fixnum */ \ - msgpack_pack_append_buffer(x, (unsigned char*)&d, 1); \ - } else { \ - /* unsigned 8 */ \ - const unsigned char buf[2] = {0xcc, (uint8_t)d}; \ - msgpack_pack_append_buffer(x, buf, 2); \ - } \ - } else { \ - if(d < (1ULL<<16)) { \ - /* signed 16 */ \ - const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; \ - msgpack_pack_append_buffer(x, buf, 3); \ - } else if(d < (1ULL<<32)) { \ - /* signed 32 */ \ - const unsigned char buf[5] = {0xce, STORE_BE32(d)}; \ - msgpack_pack_append_buffer(x, buf, 5); \ - } else { \ - /* signed 64 */ \ - const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; \ - msgpack_pack_append_buffer(x, buf, 9); \ - } \ - } \ -} while(0) -msgpack_pack_inline_func(int)(msgpack_pack_user x, int d) +#ifdef msgpack_pack_inline_func_fastint + +msgpack_pack_inline_func_fastint(uint8)(msgpack_pack_user x, uint8_t d) { -#if SIZEOF_INT == 8 - msgpack_pack_compress_int64(x, d); -#else - msgpack_pack_compress_int32(x, d); -#endif + const unsigned char buf[2] = {0xcc, d}; + msgpack_pack_append_buffer(x, buf, 2); } -msgpack_pack_inline_func(unsigned_int)(msgpack_pack_user x, unsigned int d) -{ -#if SIZEOF_INT == 8 - msgpack_pack_compress_uint64(x, d); -#else - msgpack_pack_compress_uint32(x, d); -#endif -} - -msgpack_pack_inline_func(long)(msgpack_pack_user x, long d) -{ -#if SIZEOF_LONG == 8 - msgpack_pack_compress_int64(x, d); -#else - msgpack_pack_compress_int32(x, d); -#endif -} - -msgpack_pack_inline_func(unsigned_long)(msgpack_pack_user x, unsigned long d) -{ -#if SIZEOF_LONG == 8 - msgpack_pack_compress_uint64(x, d); -#else - msgpack_pack_compress_uint32(x, d); -#endif -} - -#undef msgpack_pack_compress_int32 -#undef msgpack_pack_compress_uint32 -#undef msgpack_pack_compress_int64 -#undef msgpack_pack_compress_uint64 - -msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d) -{ - if(d < 128) { - msgpack_pack_append_buffer(x, &d, 1); - } else { - const unsigned char buf[2] = {0xcc, d}; - msgpack_pack_append_buffer(x, buf, 2); - } -} - -msgpack_pack_inline_func(uint16)(msgpack_pack_user x, uint16_t d) +msgpack_pack_inline_func_fastint(uint16)(msgpack_pack_user x, uint16_t d) { const unsigned char buf[3] = {0xcd, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func(uint32)(msgpack_pack_user x, uint32_t d) +msgpack_pack_inline_func_fastint(uint32)(msgpack_pack_user x, uint32_t d) { const unsigned char buf[5] = {0xce, STORE_BE32(d)}; msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func(uint64)(msgpack_pack_user x, uint64_t d) +msgpack_pack_inline_func_fastint(uint64)(msgpack_pack_user x, uint64_t d) { const unsigned char buf[9] = {0xcf, STORE_BE64(d)}; msgpack_pack_append_buffer(x, buf, 9); } -msgpack_pack_inline_func(int8)(msgpack_pack_user x, int8_t d) +msgpack_pack_inline_func_fastint(int8)(msgpack_pack_user x, int8_t d) { - if(d > 0) { - msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); - } else if(d >= -32) { - msgpack_pack_append_buffer(x, (uint8_t*)&d, 1); - } else { - const unsigned char buf[2] = {0xd0, d}; - msgpack_pack_append_buffer(x, buf, 2); - } + const unsigned char buf[2] = {0xd0, d}; + msgpack_pack_append_buffer(x, buf, 2); } -msgpack_pack_inline_func(int16)(msgpack_pack_user x, int16_t d) +msgpack_pack_inline_func_fastint(int16)(msgpack_pack_user x, int16_t d) { const unsigned char buf[3] = {0xd1, STORE_BE16(d)}; msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func(int32)(msgpack_pack_user x, int32_t d) +msgpack_pack_inline_func_fastint(int32)(msgpack_pack_user x, int32_t d) { const unsigned char buf[5] = {0xd2, STORE_BE32(d)}; msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func(int64)(msgpack_pack_user x, int64_t d) +msgpack_pack_inline_func_fastint(int64)(msgpack_pack_user x, int64_t d) { const unsigned char buf[9] = {0xd3, STORE_BE64(d)}; msgpack_pack_append_buffer(x, buf, 9); } +#undef msgpack_pack_inline_func_fastint +#endif + + +msgpack_pack_inline_func(uint8)(msgpack_pack_user x, uint8_t d) +{ + msgpack_pack_real_uint8(x, d); +} + +msgpack_pack_inline_func(uint16)(msgpack_pack_user x, uint16_t d) +{ + msgpack_pack_real_uint16(x, d); +} + +msgpack_pack_inline_func(uint32)(msgpack_pack_user x, uint32_t d) +{ + msgpack_pack_real_uint32(x, d); +} + +msgpack_pack_inline_func(uint64)(msgpack_pack_user x, uint64_t d) +{ + msgpack_pack_real_uint64(x, d); +} + +msgpack_pack_inline_func(int8)(msgpack_pack_user x, int8_t d) +{ + msgpack_pack_real_int8(x, d); +} + +msgpack_pack_inline_func(int16)(msgpack_pack_user x, int16_t d) +{ + msgpack_pack_real_int16(x, d); +} + +msgpack_pack_inline_func(int32)(msgpack_pack_user x, int32_t d) +{ + msgpack_pack_real_int32(x, d); +} + +msgpack_pack_inline_func(int64)(msgpack_pack_user x, int64_t d) +{ + msgpack_pack_real_int64(x, d); +} + + +#ifdef msgpack_pack_inline_func_cint + +msgpack_pack_inline_func_cint(short)(msgpack_pack_user x, short d) +{ +#if defined(SIZEOF_SHORT) || defined(SHRT_MAX) +#if SIZEOF_SHORT == 2 || SHRT_MAX == 0x7fff + msgpack_pack_real_int16(x, d); +#elif SIZEOF_SHORT == 4 || SHRT_MAX == 0x7fffffff + msgpack_pack_real_int32(x, d); +#else + msgpack_pack_real_int64(x, d); +#endif +#else +if(sizeof(short) == 2) { + msgpack_pack_real_int16(x, d); +} else if(sizeof(short) == 4) { + msgpack_pack_real_int32(x, d); +} else { + msgpack_pack_real_int64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(int)(msgpack_pack_user x, int d) +{ +#if defined(SIZEOF_INT) || defined(INT_MAX) +#if SIZEOF_INT == 2 || INT_MAX == 0x7fff + msgpack_pack_real_int16(x, d); +#elif SIZEOF_INT == 4 || INT_MAX == 0x7fffffff + msgpack_pack_real_int32(x, d); +#else + msgpack_pack_real_int64(x, d); +#endif +#else +if(sizeof(int) == 2) { + msgpack_pack_real_int16(x, d); +} else if(sizeof(int) == 4) { + msgpack_pack_real_int32(x, d); +} else { + msgpack_pack_real_int64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(long)(msgpack_pack_user x, long d) +{ +#if defined(SIZEOF_LONG) || defined(LONG_MAX) +#if SIZEOF_LONG == 2 || LONG_MAX == 0x7fffL + msgpack_pack_real_int16(x, d); +#elif SIZEOF_LONG == 4 || LONG_MAX == 0x7fffffffL + msgpack_pack_real_int32(x, d); +#else + msgpack_pack_real_int64(x, d); +#endif +#else +if(sizeof(long) == 2) { + msgpack_pack_real_int16(x, d); +} else if(sizeof(long) == 4) { + msgpack_pack_real_int32(x, d); +} else { + msgpack_pack_real_int64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(long_long)(msgpack_pack_user x, long long d) +{ +#if defined(SIZEOF_LONG_LONG) || defined(LLONG_MAX) +#if SIZEOF_LONG_LONG == 2 || LLONG_MAX == 0x7fffL + msgpack_pack_real_int16(x, d); +#elif SIZEOF_LONG_LONG == 4 || LLONG_MAX == 0x7fffffffL + msgpack_pack_real_int32(x, d); +#else + msgpack_pack_real_int64(x, d); +#endif +#else +if(sizeof(long long) == 2) { + msgpack_pack_real_int16(x, d); +} else if(sizeof(long long) == 4) { + msgpack_pack_real_int32(x, d); +} else { + msgpack_pack_real_int64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(unsigned_short)(msgpack_pack_user x, unsigned short d) +{ +#if defined(SIZEOF_SHORT) || defined(USHRT_MAX) +#if SIZEOF_SHORT == 2 || USHRT_MAX == 0xffffU + msgpack_pack_real_uint16(x, d); +#elif SIZEOF_SHORT == 4 || USHRT_MAX == 0xffffffffU + msgpack_pack_real_uint32(x, d); +#else + msgpack_pack_real_uint64(x, d); +#endif +#else +if(sizeof(unsigned short) == 2) { + msgpack_pack_real_uint16(x, d); +} else if(sizeof(unsigned short) == 4) { + msgpack_pack_real_uint32(x, d); +} else { + msgpack_pack_real_uint64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(unsigned_int)(msgpack_pack_user x, unsigned int d) +{ +#if defined(SIZEOF_INT) || defined(UINT_MAX) +#if SIZEOF_INT == 2 || UINT_MAX == 0xffffU + msgpack_pack_real_uint16(x, d); +#elif SIZEOF_INT == 4 || UINT_MAX == 0xffffffffU + msgpack_pack_real_uint32(x, d); +#else + msgpack_pack_real_uint64(x, d); +#endif +#else +if(sizeof(unsigned int) == 2) { + msgpack_pack_real_uint16(x, d); +} else if(sizeof(unsigned int) == 4) { + msgpack_pack_real_uint32(x, d); +} else { + msgpack_pack_real_uint64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(unsigned_long)(msgpack_pack_user x, unsigned long d) +{ +#if defined(SIZEOF_LONG) || defined(ULONG_MAX) +#if SIZEOF_LONG == 2 || ULONG_MAX == 0xffffUL + msgpack_pack_real_uint16(x, d); +#elif SIZEOF_LONG == 4 || ULONG_MAX == 0xffffffffUL + msgpack_pack_real_uint32(x, d); +#else + msgpack_pack_real_uint64(x, d); +#endif +#else +if(sizeof(unsigned int) == 2) { + msgpack_pack_real_uint16(x, d); +} else if(sizeof(unsigned int) == 4) { + msgpack_pack_real_uint32(x, d); +} else { + msgpack_pack_real_uint64(x, d); +} +#endif +} + +msgpack_pack_inline_func_cint(unsigned_long_long)(msgpack_pack_user x, unsigned long long d) +{ +#if defined(SIZEOF_LONG_LONG) || defined(ULLONG_MAX) +#if SIZEOF_LONG_LONG == 2 || ULLONG_MAX == 0xffffUL + msgpack_pack_real_uint16(x, d); +#elif SIZEOF_LONG_LONG == 4 || ULLONG_MAX == 0xffffffffUL + msgpack_pack_real_uint32(x, d); +#else + msgpack_pack_real_uint64(x, d); +#endif +#else +if(sizeof(unsigned long long) == 2) { + msgpack_pack_real_uint16(x, d); +} else if(sizeof(unsigned long long) == 4) { + msgpack_pack_real_uint32(x, d); +} else { + msgpack_pack_real_uint64(x, d); +} +#endif +} + +#undef msgpack_pack_inline_func_cint +#endif + + /* * Float @@ -430,3 +668,12 @@ msgpack_pack_inline_func(raw_body)(msgpack_pack_user x, const void* b, size_t l) #undef STORE_BE32 #undef STORE_BE64 +#undef msgpack_pack_real_uint8 +#undef msgpack_pack_real_uint16 +#undef msgpack_pack_real_uint32 +#undef msgpack_pack_real_uint64 +#undef msgpack_pack_real_int8 +#undef msgpack_pack_real_int16 +#undef msgpack_pack_real_int32 +#undef msgpack_pack_real_int64 + diff --git a/ruby/gem/Manifest.txt b/ruby/gem/Manifest.txt index 388135e8..17e2b788 100644 --- a/ruby/gem/Manifest.txt +++ b/ruby/gem/Manifest.txt @@ -10,6 +10,7 @@ ext/pack.h ext/rbinit.c ext/unpack.c ext/unpack.h +msgpack/pack_define.h msgpack/pack_template.h msgpack/unpack_define.h msgpack/unpack_template.h diff --git a/ruby/gengem.sh b/ruby/gengem.sh index 000c7089..de9f4a5f 100755 --- a/ruby/gengem.sh +++ b/ruby/gengem.sh @@ -7,6 +7,7 @@ cp rbinit.c gem/ext/ cp unpack.c gem/ext/ cp unpack.h gem/ext/ #cp ../README gem/README.txt +cp ../msgpack/pack_define.h gem/msgpack/ cp ../msgpack/pack_template.h gem/msgpack/ cp ../msgpack/unpack_define.h gem/msgpack/ cp ../msgpack/unpack_template.h gem/msgpack/ diff --git a/ruby/pack.c b/ruby/pack.c index 5262024d..c399c25a 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -16,41 +16,19 @@ * limitations under the License. */ #include "ruby.h" -#include -#include +#include "msgpack/pack_define.h" #define msgpack_pack_inline_func(name) \ static inline void msgpack_pack_##name +#define msgpack_pack_inline_func_cint(name) \ + static inline void msgpack_pack_##name + #define msgpack_pack_user VALUE #define msgpack_pack_append_buffer(user, buf, len) \ rb_str_buf_cat(user, (const void*)buf, len) -/* -static void msgpack_pack_int(VALUE x, int d); -static void msgpack_pack_unsigned_int(VALUE x, unsigned int d); -static void msgpack_pack_long(VALUE x, long d); -static void msgpack_pack_unsigned_long(VALUE x, unsigned long d); -static void msgpack_pack_uint8(VALUE x, uint8_t d); -static void msgpack_pack_uint16(VALUE x, uint16_t d); -static void msgpack_pack_uint32(VALUE x, uint32_t d); -static void msgpack_pack_uint64(VALUE x, uint64_t d); -static void msgpack_pack_int8(VALUE x, int8_t d); -static void msgpack_pack_int16(VALUE x, int16_t d); -static void msgpack_pack_int32(VALUE x, int32_t d); -static void msgpack_pack_int64(VALUE x, int64_t d); -static void msgpack_pack_float(VALUE x, float d); -static void msgpack_pack_double(VALUE x, double d); -static void msgpack_pack_nil(VALUE x); -static void msgpack_pack_true(VALUE x); -static void msgpack_pack_false(VALUE x); -static void msgpack_pack_array(VALUE x, unsigned int n); -static void msgpack_pack_map(VALUE x, unsigned int n); -static void msgpack_pack_raw(VALUE x, size_t l); -static void msgpack_pack_raw_body(VALUE x, const void* b, size_t l); -*/ - #include "msgpack/pack_template.h"