commit 40624fc1c3b8299c14cb2808b9aac1bc809b9a99 Author: peshkurov Date: Tue Jun 7 11:54:38 2016 +0300 mobile-logs-api-server: added gzip. [#MOBMET-3693] diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000000..570373a51d4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + + +# Matches multiple files with brace expansion notation +# Set default charset +[*.{c,cpp,cxx,h,hpp,hxx,py}] +charset = utf-8 +indent_style = tab +indent_size = 4 +trim_trailing_whitespace = true diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000000..bcc7d57b904 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +contrib/* linguist-vendored +*.h linguist-language=C++ diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..84bab000d3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,197 @@ +# emacs files +*~ +*\# + +# auto generated files +*.logrt + +build + +# callgrind files +callgrind.out.* + +# ignore kdevelop files +*.kdev4 +*.kdev_include_paths + +# ignore perf output +*/perf.data + +# ignore build files +CMakeCache.txt +CMakeFiles +Makefile +cmake_install.cmake +CTestTestfile.cmake +*.a +*.o + +# ignore generated files +*-metrika-yandex + +test.cpp +utils/compressor/compressor +utils/corrector_utf8/corrector_utf8 +utils/iotest/iotest +utils/iotest/iotest_aio +utils/iotest/iotest_nonblock +utils/config-processor/config-processor +CPackConfig.cmake +CPackSourceConfig.cmake +contrib/libpoco/Poco/ +contrib/libpoco/bin/ +contrib/libpoco/cmake_uninstall.cmake +contrib/libre2/re2_st/ +dbms/src/Client/clickhouse-benchmark +dbms/src/Client/clickhouse-client +dbms/src/Client/tests/test-connect +dbms/src/Common/tests/arena_with_free_lists +dbms/src/Common/tests/auto_array +dbms/src/Common/tests/compact_array +dbms/src/Common/tests/hash_table +dbms/src/Common/tests/hashes_test +dbms/src/Common/tests/int_hashes_perf +dbms/src/Common/tests/lru_cache +dbms/src/Common/tests/parallel_aggregation +dbms/src/Common/tests/parallel_aggregation2 +dbms/src/Common/tests/radix_sort +dbms/src/Common/tests/shell_command_test +dbms/src/Common/tests/simple_cache +dbms/src/Common/tests/sip_hash +dbms/src/Common/tests/sip_hash_perf +dbms/src/Common/tests/small_table +dbms/src/Core/tests/exception +dbms/src/Core/tests/field +dbms/src/Core/tests/rvo_test +dbms/src/Core/tests/string_pool +dbms/src/DataStreams/tests/aggregating_stream +dbms/src/DataStreams/tests/block_row_transforms +dbms/src/DataStreams/tests/block_tab_separated_streams +dbms/src/DataStreams/tests/collapsing_sorted_stream +dbms/src/DataStreams/tests/expression_stream +dbms/src/DataStreams/tests/filter_stream +dbms/src/DataStreams/tests/filter_stream_hitlog +dbms/src/DataStreams/tests/fork_streams +dbms/src/DataStreams/tests/glue_streams +dbms/src/DataStreams/tests/json_streams +dbms/src/DataStreams/tests/native_streams +dbms/src/DataStreams/tests/sorting_stream +dbms/src/DataStreams/tests/tab_separated_streams +dbms/src/DataStreams/tests/union_stream +dbms/src/DataStreams/tests/union_stream2 +dbms/src/DataTypes/tests/data_type_string +dbms/src/DataTypes/tests/data_types_number_fixed +dbms/src/Functions/tests/functions_arithmetic +dbms/src/Functions/tests/logical_functions_performance +dbms/src/Functions/tests/number_traits +dbms/src/IO/tests/async_write +dbms/src/IO/tests/cached_compressed_read_buffer +dbms/src/IO/tests/compressed_buffer +dbms/src/IO/tests/hashing_read_buffer +dbms/src/IO/tests/hashing_write_buffer +dbms/src/IO/tests/io_and_exceptions +dbms/src/IO/tests/io_operators +dbms/src/IO/tests/mempbrk +dbms/src/IO/tests/o_direct_and_dirty_pages +dbms/src/IO/tests/parse_int_perf +dbms/src/IO/tests/parse_int_perf2 +dbms/src/IO/tests/read_buffer +dbms/src/IO/tests/read_buffer_aio +dbms/src/IO/tests/read_buffer_perf +dbms/src/IO/tests/read_escaped_string +dbms/src/IO/tests/read_float_perf +dbms/src/IO/tests/read_write_int +dbms/src/IO/tests/valid_utf8 +dbms/src/IO/tests/valid_utf8_perf +dbms/src/IO/tests/var_uint +dbms/src/IO/tests/write_buffer +dbms/src/IO/tests/write_buffer_aio +dbms/src/IO/tests/write_buffer_perf +dbms/src/Interpreters/tests/address_patterns +dbms/src/Interpreters/tests/aggregate +dbms/src/Interpreters/tests/compiler_test +dbms/src/Interpreters/tests/create_query +dbms/src/Interpreters/tests/expression +dbms/src/Interpreters/tests/expression_analyzer +dbms/src/Interpreters/tests/hash_map +dbms/src/Interpreters/tests/hash_map2 +dbms/src/Interpreters/tests/hash_map3 +dbms/src/Interpreters/tests/hash_map_string +dbms/src/Interpreters/tests/hash_map_string_2 +dbms/src/Interpreters/tests/hash_map_string_3 +dbms/src/Interpreters/tests/hash_map_string_small +dbms/src/Interpreters/tests/in_join_subqueries_preprocessor +dbms/src/Interpreters/tests/logical_expressions_optimizer +dbms/src/Interpreters/tests/select_query +dbms/src/Interpreters/tests/two_level_hash_map +dbms/src/Interpreters/tests/users +dbms/src/Parsers/tests/create_parser +dbms/src/Parsers/tests/select_parser +dbms/src/Server/clickhouse-server +dbms/src/Server/clickhouse-server.init +dbms/src/Storages/tests/hit_log +dbms/src/Storages/tests/merge_tree +dbms/src/Storages/tests/part_checker +dbms/src/Storages/tests/part_name +dbms/src/Storages/tests/pk_condition +dbms/src/Storages/tests/seek_speed_test +dbms/src/Storages/tests/storage_log +dbms/src/Storages/tests/system_numbers +libs/libcommon/src/revision.h +libs/libcommon/src/tests/date_lut2 +libs/libcommon/src/tests/date_lut3 +libs/libcommon/src/tests/date_lut4 +libs/libcommon/src/tests/date_lut_init +libs/libcommon/src/tests/multi_version +libs/libmysqlxx/src/tests/failover +libs/libmysqlxx/src/tests/mysqlxx_test +libs/libzkutil/src/tests/zkutil_expiration_test +libs/libzkutil/src/tests/zkutil_test +libs/libzkutil/src/tests/zkutil_test_async +libs/libzkutil/src/tests/zkutil_test_commands +libs/libzkutil/src/tests/zkutil_test_lock +libs/libzkutil/src/tests/zkutil_zookeeper_holder +utils/zookeeper-create-entry-to-download-part/zookeeper-create-entry-to-download-part +utils/zookeeper-dump-tree/zookeeper-dump-tree +utils/zookeeper-remove-by-list/zookeeper-remove-by-list + +dbms/src/Server/data/* +dbms/src/Server/metadata/* +dbms/src/Server/status +config-9001.xml + +*-preprocessed.xml + +core +vgcore* + +*.install +*.deb +*.build +*.upload +*.changes +build-stamp +configure-stamp +debian/changelog +debian/*.debhelper.log +debian/*.cron.d +debian/*.init +debian/*.debhelper +debian/*.substvars + +*.bin +*.mrk + +.dupload.conf + +# Netbeans project files +nbproject/* + +# JetBrains project files +.idea + +config-preprocessed.xml + +# Protobuf +*.pb.cpp +*.pb.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000000..908943122c1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "private"] + path = private + url = git@github.yandex-team.ru:Metrika/ClickHouse_private.git diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 00000000000..93b368fa98b --- /dev/null +++ b/AUTHORS @@ -0,0 +1,22 @@ +The following authors have created the source code of "ClickHouse" +published and distributed by YANDEX LLC as the owner: + +Alexey Milovidov +Alexey Arno +Andrey Mironov +Michael Kolupaev +Pavel Kartavyy +Evgeniy Gatov +Sergey Fedorov +Vyacheslav Alipov +Vladimir Chebotarev +Dmitry Galuza +Sergey Magidovich +Anton Tikhonov +Alexey Vasiliev +Andrey Urusov +Vsevolod Orlov +Roman Peshkurov +Michael Razuvaev +Ilya Korolev +Maxim Nikulin diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000000..30612cc5d3d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,129 @@ +SET(CMAKE_C_COMPILER "gcc-5") +SET(CMAKE_CXX_COMPILER "g++-5") + +project (METRICA) +cmake_minimum_required(VERSION 2.6) + +# отключаем варнинг о том, что в каждой директории должен быть CMakeLists.txt +CMAKE_POLICY(SET CMP0014 OLD) + +IF(NOT CMAKE_BUILD_TYPE) + message(STATUS "CMAKE_BUILD_TYPE is not set, set to default = RELWITHDEBINFO") + SET(CMAKE_BUILD_TYPE "RELWITHDEBINFO") +ENDIF() +MESSAGE( STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE} ) + +set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Debug;Release;MinSizeRel" CACHE STRING "" FORCE) + +IF (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*)") + SET(AARCH64 1) +ENDIF() + +IF (NOT AARCH64) + SET(MACHINE_FLAGS "-msse4 -mpopcnt") +ENDIF() + +SET(CMAKE_BUILD_COLOR_MAKEFILE ON) +SET(CMAKE_CXX_FLAGS "-std=gnu++1y -Wall -Werror -Wnon-virtual-dtor ${MACHINE_FLAGS}") +SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") +SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g") +SET(CMAKE_C_FLAGS "-Wall -Werror ${MACHINE_FLAGS}") +SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") +SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -g") +SET(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") + +# cmake -DCMAKE_BUILD_TYPE=Debug .. +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") + +# Флаги для test coverage +SET(TEST_COVERAGE TRUE CACHE BOOL "Add compliler flags for test coverage") +IF (TEST_COVERAGE) +SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -fprofile-arcs -ftest-coverage -fPIC -DIS_DEBUG") +ENDIF(TEST_COVERAGE) + +# Собирать тесты? +IF (NOT DEFINED TESTS) + MESSAGE(STATUS "Tests are enabled") + SET(TESTS YES) +ENDIF() + +# тесты запускать с помощью "make check" +IF(TESTS) + INCLUDE(add.test.cmake) +ENDIF(TESTS) + +# Префикс для установки +SET(CMAKE_INSTALL_PREFIX /usr) + +# Директория для файлов, специфичных для Яндекса +SET(CLICKHOUSE_PRIVATE_DIR ${METRICA_SOURCE_DIR}/private/) + +include_directories (${METRICA_SOURCE_DIR}/contrib/libcityhash/) +include_directories (${METRICA_SOURCE_DIR}/contrib/liblz4/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libdivide/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libdouble-conversion/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libcpuid/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libzstd/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libfarmhash/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libmetrohash/src) +include_directories (${METRICA_SOURCE_DIR}/contrib/libsparsehash/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libre2/) +include_directories (${METRICA_BINARY_DIR}/contrib/libre2/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libboost-threadpool/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Foundation/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Util/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Net/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/NetSSL_OpenSSL/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Data/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Data/MySQL/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Data/ODBC/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Data/SQLite/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Crypto/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/XML/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/JSON/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/MongoDB/include/) +include_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/Zip/include/) + +include_directories (${METRICA_SOURCE_DIR}/libs/libcommon/include/) +include_directories (${METRICA_SOURCE_DIR}/libs/libdaemon/include/) +include_directories (${METRICA_SOURCE_DIR}/libs/libpocoext/include/) +include_directories (${METRICA_SOURCE_DIR}/libs/libmysqlxx/include/) +include_directories (${METRICA_SOURCE_DIR}/libs/libzkutil/include/) + +include_directories (${METRICA_SOURCE_DIR}/tools/) +include_directories (${METRICA_SOURCE_DIR}/dbms/include) +include_directories (${METRICA_SOURCE_DIR}) +include_directories (${METRICA_BINARY_DIR}) + +include_directories (/usr/local/include/glib-2.0/) +include_directories (/usr/local/lib/glib-2.0/include/) +include_directories (/usr/include/glib-2.0/) + +IF (AARCH64) + include_directories (/usr/lib/aarch64-linux-gnu/glib-2.0/include/) +ELSE() + include_directories (/usr/lib/x86_64-linux-gnu/glib-2.0/include/) +ENDIF() + +include_directories (/usr/local/include/) + +link_directories (${METRICA_SOURCE_DIR}/contrib/libcityhash) +link_directories (${METRICA_SOURCE_DIR}/contrib/liblz4) +link_directories (${METRICA_SOURCE_DIR}/contrib/libdouble-conversion) +link_directories (${METRICA_SOURCE_DIR}/contrib/libcpuid) +link_directories (${METRICA_SOURCE_DIR}/contrib/libzstd) +link_directories (${METRICA_SOURCE_DIR}/contrib/libre2) +link_directories (${METRICA_SOURCE_DIR}/contrib/libpoco/lib) + +link_directories (${METRICA_BINARY_DIR}/libs/libcommon) +link_directories (${METRICA_BINARY_DIR}/libs/libdaemon) +link_directories (${METRICA_BINARY_DIR}/libs/libpocoext) +link_directories (${METRICA_BINARY_DIR}/libs/libmysqlxx) + +link_directories (${METRICA_BINARY_DIR}/dbms) +link_directories (/usr/local/lib) + +add_subdirectory (contrib) +add_subdirectory (libs) +add_subdirectory (utils) +add_subdirectory (dbms) diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000000..54aca355091 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2016 YANDEX LLC + + 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. diff --git a/add.test.cmake b/add.test.cmake new file mode 100644 index 00000000000..0585f0951d4 --- /dev/null +++ b/add.test.cmake @@ -0,0 +1,15 @@ +# добавляем вывод программы при ошибке теста +enable_testing() +if (CMAKE_CONFIGURATION_TYPES) + add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} + --force-new-ctest-process --output-on-failure + --build-config "$") +else() + add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} + --force-new-ctest-process --output-on-failure) +endif() + +macro (add_check target) + add_test(NAME test_${target} COMMAND ${target} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + add_dependencies(check ${target}) +endmacro (add_check) \ No newline at end of file diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt new file mode 100644 index 00000000000..28350e450a5 --- /dev/null +++ b/contrib/CMakeLists.txt @@ -0,0 +1,13 @@ +add_subdirectory (libcityhash) +add_subdirectory (liblz4) +add_subdirectory (libdouble-conversion) +add_subdirectory (libzstd) +add_subdirectory (libfarmhash) +add_subdirectory (libmetrohash) +add_subdirectory (libpoco) +add_subdirectory (libre2) +add_subdirectory (libboost-threadpool) + +IF (NOT AARCH64) + add_subdirectory (libcpuid) +ENDIF() diff --git a/contrib/libboost-threadpool/COPYING b/contrib/libboost-threadpool/COPYING new file mode 100644 index 00000000000..42bd7151394 --- /dev/null +++ b/contrib/libboost-threadpool/COPYING @@ -0,0 +1,5 @@ +Copyright (c) 2005-2007 Philipp Henkel + +Use, modification, and distribution are subject to the +Boost Software License, Version 1.0. (See accompanying file +LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) diff --git a/contrib/libboost-threadpool/LICENSE_1_0.txt b/contrib/libboost-threadpool/LICENSE_1_0.txt new file mode 100644 index 00000000000..36b7cd93cdf --- /dev/null +++ b/contrib/libboost-threadpool/LICENSE_1_0.txt @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/contrib/libboost-threadpool/threadpool.hpp b/contrib/libboost-threadpool/threadpool.hpp new file mode 100644 index 00000000000..3777adb3459 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool.hpp @@ -0,0 +1,28 @@ +/*! \file +* \brief Main include. +* +* This is the only file you have to include in order to use the +* complete threadpool library. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + +#ifndef THREADPOOL_HPP_INCLUDED +#define THREADPOOL_HPP_INCLUDED + +#include "threadpool/future.hpp" +#include "threadpool/pool.hpp" + +#include "threadpool/pool_adaptors.hpp" +#include "threadpool/task_adaptors.hpp" + + +#endif // THREADPOOL_HPP_INCLUDED + diff --git a/contrib/libboost-threadpool/threadpool/detail/future.hpp b/contrib/libboost-threadpool/threadpool/detail/future.hpp new file mode 100644 index 00000000000..4e4b35bebe1 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/detail/future.hpp @@ -0,0 +1,215 @@ +/*! \file +* \brief TODO. +* +* TODO. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED +#define THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED + + +#include "locking_ptr.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace boost { namespace threadpool { namespace detail +{ + +template +class future_impl +{ +public: + typedef Result const & result_type; //!< Indicates the functor's result type. + + typedef Result future_result_type; //!< Indicates the future's result type. + typedef future_impl future_type; + +private: + volatile bool m_ready; + volatile future_result_type m_result; + + mutable mutex m_monitor; + mutable condition m_condition_ready; + + volatile bool m_is_cancelled; + volatile bool m_executing; + +public: + + +public: + + future_impl() + : m_ready(false) + , m_is_cancelled(false) + { + } + + bool ready() const volatile + { + return m_ready; + } + + void wait() const volatile + { + const future_type* self = const_cast(this); + mutex::scoped_lock lock(self->m_monitor); + + while(!m_ready) + { + self->m_condition_ready.wait(lock); + } + } + + + bool timed_wait(boost::xtime const & timestamp) const + { + const future_type* self = const_cast(this); + mutex::scoped_lock lock(self->m_monitor); + + while(!m_ready) + { + if(!self->m_condition_ready.timed_wait(lock, timestamp)) return false; + } + + return true; + } + + + result_type operator()() const volatile + { + wait(); +/* + if( throw_exception_ != 0 ) + { + throw_exception_( this ); + } +*/ + + return *(const_cast(&m_result)); + } + + + void set_value(future_result_type const & r) volatile + { + locking_ptr lockedThis(*this, m_monitor); + if(!m_ready && !m_is_cancelled) + { + lockedThis->m_result = r; + lockedThis->m_ready = true; + lockedThis->m_condition_ready.notify_all(); + } + } +/* + template void set_exception() // throw() + { + m_impl->template set_exception(); + } + + template void set_exception( char const * what ) // throw() + { + m_impl->template set_exception( what ); + } + */ + + + bool cancel() volatile + { + if(!m_ready || m_executing) + { + m_is_cancelled = true; + return true; + } + else + { + return false; + } + } + + + bool is_cancelled() const volatile + { + return m_is_cancelled; + } + + + void set_execution_status(bool executing) volatile + { + m_executing = executing; + } +}; + + +template< + template class Future, + typename Function +> +class future_impl_task_func +{ + +public: + typedef void result_type; //!< Indicates the functor's result type. + + typedef Function function_type; //!< Indicates the function's type. + typedef typename result_of::type future_result_type; //!< Indicates the future's result type. + typedef Future future_type; //!< Indicates the future's type. + + // The task is required to be a nullary function. + BOOST_STATIC_ASSERT(function_traits::arity == 0); + + // The task function's result type is required not to be void. + BOOST_STATIC_ASSERT(!is_void::value); + +private: + function_type m_function; + shared_ptr m_future; + +public: + future_impl_task_func(function_type const & function, shared_ptr const & future) + : m_function(function) + , m_future(future) + { + } + + void operator()() + { + if(m_function) + { + m_future->set_execution_status(true); + if(!m_future->is_cancelled()) + { + // TODO future exeception handling + m_future->set_value(m_function()); + } + m_future->set_execution_status(false); // TODO consider exceptions + } + } + +}; + + + + + +} } } // namespace boost::threadpool::detail + +#endif // THREADPOOL_DETAIL_FUTURE_IMPL_HPP_INCLUDED + + diff --git a/contrib/libboost-threadpool/threadpool/detail/locking_ptr.hpp b/contrib/libboost-threadpool/threadpool/detail/locking_ptr.hpp new file mode 100644 index 00000000000..2d44db70fe9 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/detail/locking_ptr.hpp @@ -0,0 +1,102 @@ +/*! \file +* \brief The locking_ptr is smart pointer with a scoped locking mechanism. +* +* The class is a wrapper for a volatile pointer. It enables synchronized access to the +* internal pointer by locking the passed mutex. +* locking_ptr is based on Andrei Alexandrescu's LockingPtr. For more information +* see article "volatile - Multithreaded Programmer's Best Friend" by A. Alexandrescu. +* +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_DETAIL_LOCKING_PTR_HPP_INCLUDED +#define THREADPOOL_DETAIL_LOCKING_PTR_HPP_INCLUDED + +#include + +// Support for old boost::thread +//********************************************** +#include +#ifndef BOOST_THREAD_MUTEX_HPP +#include +#endif +//********************************************** + +namespace boost { namespace threadpool { namespace detail +{ + +/*! \brief Smart pointer with a scoped locking mechanism. + * + * This class is a wrapper for a volatile pointer. It enables synchronized access to the + * internal pointer by locking the passed mutex. + */ + template + class locking_ptr + : private noncopyable + { + T* m_obj; //!< The instance pointer. + Mutex & m_mutex; //!< Mutex is used for scoped locking. + + public: + /// Constructor. + locking_ptr(volatile T& obj, const volatile Mutex& mtx) + : m_obj(const_cast(&obj)) + , m_mutex(*const_cast(&mtx)) + { + // Lock mutex + +#ifndef BOOST_THREAD_MUTEX_HPP + // Support for old boost::thread + boost::detail::thread::lock_ops::lock(m_mutex); +#else + m_mutex.lock(); +#endif + } + + + /// Destructor. + ~locking_ptr() + { + // Unlock mutex +#ifndef BOOST_THREAD_MUTEX_HPP + // Support for old boost::thread + boost::detail::thread::lock_ops::unlock(m_mutex); +#else + m_mutex.unlock(); +#endif + } + + + /*! Returns a reference to the stored instance. + * \return The instance's reference. + */ + T& operator*() const + { + return *m_obj; + } + + + /*! Returns a pointer to the stored instance. + * \return The instance's pointer. + */ + T* operator->() const + { + return m_obj; + } + }; + + +} } } // namespace boost::threadpool::detail + + +#endif // THREADPOOL_DETAIL_LOCKING_PTR_HPP_INCLUDED + diff --git a/contrib/libboost-threadpool/threadpool/detail/pool_core.hpp b/contrib/libboost-threadpool/threadpool/detail/pool_core.hpp new file mode 100644 index 00000000000..87ec1873cf8 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/detail/pool_core.hpp @@ -0,0 +1,453 @@ +/*! \file +* \brief Thread pool core. +* +* This file contains the threadpool's core class: pool. +* +* Thread pools are a mechanism for asynchronous and parallel processing +* within the same process. The pool class provides a convenient way +* for dispatching asynchronous tasks as functions objects. The scheduling +* of these tasks can be easily controlled by using customized schedulers. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_POOL_CORE_HPP_INCLUDED +#define THREADPOOL_POOL_CORE_HPP_INCLUDED + + + + +#include "locking_ptr.hpp" +#include "worker_thread.hpp" + +#include "../task_adaptors.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +/// The namespace threadpool contains a thread pool and related utility classes. +namespace boost { namespace threadpool { namespace detail +{ + + /*! \brief Thread pool. + * + * Thread pools are a mechanism for asynchronous and parallel processing + * within the same process. The pool class provides a convenient way + * for dispatching asynchronous tasks as functions objects. The scheduling + * of these tasks can be easily controlled by using customized schedulers. + * A task must not throw an exception. + * + * A pool_impl is DefaultConstructible and NonCopyable. + * + * \param Task A function object which implements the operator 'void operator() (void) const'. The operator () is called by the pool to execute the task. Exceptions are ignored. + * \param Scheduler A task container which determines how tasks are scheduled. It is guaranteed that this container is accessed only by one thread at a time. The scheduler shall not throw exceptions. + * + * \remarks The pool class is thread-safe. + * + * \see Tasks: task_func, prio_task_func + * \see Scheduling policies: fifo_scheduler, lifo_scheduler, prio_scheduler + */ + template < + typename Task, + + template class SchedulingPolicy, + template class SizePolicy, + template class SizePolicyController, + template class ShutdownPolicy + > + class pool_core + : public enable_shared_from_this< pool_core > + , private noncopyable + { + + public: // Type definitions + typedef Task task_type; //!< Indicates the task's type. + typedef SchedulingPolicy scheduler_type; //!< Indicates the scheduler's type. + typedef pool_core pool_type; //!< Indicates the thread pool's type. + typedef SizePolicy size_policy_type; //!< Indicates the sizer's type. + //typedef typename size_policy_type::size_controller size_controller_type; + + typedef SizePolicyController size_controller_type; + +// typedef SizePolicy::size_controller size_controller_type; + typedef ShutdownPolicy shutdown_policy_type;//!< Indicates the shutdown policy's type. + + typedef worker_thread worker_type; + + // The task is required to be a nullary function. + BOOST_STATIC_ASSERT(function_traits::arity == 0); + + // The task function's result type is required to be void. + BOOST_STATIC_ASSERT(is_void::type >::value); + + + private: // Friends + friend class worker_thread; + +#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x580) // Tested with CC: Sun C++ 5.8 Patch 121018-08 2006/12/06 + friend class SizePolicy; + friend class ShutdownPolicy; +#else + friend class SizePolicy; + friend class ShutdownPolicy; +#endif + + private: // The following members may be accessed by _multiple_ threads at the same time: + volatile size_t m_worker_count; + volatile size_t m_target_worker_count; + volatile size_t m_active_worker_count; + + + + private: // The following members are accessed only by _one_ thread at the same time: + scheduler_type m_scheduler; + scoped_ptr m_size_policy; // is never null + + bool m_terminate_all_workers; // Indicates if termination of all workers was triggered. + std::vector > m_terminated_workers; // List of workers which are terminated but not fully destructed. + + private: // The following members are implemented thread-safe: + mutable recursive_mutex m_monitor; + mutable condition m_worker_idle_or_terminated_event; // A worker is idle or was terminated. + mutable condition m_task_or_terminate_workers_event; // Task is available OR total worker count should be reduced. + + public: + /// Constructor. + pool_core() + : m_worker_count(0) + , m_target_worker_count(0) + , m_active_worker_count(0) + , m_terminate_all_workers(false) + { + pool_type volatile & self_ref = *this; + m_size_policy.reset(new size_policy_type(self_ref)); + + m_scheduler.clear(); + } + + + /// Destructor. + ~pool_core() + { + } + + /*! Gets the size controller which manages the number of threads in the pool. + * \return The size controller. + * \see SizePolicy + */ + size_controller_type size_controller() + { + return size_controller_type(*m_size_policy, this->shared_from_this()); + } + + /*! Gets the number of threads in the pool. + * \return The number of threads. + */ + size_t size() const volatile + { + return m_worker_count; + } + +// TODO is only called once + void shutdown() + { + ShutdownPolicy::shutdown(*this); + } + + /*! Schedules a task for asynchronous execution. The task will be executed once only. + * \param task The task function object. It should not throw execeptions. + * \return true, if the task could be scheduled and false otherwise. + */ + bool schedule(task_type const & task) volatile + { + locking_ptr lockedThis(*this, m_monitor); + + if(lockedThis->m_scheduler.push(task)) + { + lockedThis->m_task_or_terminate_workers_event.notify_one(); + return true; + } + else + { + return false; + } + } + + + /*! Returns the number of tasks which are currently executed. + * \return The number of active tasks. + */ + size_t active() const volatile + { + return m_active_worker_count; + } + + + /*! Returns the number of tasks which are ready for execution. + * \return The number of pending tasks. + */ + size_t pending() const volatile + { + locking_ptr lockedThis(*this, m_monitor); + return lockedThis->m_scheduler.size(); + } + + + /*! Removes all pending tasks from the pool's scheduler. + */ + void clear() volatile + { + locking_ptr lockedThis(*this, m_monitor); + lockedThis->m_scheduler.clear(); + } + + + /*! Indicates that there are no tasks pending. + * \return true if there are no tasks ready for execution. + * \remarks This function is more efficient that the check 'pending() == 0'. + */ + bool empty() const volatile + { + locking_ptr lockedThis(*this, m_monitor); + return lockedThis->m_scheduler.empty(); + } + + + /*! The current thread of execution is blocked until the sum of all active + * and pending tasks is equal or less than a given threshold. + * \param task_threshold The maximum number of tasks in pool and scheduler. + */ + void wait(size_t const task_threshold = 0) const volatile + { + const pool_type* self = const_cast(this); + recursive_mutex::scoped_lock lock(self->m_monitor); + + if(0 == task_threshold) + { + while(0 != self->m_active_worker_count || !self->m_scheduler.empty()) + { + self->m_worker_idle_or_terminated_event.wait(lock); + } + } + else + { + while(task_threshold < self->m_active_worker_count + self->m_scheduler.size()) + { + self->m_worker_idle_or_terminated_event.wait(lock); + } + } + } + + /*! The current thread of execution is blocked until the timestamp is met + * or the sum of all active and pending tasks is equal or less + * than a given threshold. + * \param timestamp The time when function returns at the latest. + * \param task_threshold The maximum number of tasks in pool and scheduler. + * \return true if the task sum is equal or less than the threshold, false otherwise. + */ + bool wait(xtime const & timestamp, size_t const task_threshold = 0) const volatile + { + const pool_type* self = const_cast(this); + recursive_mutex::scoped_lock lock(self->m_monitor); + + if(0 == task_threshold) + { + while(0 != self->m_active_worker_count || !self->m_scheduler.empty()) + { + if(!self->m_worker_idle_or_terminated_event.timed_wait(lock, timestamp)) return false; + } + } + else + { + while(task_threshold < self->m_active_worker_count + self->m_scheduler.size()) + { + if(!self->m_worker_idle_or_terminated_event.timed_wait(lock, timestamp)) return false; + } + } + + return true; + } + + + private: + + + void terminate_all_workers(bool const wait) volatile + { + pool_type* self = const_cast(this); + recursive_mutex::scoped_lock lock(self->m_monitor); + + self->m_terminate_all_workers = true; + + m_target_worker_count = 0; + self->m_task_or_terminate_workers_event.notify_all(); + + if(wait) + { + while(m_worker_count > 0) + { + self->m_worker_idle_or_terminated_event.wait(lock); + } + + for(typename std::vector >::iterator it = self->m_terminated_workers.begin(); + it != self->m_terminated_workers.end(); + ++it) + { + (*it)->join(); + } + self->m_terminated_workers.clear(); + } + } + + + /*! Changes the number of worker threads in the pool. The resizing + * is handled by the SizePolicy. + * \param threads The new number of worker threads. + * \return true, if pool will be resized and false if not. + */ + bool resize(size_t const worker_count) volatile + { + locking_ptr lockedThis(*this, m_monitor); + + if(!m_terminate_all_workers) + { + m_target_worker_count = worker_count; + } + else + { + return false; + } + + + if(m_worker_count <= m_target_worker_count) + { // increase worker count + while(m_worker_count < m_target_worker_count) + { + try + { + worker_thread::create_and_attach(lockedThis->shared_from_this()); + m_worker_count++; + m_active_worker_count++; + } + catch(thread_resource_error) + { + return false; + } + } + } + else + { // decrease worker count + lockedThis->m_task_or_terminate_workers_event.notify_all(); // TODO: Optimize number of notified workers + } + + return true; + } + + + // worker died with unhandled exception + void worker_died_unexpectedly(shared_ptr worker) volatile + { + locking_ptr lockedThis(*this, m_monitor); + + m_worker_count--; + m_active_worker_count--; + lockedThis->m_worker_idle_or_terminated_event.notify_all(); + + if(m_terminate_all_workers) + { + lockedThis->m_terminated_workers.push_back(worker); + } + else + { + lockedThis->m_size_policy->worker_died_unexpectedly(m_worker_count); + } + } + + void worker_destructed(shared_ptr worker) volatile + { + locking_ptr lockedThis(*this, m_monitor); + m_worker_count--; + m_active_worker_count--; + lockedThis->m_worker_idle_or_terminated_event.notify_all(); + + if(m_terminate_all_workers) + { + lockedThis->m_terminated_workers.push_back(worker); + } + } + + + bool execute_task() volatile + { + function0 task; + + { // fetch task + pool_type* lockedThis = const_cast(this); + recursive_mutex::scoped_lock lock(lockedThis->m_monitor); + + // decrease number of threads if necessary + if(m_worker_count > m_target_worker_count) + { + return false; // terminate worker + } + + + // wait for tasks + while(lockedThis->m_scheduler.empty()) + { + // decrease number of workers if necessary + if(m_worker_count > m_target_worker_count) + { + return false; // terminate worker + } + else + { + m_active_worker_count--; + lockedThis->m_worker_idle_or_terminated_event.notify_all(); + lockedThis->m_task_or_terminate_workers_event.wait(lock); + m_active_worker_count++; + } + } + + task = lockedThis->m_scheduler.top(); + lockedThis->m_scheduler.pop(); + } + + // call task function + if(task) + { + task(); + } + + //guard->disable(); + return true; + } + }; + + + + +} } } // namespace boost::threadpool::detail + +#endif // THREADPOOL_POOL_CORE_HPP_INCLUDED diff --git a/contrib/libboost-threadpool/threadpool/detail/scope_guard.hpp b/contrib/libboost-threadpool/threadpool/detail/scope_guard.hpp new file mode 100644 index 00000000000..68634654ba5 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/detail/scope_guard.hpp @@ -0,0 +1,65 @@ +/*! \file +* \brief TODO. +* +* TODO. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED +#define THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED + + + +#include + + +namespace boost { namespace threadpool { namespace detail +{ + +// TODO documentation +class scope_guard +: private boost::noncopyable +{ + function0 const m_function; + bool m_is_active; + +public: + scope_guard(function0 const & call_on_exit) + : m_function(call_on_exit) + , m_is_active(true) + { + } + + ~scope_guard() + { + if(m_is_active && m_function) + { + m_function(); + } + } + + void disable() + { + m_is_active = false; + } +}; + + + + + + +} } } // namespace boost::threadpool::detail + +#endif // THREADPOOL_DETAIL_SCOPE_GUARD_HPP_INCLUDED + + diff --git a/contrib/libboost-threadpool/threadpool/detail/worker_thread.hpp b/contrib/libboost-threadpool/threadpool/detail/worker_thread.hpp new file mode 100644 index 00000000000..d33467a5279 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/detail/worker_thread.hpp @@ -0,0 +1,115 @@ +/*! \file +* \brief Thread pool worker. +* +* The worker thread instance is attached to a pool +* and executes tasks of this pool. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + +#ifndef THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED +#define THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED + + +#include "scope_guard.hpp" + +#include +#include +#include +#include +#include + + +namespace boost { namespace threadpool { namespace detail +{ + + /*! \brief Thread pool worker. + * + * A worker_thread represents a thread of execution. The worker is attached to a + * thread pool and processes tasks of that pool. The lifetime of the worker and its + * internal boost::thread is managed automatically. + * + * This class is a helper class and cannot be constructed or accessed directly. + * + * \see pool_core + */ + template + class worker_thread + : public enable_shared_from_this< worker_thread > + , private noncopyable + { + public: + typedef Pool pool_type; //!< Indicates the pool's type. + + private: + shared_ptr m_pool; //!< Pointer to the pool which created the worker. + shared_ptr m_thread; //!< Pointer to the thread which executes the run loop. + + + /*! Constructs a new worker. + * \param pool Pointer to it's parent pool. + * \see function create_and_attach + */ + worker_thread(shared_ptr const & pool) + : m_pool(pool) + { + assert(pool); + } + + + /*! Notifies that an exception occurred in the run loop. + */ + void died_unexpectedly() + { + m_pool->worker_died_unexpectedly(this->shared_from_this()); + } + + + public: + /*! Executes pool's tasks sequentially. + */ + void run() + { + scope_guard notify_exception(bind(&worker_thread::died_unexpectedly, this)); + + while(m_pool->execute_task()) {} + + notify_exception.disable(); + m_pool->worker_destructed(this->shared_from_this()); + } + + + /*! Joins the worker's thread. + */ + void join() + { + m_thread->join(); + } + + + /*! Constructs a new worker thread and attaches it to the pool. + * \param pool Pointer to the pool. + */ + static void create_and_attach(shared_ptr const & pool) + { + shared_ptr worker(new worker_thread(pool)); + if(worker) + { + worker->m_thread.reset(new boost::thread(bind(&worker_thread::run, worker))); + } + } + + }; + + +} } } // namespace boost::threadpool::detail + +#endif // THREADPOOL_DETAIL_WORKER_THREAD_HPP_INCLUDED + diff --git a/contrib/libboost-threadpool/threadpool/future.hpp b/contrib/libboost-threadpool/threadpool/future.hpp new file mode 100644 index 00000000000..57568edefd9 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/future.hpp @@ -0,0 +1,144 @@ +/*! \file +* \brief TODO. +* +* TODO. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + +#ifndef THREADPOOL_FUTURE_HPP_INCLUDED +#define THREADPOOL_FUTURE_HPP_INCLUDED + + + +#include "detail/future.hpp" +#include + +//#include "pool.hpp" +//#include + +//#include + + +namespace boost { namespace threadpool +{ + + /*! \brief Experimental. Do not use in production code. TODO. + * + * TODO Future + * + * \see TODO + * + */ + + +template +class future +{ +private: + shared_ptr > m_impl; + +public: + typedef Result const & result_type; //!< Indicates the functor's result type. + typedef Result future_result_type; //!< Indicates the future's result type. + + +public: + + future() + : m_impl(new detail::future_impl()) // TODO remove this + { + } + + // only for internal usage + future(shared_ptr > const & impl) + : m_impl(impl) + { + } + + bool ready() const + { + return m_impl->ready(); + } + + void wait() const + { + m_impl->wait(); + } + + bool timed_wait(boost::xtime const & timestamp) const + { + return m_impl->timed_wait(timestamp); + } + + result_type operator()() // throw( thread::cancelation_exception, ... ) + { + return (*m_impl)(); + } + + result_type get() // throw( thread::cancelation_exception, ... ) + { + return (*m_impl)(); + } + + bool cancel() + { + return m_impl->cancel(); + } + + bool is_cancelled() const + { + return m_impl->is_cancelled(); + } +}; + + + + + +template +typename disable_if < + is_void< typename result_of< Function() >::type >, + future< typename result_of< Function() >::type > +>::type +schedule(Pool& pool, const Function& task) +{ + typedef typename result_of< Function() >::type future_result_type; + + // create future impl and future + shared_ptr > impl(new detail::future_impl); + future res(impl); + + // schedule future impl + pool.schedule(detail::future_impl_task_func(task, impl)); + + // return future + return res; + +/* + TODO + if(pool->schedule(bind(&Future::run, future))) + { + return future; + } + else + { + // construct empty future + return error_future; + } + */ +} + + + +} } // namespace boost::threadpool + +#endif // THREADPOOL_FUTURE_HPP_INCLUDED + diff --git a/contrib/libboost-threadpool/threadpool/pool.hpp b/contrib/libboost-threadpool/threadpool/pool.hpp new file mode 100644 index 00000000000..87ec33021b0 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/pool.hpp @@ -0,0 +1,232 @@ +/*! \file +* \brief Thread pool core. +* +* This file contains the threadpool's core class: pool. +* +* Thread pools are a mechanism for asynchronous and parallel processing +* within the same process. The pool class provides a convenient way +* for dispatching asynchronous tasks as functions objects. The scheduling +* of these tasks can be easily controlled by using customized schedulers. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_POOL_HPP_INCLUDED +#define THREADPOOL_POOL_HPP_INCLUDED + +#include + +#include "detail/pool_core.hpp" + +#include "task_adaptors.hpp" + +#include "detail/locking_ptr.hpp" + +#include "scheduling_policies.hpp" +#include "size_policies.hpp" +#include "shutdown_policies.hpp" + + + +/// The namespace threadpool contains a thread pool and related utility classes. +namespace boost { namespace threadpool +{ + + + + /*! \brief Thread pool. + * + * Thread pools are a mechanism for asynchronous and parallel processing + * within the same process. The pool class provides a convenient way + * for dispatching asynchronous tasks as functions objects. The scheduling + * of these tasks can be easily controlled by using customized schedulers. + * A task must not throw an exception. + * + * A pool is DefaultConstructible, CopyConstructible and Assignable. + * It has reference semantics; all copies of the same pool are equivalent and interchangeable. + * All operations on a pool except assignment are strongly thread safe or sequentially consistent; + * that is, the behavior of concurrent calls is as if the calls have been issued sequentially in an unspecified order. + * + * \param Task A function object which implements the operator 'void operator() (void) const'. The operator () is called by the pool to execute the task. Exceptions are ignored. + * \param SchedulingPolicy A task container which determines how tasks are scheduled. It is guaranteed that this container is accessed only by one thread at a time. The scheduler shall not throw exceptions. + * + * \remarks The pool class is thread-safe. + * + * \see Tasks: task_func, prio_task_func + * \see Scheduling policies: fifo_scheduler, lifo_scheduler, prio_scheduler + */ + template < + typename Task = task_func, + template class SchedulingPolicy = fifo_scheduler, + template class SizePolicy = static_size, + template class SizePolicyController = resize_controller, + template class ShutdownPolicy = wait_for_all_tasks + > + class thread_pool + { + typedef detail::pool_core pool_core_type; + shared_ptr m_core; // pimpl idiom + shared_ptr m_shutdown_controller; // If the last pool holding a pointer to the core is deleted the controller shuts the pool down. + + public: // Type definitions + typedef Task task_type; //!< Indicates the task's type. + typedef SchedulingPolicy scheduler_type; //!< Indicates the scheduler's type. + /* typedef thread_pool pool_type; //!< Indicates the thread pool's type. + */ + typedef SizePolicy size_policy_type; + typedef SizePolicyController size_controller_type; + + + public: + /*! Constructor. + * \param initial_threads The pool is immediately resized to set the specified number of threads. The pool's actual number threads depends on the SizePolicy. + */ + thread_pool(size_t initial_threads = 0) + : m_core(new pool_core_type) + , m_shutdown_controller(static_cast(0), bind(&pool_core_type::shutdown, m_core)) + { + size_policy_type::init(*m_core, initial_threads); + } + + + /*! Gets the size controller which manages the number of threads in the pool. + * \return The size controller. + * \see SizePolicy + */ + size_controller_type size_controller() + { + return m_core->size_controller(); + } + + + /*! Gets the number of threads in the pool. + * \return The number of threads. + */ + size_t size() const + { + return m_core->size(); + } + + + /*! Schedules a task for asynchronous execution. The task will be executed once only. + * \param task The task function object. It should not throw execeptions. + * \return true, if the task could be scheduled and false otherwise. + */ + bool schedule(task_type const & task) + { + return m_core->schedule(task); + } + + + /*! Returns the number of tasks which are currently executed. + * \return The number of active tasks. + */ + size_t active() const + { + return m_core->active(); + } + + + /*! Returns the number of tasks which are ready for execution. + * \return The number of pending tasks. + */ + size_t pending() const + { + return m_core->pending(); + } + + + /*! Removes all pending tasks from the pool's scheduler. + */ + void clear() + { + m_core->clear(); + } + + + /*! Indicates that there are no tasks pending. + * \return true if there are no tasks ready for execution. + * \remarks This function is more efficient that the check 'pending() == 0'. + */ + bool empty() const + { + return m_core->empty(); + } + + + /*! The current thread of execution is blocked until the sum of all active + * and pending tasks is equal or less than a given threshold. + * \param task_threshold The maximum number of tasks in pool and scheduler. + */ + void wait(size_t task_threshold = 0) const + { + m_core->wait(task_threshold); + } + + + /*! The current thread of execution is blocked until the timestamp is met + * or the sum of all active and pending tasks is equal or less + * than a given threshold. + * \param timestamp The time when function returns at the latest. + * \param task_threshold The maximum number of tasks in pool and scheduler. + * \return true if the task sum is equal or less than the threshold, false otherwise. + */ + bool wait(xtime const & timestamp, size_t task_threshold = 0) const + { + return m_core->wait(timestamp, task_threshold); + } + }; + + + + /*! \brief Fifo pool. + * + * The pool's tasks are fifo scheduled task_func functors. + * + */ + typedef thread_pool fifo_pool; + + + /*! \brief Lifo pool. + * + * The pool's tasks are lifo scheduled task_func functors. + * + */ + typedef thread_pool lifo_pool; + + + /*! \brief Pool for prioritized task. + * + * The pool's tasks are prioritized prio_task_func functors. + * + */ + typedef thread_pool prio_pool; + + + /*! \brief A standard pool. + * + * The pool's tasks are fifo scheduled task_func functors. + * + */ + typedef fifo_pool pool; + + + +} } // namespace boost::threadpool + +#endif // THREADPOOL_POOL_HPP_INCLUDED diff --git a/contrib/libboost-threadpool/threadpool/pool_adaptors.hpp b/contrib/libboost-threadpool/threadpool/pool_adaptors.hpp new file mode 100644 index 00000000000..6cde152d2ae --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/pool_adaptors.hpp @@ -0,0 +1,70 @@ +/*! \file +* \brief Pool adaptors. +* +* This file contains an easy-to-use adaptor similar to a smart +* pointer for the pool class. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED +#define THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED + +#include + + +namespace boost { namespace threadpool +{ + + +// TODO convenience scheduling function + /*! Schedules a Runnable for asynchronous execution. A Runnable is an arbitrary class with a run() + * member function. This a convenience shorthand for pool->schedule(bind(&Runnable::run, task_object)). + * \param + * \param obj The Runnable object. The member function run() will be exectued and should not throw execeptions. + * \return true, if the task could be scheduled and false otherwise. + */ + template + bool schedule(Pool& pool, shared_ptr const & obj) + { + return pool->schedule(bind(&Runnable::run, obj)); + } + + /*! Schedules a task for asynchronous execution. The task will be executed once only. + * \param task The task function object. + */ + template + typename enable_if < + is_void< typename result_of< typename Pool::task_type() >::type >, + bool + >::type + schedule(Pool& pool, typename Pool::task_type const & task) + { + return pool.schedule(task); + } + + + template + typename enable_if < + is_void< typename result_of< typename Pool::task_type() >::type >, + bool + >::type + schedule(shared_ptr const pool, typename Pool::task_type const & task) + { + return pool->schedule(task); + } + + +} } // namespace boost::threadpool + +#endif // THREADPOOL_POOL_ADAPTORS_HPP_INCLUDED + + diff --git a/contrib/libboost-threadpool/threadpool/scheduling_policies.hpp b/contrib/libboost-threadpool/threadpool/scheduling_policies.hpp new file mode 100644 index 00000000000..2d6c7c1baed --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/scheduling_policies.hpp @@ -0,0 +1,262 @@ +/*! \file +* \brief Task scheduling policies. +* +* This file contains some fundamental scheduling policies for the pool class. +* A scheduling policy is realized by a task container which controls the access to +* the tasks. Fundamentally the container determines the order the tasks are processed +* by the thread pool. +* The task containers need not to be thread-safe because they are used by the pool +* in thread-safe way. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED +#define THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED + + +#include +#include + +#include "task_adaptors.hpp" + +namespace boost { namespace threadpool +{ + + /*! \brief SchedulingPolicy which implements FIFO ordering. + * + * This container implements a FIFO scheduling policy. + * The first task to be added to the scheduler will be the first to be removed. + * The processing proceeds sequentially in the same order. + * FIFO stands for "first in, first out". + * + * \param Task A function object which implements the operator()(void). + * + */ + template + class fifo_scheduler + { + public: + typedef Task task_type; //!< Indicates the scheduler's task type. + + protected: + std::deque m_container; //!< Internal task container. + + + public: + /*! Adds a new task to the scheduler. + * \param task The task object. + * \return true, if the task could be scheduled and false otherwise. + */ + bool push(task_type const & task) + { + m_container.push_back(task); + return true; + } + + /*! Removes the task which should be executed next. + */ + void pop() + { + m_container.pop_front(); + } + + /*! Gets the task which should be executed next. + * \return The task object to be executed. + */ + task_type const & top() const + { + return m_container.front(); + } + + /*! Gets the current number of tasks in the scheduler. + * \return The number of tasks. + * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. + */ + size_t size() const + { + return m_container.size(); + } + + /*! Checks if the scheduler is empty. + * \return true if the scheduler contains no tasks, false otherwise. + * \remarks Is more efficient than size() == 0. + */ + bool empty() const + { + return m_container.empty(); + } + + /*! Removes all tasks from the scheduler. + */ + void clear() + { + m_container.clear(); + } + }; + + + + /*! \brief SchedulingPolicy which implements LIFO ordering. + * + * This container implements a LIFO scheduling policy. + * The last task to be added to the scheduler will be the first to be removed. + * LIFO stands for "last in, first out". + * + * \param Task A function object which implements the operator()(void). + * + */ + template + class lifo_scheduler + { + public: + typedef Task task_type; //!< Indicates the scheduler's task type. + + protected: + std::deque m_container; //!< Internal task container. + + public: + /*! Adds a new task to the scheduler. + * \param task The task object. + * \return true, if the task could be scheduled and false otherwise. + */ + bool push(task_type const & task) + { + m_container.push_front(task); + return true; + } + + /*! Removes the task which should be executed next. + */ + void pop() + { + m_container.pop_front(); + } + + /*! Gets the task which should be executed next. + * \return The task object to be executed. + */ + task_type const & top() const + { + return m_container.front(); + } + + /*! Gets the current number of tasks in the scheduler. + * \return The number of tasks. + * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. + */ + size_t size() const + { + return m_container.size(); + } + + /*! Checks if the scheduler is empty. + * \return true if the scheduler contains no tasks, false otherwise. + * \remarks Is more efficient than size() == 0. + */ + bool empty() const + { + return m_container.empty(); + } + + /*! Removes all tasks from the scheduler. + */ + void clear() + { + m_container.clear(); + } + + }; + + + + /*! \brief SchedulingPolicy which implements prioritized ordering. + * + * This container implements a scheduling policy based on task priorities. + * The task with highest priority will be the first to be removed. + * It must be possible to compare two tasks using operator<. + * + * \param Task A function object which implements the operator() and operator<. operator< must be a partial ordering. + * + * \see prio_thread_func + * + */ + template + class prio_scheduler + { + public: + typedef Task task_type; //!< Indicates the scheduler's task type. + + protected: + std::priority_queue m_container; //!< Internal task container. + + + public: + /*! Adds a new task to the scheduler. + * \param task The task object. + * \return true, if the task could be scheduled and false otherwise. + */ + bool push(task_type const & task) + { + m_container.push(task); + return true; + } + + /*! Removes the task which should be executed next. + */ + void pop() + { + m_container.pop(); + } + + /*! Gets the task which should be executed next. + * \return The task object to be executed. + */ + task_type const & top() const + { + return m_container.top(); + } + + /*! Gets the current number of tasks in the scheduler. + * \return The number of tasks. + * \remarks Prefer empty() to size() == 0 to check if the scheduler is empty. + */ + size_t size() const + { + return m_container.size(); + } + + /*! Checks if the scheduler is empty. + * \return true if the scheduler contains no tasks, false otherwise. + * \remarks Is more efficient than size() == 0. + */ + bool empty() const + { + return m_container.empty(); + } + + /*! Removes all tasks from the scheduler. + */ + void clear() + { + while(!m_container.empty()) + { + m_container.pop(); + } + } + }; + + +} } // namespace boost::threadpool + + +#endif // THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED + diff --git a/contrib/libboost-threadpool/threadpool/shutdown_policies.hpp b/contrib/libboost-threadpool/threadpool/shutdown_policies.hpp new file mode 100644 index 00000000000..047a6eb516d --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/shutdown_policies.hpp @@ -0,0 +1,83 @@ +/*! \file +* \brief Shutdown policies. +* +* This file contains shutdown policies for thread_pool. +* A shutdown policy controls the pool's behavior from the time +* when the pool is not referenced any longer. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_SHUTDOWN_POLICIES_HPP_INCLUDED +#define THREADPOOL_SHUTDOWN_POLICIES_HPP_INCLUDED + + + +/// The namespace threadpool contains a thread pool and related utility classes. +namespace boost { namespace threadpool +{ + + +/*! \brief ShutdownPolicy which waits for the completion of all tasks + * and the worker termination afterwards. + * + * \param Pool The pool's core type. + */ + template + class wait_for_all_tasks + { + public: + static void shutdown(Pool& pool) + { + pool.wait(); + pool.terminate_all_workers(true); + } + }; + + + /*! \brief ShutdownPolicy which waits for the completion of all active tasks + * and the worker termination afterwards. + * + * \param Pool The pool's core type. + */ + template + class wait_for_active_tasks + { + public: + static void shutdown(Pool& pool) + { + pool.clear(); + pool.wait(); + pool.terminate_all_workers(true); + } + }; + + + /*! \brief ShutdownPolicy which does not wait for any tasks or worker termination. + * + * This policy does not wait for any tasks. Nevertheless all active tasks will be processed completely. + * + * \param Pool The pool's core type. + */ + template + class immediately + { + public: + static void shutdown(Pool& pool) + { + pool.clear(); + pool.terminate_all_workers(false); + } + }; + +} } // namespace boost::threadpool + +#endif // THREADPOOL_SHUTDOWN_POLICIES_HPP_INCLUDED diff --git a/contrib/libboost-threadpool/threadpool/size_policies.hpp b/contrib/libboost-threadpool/threadpool/size_policies.hpp new file mode 100644 index 00000000000..e3c08038db5 --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/size_policies.hpp @@ -0,0 +1,99 @@ +/*! \file +* \brief Size policies. +* +* This file contains size policies for thread_pool. A size +* policy controls the number of worker threads in the pool. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_SIZE_POLICIES_HPP_INCLUDED +#define THREADPOOL_SIZE_POLICIES_HPP_INCLUDED + + + +/// The namespace threadpool contains a thread pool and related utility classes. +namespace boost { namespace threadpool +{ + + /*! \brief SizePolicyController which provides no functionality. + * + * \param Pool The pool's core type. + */ + template + struct empty_controller + { + empty_controller(typename Pool::size_policy_type&, shared_ptr) {} + }; + + + /*! \brief SizePolicyController which allows resizing. + * + * \param Pool The pool's core type. + */ + template< typename Pool > + class resize_controller + { + typedef typename Pool::size_policy_type size_policy_type; + reference_wrapper m_policy; + shared_ptr m_pool; //!< to make sure that the pool is alive (the policy pointer is valid) as long as the controller exists + + public: + resize_controller(size_policy_type& policy, shared_ptr pool) + : m_policy(policy) + , m_pool(pool) + { + } + + bool resize(size_t worker_count) + { + return m_policy.get().resize(worker_count); + } + }; + + + /*! \brief SizePolicy which preserves the thread count. + * + * \param Pool The pool's core type. + */ + template + class static_size + { + reference_wrapper m_pool; + + public: + static void init(Pool& pool, size_t const worker_count) + { + pool.resize(worker_count); + } + + static_size(Pool volatile & pool) + : m_pool(pool) + {} + + bool resize(size_t const worker_count) + { + return m_pool.get().resize(worker_count); + } + + void worker_died_unexpectedly(size_t const new_worker_count) + { + m_pool.get().resize(new_worker_count + 1); + } + + // TODO this functions are not called yet + void task_scheduled() {} + void task_finished() {} + }; + +} } // namespace boost::threadpool + +#endif // THREADPOOL_SIZE_POLICIES_HPP_INCLUDED diff --git a/contrib/libboost-threadpool/threadpool/task_adaptors.hpp b/contrib/libboost-threadpool/threadpool/task_adaptors.hpp new file mode 100644 index 00000000000..7e2dcca51ff --- /dev/null +++ b/contrib/libboost-threadpool/threadpool/task_adaptors.hpp @@ -0,0 +1,176 @@ +/*! \file +* \brief Task adaptors. +* +* This file contains adaptors for task function objects. +* +* Copyright (c) 2005-2007 Philipp Henkel +* +* Use, modification, and distribution are subject to the +* Boost Software License, Version 1.0. (See accompanying file +* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +* +* http://threadpool.sourceforge.net +* +*/ + + +#ifndef THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED +#define THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED + +#include + +#if BOOST_VERSION >= 105000 + #ifndef TIME_UTC + #define TIME_UTC TIME_UTC_ + #endif +#endif + + +#include +#include +#include + + +namespace boost { namespace threadpool +{ + + /*! \brief Standard task function object. + * + * This function object wraps a nullary function which returns void. + * The wrapped function is invoked by calling the operator (). + * + * \see boost function library + * + */ + typedef function0 task_func; + + + + + /*! \brief Prioritized task function object. + * + * This function object wraps a task_func object and binds a priority to it. + * prio_task_funcs can be compared using the operator < which realises a partial ordering. + * The wrapped task function is invoked by calling the operator (). + * + * \see prio_scheduler + * + */ + class prio_task_func + { + private: + unsigned int m_priority; //!< The priority of the task's function. + task_func m_function; //!< The task's function. + + public: + typedef void result_type; //!< Indicates the functor's result type. + + public: + /*! Constructor. + * \param priority The priority of the task. + * \param function The task's function object. + */ + prio_task_func(unsigned int const priority, task_func const & function) + : m_priority(priority) + , m_function(function) + { + } + + /*! Executes the task function. + */ + void operator() (void) const + { + if(m_function) + { + m_function(); + } + } + + /*! Comparison operator which realises a partial ordering based on priorities. + * \param rhs The object to compare with. + * \return true if the priority of *this is less than right hand side's priority, false otherwise. + */ + bool operator< (const prio_task_func& rhs) const + { + return m_priority < rhs.m_priority; + } + + }; // prio_task_func + + + + + + + + + /*! \brief Looped task function object. + * + * This function object wraps a boolean thread function object. + * The wrapped task function is invoked by calling the operator () and it is executed in regular + * time intervals until false is returned. The interval length may be zero. + * Please note that a pool's thread is engaged as long as the task is looped. + * + */ + class looped_task_func + { + private: + function0 m_function; //!< The task's function. + unsigned int m_break_s; //!< Duration of breaks in seconds. + unsigned int m_break_ns; //!< Duration of breaks in nano seconds. + + public: + typedef void result_type; //!< Indicates the functor's result type. + + public: + /*! Constructor. + * \param function The task's function object which is looped until false is returned. + * \param interval The minimum break time in milli seconds before the first execution of the task function and between the following ones. + */ + looped_task_func(function0 const & function, unsigned int const interval = 0) + : m_function(function) + { + m_break_s = interval / 1000; + m_break_ns = (interval - m_break_s * 1000) * 1000 * 1000; + } + + /*! Executes the task function. + */ + void operator() (void) const + { + if(m_function) + { + if(m_break_s > 0 || m_break_ns > 0) + { // Sleep some time before first execution + xtime xt; + xtime_get(&xt, TIME_UTC); + xt.nsec += m_break_ns; + xt.sec += m_break_s; + thread::sleep(xt); + } + + while(m_function()) + { + if(m_break_s > 0 || m_break_ns > 0) + { + xtime xt; + xtime_get(&xt, TIME_UTC); + xt.nsec += m_break_ns; + xt.sec += m_break_s; + thread::sleep(xt); + } + else + { + thread::yield(); // Be fair to other threads + } + } + } + } + + }; // looped_task_func + + +} } // namespace boost::threadpool + +#endif // THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED + diff --git a/contrib/libcityhash/CMakeLists.txt b/contrib/libcityhash/CMakeLists.txt new file mode 100644 index 00000000000..bb11c44207c --- /dev/null +++ b/contrib/libcityhash/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library(cityhash + city.cc + + citycrc.h + city.h + config.h) diff --git a/contrib/libcityhash/COPYING b/contrib/libcityhash/COPYING new file mode 100644 index 00000000000..bf15194dd5a --- /dev/null +++ b/contrib/libcityhash/COPYING @@ -0,0 +1,19 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. diff --git a/contrib/libcityhash/city.cc b/contrib/libcityhash/city.cc new file mode 100644 index 00000000000..d2d0f67ca21 --- /dev/null +++ b/contrib/libcityhash/city.cc @@ -0,0 +1,470 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// CityHash, by Geoff Pike and Jyrki Alakuijala +// +// This file provides CityHash64() and related functions. +// +// It's probably possible to create even faster hash functions by +// writing a program that systematically explores some of the space of +// possible hash functions, by using SIMD instructions, or by +// compromising on hash quality. + +#include "config.h" +#include + +#include +#include // for memcpy and memset + +using namespace std; + +static uint64 UNALIGNED_LOAD64(const char *p) { + uint64 result; + memcpy(&result, p, sizeof(result)); + return result; +} + +static uint32 UNALIGNED_LOAD32(const char *p) { + uint32 result; + memcpy(&result, p, sizeof(result)); + return result; +} + +#if !defined(WORDS_BIGENDIAN) + +#define uint32_in_expected_order(x) (x) +#define uint64_in_expected_order(x) (x) + +#else + +#ifdef _MSC_VER +#include +#define bswap_32(x) _byteswap_ulong(x) +#define bswap_64(x) _byteswap_uint64(x) + +#elif defined(__APPLE__) +// Mac OS X / Darwin features +#include +#define bswap_32(x) OSSwapInt32(x) +#define bswap_64(x) OSSwapInt64(x) + +#else +#include +#endif + +#define uint32_in_expected_order(x) (bswap_32(x)) +#define uint64_in_expected_order(x) (bswap_64(x)) + +#endif // WORDS_BIGENDIAN + +#if !defined(LIKELY) +#if HAVE_BUILTIN_EXPECT +#define LIKELY(x) (__builtin_expect(!!(x), 1)) +#else +#define LIKELY(x) (x) +#endif +#endif + +static uint64 Fetch64(const char *p) { + return uint64_in_expected_order(UNALIGNED_LOAD64(p)); +} + +static uint32 Fetch32(const char *p) { + return uint32_in_expected_order(UNALIGNED_LOAD32(p)); +} + +// Some primes between 2^63 and 2^64 for various uses. +static const uint64 k0 = 0xc3a5c85c97cb3127ULL; +static const uint64 k1 = 0xb492b66fbe98f273ULL; +static const uint64 k2 = 0x9ae16a3b2f90404fULL; +static const uint64 k3 = 0xc949d7c7509e6557ULL; + +// Bitwise right rotate. Normally this will compile to a single +// instruction, especially if the shift is a manifest constant. +static uint64 Rotate(uint64 val, int shift) { + // Avoid shifting by 64: doing so yields an undefined result. + return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); +} + +// Equivalent to Rotate(), but requires the second arg to be non-zero. +// On x86-64, and probably others, it's possible for this to compile +// to a single instruction if both args are already in registers. +static uint64 RotateByAtLeast1(uint64 val, int shift) { + return (val >> shift) | (val << (64 - shift)); +} + +static uint64 ShiftMix(uint64 val) { + return val ^ (val >> 47); +} + +static uint64 HashLen16(uint64 u, uint64 v) { + return Hash128to64(uint128(u, v)); +} + +static uint64 HashLen0to16(const char *s, size_t len) { + if (len > 8) { + uint64 a = Fetch64(s); + uint64 b = Fetch64(s + len - 8); + return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b; + } + if (len >= 4) { + uint64 a = Fetch32(s); + return HashLen16(len + (a << 3), Fetch32(s + len - 4)); + } + if (len > 0) { + uint8 a = s[0]; + uint8 b = s[len >> 1]; + uint8 c = s[len - 1]; + uint32 y = static_cast(a) + (static_cast(b) << 8); + uint32 z = len + (static_cast(c) << 2); + return ShiftMix(y * k2 ^ z * k3) * k2; + } + return k2; +} + +// This probably works well for 16-byte strings as well, but it may be overkill +// in that case. +static uint64 HashLen17to32(const char *s, size_t len) { + uint64 a = Fetch64(s) * k1; + uint64 b = Fetch64(s + 8); + uint64 c = Fetch64(s + len - 8) * k2; + uint64 d = Fetch64(s + len - 16) * k0; + return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d, + a + Rotate(b ^ k3, 20) - c + len); +} + +// Return a 16-byte hash for 48 bytes. Quick and dirty. +// Callers do best to use "random-looking" values for a and b. +static pair WeakHashLen32WithSeeds( + uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) { + a += w; + b = Rotate(b + a + z, 21); + uint64 c = a; + a += x; + a += y; + b += Rotate(a, 44); + return make_pair(a + z, b + c); +} + +// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. +static pair WeakHashLen32WithSeeds( + const char* s, uint64 a, uint64 b) { + return WeakHashLen32WithSeeds(Fetch64(s), + Fetch64(s + 8), + Fetch64(s + 16), + Fetch64(s + 24), + a, + b); +} + +// Return an 8-byte hash for 33 to 64 bytes. +static uint64 HashLen33to64(const char *s, size_t len) { + uint64 z = Fetch64(s + 24); + uint64 a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0; + uint64 b = Rotate(a + z, 52); + uint64 c = Rotate(a, 37); + a += Fetch64(s + 8); + c += Rotate(a, 7); + a += Fetch64(s + 16); + uint64 vf = a + z; + uint64 vs = b + Rotate(a, 31) + c; + a = Fetch64(s + 16) + Fetch64(s + len - 32); + z = Fetch64(s + len - 8); + b = Rotate(a + z, 52); + c = Rotate(a, 37); + a += Fetch64(s + len - 24); + c += Rotate(a, 7); + a += Fetch64(s + len - 16); + uint64 wf = a + z; + uint64 ws = b + Rotate(a, 31) + c; + uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0); + return ShiftMix(r * k0 + vs) * k2; +} + +uint64 CityHash64(const char *s, size_t len) { + if (len <= 32) { + if (len <= 16) { + return HashLen0to16(s, len); + } else { + return HashLen17to32(s, len); + } + } else if (len <= 64) { + return HashLen33to64(s, len); + } + + // For strings over 64 bytes we hash the end first, and then as we + // loop we keep 56 bytes of state: v, w, x, y, and z. + uint64 x = Fetch64(s); + uint64 y = Fetch64(s + len - 16) ^ k1; + uint64 z = Fetch64(s + len - 56) ^ k0; + pair v = WeakHashLen32WithSeeds(s + len - 64, len, y); + pair w = WeakHashLen32WithSeeds(s + len - 32, len * k1, k0); + z += ShiftMix(v.second) * k1; + x = Rotate(z + x, 39) * k1; + y = Rotate(y, 33) * k1; + + // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. + len = (len - 1) & ~static_cast(63); + do { + x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y ^= v.first; + z = Rotate(z ^ w.first, 33); + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); + std::swap(z, x); + s += 64; + len -= 64; + } while (len != 0); + return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z, + HashLen16(v.second, w.second) + x); +} + +uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) { + return CityHash64WithSeeds(s, len, k2, seed); +} + +uint64 CityHash64WithSeeds(const char *s, size_t len, + uint64 seed0, uint64 seed1) { + return HashLen16(CityHash64(s, len) - seed0, seed1); +} + +// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings +// of any length representable in ssize_t. Based on City and Murmur. +static uint128 CityMurmur(const char *s, size_t len, uint128 seed) { + uint64 a = Uint128Low64(seed); + uint64 b = Uint128High64(seed); + uint64 c = 0; + uint64 d = 0; + ssize_t l = len - 16; + if (l <= 0) { // len <= 16 + a = ShiftMix(a * k1) * k1; + c = b * k1 + HashLen0to16(s, len); + d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c)); + } else { // len > 16 + c = HashLen16(Fetch64(s + len - 8) + k1, a); + d = HashLen16(b + len, c + Fetch64(s + len - 16)); + a += d; + do { + a ^= ShiftMix(Fetch64(s) * k1) * k1; + a *= k1; + b ^= a; + c ^= ShiftMix(Fetch64(s + 8) * k1) * k1; + c *= k1; + d ^= c; + s += 16; + l -= 16; + } while (l > 0); + } + a = HashLen16(a, c); + b = HashLen16(d, b); + return uint128(a ^ b, HashLen16(b, a)); +} + +uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) { + if (len < 128) { + return CityMurmur(s, len, seed); + } + + // We expect len >= 128 to be the common case. Keep 56 bytes of state: + // v, w, x, y, and z. + pair v, w; + uint64 x = Uint128Low64(seed); + uint64 y = Uint128High64(seed); + uint64 z = len * k1; + v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); + v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); + w.first = Rotate(y + z, 35) * k1 + x; + w.second = Rotate(x + Fetch64(s + 88), 53) * k1; + + // This is the same inner loop as CityHash64(), manually unrolled. + do { + x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y ^= v.first; + z = Rotate(z ^ w.first, 33); + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); + std::swap(z, x); + s += 64; + x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1; + y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; + x ^= w.second; + y ^= v.first; + z = Rotate(z ^ w.first, 33); + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y); + std::swap(z, x); + s += 64; + len -= 128; + } while (LIKELY(len >= 128)); + y += Rotate(w.first, 37) * k0 + z; + x += Rotate(v.first + z, 49) * k0; + // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. + for (size_t tail_done = 0; tail_done < len; ) { + tail_done += 32; + y = Rotate(y - x, 42) * k0 + v.second; + w.first += Fetch64(s + len - tail_done + 16); + x = Rotate(x, 49) * k0 + w.first; + w.first += v.first; + v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second); + } + // At this point our 48 bytes of state should contain more than + // enough information for a strong 128-bit hash. We use two + // different 48-byte-to-8-byte hashes to get a 16-byte final result. + x = HashLen16(x, v.first); + y = HashLen16(y, w.first); + return uint128(HashLen16(x + v.second, w.second) + y, + HashLen16(x + w.second, y + v.second)); +} + +uint128 CityHash128(const char *s, size_t len) { + if (len >= 16) { + return CityHash128WithSeed(s + 16, + len - 16, + uint128(Fetch64(s) ^ k3, + Fetch64(s + 8))); + } else if (len >= 8) { + return CityHash128WithSeed(NULL, + 0, + uint128(Fetch64(s) ^ (len * k0), + Fetch64(s + len - 8) ^ k1)); + } else { + return CityHash128WithSeed(s, len, uint128(k0, k1)); + } +} + +#ifdef __SSE4_2__ +#include +#include + +// Requires len >= 240. +static void CityHashCrc256Long(const char *s, size_t len, + uint32 seed, uint64 *result) { + uint64 a = Fetch64(s + 56) + k0; + uint64 b = Fetch64(s + 96) + k0; + uint64 c = result[1] = HashLen16(b, len); + uint64 d = result[2] = Fetch64(s + 120) * k0 + len; + uint64 e = Fetch64(s + 184) + seed; + uint64 f = seed; + uint64 g = 0; + uint64 h = 0; + uint64 i = 0; + uint64 j = 0; + uint64 t = c + d; + + // 240 bytes of input per iter. + size_t iters = len / 240; + len -= iters * 240; + do { +#define CHUNK(multiplier, z) \ + { \ + uint64 old_a = a; \ + a = Rotate(b, 41 ^ z) * multiplier + Fetch64(s); \ + b = Rotate(c, 27 ^ z) * multiplier + Fetch64(s + 8); \ + c = Rotate(d, 41 ^ z) * multiplier + Fetch64(s + 16); \ + d = Rotate(e, 33 ^ z) * multiplier + Fetch64(s + 24); \ + e = Rotate(t, 25 ^ z) * multiplier + Fetch64(s + 32); \ + t = old_a; \ + } \ + f = _mm_crc32_u64(f, a); \ + g = _mm_crc32_u64(g, b); \ + h = _mm_crc32_u64(h, c); \ + i = _mm_crc32_u64(i, d); \ + j = _mm_crc32_u64(j, e); \ + s += 40 + + CHUNK(1, 1); CHUNK(k0, 0); + CHUNK(1, 1); CHUNK(k0, 0); + CHUNK(1, 1); CHUNK(k0, 0); + } while (--iters > 0); + j += i << 32; + a = HashLen16(a, j); + h += g << 32; + b = b * k0 + h; + c = HashLen16(c, f) + i; + d = HashLen16(d, e); + pair v(j + e, HashLen16(h, t)); + h = v.second + f; + // If 0 < len < 240, hash chunks of 32 bytes each from the end of s. + for (size_t tail_done = 0; tail_done < len; ) { + tail_done += 32; + c = Rotate(c - a, 42) * k0 + v.second; + d += Fetch64(s + len - tail_done + 16); + a = Rotate(a, 49) * k0 + d; + d += v.first; + v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second); + } + + // Final mix. + e = HashLen16(a, d) + v.first; + f = HashLen16(b, c) + a; + g = HashLen16(v.first, v.second) + c; + result[0] = e + f + g + h; + a = ShiftMix((a + g) * k0) * k0 + b; + result[1] += a + result[0]; + a = ShiftMix(a * k0) * k0 + c; + result[2] += a + result[1]; + a = ShiftMix((a + e) * k0) * k0; + result[3] = a + result[2]; +} + +// Requires len < 240. +static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) { + char buf[240]; + memcpy(buf, s, len); + memset(buf + len, 0, 240 - len); + CityHashCrc256Long(buf, 240, ~static_cast(len), result); +} + +void CityHashCrc256(const char *s, size_t len, uint64 *result) { + if (LIKELY(len >= 240)) { + CityHashCrc256Long(s, len, 0, result); + } else { + CityHashCrc256Short(s, len, result); + } +} + +uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) { + if (len <= 900) { + return CityHash128WithSeed(s, len, seed); + } else { + uint64 result[4]; + CityHashCrc256(s, len, result); + uint64 u = Uint128High64(seed) + result[0]; + uint64 v = Uint128Low64(seed) + result[1]; + return uint128(HashLen16(u, v + result[2]), + HashLen16(Rotate(v, 32), u * k0 + result[3])); + } +} + +uint128 CityHashCrc128(const char *s, size_t len) { + if (len <= 900) { + return CityHash128(s, len); + } else { + uint64 result[4]; + CityHashCrc256(s, len, result); + return uint128(result[2], result[3]); + } +} + +#endif diff --git a/contrib/libcityhash/city.h b/contrib/libcityhash/city.h new file mode 100644 index 00000000000..c2ab352cb56 --- /dev/null +++ b/contrib/libcityhash/city.h @@ -0,0 +1,90 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// CityHash, by Geoff Pike and Jyrki Alakuijala +// +// This file provides a few functions for hashing strings. On x86-64 +// hardware in 2011, CityHash64() is faster than other high-quality +// hash functions, such as Murmur. This is largely due to higher +// instruction-level parallelism. CityHash64() and CityHash128() also perform +// well on hash-quality tests. +// +// CityHash128() is optimized for relatively long strings and returns +// a 128-bit hash. For strings more than about 2000 bytes it can be +// faster than CityHash64(). +// +// Functions in the CityHash family are not suitable for cryptography. +// +// WARNING: This code has not been tested on big-endian platforms! +// It is known to work well on little-endian platforms that have a small penalty +// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. +// +// By the way, for some hash functions, given strings a and b, the hash +// of a+b is easily derived from the hashes of a and b. This property +// doesn't hold for any hash functions in this file. + +#ifndef CITY_HASH_H_ +#define CITY_HASH_H_ + +#include // for size_t. +#include +#include + +typedef uint8_t uint8; +typedef uint32_t uint32; +typedef uint64_t uint64; +typedef std::pair uint128; + +inline uint64 Uint128Low64(const uint128& x) { return x.first; } +inline uint64 Uint128High64(const uint128& x) { return x.second; } + +// Hash function for a byte array. +uint64 CityHash64(const char *buf, size_t len); + +// Hash function for a byte array. For convenience, a 64-bit seed is also +// hashed into the result. +uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed); + +// Hash function for a byte array. For convenience, two seeds are also +// hashed into the result. +uint64 CityHash64WithSeeds(const char *buf, size_t len, + uint64 seed0, uint64 seed1); + +// Hash function for a byte array. +uint128 CityHash128(const char *s, size_t len); + +// Hash function for a byte array. For convenience, a 128-bit seed is also +// hashed into the result. +uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed); + +// Hash 128 input bits down to 64 bits of output. +// This is intended to be a reasonably good hash function. +inline uint64 Hash128to64(const uint128& x) { + // Murmur-inspired hashing. + const uint64 kMul = 0x9ddfea08eb382d69ULL; + uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; + a ^= (a >> 47); + uint64 b = (Uint128High64(x) ^ a) * kMul; + b ^= (b >> 47); + b *= kMul; + return b; +} + +#endif // CITY_HASH_H_ diff --git a/contrib/libcityhash/citycrc.h b/contrib/libcityhash/citycrc.h new file mode 100644 index 00000000000..318e3917ca5 --- /dev/null +++ b/contrib/libcityhash/citycrc.h @@ -0,0 +1,43 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// CityHash, by Geoff Pike and Jyrki Alakuijala +// +// This file declares the subset of the CityHash functions that require +// _mm_crc32_u64(). See the CityHash README for details. +// +// Functions in the CityHash family are not suitable for cryptography. + +#ifndef CITY_HASH_CRC_H_ +#define CITY_HASH_CRC_H_ + +#include + +// Hash function for a byte array. +uint128 CityHashCrc128(const char *s, size_t len); + +// Hash function for a byte array. For convenience, a 128-bit seed is also +// hashed into the result. +uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed); + +// Hash function for a byte array. Sets result[0] ... result[3]. +void CityHashCrc256(const char *s, size_t len, uint64 *result); + +#endif // CITY_HASH_CRC_H_ diff --git a/contrib/libcityhash/config.h b/contrib/libcityhash/config.h new file mode 100644 index 00000000000..c873732dd87 --- /dev/null +++ b/contrib/libcityhash/config.h @@ -0,0 +1,114 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define if building universal (internal helper macro) */ +/* #undef AC_APPLE_UNIVERSAL_BUILD */ + +/* Define to 1 if the compiler supports __builtin_expect. */ +#define HAVE_BUILTIN_EXPECT 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#define LT_OBJDIR ".libs/" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "cityhash-discuss@googlegroups.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "CityHash" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "CityHash 1.0.2" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "cityhash" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "1.0.2" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +/* # undef WORDS_BIGENDIAN */ +# endif +#endif + +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT32_T */ + +/* Define for Solaris 2.5.1 so the uint64_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT64_T */ + +/* Define for Solaris 2.5.1 so the uint8_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT8_T */ + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +/* #undef inline */ +#endif + +/* Define to `unsigned int' if does not define. */ +/* #undef size_t */ + +/* Define to `int' if does not define. */ +/* #undef ssize_t */ + +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint32_t */ + +/* Define to the type of an unsigned integer type of width exactly 64 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint64_t */ + +/* Define to the type of an unsigned integer type of width exactly 8 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint8_t */ diff --git a/contrib/libcpuid/CMakeLists.txt b/contrib/libcpuid/CMakeLists.txt new file mode 100644 index 00000000000..dc1b4713aff --- /dev/null +++ b/contrib/libcpuid/CMakeLists.txt @@ -0,0 +1,18 @@ +add_library(cpuid +include/cpuid/asm-bits.c +include/cpuid/cpuid_main.c +include/cpuid/libcpuid_util.c +include/cpuid/rdtsc.c +include/cpuid/recog_amd.c +include/cpuid/recog_intel.c + +include/cpuid/asm-bits.h +include/cpuid/config.h +include/cpuid/libcpuid_constants.h +include/cpuid/libcpuid.h +include/cpuid/libcpuid_types.h +include/cpuid/libcpuid_util.h +include/cpuid/rdtsc.h +include/cpuid/recog_amd.h +include/cpuid/recog_intel.h +) diff --git a/contrib/libcpuid/COPYING b/contrib/libcpuid/COPYING new file mode 100644 index 00000000000..bf4755c203f --- /dev/null +++ b/contrib/libcpuid/COPYING @@ -0,0 +1,23 @@ +Copyright 2008 Veselin Georgiev, +anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/contrib/libcpuid/README.txt b/contrib/libcpuid/README.txt new file mode 100644 index 00000000000..a6817683f8b --- /dev/null +++ b/contrib/libcpuid/README.txt @@ -0,0 +1 @@ +https://github.com/anrieff/libcpuid.git diff --git a/contrib/libcpuid/include/cpuid/asm-bits.c b/contrib/libcpuid/include/cpuid/asm-bits.c new file mode 100644 index 00000000000..ca936f2abf5 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/asm-bits.c @@ -0,0 +1,823 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "libcpuid.h" +#include "asm-bits.h" + +int cpuid_exists_by_eflags(void) +{ +#if defined(PLATFORM_X64) + return 1; /* CPUID is always present on the x86_64 */ +#elif defined(PLATFORM_X86) +# if defined(COMPILER_GCC) + int result; + __asm __volatile( + " pushfl\n" + " pop %%eax\n" + " mov %%eax, %%ecx\n" + " xor $0x200000, %%eax\n" + " push %%eax\n" + " popfl\n" + " pushfl\n" + " pop %%eax\n" + " xor %%ecx, %%eax\n" + " mov %%eax, %0\n" + " push %%ecx\n" + " popfl\n" + : "=m"(result) + : :"eax", "ecx", "memory"); + return (result != 0); +# elif defined(COMPILER_MICROSOFT) + int result; + __asm { + pushfd + pop eax + mov ecx, eax + xor eax, 0x200000 + push eax + popfd + pushfd + pop eax + xor eax, ecx + mov result, eax + push ecx + popfd + }; + return (result != 0); +# else + return 0; +# endif /* COMPILER_MICROSOFT */ +#else + return 0; +#endif /* PLATFORM_X86 */ +} + +/* + * with MSVC/AMD64, the exec_cpuid() and cpu_rdtsc() functions + * are implemented in separate .asm files. Otherwise, use inline assembly + */ +void exec_cpuid(uint32_t *regs) +{ +#ifdef INLINE_ASM_SUPPORTED +#ifdef COMPILER_GCC +# ifdef PLATFORM_X64 + __asm __volatile( + " mov %0, %%rdi\n" + + " push %%rbx\n" + " push %%rcx\n" + " push %%rdx\n" + + " mov (%%rdi), %%eax\n" + " mov 4(%%rdi), %%ebx\n" + " mov 8(%%rdi), %%ecx\n" + " mov 12(%%rdi), %%edx\n" + + " cpuid\n" + + " movl %%eax, (%%rdi)\n" + " movl %%ebx, 4(%%rdi)\n" + " movl %%ecx, 8(%%rdi)\n" + " movl %%edx, 12(%%rdi)\n" + " pop %%rdx\n" + " pop %%rcx\n" + " pop %%rbx\n" + : + :"m"(regs) + :"memory", "eax", "rdi" + ); +# else + __asm __volatile( + " mov %0, %%edi\n" + + " push %%ebx\n" + " push %%ecx\n" + " push %%edx\n" + + " mov (%%edi), %%eax\n" + " mov 4(%%edi), %%ebx\n" + " mov 8(%%edi), %%ecx\n" + " mov 12(%%edi), %%edx\n" + + " cpuid\n" + + " mov %%eax, (%%edi)\n" + " mov %%ebx, 4(%%edi)\n" + " mov %%ecx, 8(%%edi)\n" + " mov %%edx, 12(%%edi)\n" + " pop %%edx\n" + " pop %%ecx\n" + " pop %%ebx\n" + : + :"m"(regs) + :"memory", "eax", "edi" + ); +# endif /* COMPILER_GCC */ +#else +# ifdef COMPILER_MICROSOFT + __asm { + push ebx + push ecx + push edx + push edi + mov edi, regs + + mov eax, [edi] + mov ebx, [edi+4] + mov ecx, [edi+8] + mov edx, [edi+12] + + cpuid + + mov [edi], eax + mov [edi+4], ebx + mov [edi+8], ecx + mov [edi+12], edx + + pop edi + pop edx + pop ecx + pop ebx + } +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif +#endif /* INLINE_ASSEMBLY_SUPPORTED */ +} + +#ifdef INLINE_ASM_SUPPORTED +void cpu_rdtsc(uint64_t* result) +{ + uint32_t low_part, hi_part; +#ifdef COMPILER_GCC + __asm __volatile ( + " rdtsc\n" + " mov %%eax, %0\n" + " mov %%edx, %1\n" + :"=m"(low_part), "=m"(hi_part)::"memory", "eax", "edx" + ); +#else +# ifdef COMPILER_MICROSOFT + __asm { + rdtsc + mov low_part, eax + mov hi_part, edx + }; +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif /* COMPILER_GCC */ + *result = (uint64_t)low_part + (((uint64_t) hi_part) << 32); +} +#endif /* INLINE_ASM_SUPPORTED */ + +#ifdef INLINE_ASM_SUPPORTED +void busy_sse_loop(int cycles) +{ +#ifdef COMPILER_GCC +#ifndef __APPLE__ +# define XALIGN ".balign 16\n" +#else +# define XALIGN ".align 4\n" +#endif + __asm __volatile ( + " xorps %%xmm0, %%xmm0\n" + " xorps %%xmm1, %%xmm1\n" + " xorps %%xmm2, %%xmm2\n" + " xorps %%xmm3, %%xmm3\n" + " xorps %%xmm4, %%xmm4\n" + " xorps %%xmm5, %%xmm5\n" + " xorps %%xmm6, %%xmm6\n" + " xorps %%xmm7, %%xmm7\n" + XALIGN + ".bsLoop:\n" + // 0: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 1: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 2: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 3: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 4: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 5: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 6: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 7: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 8: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + // 9: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //10: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //11: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //12: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //13: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //14: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //15: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //16: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //17: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //18: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //19: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //20: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //21: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //22: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //23: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //24: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //25: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //26: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //27: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //28: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //29: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //30: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + //31: + " addps %%xmm1, %%xmm0\n" + " addps %%xmm2, %%xmm1\n" + " addps %%xmm3, %%xmm2\n" + " addps %%xmm4, %%xmm3\n" + " addps %%xmm5, %%xmm4\n" + " addps %%xmm6, %%xmm5\n" + " addps %%xmm7, %%xmm6\n" + " addps %%xmm0, %%xmm7\n" + + " dec %%eax\n" + " jnz .bsLoop\n" + ::"a"(cycles) + ); +#else +# ifdef COMPILER_MICROSOFT + __asm { + mov eax, cycles + xorps xmm0, xmm0 + xorps xmm1, xmm1 + xorps xmm2, xmm2 + xorps xmm3, xmm3 + xorps xmm4, xmm4 + xorps xmm5, xmm5 + xorps xmm6, xmm6 + xorps xmm7, xmm7 + //-- + align 16 +bsLoop: + // 0: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 1: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 2: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 3: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 4: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 5: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 6: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 7: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 8: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 9: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 10: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 11: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 12: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 13: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 14: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 15: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 16: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 17: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 18: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 19: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 20: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 21: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 22: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 23: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 24: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 25: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 26: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 27: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 28: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 29: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 30: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + // 31: + addps xmm0, xmm1 + addps xmm1, xmm2 + addps xmm2, xmm3 + addps xmm3, xmm4 + addps xmm4, xmm5 + addps xmm5, xmm6 + addps xmm6, xmm7 + addps xmm7, xmm0 + //---------------------- + dec eax + jnz bsLoop + } +# else +# error "Unsupported compiler" +# endif /* COMPILER_MICROSOFT */ +#endif /* COMPILER_GCC */ +} +#endif /* INLINE_ASSEMBLY_SUPPORTED */ diff --git a/contrib/libcpuid/include/cpuid/asm-bits.h b/contrib/libcpuid/include/cpuid/asm-bits.h new file mode 100644 index 00000000000..3a03e11ce8c --- /dev/null +++ b/contrib/libcpuid/include/cpuid/asm-bits.h @@ -0,0 +1,53 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __ASM_BITS_H__ +#define __ASM_BITS_H__ +#include "libcpuid.h" + +/* Determine Compiler: */ +#if defined(_MSC_VER) +# define COMPILER_MICROSOFT +#elif defined(__GNUC__) +# define COMPILER_GCC +#endif + +/* Determine Platform */ +#if defined(__x86_64__) || defined(_M_AMD64) +# define PLATFORM_X64 +#elif defined(__i386__) || defined(_M_IX86) +# define PLATFORM_X86 +#endif + +/* Under Windows/AMD64 with MSVC, inline assembly isn't supported */ +#if (defined(COMPILER_GCC) && defined(PLATFORM_X64)) || defined(PLATFORM_X86) +# define INLINE_ASM_SUPPORTED +#endif + +int cpuid_exists_by_eflags(void); +void exec_cpuid(uint32_t *regs); +void busy_sse_loop(int cycles); + +#endif /* __ASM_BITS_H__ */ diff --git a/contrib/libcpuid/include/cpuid/config.h b/contrib/libcpuid/include/cpuid/config.h new file mode 100644 index 00000000000..29b513c4ac1 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/config.h @@ -0,0 +1,2 @@ +/* Version number of package */ +#define VERSION "0.2.1" diff --git a/contrib/libcpuid/include/cpuid/cpuid_main.c b/contrib/libcpuid/include/cpuid/cpuid_main.c new file mode 100644 index 00000000000..96e07f6938e --- /dev/null +++ b/contrib/libcpuid/include/cpuid/cpuid_main.c @@ -0,0 +1,674 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "libcpuid.h" +#include "recog_intel.h" +#include "recog_amd.h" +#include "asm-bits.h" +#include "libcpuid_util.h" +#include "config.h" +#include +#include +#include + +/* Implementation: */ + +static int _libcpiud_errno = ERR_OK; + +int set_error(cpu_error_t err) +{ + _libcpiud_errno = (int) err; + return (int) err; +} + +static void raw_data_t_constructor(struct cpu_raw_data_t* raw) +{ + memset(raw, 0, sizeof(struct cpu_raw_data_t)); +} + +static void cpu_id_t_constructor(struct cpu_id_t* id) +{ + memset(id, 0, sizeof(struct cpu_id_t)); + id->l1_data_cache = id->l1_instruction_cache = id->l2_cache = id->l3_cache = -1; + id->l1_assoc = id->l2_assoc = id->l3_assoc = -1; + id->l1_cacheline = id->l2_cacheline = id->l3_cacheline = -1; + id->sse_size = -1; +} + +static int parse_token(const char* expected_token, const char *token, + const char *value, uint32_t array[][4], int limit, int *recognized) +{ + char format[32]; + int veax, vebx, vecx, vedx; + int index; + + if (*recognized) return 1; /* already recognized */ + if (strncmp(token, expected_token, strlen(expected_token))) return 1; /* not what we search for */ + sprintf(format, "%s[%%d]", expected_token); + *recognized = 1; + if (1 == sscanf(token, format, &index) && index >=0 && index < limit) { + if (4 == sscanf(value, "%x%x%x%x", &veax, &vebx, &vecx, &vedx)) { + array[index][0] = veax; + array[index][1] = vebx; + array[index][2] = vecx; + array[index][3] = vedx; + return 1; + } + } + return 0; +} + +/* get_total_cpus() system specific code: uses OS routines to determine total number of CPUs */ +#ifdef __APPLE__ +#include +#include +#include +#include +static int get_total_cpus(void) +{ + kern_return_t kr; + host_basic_info_data_t basic_info; + host_info_t info = (host_info_t)&basic_info; + host_flavor_t flavor = HOST_BASIC_INFO; + mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; + kr = host_info(mach_host_self(), flavor, info, &count); + if (kr != KERN_SUCCESS) return 1; + return basic_info.avail_cpus; +} +#define GET_TOTAL_CPUS_DEFINED +#endif + +#ifdef _WIN32 +#include +static int get_total_cpus(void) +{ + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + return system_info.dwNumberOfProcessors; +} +#define GET_TOTAL_CPUS_DEFINED +#endif + +#if defined linux || defined __linux__ || defined __sun +#include +#include + +static int get_total_cpus(void) +{ + return sysconf(_SC_NPROCESSORS_ONLN); +} +#define GET_TOTAL_CPUS_DEFINED +#endif + +#if defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __bsdi__ || defined __QNX__ +#include + +static int get_total_cpus(void) +{ + int mib[2] = { CTL_HW, HW_NCPU }; + int ncpus; + size_t len = sizeof(ncpus); + if (sysctl(mib, 2, &ncpus, &len, (void *) 0, 0) != 0) return 1; + return ncpus; +} +#define GET_TOTAL_CPUS_DEFINED +#endif + +#ifndef GET_TOTAL_CPUS_DEFINED +static int get_total_cpus(void) +{ + static int warning_printed = 0; + if (!warning_printed) { + warning_printed = 1; + warnf("Your system is not supported by libcpuid -- don't know how to detect the\n"); + warnf("total number of CPUs on your system. It will be reported as 1.\n"); + printf("Please use cpu_id_t.logical_cpus field instead.\n"); + } + return 1; +} +#endif /* GET_TOTAL_CPUS_DEFINED */ + + +static void load_features_common(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + const struct feature_map_t matchtable_edx1[] = { + { 0, CPU_FEATURE_FPU }, + { 1, CPU_FEATURE_VME }, + { 2, CPU_FEATURE_DE }, + { 3, CPU_FEATURE_PSE }, + { 4, CPU_FEATURE_TSC }, + { 5, CPU_FEATURE_MSR }, + { 6, CPU_FEATURE_PAE }, + { 7, CPU_FEATURE_MCE }, + { 8, CPU_FEATURE_CX8 }, + { 9, CPU_FEATURE_APIC }, + { 11, CPU_FEATURE_SEP }, + { 12, CPU_FEATURE_MTRR }, + { 13, CPU_FEATURE_PGE }, + { 14, CPU_FEATURE_MCA }, + { 15, CPU_FEATURE_CMOV }, + { 16, CPU_FEATURE_PAT }, + { 17, CPU_FEATURE_PSE36 }, + { 19, CPU_FEATURE_CLFLUSH }, + { 23, CPU_FEATURE_MMX }, + { 24, CPU_FEATURE_FXSR }, + { 25, CPU_FEATURE_SSE }, + { 26, CPU_FEATURE_SSE2 }, + { 28, CPU_FEATURE_HT }, + }; + const struct feature_map_t matchtable_ecx1[] = { + { 0, CPU_FEATURE_PNI }, + { 3, CPU_FEATURE_MONITOR }, + { 9, CPU_FEATURE_SSSE3 }, + { 12, CPU_FEATURE_FMA3 }, + { 13, CPU_FEATURE_CX16 }, + { 19, CPU_FEATURE_SSE4_1 }, + { 21, CPU_FEATURE_X2APIC }, + { 23, CPU_FEATURE_POPCNT }, + { 29, CPU_FEATURE_F16C }, + }; + const struct feature_map_t matchtable_edx81[] = { + { 11, CPU_FEATURE_SYSCALL }, + { 27, CPU_FEATURE_RDTSCP }, + { 29, CPU_FEATURE_LM }, + }; + const struct feature_map_t matchtable_ecx81[] = { + { 0, CPU_FEATURE_LAHF_LM }, + }; + const struct feature_map_t matchtable_edx87[] = { + { 8, CPU_FEATURE_CONSTANT_TSC }, + }; + if (raw->basic_cpuid[0][0] >= 1) { + match_features(matchtable_edx1, COUNT_OF(matchtable_edx1), raw->basic_cpuid[1][3], data); + match_features(matchtable_ecx1, COUNT_OF(matchtable_ecx1), raw->basic_cpuid[1][2], data); + } + if (raw->ext_cpuid[0][0] >= 0x80000001) { + match_features(matchtable_edx81, COUNT_OF(matchtable_edx81), raw->ext_cpuid[1][3], data); + match_features(matchtable_ecx81, COUNT_OF(matchtable_ecx81), raw->ext_cpuid[1][2], data); + } + if (raw->ext_cpuid[0][0] >= 0x80000007) { + match_features(matchtable_edx87, COUNT_OF(matchtable_edx87), raw->ext_cpuid[7][3], data); + } + if (data->flags[CPU_FEATURE_SSE]) { + /* apply guesswork to check if the SSE unit width is 128 bit */ + switch (data->vendor) { + case VENDOR_AMD: + data->sse_size = (data->ext_family >= 16 && data->ext_family != 17) ? 128 : 64; + break; + case VENDOR_INTEL: + data->sse_size = (data->family == 6 && data->ext_model >= 15) ? 128 : 64; + break; + default: + break; + } + /* leave the CPU_FEATURE_128BIT_SSE_AUTH 0; the advanced per-vendor detection routines + * will set it accordingly if they detect the needed bit */ + } +} + +static int cpuid_basic_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + int i, j, basic, xmodel, xfamily, ext; + char brandstr[64] = {0}; + const struct { cpu_vendor_t vendor; char match[16]; } + matchtable[NUM_CPU_VENDORS] = { + /* source: http://www.sandpile.org/ia32/cpuid.htm */ + { VENDOR_INTEL , "GenuineIntel" }, + { VENDOR_AMD , "AuthenticAMD" }, + { VENDOR_CYRIX , "CyrixInstead" }, + { VENDOR_NEXGEN , "NexGenDriven" }, + { VENDOR_TRANSMETA , "GenuineTMx86" }, + { VENDOR_UMC , "UMC UMC UMC " }, + { VENDOR_CENTAUR , "CentaurHauls" }, + { VENDOR_RISE , "RiseRiseRise" }, + { VENDOR_SIS , "SiS SiS SiS " }, + { VENDOR_NSC , "Geode by NSC" }, + }; + + memcpy(data->vendor_str + 0, &raw->basic_cpuid[0][1], 4); + memcpy(data->vendor_str + 4, &raw->basic_cpuid[0][3], 4); + memcpy(data->vendor_str + 8, &raw->basic_cpuid[0][2], 4); + data->vendor_str[12] = 0; + /* Determine vendor: */ + data->vendor = VENDOR_UNKNOWN; + for (i = 0; i < NUM_CPU_VENDORS; i++) + if (!strcmp(data->vendor_str, matchtable[i].match)) { + data->vendor = matchtable[i].vendor; + break; + } + if (data->vendor == VENDOR_UNKNOWN) + return set_error(ERR_CPU_UNKN); + basic = raw->basic_cpuid[0][0]; + if (basic >= 1) { + data->family = (raw->basic_cpuid[1][0] >> 8) & 0xf; + data->model = (raw->basic_cpuid[1][0] >> 4) & 0xf; + data->stepping = raw->basic_cpuid[1][0] & 0xf; + xmodel = (raw->basic_cpuid[1][0] >> 16) & 0xf; + xfamily = (raw->basic_cpuid[1][0] >> 20) & 0xff; + if (data->vendor == VENDOR_AMD && data->family < 0xf) + data->ext_family = data->family; + else + data->ext_family = data->family + xfamily; + data->ext_model = data->model + (xmodel << 4); + } + ext = raw->ext_cpuid[0][0] - 0x8000000; + + /* obtain the brand string, if present: */ + if (ext >= 4) { + for (i = 0; i < 3; i++) + for (j = 0; j < 4; j++) + memcpy(brandstr + i * 16 + j * 4, + &raw->ext_cpuid[2 + i][j], 4); + brandstr[48] = 0; + i = 0; + while (brandstr[i] == ' ') i++; + strncpy(data->brand_str, brandstr + i, sizeof(data->brand_str)); + data->brand_str[48] = 0; + } + load_features_common(raw, data); + data->total_logical_cpus = get_total_cpus(); + return set_error(ERR_OK); +} + +static void make_list_from_string(const char* csv, struct cpu_list_t* list) +{ + int i, n, l, last; + l = (int) strlen(csv); + n = 0; + for (i = 0; i < l; i++) if (csv[i] == ',') n++; + n++; + list->num_entries = n; + list->names = (char**) malloc(sizeof(char*) * n); + last = -1; + n = 0; + for (i = 0; i <= l; i++) if (i == l || csv[i] == ',') { + list->names[n] = (char*) malloc(i - last); + memcpy(list->names[n], &csv[last + 1], i - last - 1); + list->names[n][i - last - 1] = '\0'; + n++; + last = i; + } +} + + +/* Interface: */ + +int cpuid_get_total_cpus(void) +{ + return get_total_cpus(); +} + +int cpuid_present(void) +{ + return cpuid_exists_by_eflags(); +} + +void cpu_exec_cpuid(uint32_t eax, uint32_t* regs) +{ + regs[0] = eax; + regs[1] = regs[2] = regs[3] = 0; + exec_cpuid(regs); +} + +void cpu_exec_cpuid_ext(uint32_t* regs) +{ + exec_cpuid(regs); +} + +int cpuid_get_raw_data(struct cpu_raw_data_t* data) +{ + unsigned i; + if (!cpuid_present()) + return set_error(ERR_NO_CPUID); + for (i = 0; i < 32; i++) + cpu_exec_cpuid(i, data->basic_cpuid[i]); + for (i = 0; i < 32; i++) + cpu_exec_cpuid(0x80000000 + i, data->ext_cpuid[i]); + for (i = 0; i < 4; i++) { + memset(data->intel_fn4[i], 0, sizeof(data->intel_fn4[i])); + data->intel_fn4[i][0] = 4; + data->intel_fn4[i][2] = i; + cpu_exec_cpuid_ext(data->intel_fn4[i]); + } + for (i = 0; i < MAX_INTELFN11_LEVEL; i++) { + memset(data->intel_fn11[i], 0, sizeof(data->intel_fn11[i])); + data->intel_fn11[i][0] = 11; + data->intel_fn11[i][2] = i; + cpu_exec_cpuid_ext(data->intel_fn11[i]); + } + return set_error(ERR_OK); +} + +int cpuid_serialize_raw_data(struct cpu_raw_data_t* data, const char* filename) +{ + int i; + FILE *f; + + if (!strcmp(filename, "")) + f = stdout; + else + f = fopen(filename, "wt"); + if (!f) return set_error(ERR_OPEN); + + fprintf(f, "version=%s\n", VERSION); + for (i = 0; i < MAX_CPUID_LEVEL; i++) + fprintf(f, "basic_cpuid[%d]=%08x %08x %08x %08x\n", i, + data->basic_cpuid[i][0], data->basic_cpuid[i][1], + data->basic_cpuid[i][2], data->basic_cpuid[i][3]); + for (i = 0; i < MAX_EXT_CPUID_LEVEL; i++) + fprintf(f, "ext_cpuid[%d]=%08x %08x %08x %08x\n", i, + data->ext_cpuid[i][0], data->ext_cpuid[i][1], + data->ext_cpuid[i][2], data->ext_cpuid[i][3]); + for (i = 0; i < MAX_INTELFN4_LEVEL; i++) + fprintf(f, "intel_fn4[%d]=%08x %08x %08x %08x\n", i, + data->intel_fn4[i][0], data->intel_fn4[i][1], + data->intel_fn4[i][2], data->intel_fn4[i][3]); + for (i = 0; i < MAX_INTELFN11_LEVEL; i++) + fprintf(f, "intel_fn11[%d]=%08x %08x %08x %08x\n", i, + data->intel_fn11[i][0], data->intel_fn11[i][1], + data->intel_fn11[i][2], data->intel_fn11[i][3]); + + if (strcmp(filename, "")) + fclose(f); + return set_error(ERR_OK); +} + +int cpuid_deserialize_raw_data(struct cpu_raw_data_t* data, const char* filename) +{ + int i, len; + char line[100]; + char token[100]; + char *value; + int syntax; + int cur_line = 0; + int recognized; + FILE *f; + + raw_data_t_constructor(data); + + if (!strcmp(filename, "")) + f = stdin; + else + f = fopen(filename, "rt"); + if (!f) return set_error(ERR_OPEN); + while (fgets(line, sizeof(line), f)) { + ++cur_line; + len = (int) strlen(line); + if (len < 2) continue; + if (line[len - 1] == '\n') + line[--len] = '\0'; + for (i = 0; i < len && line[i] != '='; i++) + if (i >= len && i < 1 && len - i - 1 <= 0) { + fclose(f); + return set_error(ERR_BADFMT); + } + strncpy(token, line, i); + token[i] = '\0'; + value = &line[i + 1]; + /* try to recognize the line */ + recognized = 0; + if (!strcmp(token, "version") || !strcmp(token, "build_date")) { + recognized = 1; + } + syntax = 1; + syntax = syntax && parse_token("basic_cpuid", token, value, data->basic_cpuid, 32, &recognized); + syntax = syntax && parse_token("ext_cpuid", token, value, data->ext_cpuid, 32, &recognized); + syntax = syntax && parse_token("intel_fn4", token, value, data->intel_fn4, 4, &recognized); + syntax = syntax && parse_token("intel_fn11", token, value, data->intel_fn11, 4, &recognized); + if (!syntax) { + warnf("Error: %s:%d: Syntax error\n", filename, cur_line); + fclose(f); + return set_error(ERR_BADFMT); + } + if (!recognized) { + warnf("Warning: %s:%d not understood!\n", filename, cur_line); + } + } + + if (strcmp(filename, "")) + fclose(f); + return set_error(ERR_OK); +} + +int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + int r; + struct cpu_raw_data_t myraw; + if (!raw) { + if ((r = cpuid_get_raw_data(&myraw)) < 0) + return set_error(r); + raw = &myraw; + } + cpu_id_t_constructor(data); + if ((r = cpuid_basic_identify(raw, data)) < 0) + return set_error(r); + switch (data->vendor) { + case VENDOR_INTEL: + r = cpuid_identify_intel(raw, data); + break; + case VENDOR_AMD: + r = cpuid_identify_amd(raw, data); + break; + default: + break; + } + return set_error(r); +} + +const char* cpu_feature_str(cpu_feature_t feature) +{ + const struct { cpu_feature_t feature; const char* name; } + matchtable[] = { + { CPU_FEATURE_FPU, "fpu" }, + { CPU_FEATURE_VME, "vme" }, + { CPU_FEATURE_DE, "de" }, + { CPU_FEATURE_PSE, "pse" }, + { CPU_FEATURE_TSC, "tsc" }, + { CPU_FEATURE_MSR, "msr" }, + { CPU_FEATURE_PAE, "pae" }, + { CPU_FEATURE_MCE, "mce" }, + { CPU_FEATURE_CX8, "cx8" }, + { CPU_FEATURE_APIC, "apic" }, + { CPU_FEATURE_MTRR, "mtrr" }, + { CPU_FEATURE_SEP, "sep" }, + { CPU_FEATURE_PGE, "pge" }, + { CPU_FEATURE_MCA, "mca" }, + { CPU_FEATURE_CMOV, "cmov" }, + { CPU_FEATURE_PAT, "pat" }, + { CPU_FEATURE_PSE36, "pse36" }, + { CPU_FEATURE_PN, "pn" }, + { CPU_FEATURE_CLFLUSH, "clflush" }, + { CPU_FEATURE_DTS, "dts" }, + { CPU_FEATURE_ACPI, "acpi" }, + { CPU_FEATURE_MMX, "mmx" }, + { CPU_FEATURE_FXSR, "fxsr" }, + { CPU_FEATURE_SSE, "sse" }, + { CPU_FEATURE_SSE2, "sse2" }, + { CPU_FEATURE_SS, "ss" }, + { CPU_FEATURE_HT, "ht" }, + { CPU_FEATURE_TM, "tm" }, + { CPU_FEATURE_IA64, "ia64" }, + { CPU_FEATURE_PBE, "pbe" }, + { CPU_FEATURE_PNI, "pni" }, + { CPU_FEATURE_PCLMUL, "pclmul" }, + { CPU_FEATURE_DTS64, "dts64" }, + { CPU_FEATURE_MONITOR, "monitor" }, + { CPU_FEATURE_DS_CPL, "ds_cpl" }, + { CPU_FEATURE_VMX, "vmx" }, + { CPU_FEATURE_SMX, "smx" }, + { CPU_FEATURE_EST, "est" }, + { CPU_FEATURE_TM2, "tm2" }, + { CPU_FEATURE_SSSE3, "ssse3" }, + { CPU_FEATURE_CID, "cid" }, + { CPU_FEATURE_CX16, "cx16" }, + { CPU_FEATURE_XTPR, "xtpr" }, + { CPU_FEATURE_PDCM, "pdcm" }, + { CPU_FEATURE_DCA, "dca" }, + { CPU_FEATURE_SSE4_1, "sse4_1" }, + { CPU_FEATURE_SSE4_2, "sse4_2" }, + { CPU_FEATURE_SYSCALL, "syscall" }, + { CPU_FEATURE_XD, "xd" }, + { CPU_FEATURE_X2APIC, "x2apic"}, + { CPU_FEATURE_MOVBE, "movbe" }, + { CPU_FEATURE_POPCNT, "popcnt" }, + { CPU_FEATURE_AES, "aes" }, + { CPU_FEATURE_XSAVE, "xsave" }, + { CPU_FEATURE_OSXSAVE, "osxsave" }, + { CPU_FEATURE_AVX, "avx" }, + { CPU_FEATURE_MMXEXT, "mmxext" }, + { CPU_FEATURE_3DNOW, "3dnow" }, + { CPU_FEATURE_3DNOWEXT, "3dnowext" }, + { CPU_FEATURE_NX, "nx" }, + { CPU_FEATURE_FXSR_OPT, "fxsr_opt" }, + { CPU_FEATURE_RDTSCP, "rdtscp" }, + { CPU_FEATURE_LM, "lm" }, + { CPU_FEATURE_LAHF_LM, "lahf_lm" }, + { CPU_FEATURE_CMP_LEGACY, "cmp_legacy" }, + { CPU_FEATURE_SVM, "svm" }, + { CPU_FEATURE_SSE4A, "sse4a" }, + { CPU_FEATURE_MISALIGNSSE, "misalignsse" }, + { CPU_FEATURE_ABM, "abm" }, + { CPU_FEATURE_3DNOWPREFETCH, "3dnowprefetch" }, + { CPU_FEATURE_OSVW, "osvw" }, + { CPU_FEATURE_IBS, "ibs" }, + { CPU_FEATURE_SSE5, "sse5" }, + { CPU_FEATURE_SKINIT, "skinit" }, + { CPU_FEATURE_WDT, "wdt" }, + { CPU_FEATURE_TS, "ts" }, + { CPU_FEATURE_FID, "fid" }, + { CPU_FEATURE_VID, "vid" }, + { CPU_FEATURE_TTP, "ttp" }, + { CPU_FEATURE_TM_AMD, "tm_amd" }, + { CPU_FEATURE_STC, "stc" }, + { CPU_FEATURE_100MHZSTEPS, "100mhzsteps" }, + { CPU_FEATURE_HWPSTATE, "hwpstate" }, + { CPU_FEATURE_CONSTANT_TSC, "constant_tsc" }, + { CPU_FEATURE_XOP, "xop" }, + { CPU_FEATURE_FMA3, "fma3" }, + { CPU_FEATURE_FMA4, "fma4" }, + { CPU_FEATURE_TBM, "tbm" }, + { CPU_FEATURE_F16C, "f16c" }, + { CPU_FEATURE_RDRAND, "rdrand" }, + { CPU_FEATURE_CPB, "cpb" }, + { CPU_FEATURE_APERFMPERF, "aperfmperf" }, + { CPU_FEATURE_PFI, "pfi" }, + { CPU_FEATURE_PA, "pa" }, + { CPU_FEATURE_AVX2, "avx2" }, + }; + unsigned i, n = COUNT_OF(matchtable); + if (n != NUM_CPU_FEATURES) { + warnf("Warning: incomplete library, feature matchtable size differs from the actual number of features.\n"); + } + for (i = 0; i < n; i++) + if (matchtable[i].feature == feature) + return matchtable[i].name; + return ""; +} + +const char* cpuid_error(void) +{ + const struct { cpu_error_t error; const char *description; } + matchtable[] = { + { ERR_OK , "No error"}, + { ERR_NO_CPUID , "CPUID instruction is not supported"}, + { ERR_NO_RDTSC , "RDTSC instruction is not supported"}, + { ERR_NO_MEM , "Memory allocation failed"}, + { ERR_OPEN , "File open operation failed"}, + { ERR_BADFMT , "Bad file format"}, + { ERR_NOT_IMP , "Not implemented"}, + { ERR_CPU_UNKN , "Unsupported processor"}, + }; + unsigned i; + for (i = 0; i < COUNT_OF(matchtable); i++) + if (_libcpiud_errno == matchtable[i].error) + return matchtable[i].description; + return "Unknown error"; +} + + +const char* cpuid_lib_version(void) +{ + return VERSION; +} + +libcpuid_warn_fn_t cpuid_set_warn_function(libcpuid_warn_fn_t new_fn) +{ + libcpuid_warn_fn_t ret = _warn_fun; + _warn_fun = new_fn; + return ret; +} + +void cpuid_set_verbosiness_level(int level) +{ + _current_verboselevel = level; +} + +void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list) +{ + switch (vendor) { + case VENDOR_INTEL: + cpuid_get_list_intel(list); + break; + case VENDOR_AMD: + cpuid_get_list_amd(list); + break; + case VENDOR_CYRIX: + make_list_from_string("Cx486,Cx5x86,6x86,6x86MX,M II,MediaGX,MediaGXi,MediaGXm", list); + break; + case VENDOR_NEXGEN: + make_list_from_string("Nx586", list); + break; + case VENDOR_TRANSMETA: + make_list_from_string("Crusoe,Efficeon", list); + break; + case VENDOR_UMC: + make_list_from_string("UMC x86 CPU", list); + break; + case VENDOR_CENTAUR: + make_list_from_string("VIA C3,VIA C7,VIA Nano", list); + break; + case VENDOR_RISE: + make_list_from_string("Rise mP6", list); + break; + case VENDOR_SIS: + make_list_from_string("SiS mP6", list); + break; + case VENDOR_NSC: + make_list_from_string("Geode GXm,Geode GXLV,Geode GX1,Geode GX2", list); + break; + default: + warnf("Unknown vendor passed to cpuid_get_cpu_list()\n"); + break; + } +} + +void cpuid_free_cpu_list(struct cpu_list_t* list) +{ + int i; + if (list->num_entries <= 0) return; + for (i = 0; i < list->num_entries; i++) + free(list->names[i]); + free(list->names); +} diff --git a/contrib/libcpuid/include/cpuid/libcpuid.h b/contrib/libcpuid/include/cpuid/libcpuid.h new file mode 100644 index 00000000000..b78b0d6f514 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/libcpuid.h @@ -0,0 +1,885 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __LIBCPUID_H__ +#define __LIBCPUID_H__ +/** + * @File libcpuid.h + * @Author Veselin Georgiev + * @Date Oct 2008 + * @Version 0.2.1 + * + * Version history: + * + * 0.1.0 (2008-10-15): initial adaptation from wxfractgui sources + * 0.1.1 (2009-07-06): Added intel_fn11 fields to cpu_raw_data_t to handle + * new processor topology enumeration required on Core i7 + * 0.1.2 (2009-09-26): Added support for MSR reading through self-extracting + * kernel driver on Win32. + * 0.1.3 (2010-04-20): Added support for greater more accurate CPU clock + * measurements with cpu_clock_by_ic() + * 0.2.0 (2011-10-11): Support for AMD Bulldozer CPUs, 128-bit SSE unit size + * checking. A backwards-incompatible change, since the + * sizeof cpu_id_t is now different. + * 0.2.1 (2012-05-26): Support for Ivy Bridge, and detecting the presence of + * the RdRand instruction. + */ + +/** @mainpage A simple libcpuid introduction + * + * LibCPUID provides CPU identification and access to the CPUID and RDTSC + * instructions on the x86. + *

+ * To execute CPUID, use \ref cpu_exec_cpuid
+ * To execute RDTSC, use \ref cpu_rdtsc
+ * To fetch the CPUID info needed for CPU identification, use + * \ref cpuid_get_raw_data
+ * To make sense of that data (decode, extract features), use \ref cpu_identify
+ *

+ */ + +/** @defgroup libcpuid LibCPUID + @{ */ + +/* Include some integer type specifications: */ +#include "libcpuid_types.h" + +/* Some limits and other constants */ +#include "libcpuid_constants.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief CPU vendor, as guessed from the Vendor String. + */ +typedef enum { + VENDOR_INTEL = 0, /*!< Intel CPU */ + VENDOR_AMD, /*!< AMD CPU */ + VENDOR_CYRIX, /*!< Cyrix CPU */ + VENDOR_NEXGEN, /*!< NexGen CPU */ + VENDOR_TRANSMETA, /*!< Transmeta CPU */ + VENDOR_UMC, /*!< x86 CPU by UMC */ + VENDOR_CENTAUR, /*!< x86 CPU by IDT */ + VENDOR_RISE, /*!< x86 CPU by Rise Technology */ + VENDOR_SIS, /*!< x86 CPU by SiS */ + VENDOR_NSC, /*!< x86 CPU by National Semiconductor */ + + NUM_CPU_VENDORS, /*!< Valid CPU vendor ids: 0..NUM_CPU_VENDORS - 1 */ + VENDOR_UNKNOWN = -1, +} cpu_vendor_t; +#define NUM_CPU_VENDORS NUM_CPU_VENDORS + +/** + * @brief Contains just the raw CPUID data. + * + * This contains only the most basic CPU data, required to do identification + * and feature recognition. Every processor should be identifiable using this + * data only. + */ +struct cpu_raw_data_t { + /** contains results of CPUID for eax = 0, 1, ...*/ + uint32_t basic_cpuid[MAX_CPUID_LEVEL][4]; + + /** contains results of CPUID for eax = 0x80000000, 0x80000001, ...*/ + uint32_t ext_cpuid[MAX_EXT_CPUID_LEVEL][4]; + + /** when the CPU is intel and it supports deterministic cache + information: this contains the results of CPUID for eax = 4 + and ecx = 0, 1, ... */ + uint32_t intel_fn4[MAX_INTELFN4_LEVEL][4]; + + /** when the CPU is intel and it supports leaf 0Bh (Extended Topology + enumeration leaf), this stores the result of CPUID with + eax = 11 and ecx = 0, 1, 2... */ + uint32_t intel_fn11[MAX_INTELFN11_LEVEL][4]; +}; + +/** + * @brief This contains the recognized CPU features/info + */ +struct cpu_id_t { + /** contains the CPU vendor string, e.g. "GenuineIntel" */ + char vendor_str[VENDOR_STR_MAX]; + + /** contains the brand string, e.g. "Intel(R) Xeon(TM) CPU 2.40GHz" */ + char brand_str[BRAND_STR_MAX]; + + /** contains the recognized CPU vendor */ + cpu_vendor_t vendor; + + /** + * contain CPU flags. Used to test for features. See + * the CPU_FEATURE_* macros below. @see Features + */ + uint8_t flags[CPU_FLAGS_MAX]; + + /** CPU family */ + int32_t family; + + /** CPU model */ + int32_t model; + + /** CPU stepping */ + int32_t stepping; + + /** CPU extended family */ + int32_t ext_family; + + /** CPU extended model */ + int32_t ext_model; + + /** Number of CPU cores on the current processor */ + int32_t num_cores; + + /** + * Number of logical processors on the current processor. + * Could be more than the number of physical cores, + * e.g. when the processor has HyperThreading. + */ + int32_t num_logical_cpus; + + /** + * The total number of logical processors. + * + * This is num_logical_cpus * {total physical processors in the system} + * + * If you're writing a multithreaded program and you want to run it on + * all CPUs, this is the number of threads you need. + */ + int32_t total_logical_cpus; + + /** + * L1 data cache size in KB. Could be zero, if the CPU lacks cache. + * If the size cannot be determined, it will be -1. + */ + int32_t l1_data_cache; + + /** + * L1 instruction cache size in KB. Could be zero, if the CPU lacks + * cache. If the size cannot be determined, it will be -1. + * @note On some Intel CPUs, whose instruction cache is in fact + * a trace cache, the size will be expressed in K uOps. + */ + int32_t l1_instruction_cache; + + /** + * L2 cache size in KB. Could be zero, if the CPU lacks L2 cache. + * If the size of the cache could not be determined, it will be -1 + */ + int32_t l2_cache; + + /** L3 cache size in KB. Zero on most systems */ + int32_t l3_cache; + + /** Cache associativity for the L1 data cache. -1 if undetermined */ + int32_t l1_assoc; + + /** Cache associativity for the L2 cache. -1 if undetermined */ + int32_t l2_assoc; + + /** Cache associativity for the L3 cache. -1 if undetermined */ + int32_t l3_assoc; + + /** Cache-line size for L1 data cache. -1 if undetermined */ + int32_t l1_cacheline; + + /** Cache-line size for L2 cache. -1 if undetermined */ + int32_t l2_cacheline; + + /** Cache-line size for L3 cache. -1 if undetermined */ + int32_t l3_cacheline; + + /** + * The brief and human-friendly CPU codename, which was recognized.
+ * Examples: + * @code + * +--------+--------+-------+-------+-------+---------------------------------------+-----------------------+ + * | Vendor | Family | Model | Step. | Cache | Brand String | cpu_id_t.cpu_codename | + * +--------+--------+-------+-------+-------+---------------------------------------+-----------------------+ + * | AMD | 6 | 8 | 0 | 256 | (not available - will be ignored) | "K6-2" | + * | Intel | 15 | 2 | 5 | 512 | "Intel(R) Xeon(TM) CPU 2.40GHz" | "Xeon (Prestonia)" | + * | Intel | 6 | 15 | 11 | 4096 | "Intel(R) Core(TM)2 Duo CPU E6550..." | "Conroe (Core 2 Duo)" | + * | AMD | 15 | 35 | 2 | 1024 | "Dual Core AMD Opteron(tm) Proces..." | "Opteron (Dual Core)" | + * +--------+--------+-------+-------+-------+---------------------------------------+-----------------------+ + * @endcode + */ + char cpu_codename[64]; + + /** SSE execution unit size (64 or 128; -1 if N/A) */ + int32_t sse_size; + + /** + * contain miscellaneous detection information. Used to test about specifics of + * certain detected features. See CPU_HINT_* macros below. @see Hints + */ + uint8_t detection_hints[CPU_HINTS_MAX]; +}; + +/** + * @brief CPU feature identifiers + * + * Usage: + * @code + * ... + * struct cpu_raw_data_t raw; + * struct cpu_id_t id; + * if (cpuid_get_raw_data(&raw) == 0 && cpu_identify(&raw, &id) == 0) { + * if (id.flags[CPU_FEATURE_SSE2]) { + * // The CPU has SSE2... + * ... + * } else { + * // no SSE2 + * } + * } else { + * // processor cannot be determined. + * } + * @endcode + */ +typedef enum { + CPU_FEATURE_FPU = 0, /*!< Floating point unit */ + CPU_FEATURE_VME, /*!< Virtual mode extension */ + CPU_FEATURE_DE, /*!< Debugging extension */ + CPU_FEATURE_PSE, /*!< Page size extension */ + CPU_FEATURE_TSC, /*!< Time-stamp counter */ + CPU_FEATURE_MSR, /*!< Model-specific regsisters, RDMSR/WRMSR supported */ + CPU_FEATURE_PAE, /*!< Physical address extension */ + CPU_FEATURE_MCE, /*!< Machine check exception */ + CPU_FEATURE_CX8, /*!< CMPXCHG8B instruction supported */ + CPU_FEATURE_APIC, /*!< APIC support */ + CPU_FEATURE_MTRR, /*!< Memory type range registers */ + CPU_FEATURE_SEP, /*!< SYSENTER / SYSEXIT instructions supported */ + CPU_FEATURE_PGE, /*!< Page global enable */ + CPU_FEATURE_MCA, /*!< Machine check architecture */ + CPU_FEATURE_CMOV, /*!< CMOVxx instructions supported */ + CPU_FEATURE_PAT, /*!< Page attribute table */ + CPU_FEATURE_PSE36, /*!< 36-bit page address extension */ + CPU_FEATURE_PN, /*!< Processor serial # implemented (Intel P3 only) */ + CPU_FEATURE_CLFLUSH, /*!< CLFLUSH instruction supported */ + CPU_FEATURE_DTS, /*!< Debug store supported */ + CPU_FEATURE_ACPI, /*!< ACPI support (power states) */ + CPU_FEATURE_MMX, /*!< MMX instruction set supported */ + CPU_FEATURE_FXSR, /*!< FXSAVE / FXRSTOR supported */ + CPU_FEATURE_SSE, /*!< Streaming-SIMD Extensions (SSE) supported */ + CPU_FEATURE_SSE2, /*!< SSE2 instructions supported */ + CPU_FEATURE_SS, /*!< Self-snoop */ + CPU_FEATURE_HT, /*!< Hyper-threading supported (but might be disabled) */ + CPU_FEATURE_TM, /*!< Thermal monitor */ + CPU_FEATURE_IA64, /*!< IA64 supported (Itanium only) */ + CPU_FEATURE_PBE, /*!< Pending-break enable */ + CPU_FEATURE_PNI, /*!< PNI (SSE3) instructions supported */ + CPU_FEATURE_PCLMUL, /*!< PCLMULQDQ instruction supported */ + CPU_FEATURE_DTS64, /*!< 64-bit Debug store supported */ + CPU_FEATURE_MONITOR, /*!< MONITOR / MWAIT supported */ + CPU_FEATURE_DS_CPL, /*!< CPL Qualified Debug Store */ + CPU_FEATURE_VMX, /*!< Virtualization technology supported */ + CPU_FEATURE_SMX, /*!< Safer mode exceptions */ + CPU_FEATURE_EST, /*!< Enhanced SpeedStep */ + CPU_FEATURE_TM2, /*!< Thermal monitor 2 */ + CPU_FEATURE_SSSE3, /*!< SSSE3 instructionss supported (this is different from SSE3!) */ + CPU_FEATURE_CID, /*!< Context ID supported */ + CPU_FEATURE_CX16, /*!< CMPXCHG16B instruction supported */ + CPU_FEATURE_XTPR, /*!< Send Task Priority Messages disable */ + CPU_FEATURE_PDCM, /*!< Performance capabilities MSR supported */ + CPU_FEATURE_DCA, /*!< Direct cache access supported */ + CPU_FEATURE_SSE4_1, /*!< SSE 4.1 instructions supported */ + CPU_FEATURE_SSE4_2, /*!< SSE 4.2 instructions supported */ + CPU_FEATURE_SYSCALL, /*!< SYSCALL / SYSRET instructions supported */ + CPU_FEATURE_XD, /*!< Execute disable bit supported */ + CPU_FEATURE_MOVBE, /*!< MOVBE instruction supported */ + CPU_FEATURE_POPCNT, /*!< POPCNT instruction supported */ + CPU_FEATURE_AES, /*!< AES* instructions supported */ + CPU_FEATURE_XSAVE, /*!< XSAVE/XRSTOR/etc instructions supported */ + CPU_FEATURE_OSXSAVE, /*!< non-privileged copy of OSXSAVE supported */ + CPU_FEATURE_AVX, /*!< Advanced vector extensions supported */ + CPU_FEATURE_MMXEXT, /*!< AMD MMX-extended instructions supported */ + CPU_FEATURE_3DNOW, /*!< AMD 3DNow! instructions supported */ + CPU_FEATURE_3DNOWEXT, /*!< AMD 3DNow! extended instructions supported */ + CPU_FEATURE_NX, /*!< No-execute bit supported */ + CPU_FEATURE_FXSR_OPT, /*!< FFXSR: FXSAVE and FXRSTOR optimizations */ + CPU_FEATURE_RDTSCP, /*!< RDTSCP instruction supported (AMD-only) */ + CPU_FEATURE_LM, /*!< Long mode (x86_64/EM64T) supported */ + CPU_FEATURE_LAHF_LM, /*!< LAHF/SAHF supported in 64-bit mode */ + CPU_FEATURE_CMP_LEGACY, /*!< core multi-processing legacy mode */ + CPU_FEATURE_SVM, /*!< AMD Secure virtual machine */ + CPU_FEATURE_ABM, /*!< LZCNT instruction support */ + CPU_FEATURE_MISALIGNSSE,/*!< Misaligned SSE supported */ + CPU_FEATURE_SSE4A, /*!< SSE 4a from AMD */ + CPU_FEATURE_3DNOWPREFETCH, /*!< PREFETCH/PREFETCHW support */ + CPU_FEATURE_OSVW, /*!< OS Visible Workaround (AMD) */ + CPU_FEATURE_IBS, /*!< Instruction-based sampling */ + CPU_FEATURE_SSE5, /*!< SSE 5 instructions supported (deprecated, will never be 1) */ + CPU_FEATURE_SKINIT, /*!< SKINIT / STGI supported */ + CPU_FEATURE_WDT, /*!< Watchdog timer support */ + CPU_FEATURE_TS, /*!< Temperature sensor */ + CPU_FEATURE_FID, /*!< Frequency ID control */ + CPU_FEATURE_VID, /*!< Voltage ID control */ + CPU_FEATURE_TTP, /*!< THERMTRIP */ + CPU_FEATURE_TM_AMD, /*!< AMD-specified hardware thermal control */ + CPU_FEATURE_STC, /*!< Software thermal control */ + CPU_FEATURE_100MHZSTEPS,/*!< 100 MHz multiplier control */ + CPU_FEATURE_HWPSTATE, /*!< Hardware P-state control */ + CPU_FEATURE_CONSTANT_TSC, /*!< TSC ticks at constant rate */ + CPU_FEATURE_XOP, /*!< The XOP instruction set (same as the old CPU_FEATURE_SSE5) */ + CPU_FEATURE_FMA3, /*!< The FMA3 instruction set */ + CPU_FEATURE_FMA4, /*!< The FMA4 instruction set */ + CPU_FEATURE_TBM, /*!< Trailing bit manipulation instruction support */ + CPU_FEATURE_F16C, /*!< 16-bit FP convert instruction support */ + CPU_FEATURE_RDRAND, /*!< RdRand instruction */ + CPU_FEATURE_X2APIC, /*!< x2APIC, APIC_BASE.EXTD, MSRs 0000_0800h...0000_0BFFh 64-bit ICR (+030h but not +031h), no DFR (+00Eh), SELF_IPI (+040h) also see standard level 0000_000Bh */ + CPU_FEATURE_CPB, /*!< Core performance boost */ + CPU_FEATURE_APERFMPERF, /*!< MPERF/APERF MSRs support */ + CPU_FEATURE_PFI, /*!< Processor Feedback Interface support */ + CPU_FEATURE_PA, /*!< Processor accumulator */ + CPU_FEATURE_AVX2, /*!< AVX2 instructions */ + /* termination: */ + NUM_CPU_FEATURES, +} cpu_feature_t; + +/** + * @brief CPU detection hints identifiers + * + * Usage: similar to the flags usage + */ +typedef enum { + CPU_HINT_SSE_SIZE_AUTH = 0, /*!< SSE unit size is authoritative (not only a Family/Model guesswork, but based on an actual CPUID bit) */ + /* termination */ + NUM_CPU_HINTS, +} cpu_hint_t; + +/** + * @brief Describes common library error codes + */ +typedef enum { + ERR_OK = 0, /*!< "No error" */ + ERR_NO_CPUID = -1, /*!< "CPUID instruction is not supported" */ + ERR_NO_RDTSC = -2, /*!< "RDTSC instruction is not supported" */ + ERR_NO_MEM = -3, /*!< "Memory allocation failed" */ + ERR_OPEN = -4, /*!< "File open operation failed" */ + ERR_BADFMT = -5, /*!< "Bad file format" */ + ERR_NOT_IMP = -6, /*!< "Not implemented" */ + ERR_CPU_UNKN = -7, /*!< "Unsupported processor" */ + ERR_NO_RDMSR = -8, /*!< "RDMSR instruction is not supported" */ + ERR_NO_DRIVER= -9, /*!< "RDMSR driver error (generic)" */ + ERR_NO_PERMS = -10, /*!< "No permissions to install RDMSR driver" */ + ERR_EXTRACT = -11, /*!< "Cannot extract RDMSR driver (read only media?)" */ + ERR_HANDLE = -12, /*!< "Bad handle" */ + ERR_INVMSR = -13, /*!< "Invalid MSR" */ +} cpu_error_t; + +/** + * @brief Internal structure, used in cpu_tsc_mark, cpu_tsc_unmark and + * cpu_clock_by_mark + */ +struct cpu_mark_t { + uint64_t tsc; /*!< Time-stamp from RDTSC */ + uint64_t sys_clock; /*!< In microsecond resolution */ +}; + +/** + * @brief Returns the total number of CPUs even if CPUID is not present + * @retval Number of CPUs available + */ +int cpuid_get_total_cpus(void); + +/** + * @brief Checks if the CPUID instruction is supported + * @retval 1 if CPUID is present + * @retval 0 the CPU doesn't have CPUID. + */ +int cpuid_present(void); + +/** + * @brief Executes the CPUID instruction + * @param eax - the value of the EAX register when executing CPUID + * @param regs - the results will be stored here. regs[0] = EAX, regs[1] = EBX, ... + * @note CPUID will be executed with EAX set to the given value and EBX, ECX, + * EDX set to zero. + */ +void cpu_exec_cpuid(uint32_t eax, uint32_t* regs); + +/** + * @brief Executes the CPUID instruction with the given input registers + * @note This is just a bit more generic version of cpu_exec_cpuid - it allows + * you to control all the registers. + * @param regs - Input/output. Prior to executing CPUID, EAX, EBX, ECX and + * EDX will be set to regs[0], regs[1], regs[2] and regs[3]. + * After CPUID, this array will contain the results. + */ +void cpu_exec_cpuid_ext(uint32_t* regs); + +/** + * @brief Obtains the raw CPUID data from the current CPU + * @param data - a pointer to cpu_raw_data_t structure + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +int cpuid_get_raw_data(struct cpu_raw_data_t* data); + +/** + * @brief Writes the raw CPUID data to a text file + * @param data - a pointer to cpu_raw_data_t structure + * @param filename - the path of the file, where the serialized data should be + * written. If empty, stdout will be used. + * @note This is intended primarily for debugging. On some processor, which is + * not currently supported or not completely recognized by cpu_identify, + * one can still successfully get the raw data and write it to a file. + * libcpuid developers can later import this file and debug the detection + * code as if running on the actual hardware. + * The file is simple text format of "something=value" pairs. Version info + * is also written, but the format is not intended to be neither backward- + * nor forward compatible. + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +int cpuid_serialize_raw_data(struct cpu_raw_data_t* data, const char* filename); + +/** + * @brief Reads raw CPUID data from file + * @param data - a pointer to cpu_raw_data_t structure. The deserialized data will + * be written here. + * @param filename - the path of the file, containing the serialized raw data. + * If empty, stdin will be used. + * @note This function may fail, if the file is created by different version of + * the library. Also, see the notes on cpuid_serialize_raw_data. + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t +*/ +int cpuid_deserialize_raw_data(struct cpu_raw_data_t* data, const char* filename); + +/** + * @brief Identifies the CPU + * @param raw - Input - a pointer to the raw CPUID data, which is obtained + * either by cpuid_get_raw_data or cpuid_deserialize_raw_data. + * Can also be NULL, in which case the functions calls + * cpuid_get_raw_data itself. + * @param data - Output - the decoded CPU features/info is written here. + * @note The function will not fail, even if some of the information + * cannot be obtained. Even when the CPU is new and thus unknown to + * libcpuid, some generic info, such as "AMD K9 family CPU" will be + * written to data.cpu_codename, and most other things, such as the + * CPU flags, cache sizes, etc. should be detected correctly anyway. + * However, the function CAN fail, if the CPU is completely alien to + * libcpuid. + * @note While cpu_identify() and cpuid_get_raw_data() are fast for most + * purposes, running them several thousand times per second can hamper + * performance significantly. Specifically, avoid writing "cpu feature + * checker" wrapping function, which calls cpu_identify and returns the + * value of some flag, if that function is going to be called frequently. + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +int cpu_identify(struct cpu_raw_data_t* raw, struct cpu_id_t* data); + +/** + * @brief Returns the short textual representation of a CPU flag + * @param feature - the feature, whose textual representation is wanted. + * @returns a constant string like "fpu", "tsc", "sse2", etc. + * @note the names of the returned flags are compatible with those from + * /proc/cpuinfo in Linux, with the exception of `tm_amd' + */ +const char* cpu_feature_str(cpu_feature_t feature); + +/** + * @brief Returns textual description of the last error + * + * libcpuid stores an `errno'-style error status, whose description + * can be obtained with this function. + * @note This function is not thread-safe + * @see cpu_error_t + */ +const char* cpuid_error(void); + +/** + * @brief Executes RDTSC + * + * The RDTSC (ReaD Time Stamp Counter) instruction gives access to an + * internal 64-bit counter, which usually increments at each clock cycle. + * This can be used for various timing routines, and as a very precise + * clock source. It is set to zero on system startup. Beware that may not + * increment at the same frequency as the CPU. Consecutive calls of RDTSC + * are, however, guaranteed to return monotonically-increasing values. + * + * @param result - a pointer to a 64-bit unsigned integer, where the TSC value + * will be stored + * + * @note If 100% compatibility is a concern, you must first check if the + * RDTSC instruction is present (if it is not, your program will crash + * with "invalid opcode" exception). Only some very old processors (i486, + * early AMD K5 and some Cyrix CPUs) lack that instruction - they should + * have become exceedingly rare these days. To verify RDTSC presence, + * run cpu_identify() and check flags[CPU_FEATURE_TSC]. + * + * @note The monotonically increasing nature of the TSC may be violated + * on SMP systems, if their TSC clocks run at different rate. If the OS + * doesn't account for that, the TSC drift may become arbitrary large. + */ +void cpu_rdtsc(uint64_t* result); + +/** + * @brief Store TSC and timing info + * + * This function stores the current TSC value and current + * time info from a precise OS-specific clock source in the cpu_mark_t + * structure. The sys_clock field contains time with microsecond resolution. + * The values can later be used to measure time intervals, number of clocks, + * FPU frequency, etc. + * @see cpu_rdtsc + * + * @param mark [out] - a pointer to a cpu_mark_t structure + */ +void cpu_tsc_mark(struct cpu_mark_t* mark); + +/** + * @brief Calculate TSC and timing difference + * + * @param mark - input/output: a pointer to a cpu_mark_t sturcture, which has + * already been initialized by cpu_tsc_mark. The difference in + * TSC and time will be written here. + * + * This function calculates the TSC and time difference, by obtaining the + * current TSC and timing values and subtracting the contents of the `mark' + * structure from them. Results are written in the same structure. + * + * Example: + * @code + * ... + * struct cpu_mark_t mark; + * cpu_tsc_mark(&mark); + * foo(); + * cpu_tsc_unmark(&mark); + * printf("Foo finished. Executed in %llu cycles and %llu usecs\n", + * mark.tsc, mark.sys_clock); + * ... + * @endcode + */ +void cpu_tsc_unmark(struct cpu_mark_t* mark); + +/** + * @brief Calculates the CPU clock + * + * @param mark - pointer to a cpu_mark_t structure, which has been initialized + * with cpu_tsc_mark and later `stopped' with cpu_tsc_unmark. + * + * @note For reliable results, the marked time interval should be at least about + * 10 ms. + * + * @returns the CPU clock frequency, in MHz. Due to measurement error, it will + * differ from the true value in a few least-significant bits. Accuracy depends + * on the timing interval - the more, the better. If the timing interval is + * insufficient, the result is -1. Also, see the comment on cpu_clock_measure + * for additional issues and pitfalls in using RDTSC for CPU frequency + * measurements. + */ +int cpu_clock_by_mark(struct cpu_mark_t* mark); + +/** + * @brief Returns the CPU clock, as reported by the OS + * + * This function uses OS-specific functions to obtain the CPU clock. It may + * differ from the true clock for several reasons:

+ * + * i) The CPU might be in some power saving state, while the OS reports its + * full-power frequency, or vice-versa.
+ * ii) In some cases you can raise or lower the CPU frequency with overclocking + * utilities and the OS will not notice. + * + * @returns the CPU clock frequency in MHz. If the OS is not (yet) supported + * or lacks the necessary reporting machinery, the return value is -1 + */ +int cpu_clock_by_os(void); + +/** + * @brief Measure the CPU clock frequency + * + * @param millis - How much time to waste in the busy-wait cycle. In millisecs. + * Useful values 10 - 1000 + * @param quad_check - Do a more thorough measurement if nonzero + * (see the explanation). + * + * The function performs a busy-wait cycle for the given time and calculates + * the CPU frequency by the difference of the TSC values. The accuracy of the + * calculation depends on the length of the busy-wait cycle: more is better, + * but 100ms should be enough for most purposes. + * + * While this will calculate the CPU frequency correctly in most cases, there are + * several reasons why it might be incorrect:
+ * + * i) RDTSC doesn't guarantee it will run at the same clock as the CPU. + * Apparently there aren't CPUs at the moment, but still, there's no + * guarantee.
+ * ii) The CPU might be in a low-frequency power saving mode, and the CPU + * might be switched to higher frequency at any time. If this happens + * during the measurement, the result can be anywhere between the + * low and high frequencies. Also, if you're interested in the + * high frequency value only, this function might return the low one + * instead.
+ * iii) On SMP systems exhibiting TSC drift (see \ref cpu_rdtsc) + * + * the quad_check option will run four consecutive measurements and + * then return the average of the two most-consistent results. The total + * runtime of the function will still be `millis' - consider using + * a bit more time for the timing interval. + * + * Finally, for benchmarking / CPU intensive applications, the best strategy is + * to use the cpu_tsc_mark() / cpu_tsc_unmark() / cpu_clock_by_mark() method. + * Begin by mark()-ing about one second after application startup (allowing the + * power-saving manager to kick in and rise the frequency during that time), + * then unmark() just before application finishing. The result will most + * acurately represent at what frequency your app was running. + * + * @returns the CPU clock frequency in MHz (within some measurement error + * margin). If RDTSC is not supported, the result is -1. + */ +int cpu_clock_measure(int millis, int quad_check); + +/** + * @brief Measure the CPU clock frequency using instruction-counting + * + * @param millis - how much time to allocate for each run, in milliseconds + * @param runs - how many runs to perform + * + * The function performs a busy-wait cycle using a known number of "heavy" (SSE) + * instructions. These instructions run at (more or less guaranteed) 1 IPC rate, + * so by running a busy loop for a fixed amount of time, and measuring the + * amount of instructions done, the CPU clock is accurately measured. + * + * Of course, this function is still affected by the power-saving schemes, so + * the warnings as of cpu_clock_measure() still apply. However, this function is + * immune to problems with detection, related to the Intel Nehalem's "Turbo" + * mode, where the internal clock is raised, but the RDTSC rate is unaffected. + * + * The function will run for about (millis * runs) milliseconds. + * You can make only a single busy-wait run (runs == 1); however, this can + * be affected by task scheduling (which will break the counting), so allowing + * more than one run is recommended. As run length is not imperative for + * accurate readings (e.g., 50ms is sufficient), you can afford a lot of short + * runs, e.g. 10 runs of 50ms or 20 runs of 25ms. + * + * Recommended values - millis = 50, runs = 4. For more robustness, + * increase the number of runs. + * + * NOTE: on Bulldozer and later CPUs, the busy-wait cycle runs at 1.4 IPC, thus + * the results are skewed. This is corrected internally by dividing the resulting + * value by 1.4. + * However, this only occurs if the thread is executed on a single CMT + * module - if there are other threads competing for resources, the results are + * unpredictable. Make sure you run cpu_clock_by_ic() on a CPU that is free from + * competing threads, or if there are such threads, they shouldn't exceed the + * number of modules. On a Bulldozer X8, that means 4 threads. + * + * @returns the CPU clock frequency in MHz (within some measurement error + * margin). If SSE is not supported, the result is -1. If the input parameters + * are incorrect, or some other internal fault is detected, the result is -2. + */ +int cpu_clock_by_ic(int millis, int runs); + +/** + * @brief Get the CPU clock frequency (all-in-one method) + * + * This is an all-in-one method for getting the CPU clock frequency. + * It tries to use the OS for that. If the OS doesn't have this info, it + * uses cpu_clock_measure with 200ms time interval and quadruple checking. + * + * @returns the CPU clock frequency in MHz. If every possible method fails, + * the result is -1. + */ +int cpu_clock(void); + +/** + * @brief Returns the libcpuid version + * + * @returns the string representation of the libcpuid version, like "0.1.1" + */ +const char* cpuid_lib_version(void); + +typedef void (*libcpuid_warn_fn_t) (const char *msg); +/** + * @brief Sets the warning print function + * + * In some cases, the internal libcpuid machinery would like to emit useful + * debug warnings. By default, these warnings are written to stderr. However, + * you can set a custom function that will receive those warnings. + * + * @param warn_fun - the warning function you want to set. If NULL, warnings + * are disabled. The function takes const char* argument. + * + * @returns the current warning function. You can use the return value to + * keep the previous warning function and restore it at your discretion. + */ +libcpuid_warn_fn_t cpuid_set_warn_function(libcpuid_warn_fn_t warn_fun); + +/** + * @brief Sets the verbosiness level + * + * When the verbosiness level is above zero, some functions might print + * diagnostic information about what are they doing. The higher the level is, + * the more detail is printed. Level zero is guaranteed to omit all such + * output. The output is written using the same machinery as the warnings, + * @see cpuid_set_warn_function() + * + * @param level the desired verbosiness level. Useful values 0..2 inclusive + */ +void cpuid_set_verbosiness_level(int level); + + +/** + * @brief a structure that holds a list of processor names + */ +struct cpu_list_t { + /** Number of entries in the list */ + int num_entries; + /** Pointers to names. There will be num_entries of them */ + char **names; +}; + +/** + * @brief Gets a list of all known CPU names from a specific vendor. + * + * This function compiles a list of all known CPU (code)names + * (i.e. the possible values of cpu_id_t::cpu_codename) for the given vendor. + * + * There are about 100 entries for Intel and AMD, and a few for the other + * vendors. The list is written out in approximate chronological introduction + * order of the parts. + * + * @param vendor the vendor to be queried + * @param list [out] the resulting list will be written here. + * NOTE: As the memory is dynamically allocated, be sure to call + * cpuid_free_cpu_list() after you're done with the data + * @see cpu_list_t + */ +void cpuid_get_cpu_list(cpu_vendor_t vendor, struct cpu_list_t* list); + +/** + * @brief Frees a CPU list + * + * This function deletes all the memory associated with a CPU list, as obtained + * by cpuid_get_cpu_list() + * + * @param list - the list to be free()'d. + */ +void cpuid_free_cpu_list(struct cpu_list_t* list); + +/** + * @brief Starts/opens a driver, needed to read MSRs (Model Specific Registers) + * + * On systems that support it, this function will create a temporary + * system driver, that has privileges to execute the RDMSR instruction. + * After the driver is created, you can read MSRs by calling \ref cpu_rdmsr + * + * @returns a handle to the driver on success, and NULL on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +struct msr_driver_t; +struct msr_driver_t* cpu_msr_driver_open(void); + +/** + * @brief Reads a Model-Specific Register (MSR) + * + * If the CPU has MSRs (as indicated by the CPU_FEATURE_MSR flag), you can + * read a MSR with the given index by calling this function. + * + * There are several prerequisites you must do before reading MSRs: + * 1) You must ensure the CPU has RDMSR. Check the CPU_FEATURE_MSR flag + * in cpu_id_t::flags + * 2) You must ensure that the CPU implements the specific MSR you intend to + * read. + * 3) You must open a MSR-reader driver. RDMSR is a privileged instruction and + * needs ring-0 access in order to work. This temporary driver is created + * by calling \ref cpu_msr_driver_open + * + * @param handle - a handle to the MSR reader driver, as created by + * cpu_msr_driver_open + * @param msr_index - the numeric ID of the MSR you want to read + * @param result - a pointer to a 64-bit integer, where the MSR value is stored + * + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +int cpu_rdmsr(struct msr_driver_t* handle, int msr_index, uint64_t* result); + + +typedef enum { + INFO_MPERF, /*!< Maximum performance frequency clock. This + is a counter, which increments as a + proportion of the actual processor speed */ + INFO_APERF, /*!< Actual performance frequency clock. This + accumulates the core clock counts when the + core is active. */ + INFO_CUR_MULTIPLIER, /*!< Current CPU:FSB ratio, multiplied by 100. + e.g., a CPU:FSB value of 18.5 reads as + 1850. */ + INFO_MAX_MULTIPLIER, /*!< Maxumum CPU:FSB ratio for this CPU, + multiplied by 100 */ + INFO_TEMPERATURE, /*!< The current core temperature in Celsius */ + INFO_THROTTLING, /*!< 1 if the current logical processor is + throttling. 0 if it is running normally. */ +} cpu_msrinfo_request_t; + +/** + * @brief Reads extended CPU information from Model-Specific Registers. + * @param handle - a handle to an open MSR driver, @see cpu_msr_driver_open + * @param which - which info field should be returned. A list of + * available information entities is listed in the + * cpu_msrinfo_request_t enum. + * @retval - if the requested information is available for the current + * processor model, the respective value is returned. + * if no information is available, or the CPU doesn't support + * the query, the special value CPU_INVALID_VALUE is returned + */ +int cpu_msrinfo(struct msr_driver_t* handle, cpu_msrinfo_request_t which); +#define CPU_INVALID_VALUE 0x3fffffff + +/** + * @brief Closes an open MSR driver + * + * This function unloads the MSR driver opened by cpu_msr_driver_open and + * frees any resources associated with it. + * + * @param handle - a handle to the MSR reader driver, as created by + * cpu_msr_driver_open + * + * @returns zero if successful, and some negative number on error. + * The error message can be obtained by calling \ref cpuid_error. + * @see cpu_error_t + */ +int cpu_msr_driver_close(struct msr_driver_t* handle); + +#ifdef __cplusplus +}; /* extern "C" */ +#endif + + +/** @} */ + +#endif /* __LIBCPUID_H__ */ diff --git a/contrib/libcpuid/include/cpuid/libcpuid_constants.h b/contrib/libcpuid/include/cpuid/libcpuid_constants.h new file mode 100644 index 00000000000..8af4718c906 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/libcpuid_constants.h @@ -0,0 +1,44 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * @File libcpuid_constants.h + * @Author Veselin Georgiev + * @Brief Some limits and constants for libcpuid + */ + +#ifndef __LIBCPUID_CONSTANTS_H__ +#define __LIBCPUID_CONSTANTS_H__ + +#define VENDOR_STR_MAX 16 +#define BRAND_STR_MAX 64 +#define CPU_FLAGS_MAX 128 +#define MAX_CPUID_LEVEL 32 +#define MAX_EXT_CPUID_LEVEL 32 +#define MAX_INTELFN4_LEVEL 4 +#define MAX_INTELFN11_LEVEL 4 +#define CPU_HINTS_MAX 16 + +#endif /* __LIBCPUID_CONSTANTS_H__ */ diff --git a/contrib/libcpuid/include/cpuid/libcpuid_types.h b/contrib/libcpuid/include/cpuid/libcpuid_types.h new file mode 100644 index 00000000000..6dd18a97724 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/libcpuid_types.h @@ -0,0 +1,37 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * @File libcpuid_types.h + * @Author Veselin Georgiev + * @Brief Type specifications for libcpuid. + */ + +#ifndef __LIBCPUID_TYPES_H__ +#define __LIBCPUID_TYPES_H__ + +# include + +#endif /* __LIBCPUID_TYPES_H__ */ diff --git a/contrib/libcpuid/include/cpuid/libcpuid_util.c b/contrib/libcpuid/include/cpuid/libcpuid_util.c new file mode 100644 index 00000000000..c08f63a4d2a --- /dev/null +++ b/contrib/libcpuid/include/cpuid/libcpuid_util.c @@ -0,0 +1,182 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include "libcpuid.h" +#include "libcpuid_util.h" + +int _current_verboselevel; + +void match_features(const struct feature_map_t* matchtable, int count, uint32_t reg, struct cpu_id_t* data) +{ + int i; + for (i = 0; i < count; i++) + if (reg & (1 << matchtable[i].bit)) + data->flags[matchtable[i].feature] = 1; +} + +static void default_warn(const char *msg) +{ + fprintf(stderr, "%s", msg); +} + +libcpuid_warn_fn_t _warn_fun = default_warn; + +#if defined(_MSC_VER) +# define vsnprintf _vsnprintf +#endif +void warnf(const char* format, ...) +{ + char buff[1024]; + va_list va; + if (!_warn_fun) return; + va_start(va, format); + vsnprintf(buff, sizeof(buff), format, va); + va_end(va); + _warn_fun(buff); +} + +void debugf(int verboselevel, const char* format, ...) +{ + char buff[1024]; + va_list va; + if (verboselevel > _current_verboselevel) return; + va_start(va, format); + vsnprintf(buff, sizeof(buff), format, va); + va_end(va); + _warn_fun(buff); +} + +static int score(const struct match_entry_t* entry, const struct cpu_id_t* data, + int brand_code, int model_code) +{ + int res = 0; + if (entry->family == data->family ) res++; + if (entry->model == data->model ) res++; + if (entry->stepping == data->stepping ) res++; + if (entry->ext_family == data->ext_family) res++; + if (entry->ext_model == data->ext_model ) res++; + if (entry->ncores == data->num_cores ) res++; + if (entry->l2cache == data->l2_cache ) res++; + if (entry->l3cache == data->l3_cache ) res++; + if (entry->brand_code == brand_code ) res++; + if (entry->model_code == model_code ) res++; + return res; +} + +void match_cpu_codename(const struct match_entry_t* matchtable, int count, + struct cpu_id_t* data, int brand_code, int model_code) +{ + int bestscore = -1; + int bestindex = 0; + int i, t; + + debugf(3, "Matching cpu f:%d, m:%d, s:%d, xf:%d, xm:%d, ncore:%d, l2:%d, bcode:%d, code:%d\n", + data->family, data->model, data->stepping, data->ext_family, + data->ext_model, data->num_cores, data->l2_cache, brand_code, model_code); + + for (i = 0; i < count; i++) { + t = score(&matchtable[i], data, brand_code, model_code); + debugf(3, "Entry %d, `%s', score %d\n", i, matchtable[i].name, t); + if (t > bestscore) { + debugf(2, "Entry `%s' selected - best score so far (%d)\n", matchtable[i].name, t); + bestscore = t; + bestindex = i; + } + } + strcpy(data->cpu_codename, matchtable[bestindex].name); +} + +void generic_get_cpu_list(const struct match_entry_t* matchtable, int count, + struct cpu_list_t* list) +{ + int i, j, n, good; + n = 0; + list->names = (char**) malloc(sizeof(char*) * count); + for (i = 0; i < count; i++) { + if (strstr(matchtable[i].name, "Unknown")) continue; + good = 1; + for (j = n - 1; j >= 0; j--) + if (!strcmp(list->names[j], matchtable[i].name)) { + good = 0; + break; + } + if (!good) continue; + list->names[n++] = strdup(matchtable[i].name); + } + list->num_entries = n; +} + +static int xmatch_entry(char c, const char* p) +{ + int i, j; + if (c == 0) return -1; + if (c == p[0]) return 1; + if (p[0] == '.') return 1; + if (p[0] == '#' && isdigit(c)) return 1; + if (p[0] == '[') { + j = 1; + while (p[j] && p[j] != ']') j++; + if (!p[j]) return -1; + for (i = 1; i < j; i++) + if (p[i] == c) return j + 1; + } + return -1; +} + +int match_pattern(const char* s, const char* p) +{ + int i, j, dj, k, n, m; + n = (int) strlen(s); + m = (int) strlen(p); + for (i = 0; i < n; i++) { + if (xmatch_entry(s[i], p) != -1) { + j = 0; + k = 0; + while (j < m && ((dj = xmatch_entry(s[i + k], p + j)) != -1)) { + k++; + j += dj; + } + if (j == m) return i + 1; + } + } + return 0; +} + +struct cpu_id_t* get_cached_cpuid(void) +{ + static int initialized = 0; + static struct cpu_id_t id; + if (initialized) return &id; + if (cpu_identify(NULL, &id)) + memset(&id, 0, sizeof(id)); + initialized = 1; + return &id; +} diff --git a/contrib/libcpuid/include/cpuid/libcpuid_util.h b/contrib/libcpuid/include/cpuid/libcpuid_util.h new file mode 100644 index 00000000000..34e1efe960a --- /dev/null +++ b/contrib/libcpuid/include/cpuid/libcpuid_util.h @@ -0,0 +1,87 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __LIBCPUID_UTIL_H__ +#define __LIBCPUID_UTIL_H__ + +#define COUNT_OF(array) (sizeof(array) / sizeof(array[0])) + +struct feature_map_t { + unsigned bit; + cpu_feature_t feature; +}; + +void match_features(const struct feature_map_t* matchtable, int count, + uint32_t reg, struct cpu_id_t* data); + +struct match_entry_t { + int family, model, stepping, ext_family, ext_model; + int ncores, l2cache, l3cache, brand_code, model_code; + char name[32]; +}; + +void match_cpu_codename(const struct match_entry_t* matchtable, int count, + struct cpu_id_t* data, int brand_code, int model_code); + +void warnf(const char* format, ...) +#ifdef __GNUC__ +__attribute__((format(printf, 1, 2))) +#endif +; +void debugf(int verboselevel, const char* format, ...) +#ifdef __GNUC__ +__attribute__((format(printf, 2, 3))) +#endif +; +void generic_get_cpu_list(const struct match_entry_t* matchtable, int count, + struct cpu_list_t* list); + +/* + * Seek for a pattern in `haystack'. + * Pattern may be an fixed string, or contain the special metacharacters + * '.' - match any single character + * '#' - match any digit + * '[] - match any of the given chars (regex-like ranges are not + * supported) + * Return val: 0 if the pattern is not found. Nonzero if it is found (actually, + * x + 1 where x is the index where the match is found). + */ +int match_pattern(const char* haystack, const char* pattern); + +/* + * Gets an initialized cpu_id_t. It is cached, so that internal libcpuid + * machinery doesn't need to issue cpu_identify more than once. + */ +struct cpu_id_t* get_cached_cpuid(void); + +/* + * Sets the current errno + */ +int set_error(cpu_error_t err); + +extern libcpuid_warn_fn_t _warn_fun; +extern int _current_verboselevel; + +#endif /* __LIBCPUID_UTIL_H__ */ diff --git a/contrib/libcpuid/include/cpuid/rdtsc.c b/contrib/libcpuid/include/cpuid/rdtsc.c new file mode 100644 index 00000000000..5930681561a --- /dev/null +++ b/contrib/libcpuid/include/cpuid/rdtsc.c @@ -0,0 +1,295 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include +#include "libcpuid.h" +#include "libcpuid_util.h" +#include "asm-bits.h" +#include "rdtsc.h" + +#ifdef _WIN32 +#include +void sys_precise_clock(uint64_t *result) +{ + double c, f; + LARGE_INTEGER freq, counter; + QueryPerformanceCounter(&counter); + QueryPerformanceFrequency(&freq); + c = (double) counter.QuadPart; + f = (double) freq.QuadPart; + *result = (uint64_t) ( c * 1000000.0 / f ); +} +#else +/* assuming Linux, Mac OS or other POSIX */ +#include +void sys_precise_clock(uint64_t *result) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + *result = (uint64_t) tv.tv_sec * (uint64_t) 1000000 + + (uint64_t) tv.tv_usec; +} +#endif /* _WIN32 */ + +/* out = a - b */ +static void mark_t_subtract(struct cpu_mark_t* a, struct cpu_mark_t* b, struct cpu_mark_t *out) +{ + out->tsc = a->tsc - b->tsc; + out->sys_clock = a->sys_clock - b->sys_clock; +} + +void cpu_tsc_mark(struct cpu_mark_t* mark) +{ + cpu_rdtsc(&mark->tsc); + sys_precise_clock(&mark->sys_clock); +} + +void cpu_tsc_unmark(struct cpu_mark_t* mark) +{ + struct cpu_mark_t temp; + cpu_tsc_mark(&temp); + mark_t_subtract(&temp, mark, mark); +} + + +int cpu_clock_by_mark(struct cpu_mark_t* mark) +{ + uint64_t result; + + /* Check if some subtraction resulted in a negative number: */ + if ((mark->tsc >> 63) != 0 || (mark->sys_clock >> 63) != 0) return -1; + + /* Divide-by-zero check: */ + if (mark->sys_clock == 0) return -1; + + /* Check if the result fits in 32bits */ + result = mark->tsc / mark->sys_clock; + if (result > (uint64_t) 0x7fffffff) return -1; + return (int) result; +} + +#ifdef _WIN32 +int cpu_clock_by_os(void) +{ + HKEY key; + DWORD result; + DWORD size = 4; + + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &key) != ERROR_SUCCESS) + return -1; + + if (RegQueryValueEx(key, TEXT("~MHz"), NULL, NULL, (LPBYTE) &result, (LPDWORD) &size) != ERROR_SUCCESS) { + RegCloseKey(key); + return -1; + } + RegCloseKey(key); + + return (int)result; +} +#else +#ifdef __APPLE__ +#include +#include +/* Assuming Mac OS X with hw.cpufrequency sysctl */ +int cpu_clock_by_os(void) +{ + long long result = -1; + size_t size = sizeof(result); + if (sysctlbyname("hw.cpufrequency", &result, &size, NULL, 0)) + return -1; + return (int) (result / (long long) 1000000); +} +#else +/* Assuming Linux with /proc/cpuinfo */ +int cpu_clock_by_os(void) +{ + FILE *f; + char line[1024], *s; + int result; + + f = fopen("/proc/cpuinfo", "rt"); + if (!f) return -1; + + while (fgets(line, sizeof(line), f)) { + if (!strncmp(line, "cpu MHz", 7)) { + s = strchr(line, ':'); + if (s && 1 == sscanf(s, ":%d.", &result)) { + fclose(f); + return result; + } + } + } + fclose(f); + return -1; +} +#endif /* __APPLE__ */ +#endif /* _WIN32 */ + +/* Emulate doing useful CPU intensive work */ +static int busy_loop(int amount) +{ + int i, j, k, s = 0; + static volatile int data[42] = {32, 12, -1, 5, 23, 0 }; + for (i = 0; i < amount; i++) + for (j = 0; j < 65536; j++) + for (k = 0; k < 42; k++) + s += data[k]; + return s; +} + +int busy_loop_delay(int milliseconds) +{ + int cycles = 0, r = 0, first = 1; + uint64_t a, b, c; + sys_precise_clock(&a); + while (1) { + sys_precise_clock(&c); + if ((c - a) / 1000 > milliseconds) return r; + r += busy_loop(cycles); + if (first) { + first = 0; + } else { + if (c - b < 1000) cycles *= 2; + if (c - b > 10000) cycles /= 2; + } + b = c; + } +} + +int cpu_clock_measure(int millis, int quad_check) +{ + struct cpu_mark_t begin[4], end[4], temp, temp2; + int results[4], cycles, n, k, i, j, bi, bj, mdiff, diff, _zero = 0; + uint64_t tl; + + if (millis < 1) return -1; + tl = millis * (uint64_t) 1000; + if (quad_check) + tl /= 4; + n = quad_check ? 4 : 1; + cycles = 1; + for (k = 0; k < n; k++) { + cpu_tsc_mark(&begin[k]); + end[k] = begin[k]; + do { + /* Run busy loop, and fool the compiler that we USE the garbishy + value it calculates */ + _zero |= (1 & busy_loop(cycles)); + cpu_tsc_mark(&temp); + mark_t_subtract(&temp, &end[k], &temp2); + /* If busy loop is too short, increase it */ + if (temp2.sys_clock < tl / 8) + cycles *= 2; + end[k] = temp; + } while (end[k].sys_clock - begin[k].sys_clock < tl); + mark_t_subtract(&end[k], &begin[k], &temp); + results[k] = cpu_clock_by_mark(&temp); + } + if (n == 1) return results[0]; + mdiff = 0x7fffffff; + bi = bj = -1; + for (i = 0; i < 4; i++) { + for (j = i + 1; j < 4; j++) { + diff = results[i] - results[j]; + if (diff < 0) diff = -diff; + if (diff < mdiff) { + mdiff = diff; + bi = i; + bj = j; + } + } + } + if (results[bi] == -1) return -1; + return (results[bi] + results[bj] + _zero) / 2; +} + +int cpu_clock_by_ic(int millis, int runs) +{ + int max_value = 0, cur_value, i, ri, cycles_inner, cycles_outer, c; + struct cpu_id_t* id; + uint64_t t0, t1, tl, hz; + int multiplier_numerator = 1, multiplier_denom = 1; + if (millis <= 0 || runs <= 0) return -2; + id = get_cached_cpuid(); + // if there aren't SSE instructions - we can't run the test at all + if (!id || !id->flags[CPU_FEATURE_SSE]) return -1; + // + if (id->sse_size < 128) { + debugf(1, "SSE execution path is 64-bit\n"); + // on a CPU with half SSE unit length, SSE instructions execute at 0.5 IPC; + // the resulting value must be multiplied by 2: + multiplier_numerator = 2; + } else { + debugf(1, "SSE execution path is 128-bit\n"); + } + // + // on a Bulldozer or later CPU, SSE instructions execute at 1.4 IPC, handle that as well: + if (id->vendor == VENDOR_AMD && id->ext_family >= 21) { + debugf(1, "cpu_clock_by_ic: Bulldozer (or later) detected, dividing result by 1.4\n"); + multiplier_numerator = 5; + multiplier_denom = 7; // multiply by 5/7, to divide by 1.4 + } + // + tl = millis * 125; // (*1000 / 8) + cycles_inner = 128; + cycles_outer = 1; + do { + if (cycles_inner < 1000000000) cycles_inner *= 2; + else cycles_outer *= 2; + sys_precise_clock(&t0); + for (i = 0; i < cycles_outer; i++) + busy_sse_loop(cycles_inner); + sys_precise_clock(&t1); + } while (t1 - t0 < tl); + debugf(2, "inner: %d, outer: %d\n", cycles_inner, cycles_outer); + for (ri = 0; ri < runs; ri++) { + sys_precise_clock(&t0); + c = 0; + do { + c++; + for (i = 0; i < cycles_outer; i++) + busy_sse_loop(cycles_inner); + sys_precise_clock(&t1); + } while (t1 - t0 < tl * (uint64_t) 8); + // cpu_Hz = cycles_inner * cycles_outer * 256 / (t1 - t0) * 1000000 + debugf(2, "c = %d, td = %d\n", c, (int) (t1 - t0)); + hz = ((uint64_t) cycles_inner * (uint64_t) 256 + 12) * + (uint64_t) cycles_outer * (uint64_t) multiplier_numerator * (uint64_t) c * (uint64_t) 1000000 + / ((t1 - t0) * (uint64_t) multiplier_denom); + cur_value = (int) (hz / 1000000); + if (cur_value > max_value) max_value = cur_value; + } + return max_value; +} + +int cpu_clock(void) +{ + int result; + result = cpu_clock_by_os(); + if (result <= 0) + result = cpu_clock_measure(200, 1); + return result; +} diff --git a/contrib/libcpuid/include/cpuid/rdtsc.h b/contrib/libcpuid/include/cpuid/rdtsc.h new file mode 100644 index 00000000000..b4aaf99a570 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/rdtsc.h @@ -0,0 +1,33 @@ +/* + * Copyright 2010 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __RDTSC_H__ +#define __RDTSC_H__ + +void sys_precise_clock(uint64_t *result); +int busy_loop_delay(int milliseconds); + + +#endif /* __RDTSC_H__ */ diff --git a/contrib/libcpuid/include/cpuid/recog_amd.c b/contrib/libcpuid/include/cpuid/recog_amd.c new file mode 100644 index 00000000000..c5390b9fd24 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/recog_amd.c @@ -0,0 +1,469 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include "libcpuid.h" +#include "recog_amd.h" +#include "libcpuid_util.h" + +enum _amd_code_t { + NA, + NO_CODE, + OPTERON_GENERIC, + OPTERON_800, + ATHLON_XP, + ATHLON_XP_M, + ATHLON_XP_M_LV, + ATHLON, + ATHLON_MP, + MOBILE_ATHLON64, + ATHLON_FX, + DURON, + DURON_MP, + MOBILE_DURON, + MOBILE_SEMPRON, + OPTERON_SINGLE, + OPTERON_DUALCORE, + OPTERON_800_DUALCORE, + MOBILE_TURION, + ATHLON_64, + ATHLON_64_FX, + TURION_64, + TURION_X2, + SEMPRON, + M_SEMPRON, + SEMPRON_DUALCORE, + PHENOM, + PHENOM2, + ATHLON_64_X2, + ATHLON_64_X3, + ATHLON_64_X4, + FUSION_C, + FUSION_E, + FUSION_EA, + FUSION_Z, +}; +typedef enum _amd_code_t amd_code_t; + +const struct match_entry_t cpudb_amd[] = { + { -1, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown AMD CPU" }, + + /* 486 and the likes */ + { 4, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown AMD 486" }, + { 4, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "AMD 486DX2" }, + { 4, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "AMD 486DX2WB" }, + { 4, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "AMD 486DX4" }, + { 4, 9, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "AMD 486DX4WB" }, + + /* Pentia clones */ + { 5, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown AMD 586" }, + { 5, 0, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K5" }, + { 5, 1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K5" }, + { 5, 2, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K5" }, + { 5, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K5" }, + + /* The K6 */ + { 5, 6, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K6" }, + { 5, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K6" }, + + { 5, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K6-2" }, + { 5, 9, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K6-III" }, + { 5, 10, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown K6" }, + { 5, 11, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown K6" }, + { 5, 12, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown K6" }, + { 5, 13, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "K6-2+" }, + + /* Athlon et al. */ + { 6, 1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Athlon (Slot-A)" }, + { 6, 2, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Athlon (Slot-A)" }, + { 6, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Duron (Spitfire)" }, + { 6, 4, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Athlon (ThunderBird)" }, + + { 6, 6, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Athlon" }, + { 6, 6, -1, -1, -1, 1, -1, -1, ATHLON , 0, "Athlon (Palomino)" }, + { 6, 6, -1, -1, -1, 1, -1, -1, ATHLON_MP , 0, "Athlon MP (Palomino)" }, + { 6, 6, -1, -1, -1, 1, -1, -1, DURON , 0, "Duron (Palomino)" }, + { 6, 6, -1, -1, -1, 1, -1, -1, ATHLON_XP , 0, "Athlon XP" }, + + { 6, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Athlon XP" }, + { 6, 7, -1, -1, -1, 1, -1, -1, DURON , 0, "Duron (Morgan)" }, + + { 6, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Athlon XP" }, + { 6, 8, -1, -1, -1, 1, -1, -1, ATHLON , 0, "Athlon XP (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, ATHLON_XP , 0, "Athlon XP (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, DURON , 0, "Duron (Applebred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, SEMPRON , 0, "Sempron (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, 128, -1, SEMPRON , 0, "Sempron (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, 256, -1, SEMPRON , 0, "Sempron (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, ATHLON_MP , 0, "Athlon MP (Thoroughbred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, ATHLON_XP_M , 0, "Mobile Athlon (T-Bred)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, ATHLON_XP_M_LV , 0, "Mobile Athlon (T-Bred)" }, + + { 6, 10, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Athlon XP (Barton)" }, + { 6, 10, -1, -1, -1, 1, 512, -1, ATHLON_XP , 0, "Athlon XP (Barton)" }, + { 6, 10, -1, -1, -1, 1, 512, -1, SEMPRON , 0, "Sempron (Barton)" }, + { 6, 10, -1, -1, -1, 1, 256, -1, SEMPRON , 0, "Sempron (Thorton)" }, + { 6, 10, -1, -1, -1, 1, 256, -1, ATHLON_XP , 0, "Athlon XP (Thorton)" }, + { 6, 10, -1, -1, -1, 1, -1, -1, ATHLON_MP , 0, "Athlon MP (Barton)" }, + { 6, 10, -1, -1, -1, 1, -1, -1, ATHLON_XP_M , 0, "Mobile Athlon (Barton)" }, + { 6, 10, -1, -1, -1, 1, -1, -1, ATHLON_XP_M_LV , 0, "Mobile Athlon (Barton)" }, + + /* K8 Architecture */ + { 15, -1, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Unknown K8" }, + { 15, -1, -1, 16, -1, 1, -1, -1, NO_CODE , 0, "Unknown K9" }, + + { 15, -1, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Unknown A64" }, + { 15, -1, -1, 15, -1, 1, -1, -1, OPTERON_SINGLE , 0, "Opteron" }, + { 15, -1, -1, 15, -1, 2, -1, -1, OPTERON_DUALCORE , 0, "Opteron (Dual Core)" }, + { 15, 3, -1, 15, -1, 1, -1, -1, OPTERON_SINGLE , 0, "Opteron" }, + { 15, 3, -1, 15, -1, 2, -1, -1, OPTERON_DUALCORE , 0, "Opteron (Dual Core)" }, + { 15, -1, -1, 15, -1, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (512K)" }, + { 15, -1, -1, 15, -1, 1, 1024, -1, ATHLON_64 , 0, "Athlon 64 (1024K)" }, + { 15, -1, -1, 15, -1, 1, -1, -1, ATHLON_FX , 0, "Athlon FX" }, + { 15, -1, -1, 15, -1, 1, -1, -1, ATHLON_64_FX , 0, "Athlon 64 FX" }, + { 15, -1, -1, 15, -1, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (512K)" }, + { 15, -1, -1, 15, -1, 2, 1024, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (1024K)" }, + { 15, -1, -1, 15, -1, 1, 512, -1, TURION_64 , 0, "Turion 64 (512K)" }, + { 15, -1, -1, 15, -1, 1, 1024, -1, TURION_64 , 0, "Turion 64 (1024K)" }, + { 15, -1, -1, 15, -1, 2, 512, -1, TURION_X2 , 0, "Turion 64 X2 (512K)" }, + { 15, -1, -1, 15, -1, 2, 1024, -1, TURION_X2 , 0, "Turion 64 X2 (1024K)" }, + { 15, -1, -1, 15, -1, 1, 128, -1, SEMPRON , 0, "A64 Sempron (128K)" }, + { 15, -1, -1, 15, -1, 1, 256, -1, SEMPRON , 0, "A64 Sempron (256K)" }, + { 15, -1, -1, 15, -1, 1, 512, -1, SEMPRON , 0, "A64 Sempron (512K)" }, + { 15, -1, -1, 15, 0x4f, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Orleans/512K)" }, + { 15, -1, -1, 15, 0x5f, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Orleans/512K)" }, + { 15, -1, -1, 15, 0x2f, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Venice/512K)" }, + { 15, -1, -1, 15, 0x2c, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Venice/512K)" }, + { 15, -1, -1, 15, 0x1f, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Winchester/512K)" }, + { 15, -1, -1, 15, 0x0c, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (Newcastle/512K)" }, + { 15, -1, -1, 15, 0x27, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (San Diego/512K)" }, + { 15, -1, -1, 15, 0x37, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (San Diego/512K)" }, + { 15, -1, -1, 15, 0x04, 1, 512, -1, ATHLON_64 , 0, "Athlon 64 (ClawHammer/512K)" }, + + { 15, -1, -1, 15, 0x5f, 1, 1024, -1, ATHLON_64 , 0, "Athlon 64 (Orleans/1024K)" }, + { 15, -1, -1, 15, 0x27, 1, 1024, -1, ATHLON_64 , 0, "Athlon 64 (San Diego/1024K)" }, + { 15, -1, -1, 15, 0x04, 1, 1024, -1, ATHLON_64 , 0, "Athlon 64 (ClawHammer/1024K)" }, + + { 15, -1, -1, 15, 0x4b, 2, 256, -1, SEMPRON_DUALCORE , 0, "Athlon 64 X2 (Windsor/256K)" }, + + { 15, -1, -1, 15, 0x23, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Toledo/512K)" }, + { 15, -1, -1, 15, 0x4b, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Windsor/512K)" }, + { 15, -1, -1, 15, 0x43, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Windsor/512K)" }, + { 15, -1, -1, 15, 0x6b, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Brisbane/512K)" }, + { 15, -1, -1, 15, 0x2b, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Manchester/512K)"}, + + { 15, -1, -1, 15, 0x23, 2, 1024, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Toledo/1024K)" }, + { 15, -1, -1, 15, 0x43, 2, 1024, -1, ATHLON_64_X2 , 0, "Athlon 64 X2 (Windsor/1024K)" }, + + { 15, -1, -1, 15, 0x08, 1, 128, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Dublin/128K)"}, + { 15, -1, -1, 15, 0x08, 1, 256, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Dublin/256K)"}, + { 15, -1, -1, 15, 0x0c, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Paris)" }, + { 15, -1, -1, 15, 0x1c, 1, 128, -1, SEMPRON , 0, "Sempron 64 (Palermo/128K)" }, + { 15, -1, -1, 15, 0x1c, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Palermo/256K)" }, + { 15, -1, -1, 15, 0x1c, 1, 128, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Sonora/128K)"}, + { 15, -1, -1, 15, 0x1c, 1, 256, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Sonora/256K)"}, + { 15, -1, -1, 15, 0x2c, 1, 128, -1, SEMPRON , 0, "Sempron 64 (Palermo/128K)" }, + { 15, -1, -1, 15, 0x2c, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Palermo/256K)" }, + { 15, -1, -1, 15, 0x2c, 1, 128, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Albany/128K)"}, + { 15, -1, -1, 15, 0x2c, 1, 256, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Albany/256K)"}, + { 15, -1, -1, 15, 0x2f, 1, 128, -1, SEMPRON , 0, "Sempron 64 (Palermo/128K)" }, + { 15, -1, -1, 15, 0x2f, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Palermo/256K)" }, + { 15, -1, -1, 15, 0x4f, 1, 128, -1, SEMPRON , 0, "Sempron 64 (Manila/128K)" }, + { 15, -1, -1, 15, 0x4f, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Manila/256K)" }, + { 15, -1, -1, 15, 0x5f, 1, 128, -1, SEMPRON , 0, "Sempron 64 (Manila/128K)" }, + { 15, -1, -1, 15, 0x5f, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Manila/256K)" }, + { 15, -1, -1, 15, 0x6b, 2, 256, -1, SEMPRON , 0, "Sempron 64 Dual (Sherman/256K)"}, + { 15, -1, -1, 15, 0x6b, 2, 512, -1, SEMPRON , 0, "Sempron 64 Dual (Sherman/512K)"}, + { 15, -1, -1, 15, 0x7f, 1, 256, -1, SEMPRON , 0, "Sempron 64 (Sparta/256K)" }, + { 15, -1, -1, 15, 0x7f, 1, 512, -1, SEMPRON , 0, "Sempron 64 (Sparta/512K)" }, + { 15, -1, -1, 15, 0x4c, 1, 256, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Keene/256K)"}, + { 15, -1, -1, 15, 0x4c, 1, 512, -1, M_SEMPRON , 0, "Mobile Sempron 64 (Keene/512K)"}, + { 15, -1, -1, 15, -1, 2, -1, -1, SEMPRON_DUALCORE , 0, "Sempron Dual Core" }, + + { 15, -1, -1, 15, 0x24, 1, 512, -1, TURION_64 , 0, "Turion 64 (Lancaster/512K)" }, + { 15, -1, -1, 15, 0x24, 1, 1024, -1, TURION_64 , 0, "Turion 64 (Lancaster/1024K)" }, + { 15, -1, -1, 15, 0x48, 2, 256, -1, TURION_X2 , 0, "Turion X2 (Taylor)" }, + { 15, -1, -1, 15, 0x48, 2, 512, -1, TURION_X2 , 0, "Turion X2 (Trinidad)" }, + { 15, -1, -1, 15, 0x4c, 1, 512, -1, TURION_64 , 0, "Turion 64 (Richmond)" }, + { 15, -1, -1, 15, 0x68, 2, 256, -1, TURION_X2 , 0, "Turion X2 (Tyler/256K)" }, + { 15, -1, -1, 15, 0x68, 2, 512, -1, TURION_X2 , 0, "Turion X2 (Tyler/512K)" }, + { 15, -1, -1, 17, 3, 2, 512, -1, TURION_X2 , 0, "Turion X2 (Griffin/512K)" }, + { 15, -1, -1, 17, 3, 2, 1024, -1, TURION_X2 , 0, "Turion X2 (Griffin/1024K)" }, + + /* K9 Architecture */ + { 15, -1, -1, 16, -1, 1, -1, -1, PHENOM , 0, "Unknown AMD Phenom" }, + { 15, 2, -1, 16, -1, 1, -1, -1, PHENOM , 0, "Phenom" }, + { 15, 2, -1, 16, -1, 3, -1, -1, PHENOM , 0, "Phenom X3 (Toliman)" }, + { 15, 2, -1, 16, -1, 4, -1, -1, PHENOM , 0, "Phenom X4 (Agena)" }, + { 15, 2, -1, 16, -1, 3, 512, -1, PHENOM , 0, "Phenom X3 (Toliman/256K)" }, + { 15, 2, -1, 16, -1, 3, 512, -1, PHENOM , 0, "Phenom X3 (Toliman/512K)" }, + { 15, 2, -1, 16, -1, 4, 128, -1, PHENOM , 0, "Phenom X4 (Agena/128K)" }, + { 15, 2, -1, 16, -1, 4, 256, -1, PHENOM , 0, "Phenom X4 (Agena/256K)" }, + { 15, 2, -1, 16, -1, 4, 512, -1, PHENOM , 0, "Phenom X4 (Agena/512K)" }, + { 15, 2, -1, 16, -1, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon X2 (Kuma)" }, + /* Phenom II derivates: */ + { 15, 4, -1, 16, -1, 4, -1, -1, NO_CODE , 0, "Phenom (Deneb-based)" }, + { 15, 4, -1, 16, -1, 1, 1024, -1, SEMPRON , 0, "Sempron (Sargas)" }, + { 15, 4, -1, 16, -1, 2, 512, -1, PHENOM2 , 0, "Phenom II X2 (Callisto)" }, + { 15, 4, -1, 16, -1, 3, 512, -1, PHENOM2 , 0, "Phenom II X3 (Heka)" }, + { 15, 4, -1, 16, -1, 4, 512, -1, PHENOM2 , 0, "Phenom II X4" }, + { 15, 4, -1, 16, 4, 4, 512, -1, PHENOM2 , 0, "Phenom II X4 (Deneb)" }, + { 15, 5, -1, 16, 5, 4, 512, -1, PHENOM2 , 0, "Phenom II X4 (Deneb)" }, + { 15, 4, -1, 16, 10, 4, 512, -1, PHENOM2 , 0, "Phenom II X4 (Zosma)" }, + { 15, 4, -1, 16, 10, 6, 512, -1, PHENOM2 , 0, "Phenom II X6 (Thuban)" }, + + { 15, 4, -1, 16, -1, 2, 1024, -1, ATHLON_64_X2 , 0, "Athlon II X2 (Regor)" }, + { 15, 4, -1, 16, -1, 2, 512, -1, ATHLON_64_X2 , 0, "Athlon II X2 (Regor)" }, + { 15, 5, -1, 16, 5, 3, 512, -1, ATHLON_64_X3 , 0, "Athlon II X3 (Rana)" }, + { 15, 5, -1, 16, 5, 4, 512, -1, ATHLON_64_X4 , 0, "Athlon II X4 (Propus)" }, + /* 2011 CPUs with AMD fusion: */ + { 15, -1, -1, 20, 1, 1, 512, -1, FUSION_C , 0, "Brazos Ontario" }, + { 15, -1, -1, 20, 1, 2, 512, -1, FUSION_C , 0, "Brazos Ontario (Dual-core)" }, + { 15, -1, -1, 20, 1, 1, 512, -1, FUSION_E , 0, "Brazos Zacate" }, + { 15, -1, -1, 20, 1, 2, 512, -1, FUSION_E , 0, "Brazos Zacate (Dual-core)" }, + { 15, -1, -1, 20, 1, 1, 512, -1, FUSION_Z , 0, "Brazos Desna" }, + { 15, -1, -1, 18, 1, 2, 512, -1, FUSION_EA , 0, "Llano X2" }, + { 15, -1, -1, 18, 1, 2, 1024, -1, FUSION_EA , 0, "Llano X2" }, + { 15, -1, -1, 18, 1, 3, 1024, -1, FUSION_EA , 0, "Llano X3" }, + { 15, -1, -1, 18, 1, 4, 1024, -1, FUSION_EA , 0, "Llano X4" }, + + /* Newer Opterons: */ + { 15, 9, -1, 16, 9, 8, -1, -1, OPTERON_GENERIC , 0, "Magny-Cours Opteron" }, + + /* Bulldozer CPUs: */ + { 15, -1, -1, 21, 1, 4, 2048, -1, NO_CODE , 0, "Bulldozer X2" }, + { 15, -1, -1, 21, 1, 6, 2048, -1, NO_CODE , 0, "Bulldozer X3" }, + { 15, -1, -1, 21, 1, 8, 2048, -1, NO_CODE , 0, "Bulldozer X4" }, + { 15, -1, -1, 21, 2, 4, 2048, -1, NO_CODE , 0, "Vishera X2" }, + { 15, -1, -1, 21, 2, 6, 2048, -1, NO_CODE , 0, "Vishera X3" }, + { 15, -1, -1, 21, 2, 8, 2048, -1, NO_CODE , 0, "Vishera X4" }, +}; + + +static void load_amd_features(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + const struct feature_map_t matchtable_edx81[] = { + { 20, CPU_FEATURE_NX }, + { 22, CPU_FEATURE_MMXEXT }, + { 25, CPU_FEATURE_FXSR_OPT }, + { 30, CPU_FEATURE_3DNOWEXT }, + { 31, CPU_FEATURE_3DNOW }, + }; + const struct feature_map_t matchtable_ecx81[] = { + { 1, CPU_FEATURE_CMP_LEGACY }, + { 2, CPU_FEATURE_SVM }, + { 5, CPU_FEATURE_ABM }, + { 6, CPU_FEATURE_SSE4A }, + { 7, CPU_FEATURE_MISALIGNSSE }, + { 8, CPU_FEATURE_3DNOWPREFETCH }, + { 9, CPU_FEATURE_OSVW }, + { 10, CPU_FEATURE_IBS }, + { 11, CPU_FEATURE_XOP }, + { 12, CPU_FEATURE_SKINIT }, + { 13, CPU_FEATURE_WDT }, + { 16, CPU_FEATURE_FMA4 }, + }; + const struct feature_map_t matchtable_edx87[] = { + { 0, CPU_FEATURE_TS }, + { 1, CPU_FEATURE_FID }, + { 2, CPU_FEATURE_VID }, + { 3, CPU_FEATURE_TTP }, + { 4, CPU_FEATURE_TM_AMD }, + { 5, CPU_FEATURE_STC }, + { 6, CPU_FEATURE_100MHZSTEPS }, + { 7, CPU_FEATURE_HWPSTATE }, + /* id 8 is handled in common */ + { 9, CPU_FEATURE_CPB }, + { 10, CPU_FEATURE_APERFMPERF }, + { 11, CPU_FEATURE_PFI }, + { 12, CPU_FEATURE_PA }, + }; + if (raw->ext_cpuid[0][0] >= 0x80000001) { + match_features(matchtable_edx81, COUNT_OF(matchtable_edx81), raw->ext_cpuid[1][3], data); + match_features(matchtable_ecx81, COUNT_OF(matchtable_ecx81), raw->ext_cpuid[1][2], data); + } + if (raw->ext_cpuid[0][0] >= 0x80000001) + match_features(matchtable_edx87, COUNT_OF(matchtable_edx87), raw->ext_cpuid[7][3], data); + if (raw->ext_cpuid[0][0] >= 0x8000001a) { + /* We have the extended info about SSE unit size */ + data->detection_hints[CPU_HINT_SSE_SIZE_AUTH] = 1; + data->sse_size = (raw->ext_cpuid[0x1a][0] & 1) ? 128 : 64; + } +} + +static void decode_amd_cache_info(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + int l3_result; + const int assoc_table[16] = { + 0, 1, 2, 0, 4, 0, 8, 0, 16, 0, 32, 48, 64, 92, 128, 255 + }; + unsigned n = raw->ext_cpuid[0][0]; + + if (n >= 0x80000005) { + data->l1_data_cache = (raw->ext_cpuid[5][2] >> 24) & 0xff; + data->l1_assoc = (raw->ext_cpuid[5][2] >> 16) & 0xff; + data->l1_cacheline = (raw->ext_cpuid[5][2]) & 0xff; + data->l1_instruction_cache = (raw->ext_cpuid[5][3] >> 24) & 0xff; + } + if (n >= 0x80000006) { + data->l2_cache = (raw->ext_cpuid[6][2] >> 16) & 0xffff; + data->l2_assoc = assoc_table[(raw->ext_cpuid[6][2] >> 12) & 0xf]; + data->l2_cacheline = (raw->ext_cpuid[6][2]) & 0xff; + + l3_result = (raw->ext_cpuid[6][3] >> 18); + if (l3_result > 0) { + l3_result = 512 * l3_result; /* AMD spec says it's a range, + but we take the lower bound */ + data->l3_cache = l3_result; + data->l3_assoc = assoc_table[(raw->ext_cpuid[6][3] >> 12) & 0xf]; + data->l3_cacheline = (raw->ext_cpuid[6][3]) & 0xff; + } else { + data->l3_cache = 0; + } + } +} + +static void decode_amd_number_of_cores(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + int logical_cpus = -1, num_cores = -1; + + if (raw->basic_cpuid[0][0] >= 1) { + logical_cpus = (raw->basic_cpuid[1][1] >> 16) & 0xff; + if (raw->ext_cpuid[0][0] >= 8) { + num_cores = 1 + (raw->ext_cpuid[8][2] & 0xff); + } + } + if (data->flags[CPU_FEATURE_HT]) { + if (num_cores > 1) { + data->num_cores = num_cores; + data->num_logical_cpus = logical_cpus; + } else { + data->num_cores = 1; + data->num_logical_cpus = (logical_cpus >= 2 ? logical_cpus : 2); + } + } else { + data->num_cores = data->num_logical_cpus = 1; + } +} + +static int amd_has_turion_modelname(const char *bs) +{ + /* We search for something like TL-60. Ahh, I miss regexes...*/ + int i, l, k; + char code[3] = {0}; + const char* codes[] = { "ML", "MT", "MK", "TK", "TL", "RM", "ZM", "" }; + l = (int) strlen(bs); + for (i = 3; i < l - 2; i++) { + if (bs[i] == '-' && + isupper(bs[i-1]) && isupper(bs[i-2]) && !isupper(bs[i-3]) && + isdigit(bs[i+1]) && isdigit(bs[i+2]) && !isdigit(bs[i+3])) + { + code[0] = bs[i-2]; + code[1] = bs[i-1]; + for (k = 0; codes[k][0]; k++) + if (!strcmp(codes[k], code)) return 1; + } + } + return 0; +} + +static amd_code_t decode_amd_codename_part1(const char *bs) +{ + int is_dual = 0, is_quad = 0, is_tri = 0; + if (strstr(bs, "Dual Core") || + strstr(bs, "Dual-Core") || + strstr(bs, " X2 ")) + is_dual = 1; + if (strstr(bs, " X4 ")) is_quad = 1; + if (strstr(bs, " X3 ")) is_tri = 1; + if (strstr(bs, "Opteron")) { + return is_dual ? OPTERON_DUALCORE : OPTERON_SINGLE; + } + if (strstr(bs, "Phenom")) { + if (strstr(bs, "II")) return PHENOM2; + else return PHENOM; + } + if (amd_has_turion_modelname(bs)) { + return is_dual ? TURION_X2 : TURION_64; + } + if (strstr(bs, "Athlon(tm) 64 FX")) return ATHLON_64_FX; + if (strstr(bs, "Athlon(tm) FX")) return ATHLON_FX; + if (strstr(bs, "Athlon(tm) 64") || strstr(bs, "Athlon(tm) II X") || match_pattern(bs, "Athlon(tm) X#")) { + if (is_quad) return ATHLON_64_X4; + if (is_dual) return ATHLON_64_X2; + if (is_tri) return ATHLON_64_X3; + return ATHLON_64; + } + if (strstr(bs, "Turion")) { + return is_dual ? TURION_X2 : TURION_64; + } + + if (strstr(bs, "mobile") || strstr(bs, "Mobile")) { + if (strstr(bs, "Athlon(tm) XP-M (LV)")) return ATHLON_XP_M_LV; + if (strstr(bs, "Athlon(tm) XP")) return ATHLON_XP_M; + if (strstr(bs, "Sempron(tm)")) return M_SEMPRON; + if (strstr(bs, "Athlon")) return MOBILE_ATHLON64; + if (strstr(bs, "Duron")) return MOBILE_DURON; + + } else { + if (strstr(bs, "Athlon(tm) XP")) return ATHLON_XP; + if (strstr(bs, "Athlon(tm) MP")) return ATHLON_MP; + if (strstr(bs, "Sempron(tm)")) return SEMPRON; + if (strstr(bs, "Duron")) return DURON; + if (strstr(bs, "Athlon")) return ATHLON; + } + if (match_pattern(bs, "C-##")) return FUSION_C; + if (match_pattern(bs, "E-###")) return FUSION_E; + if (match_pattern(bs, "Z-##")) return FUSION_Z; + if (match_pattern(bs, "E#-####") || match_pattern(bs, "A#-####")) return FUSION_EA; + + return NO_CODE; +} + +static void decode_amd_codename(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + amd_code_t code = decode_amd_codename_part1(data->brand_str); + + if (code == ATHLON_64_X2 && data->l2_cache < 512) + code = SEMPRON_DUALCORE; + match_cpu_codename(cpudb_amd, COUNT_OF(cpudb_amd), data, code, 0); +} + +int cpuid_identify_amd(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + load_amd_features(raw, data); + decode_amd_cache_info(raw, data); + decode_amd_number_of_cores(raw, data); + decode_amd_codename(raw, data); + return 0; +} + +void cpuid_get_list_amd(struct cpu_list_t* list) +{ + generic_get_cpu_list(cpudb_amd, COUNT_OF(cpudb_amd), list); +} diff --git a/contrib/libcpuid/include/cpuid/recog_amd.h b/contrib/libcpuid/include/cpuid/recog_amd.h new file mode 100644 index 00000000000..e0f3b61f727 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/recog_amd.h @@ -0,0 +1,32 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __RECOG_AMD_H__ +#define __RECOG_AMD_H__ + +int cpuid_identify_amd(struct cpu_raw_data_t* raw, struct cpu_id_t* data); +void cpuid_get_list_amd(struct cpu_list_t* list); + +#endif /* __RECOG_AMD_H__ */ diff --git a/contrib/libcpuid/include/cpuid/recog_intel.c b/contrib/libcpuid/include/cpuid/recog_intel.c new file mode 100644 index 00000000000..625b2777f51 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/recog_intel.c @@ -0,0 +1,791 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include +#include "libcpuid.h" +#include "recog_intel.h" +#include "libcpuid_util.h" + + +enum _intel_code_t { + NA, + NO_CODE, + PENTIUM = 10, + MOBILE_PENTIUM, + + XEON = 20, + XEON_IRWIN, + XEONMP, + XEON_POTOMAC, + XEON_I7, + XEON_GAINESTOWN, + XEON_WESTMERE, + + MOBILE_PENTIUM_M = 30, + CELERON, + MOBILE_CELERON, + NOT_CELERON, + + + CORE_SOLO = 40, + MOBILE_CORE_SOLO, + CORE_DUO, + MOBILE_CORE_DUO, + + WOLFDALE = 50, + MEROM, + PENRYN, + QUAD_CORE, + DUAL_CORE_HT, + QUAD_CORE_HT, + MORE_THAN_QUADCORE, + PENTIUM_D, + + ATOM = 60, + ATOM_SILVERTHORNE, + ATOM_DIAMONDVILLE, + ATOM_PINEVIEW, + ATOM_CEDARVIEW, + + CORE_I3 = 70, + CORE_I5, + CORE_I7, + CORE_IVY3, /* 22nm Core-iX */ + CORE_IVY5, + CORE_IVY7, + CORE_HASWELL3, /* 22nm Core-iX, Haswell */ + CORE_HASWELL5, + CORE_HASWELL7, +}; +typedef enum _intel_code_t intel_code_t; + +enum _intel_model_t { + UNKNOWN = -1, + _3000 = 100, + _3100, + _3200, + X3200, + _3300, + X3300, + _5100, + _5200, + _5300, + _5400, + _2xxx, /* Core i[357] 2xxx */ + _3xxx, /* Core i[357] 3xxx */ +}; +typedef enum _intel_model_t intel_model_t; + +const struct match_entry_t cpudb_intel[] = { + { -1, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Intel CPU" }, + + /* i486 */ + { 4, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown i486" }, + { 4, 0, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX-25/33" }, + { 4, 1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX-50" }, + { 4, 2, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 SX" }, + { 4, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX2" }, + { 4, 4, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 SL" }, + { 4, 5, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 SX2" }, + { 4, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX2 WriteBack" }, + { 4, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX4" }, + { 4, 9, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "i486 DX4 WriteBack" }, + + /* All Pentia: + Pentium 1 */ + { 5, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Pentium" }, + { 5, 0, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium A-Step" }, + { 5, 1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium 1 (0.8u)" }, + { 5, 2, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium 1 (0.35u)" }, + { 5, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium OverDrive" }, + { 5, 4, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium 1 (0.35u)" }, + { 5, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium 1 (0.35u)" }, + { 5, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium MMX (0.25u)" }, + + /* Pentium 2 / 3 / M / Conroe / whatsnext - all P6 based. */ + { 6, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown P6" }, + { 6, 0, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium Pro" }, + { 6, 1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium Pro" }, + { 6, 3, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium II (Klamath)" }, + { 6, 5, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium II (Deschutes)" }, + { 6, 5, -1, -1, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile Pentium II (Tonga)"}, + { 6, 6, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium II (Dixon)" }, + + { 6, 3, -1, -1, -1, 1, -1, -1, XEON , 0, "P-II Xeon" }, + { 6, 5, -1, -1, -1, 1, -1, -1, XEON , 0, "P-II Xeon" }, + { 6, 6, -1, -1, -1, 1, -1, -1, XEON , 0, "P-II Xeon" }, + + { 6, 5, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-II Celeron (no L2)" }, + { 6, 6, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-II Celeron (128K)" }, + + /* -------------------------------------------------- */ + + { 6, 7, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium III (Katmai)" }, + { 6, 8, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium III (Coppermine)"}, + { 6, 10, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium III (Coppermine)"}, + { 6, 11, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Pentium III (Tualatin)" }, + + { 6, 7, -1, -1, -1, 1, -1, -1, XEON , 0, "P-III Xeon" }, + { 6, 8, -1, -1, -1, 1, -1, -1, XEON , 0, "P-III Xeon" }, + { 6, 10, -1, -1, -1, 1, -1, -1, XEON , 0, "P-III Xeon" }, + { 6, 11, -1, -1, -1, 1, -1, -1, XEON , 0, "P-III Xeon" }, + + { 6, 7, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-III Celeron" }, + { 6, 8, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-III Celeron" }, + { 6, 10, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-III Celeron" }, + { 6, 11, -1, -1, -1, 1, -1, -1, CELERON , 0, "P-III Celeron" }, + + /* Netburst based (Pentium 4 and later) + classic P4s */ + { 15, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Pentium 4" }, + { 15, -1, -1, 15, -1, 1, -1, -1, CELERON , 0, "Unknown P-4 Celeron" }, + { 15, -1, -1, 15, -1, 1, -1, -1, XEON , 0, "Unknown Xeon" }, + + { 15, 0, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Willamette)" }, + { 15, 1, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Willamette)" }, + { 15, 2, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Northwood)" }, + { 15, 3, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Prescott)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Prescott)" }, + { 15, 6, -1, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium 4 (Cedar Mill)" }, + { 15, 0, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Willamette)" }, + { 15, 1, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Willamette)" }, + { 15, 2, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Northwood)" }, + { 15, 3, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Prescott)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Prescott)" }, + { 15, 6, -1, 15, -1, 1, -1, -1, MOBILE_PENTIUM , 0, "Mobile P-4 (Cedar Mill)" }, + + /* server CPUs */ + { 15, 0, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Foster)" }, + { 15, 1, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Foster)" }, + { 15, 2, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Prestonia)" }, + { 15, 2, -1, 15, -1, 1, -1, -1, XEONMP , 0, "Xeon (Gallatin)" }, + { 15, 3, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Nocona)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Nocona)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, XEON_IRWIN , 0, "Xeon (Irwindale)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, XEONMP , 0, "Xeon (Cranford)" }, + { 15, 4, -1, 15, -1, 1, -1, -1, XEON_POTOMAC , 0, "Xeon (Potomac)" }, + { 15, 6, -1, 15, -1, 1, -1, -1, XEON , 0, "Xeon (Dempsey)" }, + + /* Pentium Ds */ + { 15, 4, 4, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium D" }, + { 15, 4, -1, 15, -1, 1, -1, -1, PENTIUM_D , 0, "Pentium D" }, + { 15, 4, 7, 15, -1, 1, -1, -1, NO_CODE , 0, "Pentium D" }, + { 15, 6, -1, 15, -1, 1, -1, -1, PENTIUM_D , 0, "Pentium D" }, + + /* Celeron and Celeron Ds */ + { 15, 1, -1, 15, -1, 1, -1, -1, CELERON , 0, "P-4 Celeron (128K)" }, + { 15, 2, -1, 15, -1, 1, -1, -1, CELERON , 0, "P-4 Celeron (128K)" }, + { 15, 3, -1, 15, -1, 1, -1, -1, CELERON , 0, "Celeron D" }, + { 15, 4, -1, 15, -1, 1, -1, -1, CELERON , 0, "Celeron D" }, + { 15, 6, -1, 15, -1, 1, -1, -1, CELERON , 0, "Celeron D" }, + + /* -------------------------------------------------- */ + /* Intel Core microarchitecture - P6-based */ + + { 6, 9, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Pentium M" }, + { 6, 9, -1, -1, -1, 1, -1, -1, MOBILE_PENTIUM_M , 0, "Unknown Pentium M" }, + { 6, 9, -1, -1, -1, 1, -1, -1, PENTIUM , 0, "Pentium M (Banias)" }, + { 6, 9, -1, -1, -1, 1, -1, -1, MOBILE_PENTIUM_M , 0, "Pentium M (Banias)" }, + { 6, 9, -1, -1, -1, 1, -1, -1, CELERON , 0, "Celeron M" }, + { 6, 13, -1, -1, -1, 1, -1, -1, PENTIUM , 0, "Pentium M (Dothan)" }, + { 6, 13, -1, -1, -1, 1, -1, -1, MOBILE_PENTIUM_M , 0, "Pentium M (Dothan)" }, + { 6, 13, -1, -1, -1, 1, -1, -1, CELERON , 0, "Celeron M" }, + + { 6, 12, -1, -1, -1, -1, -1, -1, ATOM , 0, "Unknown Atom" }, + { 6, 12, -1, -1, -1, -1, -1, -1, ATOM_DIAMONDVILLE , 0, "Atom (Diamondville)" }, + { 6, 12, -1, -1, -1, -1, -1, -1, ATOM_SILVERTHORNE , 0, "Atom (Silverthorne)" }, + { 6, 12, -1, -1, -1, -1, -1, -1, ATOM_CEDARVIEW , 0, "Atom (Cedarview)" }, + { 6, 6, -1, -1, -1, -1, -1, -1, ATOM_CEDARVIEW , 0, "Atom (Cedarview)" }, + { 6, 12, -1, -1, -1, -1, -1, -1, ATOM_PINEVIEW , 0, "Atom (Pineview)" }, + + /* -------------------------------------------------- */ + + { 6, 14, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Yonah" }, + { 6, 14, -1, -1, -1, 1, -1, -1, CORE_SOLO , 0, "Yonah (Core Solo)" }, + { 6, 14, -1, -1, -1, 2, -1, -1, CORE_DUO , 0, "Yonah (Core Duo)" }, + { 6, 14, -1, -1, -1, 1, -1, -1, MOBILE_CORE_SOLO , 0, "Yonah (Core Solo)" }, + { 6, 14, -1, -1, -1, 2, -1, -1, MOBILE_CORE_DUO , 0, "Yonah (Core Duo)" }, + { 6, 14, -1, -1, -1, 1, -1, -1, CORE_SOLO , 0, "Yonah (Core Solo)" }, + + { 6, 15, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Unknown Core 2" }, + { 6, 15, -1, -1, -1, 2, 4096, -1, CORE_DUO , 0, "Conroe (Core 2 Duo)" }, + { 6, 15, -1, -1, -1, 2, 1024, -1, CORE_DUO , 0, "Conroe (Core 2 Duo) 1024K" }, + { 6, 15, -1, -1, -1, 2, 512, -1, CORE_DUO , 0, "Conroe (Core 2 Duo) 512K" }, + { 6, 15, -1, -1, -1, 4, -1, -1, QUAD_CORE , 0, "Kentsfield (Core 2 Quad)" }, + { 6, 15, -1, -1, -1, 4, 4096, -1, QUAD_CORE , 0, "Kentsfield (Core 2 Quad)" }, + { 6, 15, -1, -1, -1, 400, -1, -1, MORE_THAN_QUADCORE, 0, "More than quad-core" }, + { 6, 15, -1, -1, -1, 2, 2048, -1, CORE_DUO , 0, "Allendale (Core 2 Duo)" }, + { 6, 15, -1, -1, -1, 2, -1, -1, MOBILE_CORE_DUO , 0, "Merom (Core 2 Duo)" }, + { 6, 15, -1, -1, -1, 2, 2048, -1, MEROM , 0, "Merom (Core 2 Duo) 2048K" }, + { 6, 15, -1, -1, -1, 2, 4096, -1, MEROM , 0, "Merom (Core 2 Duo) 4096K" }, + + { 6, 15, -1, -1, 15, 1, -1, -1, CELERON , 0, "Conroe-L (Celeron)" }, + { 6, 6, -1, -1, 22, 1, -1, -1, CELERON , 0, "Conroe-L (Celeron)" }, + { 6, 15, -1, -1, 15, 2, -1, -1, CELERON , 0, "Conroe-L (Allendale)" }, + { 6, 6, -1, -1, 22, 2, -1, -1, CELERON , 0, "Conroe-L (Allendale)" }, + + + { 6, 6, -1, -1, 22, 1, -1, -1, NO_CODE , 0, "Unknown Core ?" }, + { 6, 7, -1, -1, 23, 1, -1, -1, NO_CODE , 0, "Unknown Core ?" }, + { 6, 6, -1, -1, 22, 400, -1, -1, MORE_THAN_QUADCORE, 0, "More than quad-core" }, + { 6, 7, -1, -1, 23, 400, -1, -1, MORE_THAN_QUADCORE, 0, "More than quad-core" }, + + { 6, 7, -1, -1, 23, 1, -1, -1, CORE_SOLO , 0, "Unknown Core 45nm" }, + { 6, 7, -1, -1, 23, 1, -1, -1, CORE_DUO , 0, "Unknown Core 45nm" }, + { 6, 7, -1, -1, 23, 2, 1024, -1, WOLFDALE , 0, "Celeron Wolfdale 1M" }, + { 6, 7, -1, -1, 23, 2, 2048, -1, WOLFDALE , 0, "Wolfdale (Core 2 Duo) 2M" }, + { 6, 7, -1, -1, 23, 2, 3072, -1, WOLFDALE , 0, "Wolfdale (Core 2 Duo) 3M" }, + { 6, 7, -1, -1, 23, 2, 6144, -1, WOLFDALE , 0, "Wolfdale (Core 2 Duo) 6M" }, + { 6, 7, -1, -1, 23, 1, -1, -1, MOBILE_CORE_DUO , 0, "Penryn (Core 2 Duo)" }, + { 6, 7, -1, -1, 23, 2, 3072, -1, PENRYN , 0, "Penryn (Core 2 Duo) 3M" }, + { 6, 7, -1, -1, 23, 2, 6144, -1, PENRYN , 0, "Penryn (Core 2 Duo) 6M" }, + { 6, 7, -1, -1, 23, 4, 2048, -1, QUAD_CORE , 0, "Yorkfield (Core 2 Quad) 2M"}, + { 6, 7, -1, -1, 23, 4, 3072, -1, QUAD_CORE , 0, "Yorkfield (Core 2 Quad) 3M"}, + { 6, 7, -1, -1, 23, 4, 6144, -1, QUAD_CORE , 0, "Yorkfield (Core 2 Quad) 6M"}, + + { 6, 5, -1, -1, 37, 2, -1, -1, NO_CODE , 0, "Unknown Core i3/i5 CPU" }, + { 6, 5, -1, -1, 37, 2, -1, 4096, CORE_I7 , 0, "Arrandale (Core i7)" }, + { 6, 5, -1, -1, 37, 2, -1, 3072, CORE_I5 , 0, "Arrandale (Core i5)" }, + { 6, 5, -1, -1, 37, 2, -1, 4096, CORE_I5 , 0, "Clarkdale (Core i5)" }, + { 6, 5, -1, -1, 37, 4, -1, 8192, CORE_I5 , 0, "Lynnfield (Core i5)" }, + { 6, 5, -1, -1, 37, 2, -1, 3072, CORE_I3 , 0, "Arrandale (Core i3)" }, + { 6, 5, -1, -1, 37, 2, -1, 4096, CORE_I3 , 0, "Clarkdale (Core i3)" }, + + { 6, 10, -1, -1, 42, -1, -1, -1, NO_CODE , 0, "Unknown Sandy Bridge" }, + { 6, 10, -1, -1, 42, -1, -1, -1, CORE_I7 , 0, "Sandy Bridge i7" }, + { 6, 10, -1, -1, 42, 4, -1, -1, CORE_I7 , 0, "Sandy Bridge (Core i7)" }, + { 6, 10, -1, -1, 42, 4, -1, -1, CORE_I5 , 0, "Sandy Bridge (Core i5)" }, + { 6, 10, -1, -1, 42, 2, -1, -1, CORE_I3 , 0, "Sandy Bridge (Core i3)" }, + { 6, 10, -1, -1, 42, 1, -1, -1, CELERON , 0, "Celeron (Sandy Bridge)" }, + { 6, 10, -1, -1, 42, 2, -1, -1, CELERON , 0, "Celeron (Sandy Bridge)" }, + { 6, 10, -1, -1, 42, 2, -1, -1, PENTIUM , 0, "Pentium (Sandy Bridge)" }, + + { 6, 10, -1, -1, 26, 1, -1, -1, CORE_I7 , 0, "Intel Core i7" }, + { 6, 10, -1, -1, 26, 4, -1, -1, CORE_I7 , 0, "Bloomfield (Core i7)" }, + { 6, 10, -1, -1, 30, 4, -1, -1, CORE_I7 , 0, "Lynnfield (Core i7)" }, + { 6, 10, -1, -1, 26, 4, -1, -1, XEON_I7 , 0, "Xeon (Bloomfield)" }, + + { 6, 10, -1, -1, 26, 4, -1, -1, XEON_GAINESTOWN , 0, "Xeon (Gainestown)" }, + { 6, 10, -1, -1, 26, 4, -1, 4096, XEON_GAINESTOWN , 0, "Xeon (Gainestown) 4M" }, + { 6, 10, -1, -1, 26, 4, -1, 8192, XEON_GAINESTOWN , 0, "Xeon (Gainestown) 8M" }, + + { 6, 12, -1, -1, 44, -1, -1, -1, XEON_WESTMERE , 0, "Xeon (Westmere-based)" }, + { 6, 12, -1, -1, 44, 4, -1, 12288, CORE_I7 , 0, "Gulftown (Core i7)" }, + { 6, 12, -1, -1, 44, -1, -1, 12288, XEON_WESTMERE , 0, "Xeon (Gulftown)" }, + + { 6, 13, -1, -1, 45, -1, -1, -1, XEON , 0, "Xeon (Sandy Bridge)" }, + + { 6, 13, -1, -1, 45, -1, -1, -1, CORE_I7 , 0, "Sandy Bridge-E (Core i7)" }, + { 6, 13, -1, -1, 45, -1, -1, -1, CORE_I5 , 0, "Sandy Bridge-E (Core i5)" }, + { 6, 13, -1, -1, 45, -1, -1, -1, CORE_I3 , 0, "Sandy Bridge-E (Core i3)" }, + + { 6, 10, -1, -1, 58, 4, -1, -1, CORE_IVY7 , 0, "Ivy Bridge (Core i7)" }, + { 6, 10, -1, -1, 58, 4, -1, -1, CORE_IVY5 , 0, "Ivy Bridge (Core i5)" }, + { 6, 10, -1, -1, 58, 2, -1, -1, CORE_IVY3 , 0, "Ivy Bridge (Core i3)" }, + + { 6, 12, -1, -1, 60, 4, -1, -1, CORE_HASWELL7 , 0, "Haswell (Core i7)" }, + { 6, 12, -1, -1, 60, 4, -1, -1, CORE_HASWELL5 , 0, "Haswell (Core i5)" }, + { 6, 12, -1, -1, 60, 2, -1, -1, CORE_HASWELL3 , 0, "Haswell (Core i3)" }, + + + /* Core microarchitecture-based Xeons: */ + { 6, 14, -1, -1, 14, 1, -1, -1, XEON , 0, "Xeon LV" }, + { 6, 15, -1, -1, 15, 2, 4096, -1, XEON , _5100, "Xeon (Woodcrest)" }, + { 6, 15, -1, -1, 15, 2, 2048, -1, XEON , _3000, "Xeon (Conroe/2M)" }, + { 6, 15, -1, -1, 15, 2, 4096, -1, XEON , _3000, "Xeon (Conroe/4M)" }, + { 6, 15, -1, -1, 15, 4, 4096, -1, XEON , X3200, "Xeon (Kentsfield)" }, + { 6, 15, -1, -1, 15, 4, 4096, -1, XEON , _5300, "Xeon (Clovertown)" }, + { 6, 7, -1, -1, 23, 2, 6144, -1, XEON , _3100, "Xeon (Wolfdale)" }, + { 6, 7, -1, -1, 23, 2, 6144, -1, XEON , _5200, "Xeon (Wolfdale DP)" }, + { 6, 7, -1, -1, 23, 4, 6144, -1, XEON , _5400, "Xeon (Harpertown)" }, + { 6, 7, -1, -1, 23, 4, 3072, -1, XEON , X3300, "Xeon (Yorkfield/3M)" }, + { 6, 7, -1, -1, 23, 4, 6144, -1, XEON , X3300, "Xeon (Yorkfield/6M)" }, + + /* Itaniums */ + { 7, -1, -1, -1, -1, 1, -1, -1, NO_CODE , 0, "Itanium" }, + { 15, -1, -1, 16, -1, 1, -1, -1, NO_CODE , 0, "Itanium 2" }, + +}; + + +static void load_intel_features(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + const struct feature_map_t matchtable_edx1[] = { + { 18, CPU_FEATURE_PN }, + { 21, CPU_FEATURE_DTS }, + { 22, CPU_FEATURE_ACPI }, + { 27, CPU_FEATURE_SS }, + { 29, CPU_FEATURE_TM }, + { 30, CPU_FEATURE_IA64 }, + { 31, CPU_FEATURE_PBE }, + }; + const struct feature_map_t matchtable_ecx1[] = { + { 1, CPU_FEATURE_PCLMUL }, + { 2, CPU_FEATURE_DTS64 }, + { 4, CPU_FEATURE_DS_CPL }, + { 5, CPU_FEATURE_VMX }, + { 6, CPU_FEATURE_SMX }, + { 7, CPU_FEATURE_EST }, + { 8, CPU_FEATURE_TM2 }, + { 10, CPU_FEATURE_CID }, + { 14, CPU_FEATURE_XTPR }, + { 15, CPU_FEATURE_PDCM }, + { 18, CPU_FEATURE_DCA }, + { 20, CPU_FEATURE_SSE4_2 }, + { 22, CPU_FEATURE_MOVBE }, + { 25, CPU_FEATURE_AES }, + { 26, CPU_FEATURE_XSAVE }, + { 27, CPU_FEATURE_OSXSAVE }, + { 28, CPU_FEATURE_AVX }, + { 30, CPU_FEATURE_RDRAND }, + }; + const struct feature_map_t matchtable_ebx7[] = { + { 5, CPU_FEATURE_AVX2 }, + }; + const struct feature_map_t matchtable_edx81[] = { + { 20, CPU_FEATURE_XD }, + }; + if (raw->basic_cpuid[0][0] >= 1) { + match_features(matchtable_edx1, COUNT_OF(matchtable_edx1), raw->basic_cpuid[1][3], data); + match_features(matchtable_ecx1, COUNT_OF(matchtable_ecx1), raw->basic_cpuid[1][2], data); + } + if (raw->basic_cpuid[0][0] >= 7) { + match_features(matchtable_ebx7, COUNT_OF(matchtable_ebx7), raw->basic_cpuid[7][1], data); + } + if (raw->ext_cpuid[0][0] >= 1) { + match_features(matchtable_edx81, COUNT_OF(matchtable_edx81), raw->ext_cpuid[1][3], data); + } +} + +enum _cache_type_t { + L1I, + L1D, + L2, + L3 +}; +typedef enum _cache_type_t cache_type_t; + +static void check_case(uint8_t on, cache_type_t cache, int size, int assoc, int linesize, struct cpu_id_t* data) +{ + if (!on) return; + switch (cache) { + case L1I: + data->l1_instruction_cache = size; + break; + case L1D: + data->l1_data_cache = size; + data->l1_assoc = assoc; + data->l1_cacheline = linesize; + break; + case L2: + data->l2_cache = size; + data->l2_assoc = assoc; + data->l2_cacheline = linesize; + break; + case L3: + data->l3_cache = size; + data->l3_assoc = assoc; + data->l3_cacheline = linesize; + default: + break; + } +} + +static void decode_intel_oldstyle_cache_info(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + uint8_t f[256] = {0}; + int reg, off; + uint32_t x; + for (reg = 0; reg < 4; reg++) { + x = raw->basic_cpuid[2][reg]; + if (x & 0x80000000) continue; + for (off = 0; off < 4; off++) { + f[x & 0xff] = 1; + x >>= 8; + } + } + + check_case(f[0x06], L1I, 8, 4, 32, data); + check_case(f[0x08], L1I, 16, 4, 32, data); + check_case(f[0x0A], L1D, 8, 2, 32, data); + check_case(f[0x0C], L1D, 16, 4, 32, data); + check_case(f[0x22], L3, 512, 4, 64, data); + check_case(f[0x23], L3, 1024, 8, 64, data); + check_case(f[0x25], L3, 2048, 8, 64, data); + check_case(f[0x29], L3, 4096, 8, 64, data); + check_case(f[0x2C], L1D, 32, 8, 64, data); + check_case(f[0x30], L1I, 32, 8, 64, data); + check_case(f[0x39], L2, 128, 4, 64, data); + check_case(f[0x3A], L2, 192, 6, 64, data); + check_case(f[0x3B], L2, 128, 2, 64, data); + check_case(f[0x3C], L2, 256, 4, 64, data); + check_case(f[0x3D], L2, 384, 6, 64, data); + check_case(f[0x3E], L2, 512, 4, 64, data); + check_case(f[0x41], L2, 128, 4, 32, data); + check_case(f[0x42], L2, 256, 4, 32, data); + check_case(f[0x43], L2, 512, 4, 32, data); + check_case(f[0x44], L2, 1024, 4, 32, data); + check_case(f[0x45], L2, 2048, 4, 32, data); + check_case(f[0x46], L3, 4096, 4, 64, data); + check_case(f[0x47], L3, 8192, 8, 64, data); + check_case(f[0x4A], L3, 6144, 12, 64, data); + check_case(f[0x4B], L3, 8192, 16, 64, data); + check_case(f[0x4C], L3, 12288, 12, 64, data); + check_case(f[0x4D], L3, 16384, 16, 64, data); + check_case(f[0x4E], L2, 6144, 24, 64, data); + check_case(f[0x60], L1D, 16, 8, 64, data); + check_case(f[0x66], L1D, 8, 4, 64, data); + check_case(f[0x67], L1D, 16, 4, 64, data); + check_case(f[0x68], L1D, 32, 4, 64, data); + /* The following four entries are trace cache. Intel does not + * specify a cache-line size, so we use -1 instead + */ + check_case(f[0x70], L1I, 12, 8, -1, data); + check_case(f[0x71], L1I, 16, 8, -1, data); + check_case(f[0x72], L1I, 32, 8, -1, data); + check_case(f[0x73], L1I, 64, 8, -1, data); + + check_case(f[0x78], L2, 1024, 4, 64, data); + check_case(f[0x79], L2, 128, 8, 64, data); + check_case(f[0x7A], L2, 256, 8, 64, data); + check_case(f[0x7B], L2, 512, 8, 64, data); + check_case(f[0x7C], L2, 1024, 8, 64, data); + check_case(f[0x7D], L2, 2048, 8, 64, data); + check_case(f[0x7F], L2, 512, 2, 64, data); + check_case(f[0x82], L2, 256, 8, 32, data); + check_case(f[0x83], L2, 512, 8, 32, data); + check_case(f[0x84], L2, 1024, 8, 32, data); + check_case(f[0x85], L2, 2048, 8, 32, data); + check_case(f[0x86], L2, 512, 4, 64, data); + check_case(f[0x87], L2, 1024, 8, 64, data); + + if (f[0x49]) { + /* This flag is overloaded with two meanings. On Xeon MP + * (family 0xf, model 0x6) this means L3 cache. On all other + * CPUs (notably Conroe et al), this is L2 cache. In both cases + * it means 4MB, 16-way associative, 64-byte line size. + */ + if (data->family == 0xf && data->model == 0x6) { + data->l3_cache = 4096; + data->l3_assoc = 16; + data->l3_cacheline = 64; + } else { + data->l2_cache = 4096; + data->l2_assoc = 16; + data->l2_cacheline = 64; + } + } + if (f[0x40]) { + /* Again, a special flag. It means: + * 1) If no L2 is specified, then CPU is w/o L2 (0 KB) + * 2) If L2 is specified by other flags, then, CPU is w/o L3. + */ + if (data->l2_cache == -1) { + data->l2_cache = 0; + } else { + data->l3_cache = 0; + } + } +} + +static void decode_intel_deterministic_cache_info(struct cpu_raw_data_t* raw, + struct cpu_id_t* data) +{ + int ecx; + int ways, partitions, linesize, sets, size, level, typenumber; + cache_type_t type; + for (ecx = 0; ecx < MAX_INTELFN4_LEVEL; ecx++) { + typenumber = raw->intel_fn4[ecx][0] & 0x1f; + if (typenumber == 0) break; + level = (raw->intel_fn4[ecx][0] >> 5) & 0x7; + if (level == 1 && typenumber == 1) + type = L1D; + else if (level == 1 && typenumber == 2) + type = L1I; + else if (level == 2 && typenumber == 3) + type = L2; + else if (level == 3 && typenumber == 3) + type = L3; + else { + warnf("deterministic_cache: unknown level/typenumber combo (%d/%d), cannot\n", level, typenumber); + warnf("deterministic_cache: recognize cache type\n"); + continue; + } + ways = ((raw->intel_fn4[ecx][1] >> 22) & 0x3ff) + 1; + partitions = ((raw->intel_fn4[ecx][1] >> 12) & 0x3ff) + 1; + linesize = (raw->intel_fn4[ecx][1] & 0xfff) + 1; + sets = raw->intel_fn4[ecx][2] + 1; + size = ways * partitions * linesize * sets / 1024; + check_case(1, type, size, ways, linesize, data); + } +} + +static int decode_intel_extended_topology(struct cpu_raw_data_t* raw, + struct cpu_id_t* data) +{ + int i, level_type, num_smt = -1, num_core = -1; + for (i = 0; i < MAX_INTELFN11_LEVEL; i++) { + level_type = (raw->intel_fn11[i][2] & 0xff00) >> 8; + switch (level_type) { + case 0x01: + num_smt = raw->intel_fn11[i][1] & 0xffff; + break; + case 0x02: + num_core = raw->intel_fn11[i][1] & 0xffff; + break; + default: + break; + } + } + if (num_smt == -1 || num_core == -1) return 0; + data->num_cores = num_core / num_smt; + data->num_logical_cpus = num_core; + return 1; +} + +static void decode_intel_number_of_cores(struct cpu_raw_data_t* raw, + struct cpu_id_t* data) +{ + int logical_cpus = -1, num_cores = -1; + + if (raw->basic_cpuid[0][0] >= 11) { + if (decode_intel_extended_topology(raw, data)) return; + } + + if (raw->basic_cpuid[0][0] >= 1) { + logical_cpus = (raw->basic_cpuid[1][1] >> 16) & 0xff; + if (raw->basic_cpuid[0][0] >= 4) { + num_cores = 1 + ((raw->basic_cpuid[4][0] >> 26) & 0x3f); + } + } + if (data->flags[CPU_FEATURE_HT]) { + if (num_cores > 1) { + data->num_cores = num_cores; + data->num_logical_cpus = logical_cpus; + } else { + data->num_cores = 1; + data->num_logical_cpus = (logical_cpus >= 2 ? logical_cpus : 2); + } + } else { + data->num_cores = data->num_logical_cpus = 1; + } +} + +static intel_code_t get_brand_code(struct cpu_id_t* data) +{ + intel_code_t code = NO_CODE; + int i, need_matchtable = 1, core_ix_base = 0; + const char* bs = data->brand_str; + const char* s; + const struct { intel_code_t c; const char *search; } matchtable[] = { + { XEONMP, "Xeon MP" }, + { XEONMP, "Xeon(TM) MP" }, + { XEON, "Xeon" }, + { CELERON, "Celeron" }, + { MOBILE_PENTIUM_M, "Pentium(R) M" }, + { CORE_SOLO, "Pentium(R) Dual CPU" }, + { PENTIUM_D, "Pentium(R) D" }, + { PENTIUM, "Pentium" }, + { CORE_SOLO, "Genuine Intel(R) CPU" }, + { CORE_SOLO, "Intel(R) Core(TM)" }, + { ATOM_DIAMONDVILLE, "Atom(TM) CPU [N ][23]## " }, + { ATOM_SILVERTHORNE, "Atom(TM) CPU Z" }, + { ATOM_PINEVIEW, "Atom(TM) CPU D" }, + { ATOM_CEDARVIEW, "Atom(TM) CPU N####" }, + { ATOM, "Atom(TM) CPU" }, + }; + + if (strstr(bs, "Mobile")) { + need_matchtable = 0; + if (strstr(bs, "Celeron")) + code = MOBILE_CELERON; + else if (strstr(bs, "Pentium")) + code = MOBILE_PENTIUM; + } + if ((i = match_pattern(bs, "Core(TM) i[357]")) != 0) { + /* Core i3, Core i5 or Core i7 */ + need_matchtable = 0; + + core_ix_base = CORE_I3; + + /* if it has RdRand, then it is at least Ivy Bridge */ + if (data->flags[CPU_FEATURE_RDRAND]) + core_ix_base = CORE_IVY3; + /* if it has FMA, then it is at least Haswell */ + if (data->flags[CPU_FEATURE_FMA3]) + core_ix_base = CORE_HASWELL3; + + switch (bs[i + 9]) { + case '3': code = core_ix_base + 0; break; + case '5': code = core_ix_base + 1; break; + case '7': code = core_ix_base + 2; break; + } + } + if (need_matchtable) { + for (i = 0; i < COUNT_OF(matchtable); i++) + if (match_pattern(bs, matchtable[i].search)) { + code = matchtable[i].c; + break; + } + debugf(2, "intel matchtable result is %d\n", code); + } + if (code == XEON) { + if (match_pattern(bs, "W35##") || match_pattern(bs, "[ELXW]75##")) + code = XEON_I7; + else if (match_pattern(bs, "[ELXW]55##")) + code = XEON_GAINESTOWN; + else if (match_pattern(bs, "[ELXW]56##")) + code = XEON_WESTMERE; + else if (data->l3_cache > 0) + code = XEON_IRWIN; + } + if (code == XEONMP && data->l3_cache > 0) + code = XEON_POTOMAC; + if (code == CORE_SOLO) { + s = strstr(bs, "CPU"); + if (s) { + s += 3; + while (*s == ' ') s++; + if (*s == 'T') + code = (data->num_cores == 1) ? MOBILE_CORE_SOLO : MOBILE_CORE_DUO; + } + } + if (code == CORE_SOLO) { + switch (data->num_cores) { + case 1: break; + case 2: + { + code = CORE_DUO; + if (data->num_logical_cpus > 2) + code = DUAL_CORE_HT; + break; + } + case 4: + { + code = QUAD_CORE; + if (data->num_logical_cpus > 4) + code = QUAD_CORE_HT; + break; + } + default: + code = MORE_THAN_QUADCORE; break; + } + } + + if (code == CORE_DUO && data->ext_model >= 23) { + code = WOLFDALE; + } + if (code == PENTIUM_D && data->ext_model >= 23) { + code = WOLFDALE; + } + if (code == MOBILE_CORE_DUO && data->model != 14) { + if (data->ext_model < 23) { + code = MEROM; + } else { + code = PENRYN; + } + } + return code; +} + +static intel_model_t get_model_code(struct cpu_id_t* data) +{ + int i = 0; + int l = (int) strlen(data->brand_str); + const char *bs = data->brand_str; + int mod_flags = 0, model_no = 0, ndigs = 0; + /* If the CPU is a Core ix, then just return the model number generation: */ + if ((i = match_pattern(bs, "Core(TM) i[357]")) != 0) { + i += 11; + if (i + 4 >= l) return UNKNOWN; + if (bs[i] == '2') return _2xxx; + if (bs[i] == '3') return _3xxx; + return UNKNOWN; + } + + /* For Core2-based Xeons: */ + while (i < l - 3) { + if (bs[i] == 'C' && bs[i+1] == 'P' && bs[i+2] == 'U') + break; + i++; + } + if (i >= l - 3) return UNKNOWN; + i += 3; + while (i < l - 4 && bs[i] == ' ') i++; + if (i >= l - 4) return UNKNOWN; + while (i < l - 4 && !isdigit(bs[i])) { + if (bs[i] >= 'A' && bs[i] <= 'Z') + mod_flags |= (1 << (bs[i] - 'A')); + i++; + } + if (i >= l - 4) return UNKNOWN; + while (isdigit(bs[i])) { + ndigs++; + model_no = model_no * 10 + (int) (bs[i] - '0'); + i++; + } + if (ndigs != 4) return UNKNOWN; +#define HAVE(ch, flags) ((flags & (1 << ((int)(ch-'A')))) != 0) + switch (model_no / 100) { + case 30: return _3000; + case 31: return _3100; + case 32: + { + return (HAVE('X', mod_flags)) ? X3200 : _3200; + } + case 33: + { + return (HAVE('X', mod_flags)) ? X3300 : _3300; + } + case 51: return _5100; + case 52: return _5200; + case 53: return _5300; + case 54: return _5400; + default: + return UNKNOWN; + } +#undef HAVE +} + +int cpuid_identify_intel(struct cpu_raw_data_t* raw, struct cpu_id_t* data) +{ + load_intel_features(raw, data); + if (raw->basic_cpuid[0][0] >= 4) { + /* Deterministic way is preferred, being more generic */ + decode_intel_deterministic_cache_info(raw, data); + } else if (raw->basic_cpuid[0][0] >= 2) { + decode_intel_oldstyle_cache_info(raw, data); + } + decode_intel_number_of_cores(raw, data); + match_cpu_codename(cpudb_intel, COUNT_OF(cpudb_intel), data, + get_brand_code(data), get_model_code(data)); + return 0; +} + +void cpuid_get_list_intel(struct cpu_list_t* list) +{ + generic_get_cpu_list(cpudb_intel, COUNT_OF(cpudb_intel), list); +} diff --git a/contrib/libcpuid/include/cpuid/recog_intel.h b/contrib/libcpuid/include/cpuid/recog_intel.h new file mode 100644 index 00000000000..67279373271 --- /dev/null +++ b/contrib/libcpuid/include/cpuid/recog_intel.h @@ -0,0 +1,32 @@ +/* + * Copyright 2008 Veselin Georgiev, + * anrieffNOSPAM @ mgail_DOT.com (convert to gmail) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef __RECOG_INTEL_H__ +#define __RECOG_INTEL_H__ + +int cpuid_identify_intel(struct cpu_raw_data_t* raw, struct cpu_id_t* data); +void cpuid_get_list_intel(struct cpu_list_t* list); + +#endif /*__RECOG_INTEL_H__*/ diff --git a/contrib/libdivide/LICENSE.txt b/contrib/libdivide/LICENSE.txt new file mode 100644 index 00000000000..d056b847bba --- /dev/null +++ b/contrib/libdivide/LICENSE.txt @@ -0,0 +1,20 @@ + libdivide + Copyright (C) 2010 ridiculous_fish + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + libdivide@ridiculousfish.com diff --git a/contrib/libdivide/README.txt b/contrib/libdivide/README.txt new file mode 100644 index 00000000000..2d17a68e4c2 --- /dev/null +++ b/contrib/libdivide/README.txt @@ -0,0 +1,2 @@ +https://github.com/ridiculousfish/libdivide +http://libdivide.com/ diff --git a/contrib/libdivide/libdivide.h b/contrib/libdivide/libdivide.h new file mode 100644 index 00000000000..e7123f9b801 --- /dev/null +++ b/contrib/libdivide/libdivide.h @@ -0,0 +1,1342 @@ +/* libdivide.h + Copyright 2010 ridiculous_fish +*/ + +#if defined(_WIN32) || defined(WIN32) +#define LIBDIVIDE_WINDOWS 1 +#endif + +#if defined(_MSC_VER) +#define LIBDIVIDE_VC 1 +#endif + +#ifdef __cplusplus +#include +#include +#include +#else +#include +#include +#include +#endif + +#if ! LIBDIVIDE_HAS_STDINT_TYPES && (! LIBDIVIDE_VC || _MSC_VER >= 1600) +/* Only Visual C++ 2010 and later include stdint.h */ +#include +#define LIBDIVIDE_HAS_STDINT_TYPES 1 +#endif + +#if ! LIBDIVIDE_HAS_STDINT_TYPES +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +#endif + +#if LIBDIVIDE_USE_SSE2 + #include +#endif + +#if LIBDIVIDE_VC + #include +#endif + +#ifndef __has_builtin +#define __has_builtin(x) 0 // Compatibility with non-clang compilers. +#endif + +#ifdef __ICC +#define HAS_INT128_T 0 +#else +#define HAS_INT128_T __LP64__ +#endif + +#if defined(__x86_64__) || defined(_WIN64) || defined(_M_64) +#define LIBDIVIDE_IS_X86_64 1 +#endif + +#if defined(__i386__) +#define LIBDIVIDE_IS_i386 1 +#endif + +#if __GNUC__ || __clang__ +#define LIBDIVIDE_GCC_STYLE_ASM 1 +#endif + + +/* libdivide may use the pmuldq (vector signed 32x32->64 mult instruction) which is in SSE 4.1. However, signed multiplication can be emulated efficiently with unsigned multiplication, and SSE 4.1 is currently rare, so it is OK to not turn this on */ +#ifdef LIBDIVIDE_USE_SSE4_1 +#include +#endif + +#ifdef __cplusplus +/* We place libdivide within the libdivide namespace, and that goes in an anonymous namespace so that the functions are only visible to files that #include this header and don't get external linkage. At least that's the theory. */ +namespace { +namespace libdivide { +#endif + +/* Explanation of "more" field: bit 6 is whether to use shift path. If we are using the shift path, bit 7 is whether the divisor is negative in the signed case; in the unsigned case it is 0. Bits 0-4 is shift value (for shift path or mult path). In 32 bit case, bit 5 is always 0. We use bit 7 as the "negative divisor indicator" so that we can use sign extension to efficiently go to a full-width -1. + + +u32: [0-4] shift value + [5] ignored + [6] add indicator + [7] shift path + +s32: [0-4] shift value + [5] shift path + [6] add indicator + [7] indicates negative divisor + +u64: [0-5] shift value + [6] add indicator + [7] shift path + +s64: [0-5] shift value + [6] add indicator + [7] indicates negative divisor + magic number of 0 indicates shift path (we ran out of bits!) +*/ + +enum { + LIBDIVIDE_32_SHIFT_MASK = 0x1F, + LIBDIVIDE_64_SHIFT_MASK = 0x3F, + LIBDIVIDE_ADD_MARKER = 0x40, + LIBDIVIDE_U32_SHIFT_PATH = 0x80, + LIBDIVIDE_U64_SHIFT_PATH = 0x80, + LIBDIVIDE_S32_SHIFT_PATH = 0x20, + LIBDIVIDE_NEGATIVE_DIVISOR = 0x80 +}; + + +struct libdivide_u32_t { + uint32_t magic; + uint8_t more; +}; + +struct libdivide_s32_t { + int32_t magic; + uint8_t more; +}; + +struct libdivide_u64_t { + uint64_t magic; + uint8_t more; +}; + +struct libdivide_s64_t { + int64_t magic; + uint8_t more; +}; + + + +#ifndef LIBDIVIDE_API + #ifdef __cplusplus + /* In C++, we don't want our public functions to be static, because they are arguments to templates and static functions can't do that. They get internal linkage through virtue of the anonymous namespace. In C, they should be static. */ + #define LIBDIVIDE_API + #else + #define LIBDIVIDE_API static + #endif +#endif + + +LIBDIVIDE_API struct libdivide_s32_t libdivide_s32_gen(int32_t y); +LIBDIVIDE_API struct libdivide_u32_t libdivide_u32_gen(uint32_t y); +LIBDIVIDE_API struct libdivide_s64_t libdivide_s64_gen(int64_t y); +LIBDIVIDE_API struct libdivide_u64_t libdivide_u64_gen(uint64_t y); + +LIBDIVIDE_API int32_t libdivide_s32_do(int32_t numer, const struct libdivide_s32_t *denom); +LIBDIVIDE_API uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do(int64_t numer, const struct libdivide_s64_t *denom); +LIBDIVIDE_API uint64_t libdivide_u64_do(uint64_t y, const struct libdivide_u64_t *denom); + +LIBDIVIDE_API int libdivide_u32_get_algorithm(const struct libdivide_u32_t *denom); +LIBDIVIDE_API uint32_t libdivide_u32_do_alg0(uint32_t numer, const struct libdivide_u32_t *denom); +LIBDIVIDE_API uint32_t libdivide_u32_do_alg1(uint32_t numer, const struct libdivide_u32_t *denom); +LIBDIVIDE_API uint32_t libdivide_u32_do_alg2(uint32_t numer, const struct libdivide_u32_t *denom); + +LIBDIVIDE_API int libdivide_u64_get_algorithm(const struct libdivide_u64_t *denom); +LIBDIVIDE_API uint64_t libdivide_u64_do_alg0(uint64_t numer, const struct libdivide_u64_t *denom); +LIBDIVIDE_API uint64_t libdivide_u64_do_alg1(uint64_t numer, const struct libdivide_u64_t *denom); +LIBDIVIDE_API uint64_t libdivide_u64_do_alg2(uint64_t numer, const struct libdivide_u64_t *denom); + +LIBDIVIDE_API int libdivide_s32_get_algorithm(const struct libdivide_s32_t *denom); +LIBDIVIDE_API int32_t libdivide_s32_do_alg0(int32_t numer, const struct libdivide_s32_t *denom); +LIBDIVIDE_API int32_t libdivide_s32_do_alg1(int32_t numer, const struct libdivide_s32_t *denom); +LIBDIVIDE_API int32_t libdivide_s32_do_alg2(int32_t numer, const struct libdivide_s32_t *denom); +LIBDIVIDE_API int32_t libdivide_s32_do_alg3(int32_t numer, const struct libdivide_s32_t *denom); +LIBDIVIDE_API int32_t libdivide_s32_do_alg4(int32_t numer, const struct libdivide_s32_t *denom); + +LIBDIVIDE_API int libdivide_s64_get_algorithm(const struct libdivide_s64_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do_alg0(int64_t numer, const struct libdivide_s64_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do_alg1(int64_t numer, const struct libdivide_s64_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do_alg2(int64_t numer, const struct libdivide_s64_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do_alg3(int64_t numer, const struct libdivide_s64_t *denom); +LIBDIVIDE_API int64_t libdivide_s64_do_alg4(int64_t numer, const struct libdivide_s64_t *denom); + +#if LIBDIVIDE_USE_SSE2 +LIBDIVIDE_API __m128i libdivide_u32_do_vector(__m128i numers, const struct libdivide_u32_t * denom); +LIBDIVIDE_API __m128i libdivide_s32_do_vector(__m128i numers, const struct libdivide_s32_t * denom); +LIBDIVIDE_API __m128i libdivide_u64_do_vector(__m128i numers, const struct libdivide_u64_t * denom); +LIBDIVIDE_API __m128i libdivide_s64_do_vector(__m128i numers, const struct libdivide_s64_t * denom); + +LIBDIVIDE_API __m128i libdivide_u32_do_vector_alg0(__m128i numers, const struct libdivide_u32_t * denom); +LIBDIVIDE_API __m128i libdivide_u32_do_vector_alg1(__m128i numers, const struct libdivide_u32_t * denom); +LIBDIVIDE_API __m128i libdivide_u32_do_vector_alg2(__m128i numers, const struct libdivide_u32_t * denom); + +LIBDIVIDE_API __m128i libdivide_s32_do_vector_alg0(__m128i numers, const struct libdivide_s32_t * denom); +LIBDIVIDE_API __m128i libdivide_s32_do_vector_alg1(__m128i numers, const struct libdivide_s32_t * denom); +LIBDIVIDE_API __m128i libdivide_s32_do_vector_alg2(__m128i numers, const struct libdivide_s32_t * denom); +LIBDIVIDE_API __m128i libdivide_s32_do_vector_alg3(__m128i numers, const struct libdivide_s32_t * denom); +LIBDIVIDE_API __m128i libdivide_s32_do_vector_alg4(__m128i numers, const struct libdivide_s32_t * denom); + +LIBDIVIDE_API __m128i libdivide_u64_do_vector_alg0(__m128i numers, const struct libdivide_u64_t * denom); +LIBDIVIDE_API __m128i libdivide_u64_do_vector_alg1(__m128i numers, const struct libdivide_u64_t * denom); +LIBDIVIDE_API __m128i libdivide_u64_do_vector_alg2(__m128i numers, const struct libdivide_u64_t * denom); + +LIBDIVIDE_API __m128i libdivide_s64_do_vector_alg0(__m128i numers, const struct libdivide_s64_t * denom); +LIBDIVIDE_API __m128i libdivide_s64_do_vector_alg1(__m128i numers, const struct libdivide_s64_t * denom); +LIBDIVIDE_API __m128i libdivide_s64_do_vector_alg2(__m128i numers, const struct libdivide_s64_t * denom); +LIBDIVIDE_API __m128i libdivide_s64_do_vector_alg3(__m128i numers, const struct libdivide_s64_t * denom); +LIBDIVIDE_API __m128i libdivide_s64_do_vector_alg4(__m128i numers, const struct libdivide_s64_t * denom); +#endif + + + +//////// Internal Utility Functions + +static inline uint32_t libdivide__mullhi_u32(uint32_t x, uint32_t y) { + uint64_t xl = x, yl = y; + uint64_t rl = xl * yl; + return (uint32_t)(rl >> 32); +} + +static uint64_t libdivide__mullhi_u64(uint64_t x, uint64_t y) { +#if HAS_INT128_T + __uint128_t xl = x, yl = y; + __uint128_t rl = xl * yl; + return (uint64_t)(rl >> 64); +#else + //full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64) + const uint32_t mask = 0xFFFFFFFF; + const uint32_t x0 = (uint32_t)(x & mask), x1 = (uint32_t)(x >> 32); + const uint32_t y0 = (uint32_t)(y & mask), y1 = (uint32_t)(y >> 32); + const uint32_t x0y0_hi = libdivide__mullhi_u32(x0, y0); + const uint64_t x0y1 = x0 * (uint64_t)y1; + const uint64_t x1y0 = x1 * (uint64_t)y0; + const uint64_t x1y1 = x1 * (uint64_t)y1; + + uint64_t temp = x1y0 + x0y0_hi; + uint64_t temp_lo = temp & mask, temp_hi = temp >> 32; + return x1y1 + temp_hi + ((temp_lo + x0y1) >> 32); +#endif +} + +static inline int64_t libdivide__mullhi_s64(int64_t x, int64_t y) { +#if HAS_INT128_T + __int128_t xl = x, yl = y; + __int128_t rl = xl * yl; + return (int64_t)(rl >> 64); +#else + //full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64) + const uint32_t mask = 0xFFFFFFFF; + const uint32_t x0 = (uint32_t)(x & mask), y0 = (uint32_t)(y & mask); + const int32_t x1 = (int32_t)(x >> 32), y1 = (int32_t)(y >> 32); + const uint32_t x0y0_hi = libdivide__mullhi_u32(x0, y0); + const int64_t t = x1*(int64_t)y0 + x0y0_hi; + const int64_t w1 = x0*(int64_t)y1 + (t & mask); + return x1*(int64_t)y1 + (t >> 32) + (w1 >> 32); +#endif +} + +#if LIBDIVIDE_USE_SSE2 + +static inline __m128i libdivide__u64_to_m128(uint64_t x) { +#if LIBDIVIDE_VC && ! _WIN64 + //64 bit windows doesn't seem to have an implementation of any of these load intrinsics, and 32 bit Visual C++ crashes + _declspec(align(16)) uint64_t temp[2] = {x, x}; + return _mm_load_si128((const __m128i*)temp); +#elif defined(__ICC) + uint64_t __attribute__((aligned(16))) temp[2] = {x,x}; + return _mm_load_si128((const __m128i*)temp); +#elif __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++11-narrowing" // narrowing from uint64_t (aka 'unsigned long') to 'long long' + // clang does not provide this intrinsic either + return (__m128i){x, x}; +#pragma clang diagnostic pop +#else + // everyone else gets it right + return _mm_set1_epi64x(x); +#endif +} + +static inline __m128i libdivide_get_FFFFFFFF00000000(void) { + //returns the same as _mm_set1_epi64(0xFFFFFFFF00000000ULL) without touching memory + __m128i result = _mm_set1_epi8(-1); //optimizes to pcmpeqd on OS X + return _mm_slli_epi64(result, 32); +} + +static inline __m128i libdivide_get_00000000FFFFFFFF(void) { + //returns the same as _mm_set1_epi64(0x00000000FFFFFFFFULL) without touching memory + __m128i result = _mm_set1_epi8(-1); //optimizes to pcmpeqd on OS X + result = _mm_srli_epi64(result, 32); + return result; +} + +#if __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wuninitialized" +#endif +static inline __m128i libdivide_get_0000FFFF(void) { + //returns the same as _mm_set1_epi32(0x0000FFFFULL) without touching memory + __m128i result; //we don't care what its contents are + result = _mm_cmpeq_epi8(result, result); //all 1s + result = _mm_srli_epi32(result, 16); + return result; +} +#if __clang__ +#pragma clang diagnostic pop +#endif + +static inline __m128i libdivide_s64_signbits(__m128i v) { + //we want to compute v >> 63, that is, _mm_srai_epi64(v, 63). But there is no 64 bit shift right arithmetic instruction in SSE2. So we have to fake it by first duplicating the high 32 bit values, and then using a 32 bit shift. Another option would be to use _mm_srli_epi64(v, 63) and then subtract that from 0, but that approach appears to be substantially slower for unknown reasons + __m128i hiBitsDuped = _mm_shuffle_epi32(v, _MM_SHUFFLE(3, 3, 1, 1)); + __m128i signBits = _mm_srai_epi32(hiBitsDuped, 31); + return signBits; +} + +/* Returns an __m128i whose low 32 bits are equal to amt and has zero elsewhere. */ +static inline __m128i libdivide_u32_to_m128i(uint32_t amt) { + return _mm_set_epi32(0, 0, 0, amt); +} + +static inline __m128i libdivide_s64_shift_right_vector(__m128i v, int amt) { + //implementation of _mm_sra_epi64. Here we have two 64 bit values which are shifted right to logically become (64 - amt) values, and are then sign extended from a (64 - amt) bit number. + const int b = 64 - amt; + __m128i m = libdivide__u64_to_m128(1ULL << (b - 1)); + __m128i x = _mm_srl_epi64(v, libdivide_u32_to_m128i(amt)); + __m128i result = _mm_sub_epi64(_mm_xor_si128(x, m), m); //result = x^m - m + return result; +} + +/* Here, b is assumed to contain one 32 bit value repeated four times. If it did not, the function would not work. */ +static inline __m128i libdivide__mullhi_u32_flat_vector(__m128i a, __m128i b) { + __m128i hi_product_0Z2Z = _mm_srli_epi64(_mm_mul_epu32(a, b), 32); + __m128i a1X3X = _mm_srli_epi64(a, 32); + __m128i hi_product_Z1Z3 = _mm_and_si128(_mm_mul_epu32(a1X3X, b), libdivide_get_FFFFFFFF00000000()); + return _mm_or_si128(hi_product_0Z2Z, hi_product_Z1Z3); // = hi_product_0123 +} + + +/* Here, y is assumed to contain one 64 bit value repeated twice. */ +static inline __m128i libdivide_mullhi_u64_flat_vector(__m128i x, __m128i y) { + //full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64) + const __m128i mask = libdivide_get_00000000FFFFFFFF(); + const __m128i x0 = _mm_and_si128(x, mask), x1 = _mm_srli_epi64(x, 32); //x0 is low half of 2 64 bit values, x1 is high half in low slots + const __m128i y0 = _mm_and_si128(y, mask), y1 = _mm_srli_epi64(y, 32); + const __m128i x0y0_hi = _mm_srli_epi64(_mm_mul_epu32(x0, y0), 32); //x0 happens to have the low half of the two 64 bit values in 32 bit slots 0 and 2, so _mm_mul_epu32 computes their full product, and then we shift right by 32 to get just the high values + const __m128i x0y1 = _mm_mul_epu32(x0, y1); + const __m128i x1y0 = _mm_mul_epu32(x1, y0); + const __m128i x1y1 = _mm_mul_epu32(x1, y1); + + const __m128i temp = _mm_add_epi64(x1y0, x0y0_hi); + __m128i temp_lo = _mm_and_si128(temp, mask), temp_hi = _mm_srli_epi64(temp, 32); + temp_lo = _mm_srli_epi64(_mm_add_epi64(temp_lo, x0y1), 32); + temp_hi = _mm_add_epi64(x1y1, temp_hi); + + return _mm_add_epi64(temp_lo, temp_hi); +} + +/* y is one 64 bit value repeated twice */ +static inline __m128i libdivide_mullhi_s64_flat_vector(__m128i x, __m128i y) { + __m128i p = libdivide_mullhi_u64_flat_vector(x, y); + __m128i t1 = _mm_and_si128(libdivide_s64_signbits(x), y); + p = _mm_sub_epi64(p, t1); + __m128i t2 = _mm_and_si128(libdivide_s64_signbits(y), x); + p = _mm_sub_epi64(p, t2); + return p; +} + +#ifdef LIBDIVIDE_USE_SSE4_1 + +/* b is one 32 bit value repeated four times. */ +static inline __m128i libdivide_mullhi_s32_flat_vector(__m128i a, __m128i b) { + __m128i hi_product_0Z2Z = _mm_srli_epi64(_mm_mul_epi32(a, b), 32); + __m128i a1X3X = _mm_srli_epi64(a, 32); + __m128i hi_product_Z1Z3 = _mm_and_si128(_mm_mul_epi32(a1X3X, b), libdivide_get_FFFFFFFF00000000()); + return _mm_or_si128(hi_product_0Z2Z, hi_product_Z1Z3); // = hi_product_0123 +} + +#else + +/* SSE2 does not have a signed multiplication instruction, but we can convert unsigned to signed pretty efficiently. Again, b is just a 32 bit value repeated four times. */ +static inline __m128i libdivide_mullhi_s32_flat_vector(__m128i a, __m128i b) { + __m128i p = libdivide__mullhi_u32_flat_vector(a, b); + __m128i t1 = _mm_and_si128(_mm_srai_epi32(a, 31), b); //t1 = (a >> 31) & y, arithmetic shift + __m128i t2 = _mm_and_si128(_mm_srai_epi32(b, 31), a); + p = _mm_sub_epi32(p, t1); + p = _mm_sub_epi32(p, t2); + return p; +} +#endif +#endif + +static inline int32_t libdivide__count_trailing_zeros32(uint32_t val) { +#if __GNUC__ || __has_builtin(__builtin_ctz) + /* Fast way to count trailing zeros */ + return __builtin_ctz(val); +#elif LIBDIVIDE_VC + unsigned long result; + if (_BitScanForward(&result, val)) { + return result; + } + return 0; +#else + /* Dorky way to count trailing zeros. Note that this hangs for val = 0! */ + int32_t result = 0; + val = (val ^ (val - 1)) >> 1; // Set v's trailing 0s to 1s and zero rest + while (val) { + val >>= 1; + result++; + } + return result; +#endif +} + +static inline int32_t libdivide__count_trailing_zeros64(uint64_t val) { +#if __LP64__ && (__GNUC__ || __has_builtin(__builtin_ctzll)) + /* Fast way to count trailing zeros. Note that we disable this in 32 bit because gcc does something horrible - it calls through to a dynamically bound function. */ + return __builtin_ctzll(val); +#elif LIBDIVIDE_VC && _WIN64 + unsigned long result; + if (_BitScanForward64(&result, val)) { + return result; + } + return 0; +#else + /* Pretty good way to count trailing zeros. Note that this hangs for val = 0! */ + uint32_t lo = val & 0xFFFFFFFF; + if (lo != 0) return libdivide__count_trailing_zeros32(lo); + return 32 + libdivide__count_trailing_zeros32(val >> 32); +#endif +} + +static inline int32_t libdivide__count_leading_zeros32(uint32_t val) { +#if __GNUC__ || __has_builtin(__builtin_clzll) + /* Fast way to count leading zeros */ + return __builtin_clz(val); +#elif LIBDIVIDE_VC + unsigned long result; + if (_BitScanReverse(&result, val)) { + return 31 - result; + } + return 0; +#else + /* Dorky way to count leading zeros. Note that this hangs for val = 0! */ + int32_t result = 0; + while (! (val & (1U << 31))) { + val <<= 1; + result++; + } + return result; +#endif +} + +static inline int32_t libdivide__count_leading_zeros64(uint64_t val) { +#if __GNUC__ || __has_builtin(__builtin_clzll) + /* Fast way to count leading zeros */ + return __builtin_clzll(val); +#elif LIBDIVIDE_VC && _WIN64 + unsigned long result; + if (_BitScanReverse64(&result, val)) { + return 63 - result; + } + return 0; +#else + /* Dorky way to count leading zeros. Note that this hangs for val = 0! */ + int32_t result = 0; + while (! (val & (1ULL << 63))) { + val <<= 1; + result++; + } + return result; +#endif +} + +//libdivide_64_div_32_to_32: divides a 64 bit uint {u1, u0} by a 32 bit uint {v}. The result must fit in 32 bits. Returns the quotient directly and the remainder in *r +#if (LIBDIVIDE_IS_i386 || LIBDIVIDE_IS_X86_64) && LIBDIVIDE_GCC_STYLE_ASM +static uint32_t libdivide_64_div_32_to_32(uint32_t u1, uint32_t u0, uint32_t v, uint32_t *r) { + uint32_t result; + __asm__("divl %[v]" + : "=a"(result), "=d"(*r) + : [v] "r"(v), "a"(u0), "d"(u1) + ); + return result; +} +#else +static uint32_t libdivide_64_div_32_to_32(uint32_t u1, uint32_t u0, uint32_t v, uint32_t *r) { + uint64_t n = (((uint64_t)u1) << 32) | u0; + uint32_t result = (uint32_t)(n / v); + *r = (uint32_t)(n - result * (uint64_t)v); + return result; +} +#endif + +#if LIBDIVIDE_IS_X86_64 && LIBDIVIDE_GCC_STYLE_ASM +static uint64_t libdivide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r) { + //u0 -> rax + //u1 -> rdx + //divq + uint64_t result; + __asm__("divq %[v]" + : "=a"(result), "=d"(*r) + : [v] "r"(v), "a"(u0), "d"(u1) + ); + return result; + +} +#else + +/* Code taken from Hacker's Delight, http://www.hackersdelight.org/HDcode/divlu.c . License permits inclusion here per http://www.hackersdelight.org/permissions.htm + */ +static uint64_t libdivide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r) { + const uint64_t b = (1ULL << 32); // Number base (16 bits). + uint64_t un1, un0, // Norm. dividend LSD's. + vn1, vn0, // Norm. divisor digits. + q1, q0, // Quotient digits. + un64, un21, un10,// Dividend digit pairs. + rhat; // A remainder. + int s; // Shift amount for norm. + + if (u1 >= v) { // If overflow, set rem. + if (r != NULL) // to an impossible value, + *r = (uint64_t)(-1); // and return the largest + return (uint64_t)(-1);} // possible quotient. + + /* count leading zeros */ + s = libdivide__count_leading_zeros64(v); // 0 <= s <= 63. + if (s > 0) { + v = v << s; // Normalize divisor. + un64 = (u1 << s) | ((u0 >> (64 - s)) & (-s >> 31)); + un10 = u0 << s; // Shift dividend left. + } else { + // Avoid undefined behavior. + un64 = u1 | u0; + un10 = u0; + } + + vn1 = v >> 32; // Break divisor up into + vn0 = v & 0xFFFFFFFF; // two 32-bit digits. + + un1 = un10 >> 32; // Break right half of + un0 = un10 & 0xFFFFFFFF; // dividend into two digits. + + q1 = un64/vn1; // Compute the first + rhat = un64 - q1*vn1; // quotient digit, q1. +again1: + if (q1 >= b || q1*vn0 > b*rhat + un1) { + q1 = q1 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again1;} + + un21 = un64*b + un1 - q1*v; // Multiply and subtract. + + q0 = un21/vn1; // Compute the second + rhat = un21 - q0*vn1; // quotient digit, q0. +again2: + if (q0 >= b || q0*vn0 > b*rhat + un0) { + q0 = q0 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again2;} + + if (r != NULL) // If remainder is wanted, + *r = (un21*b + un0 - q0*v) >> s; // return it. + return q1*b + q0; +} +#endif + +#if LIBDIVIDE_ASSERTIONS_ON +#define LIBDIVIDE_ASSERT(x) do { if (! (x)) { fprintf(stderr, "Assertion failure on line %ld: %s\n", (long)__LINE__, #x); exit(-1); } } while (0) +#else +#define LIBDIVIDE_ASSERT(x) +#endif + +#ifndef LIBDIVIDE_HEADER_ONLY + +////////// UINT32 + +struct libdivide_u32_t libdivide_u32_gen(uint32_t d) { + struct libdivide_u32_t result; + if ((d & (d - 1)) == 0) { + result.magic = 0; + result.more = libdivide__count_trailing_zeros32(d) | LIBDIVIDE_U32_SHIFT_PATH; + } + else { + const uint32_t floor_log_2_d = 31 - libdivide__count_leading_zeros32(d); + + uint8_t more; + uint32_t rem, proposed_m; + proposed_m = libdivide_64_div_32_to_32(1U << floor_log_2_d, 0, d, &rem); + + LIBDIVIDE_ASSERT(rem > 0 && rem < d); + const uint32_t e = d - rem; + + /* This power works if e < 2**floor_log_2_d. */ + if (e < (1U << floor_log_2_d)) { + /* This power works */ + more = floor_log_2_d; + } + else { + /* We have to use the general 33-bit algorithm. We need to compute (2**power) / d. However, we already have (2**(power-1))/d and its remainder. By doubling both, and then correcting the remainder, we can compute the larger division. */ + proposed_m += proposed_m; //don't care about overflow here - in fact, we expect it + const uint32_t twice_rem = rem + rem; + if (twice_rem >= d || twice_rem < rem) proposed_m += 1; + more = floor_log_2_d | LIBDIVIDE_ADD_MARKER; + } + result.magic = 1 + proposed_m; + result.more = more; + //result.more's shift should in general be ceil_log_2_d. But if we used the smaller power, we subtract one from the shift because we're using the smaller power. If we're using the larger power, we subtract one from the shift because it's taken care of by the add indicator. So floor_log_2_d happens to be correct in both cases. + + } + return result; +} + +uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U32_SHIFT_PATH) { + return numer >> (more & LIBDIVIDE_32_SHIFT_MASK); + } + else { + uint32_t q = libdivide__mullhi_u32(denom->magic, numer); + if (more & LIBDIVIDE_ADD_MARKER) { + uint32_t t = ((numer - q) >> 1) + q; + return t >> (more & LIBDIVIDE_32_SHIFT_MASK); + } + else { + return q >> more; //all upper bits are 0 - don't need to mask them off + } + } +} + + +int libdivide_u32_get_algorithm(const struct libdivide_u32_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U32_SHIFT_PATH) return 0; + else if (! (more & LIBDIVIDE_ADD_MARKER)) return 1; + else return 2; +} + +uint32_t libdivide_u32_do_alg0(uint32_t numer, const struct libdivide_u32_t *denom) { + return numer >> (denom->more & LIBDIVIDE_32_SHIFT_MASK); +} + +uint32_t libdivide_u32_do_alg1(uint32_t numer, const struct libdivide_u32_t *denom) { + uint32_t q = libdivide__mullhi_u32(denom->magic, numer); + return q >> denom->more; +} + +uint32_t libdivide_u32_do_alg2(uint32_t numer, const struct libdivide_u32_t *denom) { + // denom->add != 0 + uint32_t q = libdivide__mullhi_u32(denom->magic, numer); + uint32_t t = ((numer - q) >> 1) + q; + return t >> (denom->more & LIBDIVIDE_32_SHIFT_MASK); +} + + + + +#if LIBDIVIDE_USE_SSE2 +__m128i libdivide_u32_do_vector(__m128i numers, const struct libdivide_u32_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U32_SHIFT_PATH) { + return _mm_srl_epi32(numers, libdivide_u32_to_m128i(more & LIBDIVIDE_32_SHIFT_MASK)); + } + else { + __m128i q = libdivide__mullhi_u32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + if (more & LIBDIVIDE_ADD_MARKER) { + //uint32_t t = ((numer - q) >> 1) + q; + //return t >> denom->shift; + __m128i t = _mm_add_epi32(_mm_srli_epi32(_mm_sub_epi32(numers, q), 1), q); + return _mm_srl_epi32(t, libdivide_u32_to_m128i(more & LIBDIVIDE_32_SHIFT_MASK)); + + } + else { + //q >> denom->shift + return _mm_srl_epi32(q, libdivide_u32_to_m128i(more)); + } + } +} + +__m128i libdivide_u32_do_vector_alg0(__m128i numers, const struct libdivide_u32_t *denom) { + return _mm_srl_epi32(numers, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_32_SHIFT_MASK)); +} + +__m128i libdivide_u32_do_vector_alg1(__m128i numers, const struct libdivide_u32_t *denom) { + __m128i q = libdivide__mullhi_u32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + return _mm_srl_epi32(q, libdivide_u32_to_m128i(denom->more)); +} + +__m128i libdivide_u32_do_vector_alg2(__m128i numers, const struct libdivide_u32_t *denom) { + __m128i q = libdivide__mullhi_u32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + __m128i t = _mm_add_epi32(_mm_srli_epi32(_mm_sub_epi32(numers, q), 1), q); + return _mm_srl_epi32(t, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_32_SHIFT_MASK)); +} + +#endif + +/////////// UINT64 + +struct libdivide_u64_t libdivide_u64_gen(uint64_t d) { + struct libdivide_u64_t result; + if ((d & (d - 1)) == 0) { + result.more = libdivide__count_trailing_zeros64(d) | LIBDIVIDE_U64_SHIFT_PATH; + result.magic = 0; + } + else { + const uint32_t floor_log_2_d = 63 - libdivide__count_leading_zeros64(d); + + uint64_t proposed_m, rem; + uint8_t more; + proposed_m = libdivide_128_div_64_to_64(1ULL << floor_log_2_d, 0, d, &rem); //== (1 << (64 + floor_log_2_d)) / d + + LIBDIVIDE_ASSERT(rem > 0 && rem < d); + const uint64_t e = d - rem; + + /* This power works if e < 2**floor_log_2_d. */ + if (e < (1ULL << floor_log_2_d)) { + /* This power works */ + more = floor_log_2_d; + } + else { + /* We have to use the general 65-bit algorithm. We need to compute (2**power) / d. However, we already have (2**(power-1))/d and its remainder. By doubling both, and then correcting the remainder, we can compute the larger division. */ + proposed_m += proposed_m; //don't care about overflow here - in fact, we expect it + const uint64_t twice_rem = rem + rem; + if (twice_rem >= d || twice_rem < rem) proposed_m += 1; + more = floor_log_2_d | LIBDIVIDE_ADD_MARKER; + } + result.magic = 1 + proposed_m; + result.more = more; + //result.more's shift should in general be ceil_log_2_d. But if we used the smaller power, we subtract one from the shift because we're using the smaller power. If we're using the larger power, we subtract one from the shift because it's taken care of by the add indicator. So floor_log_2_d happens to be correct in both cases, which is why we do it outside of the if statement. + } + return result; +} + +uint64_t libdivide_u64_do(uint64_t numer, const struct libdivide_u64_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U64_SHIFT_PATH) { + return numer >> (more & LIBDIVIDE_64_SHIFT_MASK); + } + else { + uint64_t q = libdivide__mullhi_u64(denom->magic, numer); + if (more & LIBDIVIDE_ADD_MARKER) { + uint64_t t = ((numer - q) >> 1) + q; + return t >> (more & LIBDIVIDE_64_SHIFT_MASK); + } + else { + return q >> more; //all upper bits are 0 - don't need to mask them off + } + } +} + + +int libdivide_u64_get_algorithm(const struct libdivide_u64_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U64_SHIFT_PATH) return 0; + else if (! (more & LIBDIVIDE_ADD_MARKER)) return 1; + else return 2; +} + +uint64_t libdivide_u64_do_alg0(uint64_t numer, const struct libdivide_u64_t *denom) { + return numer >> (denom->more & LIBDIVIDE_64_SHIFT_MASK); +} + +uint64_t libdivide_u64_do_alg1(uint64_t numer, const struct libdivide_u64_t *denom) { + uint64_t q = libdivide__mullhi_u64(denom->magic, numer); + return q >> denom->more; +} + +uint64_t libdivide_u64_do_alg2(uint64_t numer, const struct libdivide_u64_t *denom) { + uint64_t q = libdivide__mullhi_u64(denom->magic, numer); + uint64_t t = ((numer - q) >> 1) + q; + return t >> (denom->more & LIBDIVIDE_64_SHIFT_MASK); +} + +#if LIBDIVIDE_USE_SSE2 +__m128i libdivide_u64_do_vector(__m128i numers, const struct libdivide_u64_t * denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_U64_SHIFT_PATH) { + return _mm_srl_epi64(numers, libdivide_u32_to_m128i(more & LIBDIVIDE_64_SHIFT_MASK)); + } + else { + __m128i q = libdivide_mullhi_u64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + if (more & LIBDIVIDE_ADD_MARKER) { + //uint32_t t = ((numer - q) >> 1) + q; + //return t >> denom->shift; + __m128i t = _mm_add_epi64(_mm_srli_epi64(_mm_sub_epi64(numers, q), 1), q); + return _mm_srl_epi64(t, libdivide_u32_to_m128i(more & LIBDIVIDE_64_SHIFT_MASK)); + } + else { + //q >> denom->shift + return _mm_srl_epi64(q, libdivide_u32_to_m128i(more)); + } + } +} + +__m128i libdivide_u64_do_vector_alg0(__m128i numers, const struct libdivide_u64_t *denom) { + return _mm_srl_epi64(numers, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_64_SHIFT_MASK)); +} + +__m128i libdivide_u64_do_vector_alg1(__m128i numers, const struct libdivide_u64_t *denom) { + __m128i q = libdivide_mullhi_u64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + return _mm_srl_epi64(q, libdivide_u32_to_m128i(denom->more)); +} + +__m128i libdivide_u64_do_vector_alg2(__m128i numers, const struct libdivide_u64_t *denom) { + __m128i q = libdivide_mullhi_u64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + __m128i t = _mm_add_epi64(_mm_srli_epi64(_mm_sub_epi64(numers, q), 1), q); + return _mm_srl_epi64(t, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_64_SHIFT_MASK)); +} + + +#endif + +/////////// SINT32 + + +static inline int32_t libdivide__mullhi_s32(int32_t x, int32_t y) { + int64_t xl = x, yl = y; + int64_t rl = xl * yl; + return (int32_t)(rl >> 32); //needs to be arithmetic shift +} + +struct libdivide_s32_t libdivide_s32_gen(int32_t d) { + struct libdivide_s32_t result; + + /* If d is a power of 2, or negative a power of 2, we have to use a shift. This is especially important because the magic algorithm fails for -1. To check if d is a power of 2 or its inverse, it suffices to check whether its absolute value has exactly one bit set. This works even for INT_MIN, because abs(INT_MIN) == INT_MIN, and INT_MIN has one bit set and is a power of 2. */ + uint32_t absD = (uint32_t)(d < 0 ? -d : d); //gcc optimizes this to the fast abs trick + if ((absD & (absD - 1)) == 0) { //check if exactly one bit is set, don't care if absD is 0 since that's divide by zero + result.magic = 0; + result.more = libdivide__count_trailing_zeros32(absD) | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0) | LIBDIVIDE_S32_SHIFT_PATH; + } + else { + const uint32_t floor_log_2_d = 31 - libdivide__count_leading_zeros32(absD); + LIBDIVIDE_ASSERT(floor_log_2_d >= 1); + + uint8_t more; + //the dividend here is 2**(floor_log_2_d + 31), so the low 32 bit word is 0 and the high word is floor_log_2_d - 1 + uint32_t rem, proposed_m; + proposed_m = libdivide_64_div_32_to_32(1U << (floor_log_2_d - 1), 0, absD, &rem); + const uint32_t e = absD - rem; + + /* We are going to start with a power of floor_log_2_d - 1. This works if works if e < 2**floor_log_2_d. */ + if (e < (1U << floor_log_2_d)) { + /* This power works */ + more = floor_log_2_d - 1; + } + else { + /* We need to go one higher. This should not make proposed_m overflow, but it will make it negative when interpreted as an int32_t. */ + proposed_m += proposed_m; + const uint32_t twice_rem = rem + rem; + if (twice_rem >= absD || twice_rem < rem) proposed_m += 1; + more = floor_log_2_d | LIBDIVIDE_ADD_MARKER | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0); //use the general algorithm + } + proposed_m += 1; + result.magic = (d < 0 ? -(int32_t)proposed_m : (int32_t)proposed_m); + result.more = more; + + } + return result; +} + +int32_t libdivide_s32_do(int32_t numer, const struct libdivide_s32_t *denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_S32_SHIFT_PATH) { + uint8_t shifter = more & LIBDIVIDE_32_SHIFT_MASK; + int32_t q = numer + ((numer >> 31) & ((1 << shifter) - 1)); + q = q >> shifter; + int32_t shiftMask = (int8_t)more >> 7; //must be arithmetic shift and then sign-extend + q = (q ^ shiftMask) - shiftMask; + return q; + } + else { + int32_t q = libdivide__mullhi_s32(denom->magic, numer); + if (more & LIBDIVIDE_ADD_MARKER) { + int32_t sign = (int8_t)more >> 7; //must be arithmetic shift and then sign extend + q += ((numer ^ sign) - sign); + } + q >>= more & LIBDIVIDE_32_SHIFT_MASK; + q += (q < 0); + return q; + } +} + +int libdivide_s32_get_algorithm(const struct libdivide_s32_t *denom) { + uint8_t more = denom->more; + int positiveDivisor = ! (more & LIBDIVIDE_NEGATIVE_DIVISOR); + if (more & LIBDIVIDE_S32_SHIFT_PATH) return (positiveDivisor ? 0 : 1); + else if (more & LIBDIVIDE_ADD_MARKER) return (positiveDivisor ? 2 : 3); + else return 4; +} + +int32_t libdivide_s32_do_alg0(int32_t numer, const struct libdivide_s32_t *denom) { + uint8_t shifter = denom->more & LIBDIVIDE_32_SHIFT_MASK; + int32_t q = numer + ((numer >> 31) & ((1 << shifter) - 1)); + return q >> shifter; +} + +int32_t libdivide_s32_do_alg1(int32_t numer, const struct libdivide_s32_t *denom) { + uint8_t shifter = denom->more & LIBDIVIDE_32_SHIFT_MASK; + int32_t q = numer + ((numer >> 31) & ((1 << shifter) - 1)); + return - (q >> shifter); +} + +int32_t libdivide_s32_do_alg2(int32_t numer, const struct libdivide_s32_t *denom) { + int32_t q = libdivide__mullhi_s32(denom->magic, numer); + q += numer; + q >>= denom->more & LIBDIVIDE_32_SHIFT_MASK; + q += (q < 0); + return q; +} + +int32_t libdivide_s32_do_alg3(int32_t numer, const struct libdivide_s32_t *denom) { + int32_t q = libdivide__mullhi_s32(denom->magic, numer); + q -= numer; + q >>= denom->more & LIBDIVIDE_32_SHIFT_MASK; + q += (q < 0); + return q; +} + +int32_t libdivide_s32_do_alg4(int32_t numer, const struct libdivide_s32_t *denom) { + int32_t q = libdivide__mullhi_s32(denom->magic, numer); + q >>= denom->more & LIBDIVIDE_32_SHIFT_MASK; + q += (q < 0); + return q; +} + +#if LIBDIVIDE_USE_SSE2 +__m128i libdivide_s32_do_vector(__m128i numers, const struct libdivide_s32_t * denom) { + uint8_t more = denom->more; + if (more & LIBDIVIDE_S32_SHIFT_PATH) { + uint32_t shifter = more & LIBDIVIDE_32_SHIFT_MASK; + __m128i roundToZeroTweak = _mm_set1_epi32((1 << shifter) - 1); //could use _mm_srli_epi32 with an all -1 register + __m128i q = _mm_add_epi32(numers, _mm_and_si128(_mm_srai_epi32(numers, 31), roundToZeroTweak)); //q = numer + ((numer >> 31) & roundToZeroTweak); + q = _mm_sra_epi32(q, libdivide_u32_to_m128i(shifter)); // q = q >> shifter + __m128i shiftMask = _mm_set1_epi32((int32_t)((int8_t)more >> 7)); //set all bits of shift mask = to the sign bit of more + q = _mm_sub_epi32(_mm_xor_si128(q, shiftMask), shiftMask); //q = (q ^ shiftMask) - shiftMask; + return q; + } + else { + __m128i q = libdivide_mullhi_s32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + if (more & LIBDIVIDE_ADD_MARKER) { + __m128i sign = _mm_set1_epi32((int32_t)(int8_t)more >> 7); //must be arithmetic shift + q = _mm_add_epi32(q, _mm_sub_epi32(_mm_xor_si128(numers, sign), sign)); // q += ((numer ^ sign) - sign); + } + q = _mm_sra_epi32(q, libdivide_u32_to_m128i(more & LIBDIVIDE_32_SHIFT_MASK)); //q >>= shift + q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); // q += (q < 0) + return q; + } +} + +__m128i libdivide_s32_do_vector_alg0(__m128i numers, const struct libdivide_s32_t *denom) { + uint8_t shifter = denom->more & LIBDIVIDE_32_SHIFT_MASK; + __m128i roundToZeroTweak = _mm_set1_epi32((1 << shifter) - 1); + __m128i q = _mm_add_epi32(numers, _mm_and_si128(_mm_srai_epi32(numers, 31), roundToZeroTweak)); + return _mm_sra_epi32(q, libdivide_u32_to_m128i(shifter)); +} + +__m128i libdivide_s32_do_vector_alg1(__m128i numers, const struct libdivide_s32_t *denom) { + uint8_t shifter = denom->more & LIBDIVIDE_32_SHIFT_MASK; + __m128i roundToZeroTweak = _mm_set1_epi32((1 << shifter) - 1); + __m128i q = _mm_add_epi32(numers, _mm_and_si128(_mm_srai_epi32(numers, 31), roundToZeroTweak)); + return _mm_sub_epi32(_mm_setzero_si128(), _mm_sra_epi32(q, libdivide_u32_to_m128i(shifter))); +} + +__m128i libdivide_s32_do_vector_alg2(__m128i numers, const struct libdivide_s32_t *denom) { + __m128i q = libdivide_mullhi_s32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + q = _mm_add_epi32(q, numers); + q = _mm_sra_epi32(q, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_32_SHIFT_MASK)); + q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); + return q; +} + +__m128i libdivide_s32_do_vector_alg3(__m128i numers, const struct libdivide_s32_t *denom) { + __m128i q = libdivide_mullhi_s32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + q = _mm_sub_epi32(q, numers); + q = _mm_sra_epi32(q, libdivide_u32_to_m128i(denom->more & LIBDIVIDE_32_SHIFT_MASK)); + q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); + return q; +} + +__m128i libdivide_s32_do_vector_alg4(__m128i numers, const struct libdivide_s32_t *denom) { + __m128i q = libdivide_mullhi_s32_flat_vector(numers, _mm_set1_epi32(denom->magic)); + q = _mm_sra_epi32(q, libdivide_u32_to_m128i(denom->more)); //q >>= shift + q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); // q += (q < 0) + return q; +} +#endif + +///////////// SINT64 + + +struct libdivide_s64_t libdivide_s64_gen(int64_t d) { + struct libdivide_s64_t result; + + /* If d is a power of 2, or negative a power of 2, we have to use a shift. This is especially important because the magic algorithm fails for -1. To check if d is a power of 2 or its inverse, it suffices to check whether its absolute value has exactly one bit set. This works even for INT_MIN, because abs(INT_MIN) == INT_MIN, and INT_MIN has one bit set and is a power of 2. */ + const uint64_t absD = (uint64_t)(d < 0 ? -d : d); //gcc optimizes this to the fast abs trick + if ((absD & (absD - 1)) == 0) { //check if exactly one bit is set, don't care if absD is 0 since that's divide by zero + result.more = libdivide__count_trailing_zeros64(absD) | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0); + result.magic = 0; + } + else { + const uint32_t floor_log_2_d = 63 - libdivide__count_leading_zeros64(absD); + + //the dividend here is 2**(floor_log_2_d + 63), so the low 64 bit word is 0 and the high word is floor_log_2_d - 1 + uint8_t more; + uint64_t rem, proposed_m; + proposed_m = libdivide_128_div_64_to_64(1ULL << (floor_log_2_d - 1), 0, absD, &rem); + const uint64_t e = absD - rem; + + /* We are going to start with a power of floor_log_2_d - 1. This works if works if e < 2**floor_log_2_d. */ + if (e < (1ULL << floor_log_2_d)) { + /* This power works */ + more = floor_log_2_d - 1; + } + else { + /* We need to go one higher. This should not make proposed_m overflow, but it will make it negative when interpreted as an int32_t. */ + proposed_m += proposed_m; + const uint64_t twice_rem = rem + rem; + if (twice_rem >= absD || twice_rem < rem) proposed_m += 1; + more = floor_log_2_d | LIBDIVIDE_ADD_MARKER | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0); + } + proposed_m += 1; + result.more = more; + result.magic = (d < 0 ? -(int64_t)proposed_m : (int64_t)proposed_m); + } + return result; +} + +int64_t libdivide_s64_do(int64_t numer, const struct libdivide_s64_t *denom) { + uint8_t more = denom->more; + int64_t magic = denom->magic; + if (magic == 0) { //shift path + uint32_t shifter = more & LIBDIVIDE_64_SHIFT_MASK; + int64_t q = numer + ((numer >> 63) & ((1LL << shifter) - 1)); + q = q >> shifter; + int64_t shiftMask = (int8_t)more >> 7; //must be arithmetic shift and then sign-extend + q = (q ^ shiftMask) - shiftMask; + return q; + } + else { + int64_t q = libdivide__mullhi_s64(magic, numer); + if (more & LIBDIVIDE_ADD_MARKER) { + int64_t sign = (int8_t)more >> 7; //must be arithmetic shift and then sign extend + q += ((numer ^ sign) - sign); + } + q >>= more & LIBDIVIDE_64_SHIFT_MASK; + q += (q < 0); + return q; + } +} + + +int libdivide_s64_get_algorithm(const struct libdivide_s64_t *denom) { + uint8_t more = denom->more; + int positiveDivisor = ! (more & LIBDIVIDE_NEGATIVE_DIVISOR); + if (denom->magic == 0) return (positiveDivisor ? 0 : 1); //shift path + else if (more & LIBDIVIDE_ADD_MARKER) return (positiveDivisor ? 2 : 3); + else return 4; +} + +int64_t libdivide_s64_do_alg0(int64_t numer, const struct libdivide_s64_t *denom) { + uint32_t shifter = denom->more & LIBDIVIDE_64_SHIFT_MASK; + int64_t q = numer + ((numer >> 63) & ((1LL << shifter) - 1)); + return q >> shifter; +} + +int64_t libdivide_s64_do_alg1(int64_t numer, const struct libdivide_s64_t *denom) { + //denom->shifter != -1 && demo->shiftMask != 0 + uint32_t shifter = denom->more & LIBDIVIDE_64_SHIFT_MASK; + int64_t q = numer + ((numer >> 63) & ((1LL << shifter) - 1)); + return - (q >> shifter); +} + +int64_t libdivide_s64_do_alg2(int64_t numer, const struct libdivide_s64_t *denom) { + int64_t q = libdivide__mullhi_s64(denom->magic, numer); + q += numer; + q >>= denom->more & LIBDIVIDE_64_SHIFT_MASK; + q += (q < 0); + return q; +} + +int64_t libdivide_s64_do_alg3(int64_t numer, const struct libdivide_s64_t *denom) { + int64_t q = libdivide__mullhi_s64(denom->magic, numer); + q -= numer; + q >>= denom->more & LIBDIVIDE_64_SHIFT_MASK; + q += (q < 0); + return q; +} + +int64_t libdivide_s64_do_alg4(int64_t numer, const struct libdivide_s64_t *denom) { + int64_t q = libdivide__mullhi_s64(denom->magic, numer); + q >>= denom->more; + q += (q < 0); + return q; +} + + +#if LIBDIVIDE_USE_SSE2 +__m128i libdivide_s64_do_vector(__m128i numers, const struct libdivide_s64_t * denom) { + uint8_t more = denom->more; + int64_t magic = denom->magic; + if (magic == 0) { //shift path + uint32_t shifter = more & LIBDIVIDE_64_SHIFT_MASK; + __m128i roundToZeroTweak = libdivide__u64_to_m128((1LL << shifter) - 1); + __m128i q = _mm_add_epi64(numers, _mm_and_si128(libdivide_s64_signbits(numers), roundToZeroTweak)); //q = numer + ((numer >> 63) & roundToZeroTweak); + q = libdivide_s64_shift_right_vector(q, shifter); // q = q >> shifter + __m128i shiftMask = _mm_set1_epi32((int32_t)((int8_t)more >> 7)); + q = _mm_sub_epi64(_mm_xor_si128(q, shiftMask), shiftMask); //q = (q ^ shiftMask) - shiftMask; + return q; + } + else { + __m128i q = libdivide_mullhi_s64_flat_vector(numers, libdivide__u64_to_m128(magic)); + if (more & LIBDIVIDE_ADD_MARKER) { + __m128i sign = _mm_set1_epi32((int32_t)((int8_t)more >> 7)); //must be arithmetic shift + q = _mm_add_epi64(q, _mm_sub_epi64(_mm_xor_si128(numers, sign), sign)); // q += ((numer ^ sign) - sign); + } + q = libdivide_s64_shift_right_vector(q, more & LIBDIVIDE_64_SHIFT_MASK); //q >>= denom->mult_path.shift + q = _mm_add_epi64(q, _mm_srli_epi64(q, 63)); // q += (q < 0) + return q; + } +} + +__m128i libdivide_s64_do_vector_alg0(__m128i numers, const struct libdivide_s64_t *denom) { + uint32_t shifter = denom->more & LIBDIVIDE_64_SHIFT_MASK; + __m128i roundToZeroTweak = libdivide__u64_to_m128((1LL << shifter) - 1); + __m128i q = _mm_add_epi64(numers, _mm_and_si128(libdivide_s64_signbits(numers), roundToZeroTweak)); + q = libdivide_s64_shift_right_vector(q, shifter); + return q; +} + +__m128i libdivide_s64_do_vector_alg1(__m128i numers, const struct libdivide_s64_t *denom) { + uint32_t shifter = denom->more & LIBDIVIDE_64_SHIFT_MASK; + __m128i roundToZeroTweak = libdivide__u64_to_m128((1LL << shifter) - 1); + __m128i q = _mm_add_epi64(numers, _mm_and_si128(libdivide_s64_signbits(numers), roundToZeroTweak)); + q = libdivide_s64_shift_right_vector(q, shifter); + return _mm_sub_epi64(_mm_setzero_si128(), q); +} + +__m128i libdivide_s64_do_vector_alg2(__m128i numers, const struct libdivide_s64_t *denom) { + __m128i q = libdivide_mullhi_s64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + q = _mm_add_epi64(q, numers); + q = libdivide_s64_shift_right_vector(q, denom->more & LIBDIVIDE_64_SHIFT_MASK); + q = _mm_add_epi64(q, _mm_srli_epi64(q, 63)); // q += (q < 0) + return q; +} + +__m128i libdivide_s64_do_vector_alg3(__m128i numers, const struct libdivide_s64_t *denom) { + __m128i q = libdivide_mullhi_s64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + q = _mm_sub_epi64(q, numers); + q = libdivide_s64_shift_right_vector(q, denom->more & LIBDIVIDE_64_SHIFT_MASK); + q = _mm_add_epi64(q, _mm_srli_epi64(q, 63)); // q += (q < 0) + return q; +} + +__m128i libdivide_s64_do_vector_alg4(__m128i numers, const struct libdivide_s64_t *denom) { + __m128i q = libdivide_mullhi_s64_flat_vector(numers, libdivide__u64_to_m128(denom->magic)); + q = libdivide_s64_shift_right_vector(q, denom->more); + q = _mm_add_epi64(q, _mm_srli_epi64(q, 63)); + return q; +} + +#endif + +/////////// C++ stuff + +#ifdef __cplusplus + +/* The C++ template design here is a total mess. This needs to be fixed by someone better at templates than I. The current design is: + +- The base is a template divider_base that takes the integer type, the libdivide struct, a generating function, a get algorithm function, a do function, and either a do vector function or a dummy int. +- The base has storage for the libdivide struct. This is the only storage (so the C++ class should be no larger than the libdivide struct). + +- Above that, there's divider_mid. This is an empty struct by default, but it is specialized against our four int types. divider_mid contains a template struct algo, that contains a typedef for a specialization of divider_base. struct algo is specialized to take an "algorithm number," where -1 means to use the general algorithm. + +- Publicly we have class divider, which inherits from divider_mid::algo. This also take an algorithm number, which defaults to -1 (the general algorithm). +- divider has a operator / which allows you to use a divider as the divisor in a quotient expression. + +*/ + +namespace libdivide_internal { + +#if LIBDIVIDE_USE_SSE2 +#define MAYBE_VECTOR(x) x +#define MAYBE_VECTOR_PARAM __m128i vector_func(__m128i, const DenomType *) +#else +#define MAYBE_VECTOR(x) 0 +#define MAYBE_VECTOR_PARAM int vector_func +#endif + + /* Some bogus unswitch functions for unsigned types so the same (presumably templated) code can work for both signed and unsigned. */ + uint32_t crash_u32(uint32_t, const libdivide_u32_t *) { abort(); return *(uint32_t *)NULL; } + uint64_t crash_u64(uint64_t, const libdivide_u64_t *) { abort(); return *(uint64_t *)NULL; } +#if LIBDIVIDE_USE_SSE2 + __m128i crash_u32_vector(__m128i, const libdivide_u32_t *) { abort(); return *(__m128i *)NULL; } + __m128i crash_u64_vector(__m128i, const libdivide_u64_t *) { abort(); return *(__m128i *)NULL; } +#endif + + template + class divider_base { + public: + DenomType denom; + divider_base(IntType d) : denom(gen_func(d)) { } + divider_base(const DenomType & d) : denom(d) { } + + IntType perform_divide(IntType val) const { return do_func(val, &denom); } +#if LIBDIVIDE_USE_SSE2 + __m128i perform_divide_vector(__m128i val) const { return vector_func(val, &denom); } +#endif + + int get_algorithm() const { return get_algo(&denom); } + }; + + + template struct divider_mid { }; + + template<> struct divider_mid { + typedef uint32_t IntType; + typedef struct libdivide_u32_t DenomType; + template struct denom { + typedef divider_base divider; + }; + + template struct algo { }; + template struct algo<-1, J> { typedef denom::divider divider; }; + template struct algo<0, J> { typedef denom::divider divider; }; + template struct algo<1, J> { typedef denom::divider divider; }; + template struct algo<2, J> { typedef denom::divider divider; }; + + /* Define two more bogus ones so that the same (templated, presumably) code can handle both signed and unsigned */ + template struct algo<3, J> { typedef denom::divider divider; }; + template struct algo<4, J> { typedef denom::divider divider; }; + + }; + + template<> struct divider_mid { + typedef int32_t IntType; + typedef struct libdivide_s32_t DenomType; + template struct denom { + typedef divider_base divider; + }; + + + template struct algo { }; + template struct algo<-1, J> { typedef denom::divider divider; }; + template struct algo<0, J> { typedef denom::divider divider; }; + template struct algo<1, J> { typedef denom::divider divider; }; + template struct algo<2, J> { typedef denom::divider divider; }; + template struct algo<3, J> { typedef denom::divider divider; }; + template struct algo<4, J> { typedef denom::divider divider; }; + + }; + + template<> struct divider_mid { + typedef uint64_t IntType; + typedef struct libdivide_u64_t DenomType; + template struct denom { + typedef divider_base divider; + }; + + template struct algo { }; + template struct algo<-1, J> { typedef denom::divider divider; }; + template struct algo<0, J> { typedef denom::divider divider; }; + template struct algo<1, J> { typedef denom::divider divider; }; + template struct algo<2, J> { typedef denom::divider divider; }; + + /* Define two more bogus ones so that the same (templated, presumably) code can handle both signed and unsigned */ + template struct algo<3, J> { typedef denom::divider divider; }; + template struct algo<4, J> { typedef denom::divider divider; }; + + + }; + + template<> struct divider_mid { + typedef int64_t IntType; + typedef struct libdivide_s64_t DenomType; + template struct denom { + typedef divider_base divider; + }; + + template struct algo { }; + template struct algo<-1, J> { typedef denom::divider divider; }; + template struct algo<0, J> { typedef denom::divider divider; }; + template struct algo<1, J> { typedef denom::divider divider; }; + template struct algo<2, J> { typedef denom::divider divider; }; + template struct algo<3, J> { typedef denom::divider divider; }; + template struct algo<4, J> { typedef denom::divider divider; }; + }; + +} + +template +class divider +{ + private: + typename libdivide_internal::divider_mid::template algo::divider sub; + template friend divider unswitch(const divider & d); + divider(const typename libdivide_internal::divider_mid::DenomType & denom) : sub(denom) { } + + public: + + /* Ordinary constructor, that takes the divisor as a parameter. */ + divider(T n) : sub(n) { } + + /* Default constructor, that divides by 1 */ + divider() : sub(1) { } + + /* Divides the parameter by the divisor, returning the quotient */ + T perform_divide(T val) const { return sub.perform_divide(val); } + +#if LIBDIVIDE_USE_SSE2 + /* Treats the vector as either two or four packed values (depending on the size), and divides each of them by the divisor, returning the packed quotients. */ + __m128i perform_divide_vector(__m128i val) const { return sub.perform_divide_vector(val); } +#endif + + /* Returns the index of algorithm, for use in the unswitch function */ + int get_algorithm() const { return sub.get_algorithm(); } // returns the algorithm for unswitching + + /* operator== */ + bool operator==(const divider & him) const { return sub.denom.magic == him.sub.denom.magic && sub.denom.more == him.sub.denom.more; } + + bool operator!=(const divider & him) const { return ! (*this == him); } +}; + +/* Returns a divider specialized for the given algorithm. */ +template +divider unswitch(const divider & d) { return divider(d.sub.denom); } + +/* Overload of the / operator for scalar division. */ +template +int_type operator/(int_type numer, const divider & denom) { + return denom.perform_divide(numer); +} + +#if LIBDIVIDE_USE_SSE2 +/* Overload of the / operator for vector division. */ +template +__m128i operator/(__m128i numer, const divider & denom) { + return denom.perform_divide_vector(numer); +} +#endif + + +#endif //__cplusplus + +#endif //LIBDIVIDE_HEADER_ONLY +#ifdef __cplusplus +} //close namespace libdivide +} //close anonymous namespace +#endif diff --git a/contrib/libdouble-conversion/CMakeLists.txt b/contrib/libdouble-conversion/CMakeLists.txt new file mode 100644 index 00000000000..6b19e861b1e --- /dev/null +++ b/contrib/libdouble-conversion/CMakeLists.txt @@ -0,0 +1,20 @@ +add_library (double-conversion +double-conversion/bignum.cc +double-conversion/bignum-dtoa.cc +double-conversion/bignum-dtoa.h +double-conversion/bignum.h +double-conversion/cached-powers.cc +double-conversion/cached-powers.h +double-conversion/diy-fp.cc +double-conversion/diy-fp.h +double-conversion/double-conversion.cc +double-conversion/double-conversion.h +double-conversion/fast-dtoa.cc +double-conversion/fast-dtoa.h +double-conversion/fixed-dtoa.cc +double-conversion/fixed-dtoa.h +double-conversion/ieee.h +double-conversion/strtod.cc +double-conversion/strtod.h +double-conversion/utils.h +) diff --git a/contrib/libdouble-conversion/COPYING b/contrib/libdouble-conversion/COPYING new file mode 100644 index 00000000000..933718a9ef9 --- /dev/null +++ b/contrib/libdouble-conversion/COPYING @@ -0,0 +1,26 @@ +Copyright 2006-2011, the V8 project authors. All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/contrib/libdouble-conversion/README b/contrib/libdouble-conversion/README new file mode 100644 index 00000000000..6d6787ae0b4 --- /dev/null +++ b/contrib/libdouble-conversion/README @@ -0,0 +1 @@ +https://github.com/google/double-conversion/tree/b2e9a94943dc9f64acc2967eebb241bf2fc10d5b diff --git a/contrib/libdouble-conversion/double-conversion/bignum-dtoa.cc b/contrib/libdouble-conversion/double-conversion/bignum-dtoa.cc new file mode 100644 index 00000000000..f1ad7a5ae8d --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/bignum-dtoa.cc @@ -0,0 +1,641 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include + +#include "bignum-dtoa.h" + +#include "bignum.h" +#include "ieee.h" + +namespace double_conversion { + +static int NormalizedExponent(uint64_t significand, int exponent) { + ASSERT(significand != 0); + while ((significand & Double::kHiddenBit) == 0) { + significand = significand << 1; + exponent = exponent - 1; + } + return exponent; +} + + +// Forward declarations: +// Returns an estimation of k such that 10^(k-1) <= v < 10^k. +static int EstimatePower(int exponent); +// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator +// and denominator. +static void InitialScaledStartValues(uint64_t significand, + int exponent, + bool lower_boundary_is_closer, + int estimated_power, + bool need_boundary_deltas, + Bignum* numerator, + Bignum* denominator, + Bignum* delta_minus, + Bignum* delta_plus); +// Multiplies numerator/denominator so that its values lies in the range 1-10. +// Returns decimal_point s.t. +// v = numerator'/denominator' * 10^(decimal_point-1) +// where numerator' and denominator' are the values of numerator and +// denominator after the call to this function. +static void FixupMultiply10(int estimated_power, bool is_even, + int* decimal_point, + Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus); +// Generates digits from the left to the right and stops when the generated +// digits yield the shortest decimal representation of v. +static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus, + bool is_even, + Vector buffer, int* length); +// Generates 'requested_digits' after the decimal point. +static void BignumToFixed(int requested_digits, int* decimal_point, + Bignum* numerator, Bignum* denominator, + Vector(buffer), int* length); +// Generates 'count' digits of numerator/denominator. +// Once 'count' digits have been produced rounds the result depending on the +// remainder (remainders of exactly .5 round upwards). Might update the +// decimal_point when rounding up (for example for 0.9999). +static void GenerateCountedDigits(int count, int* decimal_point, + Bignum* numerator, Bignum* denominator, + Vector(buffer), int* length); + + +void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, + Vector buffer, int* length, int* decimal_point) { + ASSERT(v > 0); + ASSERT(!Double(v).IsSpecial()); + uint64_t significand; + int exponent; + bool lower_boundary_is_closer; + if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) { + float f = static_cast(v); + ASSERT(f == v); + significand = Single(f).Significand(); + exponent = Single(f).Exponent(); + lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser(); + } else { + significand = Double(v).Significand(); + exponent = Double(v).Exponent(); + lower_boundary_is_closer = Double(v).LowerBoundaryIsCloser(); + } + bool need_boundary_deltas = + (mode == BIGNUM_DTOA_SHORTEST || mode == BIGNUM_DTOA_SHORTEST_SINGLE); + + bool is_even = (significand & 1) == 0; + int normalized_exponent = NormalizedExponent(significand, exponent); + // estimated_power might be too low by 1. + int estimated_power = EstimatePower(normalized_exponent); + + // Shortcut for Fixed. + // The requested digits correspond to the digits after the point. If the + // number is much too small, then there is no need in trying to get any + // digits. + if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) { + buffer[0] = '\0'; + *length = 0; + // Set decimal-point to -requested_digits. This is what Gay does. + // Note that it should not have any effect anyways since the string is + // empty. + *decimal_point = -requested_digits; + return; + } + + Bignum numerator; + Bignum denominator; + Bignum delta_minus; + Bignum delta_plus; + // Make sure the bignum can grow large enough. The smallest double equals + // 4e-324. In this case the denominator needs fewer than 324*4 binary digits. + // The maximum double is 1.7976931348623157e308 which needs fewer than + // 308*4 binary digits. + ASSERT(Bignum::kMaxSignificantBits >= 324*4); + InitialScaledStartValues(significand, exponent, lower_boundary_is_closer, + estimated_power, need_boundary_deltas, + &numerator, &denominator, + &delta_minus, &delta_plus); + // We now have v = (numerator / denominator) * 10^estimated_power. + FixupMultiply10(estimated_power, is_even, decimal_point, + &numerator, &denominator, + &delta_minus, &delta_plus); + // We now have v = (numerator / denominator) * 10^(decimal_point-1), and + // 1 <= (numerator + delta_plus) / denominator < 10 + switch (mode) { + case BIGNUM_DTOA_SHORTEST: + case BIGNUM_DTOA_SHORTEST_SINGLE: + GenerateShortestDigits(&numerator, &denominator, + &delta_minus, &delta_plus, + is_even, buffer, length); + break; + case BIGNUM_DTOA_FIXED: + BignumToFixed(requested_digits, decimal_point, + &numerator, &denominator, + buffer, length); + break; + case BIGNUM_DTOA_PRECISION: + GenerateCountedDigits(requested_digits, decimal_point, + &numerator, &denominator, + buffer, length); + break; + default: + UNREACHABLE(); + } + buffer[*length] = '\0'; +} + + +// The procedure starts generating digits from the left to the right and stops +// when the generated digits yield the shortest decimal representation of v. A +// decimal representation of v is a number lying closer to v than to any other +// double, so it converts to v when read. +// +// This is true if d, the decimal representation, is between m- and m+, the +// upper and lower boundaries. d must be strictly between them if !is_even. +// m- := (numerator - delta_minus) / denominator +// m+ := (numerator + delta_plus) / denominator +// +// Precondition: 0 <= (numerator+delta_plus) / denominator < 10. +// If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit +// will be produced. This should be the standard precondition. +static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus, + bool is_even, + Vector buffer, int* length) { + // Small optimization: if delta_minus and delta_plus are the same just reuse + // one of the two bignums. + if (Bignum::Equal(*delta_minus, *delta_plus)) { + delta_plus = delta_minus; + } + *length = 0; + for (;;) { + uint16_t digit; + digit = numerator->DivideModuloIntBignum(*denominator); + ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. + // digit = numerator / denominator (integer division). + // numerator = numerator % denominator. + buffer[(*length)++] = static_cast(digit + '0'); + + // Can we stop already? + // If the remainder of the division is less than the distance to the lower + // boundary we can stop. In this case we simply round down (discarding the + // remainder). + // Similarly we test if we can round up (using the upper boundary). + bool in_delta_room_minus; + bool in_delta_room_plus; + if (is_even) { + in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus); + } else { + in_delta_room_minus = Bignum::Less(*numerator, *delta_minus); + } + if (is_even) { + in_delta_room_plus = + Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; + } else { + in_delta_room_plus = + Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; + } + if (!in_delta_room_minus && !in_delta_room_plus) { + // Prepare for next iteration. + numerator->Times10(); + delta_minus->Times10(); + // We optimized delta_plus to be equal to delta_minus (if they share the + // same value). So don't multiply delta_plus if they point to the same + // object. + if (delta_minus != delta_plus) { + delta_plus->Times10(); + } + } else if (in_delta_room_minus && in_delta_room_plus) { + // Let's see if 2*numerator < denominator. + // If yes, then the next digit would be < 5 and we can round down. + int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator); + if (compare < 0) { + // Remaining digits are less than .5. -> Round down (== do nothing). + } else if (compare > 0) { + // Remaining digits are more than .5 of denominator. -> Round up. + // Note that the last digit could not be a '9' as otherwise the whole + // loop would have stopped earlier. + // We still have an assert here in case the preconditions were not + // satisfied. + ASSERT(buffer[(*length) - 1] != '9'); + buffer[(*length) - 1]++; + } else { + // Halfway case. + // TODO(floitsch): need a way to solve half-way cases. + // For now let's round towards even (since this is what Gay seems to + // do). + + if ((buffer[(*length) - 1] - '0') % 2 == 0) { + // Round down => Do nothing. + } else { + ASSERT(buffer[(*length) - 1] != '9'); + buffer[(*length) - 1]++; + } + } + return; + } else if (in_delta_room_minus) { + // Round down (== do nothing). + return; + } else { // in_delta_room_plus + // Round up. + // Note again that the last digit could not be '9' since this would have + // stopped the loop earlier. + // We still have an ASSERT here, in case the preconditions were not + // satisfied. + ASSERT(buffer[(*length) -1] != '9'); + buffer[(*length) - 1]++; + return; + } + } +} + + +// Let v = numerator / denominator < 10. +// Then we generate 'count' digits of d = x.xxxxx... (without the decimal point) +// from left to right. Once 'count' digits have been produced we decide wether +// to round up or down. Remainders of exactly .5 round upwards. Numbers such +// as 9.999999 propagate a carry all the way, and change the +// exponent (decimal_point), when rounding upwards. +static void GenerateCountedDigits(int count, int* decimal_point, + Bignum* numerator, Bignum* denominator, + Vector buffer, int* length) { + ASSERT(count >= 0); + for (int i = 0; i < count - 1; ++i) { + uint16_t digit; + digit = numerator->DivideModuloIntBignum(*denominator); + ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. + // digit = numerator / denominator (integer division). + // numerator = numerator % denominator. + buffer[i] = static_cast(digit + '0'); + // Prepare for next iteration. + numerator->Times10(); + } + // Generate the last digit. + uint16_t digit; + digit = numerator->DivideModuloIntBignum(*denominator); + if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { + digit++; + } + ASSERT(digit <= 10); + buffer[count - 1] = static_cast(digit + '0'); + // Correct bad digits (in case we had a sequence of '9's). Propagate the + // carry until we hat a non-'9' or til we reach the first digit. + for (int i = count - 1; i > 0; --i) { + if (buffer[i] != '0' + 10) break; + buffer[i] = '0'; + buffer[i - 1]++; + } + if (buffer[0] == '0' + 10) { + // Propagate a carry past the top place. + buffer[0] = '1'; + (*decimal_point)++; + } + *length = count; +} + + +// Generates 'requested_digits' after the decimal point. It might omit +// trailing '0's. If the input number is too small then no digits at all are +// generated (ex.: 2 fixed digits for 0.00001). +// +// Input verifies: 1 <= (numerator + delta) / denominator < 10. +static void BignumToFixed(int requested_digits, int* decimal_point, + Bignum* numerator, Bignum* denominator, + Vector(buffer), int* length) { + // Note that we have to look at more than just the requested_digits, since + // a number could be rounded up. Example: v=0.5 with requested_digits=0. + // Even though the power of v equals 0 we can't just stop here. + if (-(*decimal_point) > requested_digits) { + // The number is definitively too small. + // Ex: 0.001 with requested_digits == 1. + // Set decimal-point to -requested_digits. This is what Gay does. + // Note that it should not have any effect anyways since the string is + // empty. + *decimal_point = -requested_digits; + *length = 0; + return; + } else if (-(*decimal_point) == requested_digits) { + // We only need to verify if the number rounds down or up. + // Ex: 0.04 and 0.06 with requested_digits == 1. + ASSERT(*decimal_point == -requested_digits); + // Initially the fraction lies in range (1, 10]. Multiply the denominator + // by 10 so that we can compare more easily. + denominator->Times10(); + if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { + // If the fraction is >= 0.5 then we have to include the rounded + // digit. + buffer[0] = '1'; + *length = 1; + (*decimal_point)++; + } else { + // Note that we caught most of similar cases earlier. + *length = 0; + } + return; + } else { + // The requested digits correspond to the digits after the point. + // The variable 'needed_digits' includes the digits before the point. + int needed_digits = (*decimal_point) + requested_digits; + GenerateCountedDigits(needed_digits, decimal_point, + numerator, denominator, + buffer, length); + } +} + + +// Returns an estimation of k such that 10^(k-1) <= v < 10^k where +// v = f * 2^exponent and 2^52 <= f < 2^53. +// v is hence a normalized double with the given exponent. The output is an +// approximation for the exponent of the decimal approimation .digits * 10^k. +// +// The result might undershoot by 1 in which case 10^k <= v < 10^k+1. +// Note: this property holds for v's upper boundary m+ too. +// 10^k <= m+ < 10^k+1. +// (see explanation below). +// +// Examples: +// EstimatePower(0) => 16 +// EstimatePower(-52) => 0 +// +// Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0. +static int EstimatePower(int exponent) { + // This function estimates log10 of v where v = f*2^e (with e == exponent). + // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)). + // Note that f is bounded by its container size. Let p = 53 (the double's + // significand size). Then 2^(p-1) <= f < 2^p. + // + // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close + // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)). + // The computed number undershoots by less than 0.631 (when we compute log3 + // and not log10). + // + // Optimization: since we only need an approximated result this computation + // can be performed on 64 bit integers. On x86/x64 architecture the speedup is + // not really measurable, though. + // + // Since we want to avoid overshooting we decrement by 1e10 so that + // floating-point imprecisions don't affect us. + // + // Explanation for v's boundary m+: the computation takes advantage of + // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement + // (even for denormals where the delta can be much more important). + + const double k1Log10 = 0.30102999566398114; // 1/lg(10) + + // For doubles len(f) == 53 (don't forget the hidden bit). + const int kSignificandSize = Double::kSignificandSize; + double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10); + return static_cast(estimate); +} + + +// See comments for InitialScaledStartValues. +static void InitialScaledStartValuesPositiveExponent( + uint64_t significand, int exponent, + int estimated_power, bool need_boundary_deltas, + Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus) { + // A positive exponent implies a positive power. + ASSERT(estimated_power >= 0); + // Since the estimated_power is positive we simply multiply the denominator + // by 10^estimated_power. + + // numerator = v. + numerator->AssignUInt64(significand); + numerator->ShiftLeft(exponent); + // denominator = 10^estimated_power. + denominator->AssignPowerUInt16(10, estimated_power); + + if (need_boundary_deltas) { + // Introduce a common denominator so that the deltas to the boundaries are + // integers. + denominator->ShiftLeft(1); + numerator->ShiftLeft(1); + // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common + // denominator (of 2) delta_plus equals 2^e. + delta_plus->AssignUInt16(1); + delta_plus->ShiftLeft(exponent); + // Same for delta_minus. The adjustments if f == 2^p-1 are done later. + delta_minus->AssignUInt16(1); + delta_minus->ShiftLeft(exponent); + } +} + + +// See comments for InitialScaledStartValues +static void InitialScaledStartValuesNegativeExponentPositivePower( + uint64_t significand, int exponent, + int estimated_power, bool need_boundary_deltas, + Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus) { + // v = f * 2^e with e < 0, and with estimated_power >= 0. + // This means that e is close to 0 (have a look at how estimated_power is + // computed). + + // numerator = significand + // since v = significand * 2^exponent this is equivalent to + // numerator = v * / 2^-exponent + numerator->AssignUInt64(significand); + // denominator = 10^estimated_power * 2^-exponent (with exponent < 0) + denominator->AssignPowerUInt16(10, estimated_power); + denominator->ShiftLeft(-exponent); + + if (need_boundary_deltas) { + // Introduce a common denominator so that the deltas to the boundaries are + // integers. + denominator->ShiftLeft(1); + numerator->ShiftLeft(1); + // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common + // denominator (of 2) delta_plus equals 2^e. + // Given that the denominator already includes v's exponent the distance + // to the boundaries is simply 1. + delta_plus->AssignUInt16(1); + // Same for delta_minus. The adjustments if f == 2^p-1 are done later. + delta_minus->AssignUInt16(1); + } +} + + +// See comments for InitialScaledStartValues +static void InitialScaledStartValuesNegativeExponentNegativePower( + uint64_t significand, int exponent, + int estimated_power, bool need_boundary_deltas, + Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus) { + // Instead of multiplying the denominator with 10^estimated_power we + // multiply all values (numerator and deltas) by 10^-estimated_power. + + // Use numerator as temporary container for power_ten. + Bignum* power_ten = numerator; + power_ten->AssignPowerUInt16(10, -estimated_power); + + if (need_boundary_deltas) { + // Since power_ten == numerator we must make a copy of 10^estimated_power + // before we complete the computation of the numerator. + // delta_plus = delta_minus = 10^estimated_power + delta_plus->AssignBignum(*power_ten); + delta_minus->AssignBignum(*power_ten); + } + + // numerator = significand * 2 * 10^-estimated_power + // since v = significand * 2^exponent this is equivalent to + // numerator = v * 10^-estimated_power * 2 * 2^-exponent. + // Remember: numerator has been abused as power_ten. So no need to assign it + // to itself. + ASSERT(numerator == power_ten); + numerator->MultiplyByUInt64(significand); + + // denominator = 2 * 2^-exponent with exponent < 0. + denominator->AssignUInt16(1); + denominator->ShiftLeft(-exponent); + + if (need_boundary_deltas) { + // Introduce a common denominator so that the deltas to the boundaries are + // integers. + numerator->ShiftLeft(1); + denominator->ShiftLeft(1); + // With this shift the boundaries have their correct value, since + // delta_plus = 10^-estimated_power, and + // delta_minus = 10^-estimated_power. + // These assignments have been done earlier. + // The adjustments if f == 2^p-1 (lower boundary is closer) are done later. + } +} + + +// Let v = significand * 2^exponent. +// Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator +// and denominator. The functions GenerateShortestDigits and +// GenerateCountedDigits will then convert this ratio to its decimal +// representation d, with the required accuracy. +// Then d * 10^estimated_power is the representation of v. +// (Note: the fraction and the estimated_power might get adjusted before +// generating the decimal representation.) +// +// The initial start values consist of: +// - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power. +// - a scaled (common) denominator. +// optionally (used by GenerateShortestDigits to decide if it has the shortest +// decimal converting back to v): +// - v - m-: the distance to the lower boundary. +// - m+ - v: the distance to the upper boundary. +// +// v, m+, m-, and therefore v - m- and m+ - v all share the same denominator. +// +// Let ep == estimated_power, then the returned values will satisfy: +// v / 10^ep = numerator / denominator. +// v's boundarys m- and m+: +// m- / 10^ep == v / 10^ep - delta_minus / denominator +// m+ / 10^ep == v / 10^ep + delta_plus / denominator +// Or in other words: +// m- == v - delta_minus * 10^ep / denominator; +// m+ == v + delta_plus * 10^ep / denominator; +// +// Since 10^(k-1) <= v < 10^k (with k == estimated_power) +// or 10^k <= v < 10^(k+1) +// we then have 0.1 <= numerator/denominator < 1 +// or 1 <= numerator/denominator < 10 +// +// It is then easy to kickstart the digit-generation routine. +// +// The boundary-deltas are only filled if the mode equals BIGNUM_DTOA_SHORTEST +// or BIGNUM_DTOA_SHORTEST_SINGLE. + +static void InitialScaledStartValues(uint64_t significand, + int exponent, + bool lower_boundary_is_closer, + int estimated_power, + bool need_boundary_deltas, + Bignum* numerator, + Bignum* denominator, + Bignum* delta_minus, + Bignum* delta_plus) { + if (exponent >= 0) { + InitialScaledStartValuesPositiveExponent( + significand, exponent, estimated_power, need_boundary_deltas, + numerator, denominator, delta_minus, delta_plus); + } else if (estimated_power >= 0) { + InitialScaledStartValuesNegativeExponentPositivePower( + significand, exponent, estimated_power, need_boundary_deltas, + numerator, denominator, delta_minus, delta_plus); + } else { + InitialScaledStartValuesNegativeExponentNegativePower( + significand, exponent, estimated_power, need_boundary_deltas, + numerator, denominator, delta_minus, delta_plus); + } + + if (need_boundary_deltas && lower_boundary_is_closer) { + // The lower boundary is closer at half the distance of "normal" numbers. + // Increase the common denominator and adapt all but the delta_minus. + denominator->ShiftLeft(1); // *2 + numerator->ShiftLeft(1); // *2 + delta_plus->ShiftLeft(1); // *2 + } +} + + +// This routine multiplies numerator/denominator so that its values lies in the +// range 1-10. That is after a call to this function we have: +// 1 <= (numerator + delta_plus) /denominator < 10. +// Let numerator the input before modification and numerator' the argument +// after modification, then the output-parameter decimal_point is such that +// numerator / denominator * 10^estimated_power == +// numerator' / denominator' * 10^(decimal_point - 1) +// In some cases estimated_power was too low, and this is already the case. We +// then simply adjust the power so that 10^(k-1) <= v < 10^k (with k == +// estimated_power) but do not touch the numerator or denominator. +// Otherwise the routine multiplies the numerator and the deltas by 10. +static void FixupMultiply10(int estimated_power, bool is_even, + int* decimal_point, + Bignum* numerator, Bignum* denominator, + Bignum* delta_minus, Bignum* delta_plus) { + bool in_range; + if (is_even) { + // For IEEE doubles half-way cases (in decimal system numbers ending with 5) + // are rounded to the closest floating-point number with even significand. + in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; + } else { + in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; + } + if (in_range) { + // Since numerator + delta_plus >= denominator we already have + // 1 <= numerator/denominator < 10. Simply update the estimated_power. + *decimal_point = estimated_power + 1; + } else { + *decimal_point = estimated_power; + numerator->Times10(); + if (Bignum::Equal(*delta_minus, *delta_plus)) { + delta_minus->Times10(); + delta_plus->AssignBignum(*delta_minus); + } else { + delta_minus->Times10(); + delta_plus->Times10(); + } + } +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/bignum-dtoa.h b/contrib/libdouble-conversion/double-conversion/bignum-dtoa.h new file mode 100644 index 00000000000..34b961992d6 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/bignum-dtoa.h @@ -0,0 +1,84 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_BIGNUM_DTOA_H_ +#define DOUBLE_CONVERSION_BIGNUM_DTOA_H_ + +#include "utils.h" + +namespace double_conversion { + +enum BignumDtoaMode { + // Return the shortest correct representation. + // For example the output of 0.299999999999999988897 is (the less accurate but + // correct) 0.3. + BIGNUM_DTOA_SHORTEST, + // Same as BIGNUM_DTOA_SHORTEST but for single-precision floats. + BIGNUM_DTOA_SHORTEST_SINGLE, + // Return a fixed number of digits after the decimal point. + // For instance fixed(0.1, 4) becomes 0.1000 + // If the input number is big, the output will be big. + BIGNUM_DTOA_FIXED, + // Return a fixed number of digits, no matter what the exponent is. + BIGNUM_DTOA_PRECISION +}; + +// Converts the given double 'v' to ascii. +// The result should be interpreted as buffer * 10^(point-length). +// The buffer will be null-terminated. +// +// The input v must be > 0 and different from NaN, and Infinity. +// +// The output depends on the given mode: +// - SHORTEST: produce the least amount of digits for which the internal +// identity requirement is still satisfied. If the digits are printed +// (together with the correct exponent) then reading this number will give +// 'v' again. The buffer will choose the representation that is closest to +// 'v'. If there are two at the same distance, than the number is round up. +// In this mode the 'requested_digits' parameter is ignored. +// - FIXED: produces digits necessary to print a given number with +// 'requested_digits' digits after the decimal point. The produced digits +// might be too short in which case the caller has to fill the gaps with '0's. +// Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. +// Halfway cases are rounded up. The call toFixed(0.15, 2) thus returns +// buffer="2", point=0. +// Note: the length of the returned buffer has no meaning wrt the significance +// of its digits. That is, just because it contains '0's does not mean that +// any other digit would not satisfy the internal identity requirement. +// - PRECISION: produces 'requested_digits' where the first digit is not '0'. +// Even though the length of produced digits usually equals +// 'requested_digits', the function is allowed to return fewer digits, in +// which case the caller has to fill the missing digits with '0's. +// Halfway cases are again rounded up. +// 'BignumDtoa' expects the given buffer to be big enough to hold all digits +// and a terminating null-character. +void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, + Vector buffer, int* length, int* point); + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_BIGNUM_DTOA_H_ diff --git a/contrib/libdouble-conversion/double-conversion/bignum.cc b/contrib/libdouble-conversion/double-conversion/bignum.cc new file mode 100644 index 00000000000..8892de8f2a3 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/bignum.cc @@ -0,0 +1,766 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "bignum.h" +#include "utils.h" + +namespace double_conversion { + +Bignum::Bignum() + : bigits_(bigits_buffer_, kBigitCapacity), used_digits_(0), exponent_(0) { + for (int i = 0; i < kBigitCapacity; ++i) { + bigits_[i] = 0; + } +} + + +template +static int BitSize(S value) { + (void) value; // Mark variable as used. + return 8 * sizeof(value); +} + +// Guaranteed to lie in one Bigit. +void Bignum::AssignUInt16(uint16_t value) { + ASSERT(kBigitSize >= BitSize(value)); + Zero(); + if (value == 0) return; + + EnsureCapacity(1); + bigits_[0] = value; + used_digits_ = 1; +} + + +void Bignum::AssignUInt64(uint64_t value) { + const int kUInt64Size = 64; + + Zero(); + if (value == 0) return; + + int needed_bigits = kUInt64Size / kBigitSize + 1; + EnsureCapacity(needed_bigits); + for (int i = 0; i < needed_bigits; ++i) { + bigits_[i] = value & kBigitMask; + value = value >> kBigitSize; + } + used_digits_ = needed_bigits; + Clamp(); +} + + +void Bignum::AssignBignum(const Bignum& other) { + exponent_ = other.exponent_; + for (int i = 0; i < other.used_digits_; ++i) { + bigits_[i] = other.bigits_[i]; + } + // Clear the excess digits (if there were any). + for (int i = other.used_digits_; i < used_digits_; ++i) { + bigits_[i] = 0; + } + used_digits_ = other.used_digits_; +} + + +static uint64_t ReadUInt64(Vector buffer, + int from, + int digits_to_read) { + uint64_t result = 0; + for (int i = from; i < from + digits_to_read; ++i) { + int digit = buffer[i] - '0'; + ASSERT(0 <= digit && digit <= 9); + result = result * 10 + digit; + } + return result; +} + + +void Bignum::AssignDecimalString(Vector value) { + // 2^64 = 18446744073709551616 > 10^19 + const int kMaxUint64DecimalDigits = 19; + Zero(); + int length = value.length(); + unsigned int pos = 0; + // Let's just say that each digit needs 4 bits. + while (length >= kMaxUint64DecimalDigits) { + uint64_t digits = ReadUInt64(value, pos, kMaxUint64DecimalDigits); + pos += kMaxUint64DecimalDigits; + length -= kMaxUint64DecimalDigits; + MultiplyByPowerOfTen(kMaxUint64DecimalDigits); + AddUInt64(digits); + } + uint64_t digits = ReadUInt64(value, pos, length); + MultiplyByPowerOfTen(length); + AddUInt64(digits); + Clamp(); +} + + +static int HexCharValue(char c) { + if ('0' <= c && c <= '9') return c - '0'; + if ('a' <= c && c <= 'f') return 10 + c - 'a'; + ASSERT('A' <= c && c <= 'F'); + return 10 + c - 'A'; +} + + +void Bignum::AssignHexString(Vector value) { + Zero(); + int length = value.length(); + + int needed_bigits = length * 4 / kBigitSize + 1; + EnsureCapacity(needed_bigits); + int string_index = length - 1; + for (int i = 0; i < needed_bigits - 1; ++i) { + // These bigits are guaranteed to be "full". + Chunk current_bigit = 0; + for (int j = 0; j < kBigitSize / 4; j++) { + current_bigit += HexCharValue(value[string_index--]) << (j * 4); + } + bigits_[i] = current_bigit; + } + used_digits_ = needed_bigits - 1; + + Chunk most_significant_bigit = 0; // Could be = 0; + for (int j = 0; j <= string_index; ++j) { + most_significant_bigit <<= 4; + most_significant_bigit += HexCharValue(value[j]); + } + if (most_significant_bigit != 0) { + bigits_[used_digits_] = most_significant_bigit; + used_digits_++; + } + Clamp(); +} + + +void Bignum::AddUInt64(uint64_t operand) { + if (operand == 0) return; + Bignum other; + other.AssignUInt64(operand); + AddBignum(other); +} + + +void Bignum::AddBignum(const Bignum& other) { + ASSERT(IsClamped()); + ASSERT(other.IsClamped()); + + // If this has a greater exponent than other append zero-bigits to this. + // After this call exponent_ <= other.exponent_. + Align(other); + + // There are two possibilities: + // aaaaaaaaaaa 0000 (where the 0s represent a's exponent) + // bbbbb 00000000 + // ---------------- + // ccccccccccc 0000 + // or + // aaaaaaaaaa 0000 + // bbbbbbbbb 0000000 + // ----------------- + // cccccccccccc 0000 + // In both cases we might need a carry bigit. + + EnsureCapacity(1 + Max(BigitLength(), other.BigitLength()) - exponent_); + Chunk carry = 0; + int bigit_pos = other.exponent_ - exponent_; + ASSERT(bigit_pos >= 0); + for (int i = 0; i < other.used_digits_; ++i) { + Chunk sum = bigits_[bigit_pos] + other.bigits_[i] + carry; + bigits_[bigit_pos] = sum & kBigitMask; + carry = sum >> kBigitSize; + bigit_pos++; + } + + while (carry != 0) { + Chunk sum = bigits_[bigit_pos] + carry; + bigits_[bigit_pos] = sum & kBigitMask; + carry = sum >> kBigitSize; + bigit_pos++; + } + used_digits_ = Max(bigit_pos, used_digits_); + ASSERT(IsClamped()); +} + + +void Bignum::SubtractBignum(const Bignum& other) { + ASSERT(IsClamped()); + ASSERT(other.IsClamped()); + // We require this to be bigger than other. + ASSERT(LessEqual(other, *this)); + + Align(other); + + int offset = other.exponent_ - exponent_; + Chunk borrow = 0; + int i; + for (i = 0; i < other.used_digits_; ++i) { + ASSERT((borrow == 0) || (borrow == 1)); + Chunk difference = bigits_[i + offset] - other.bigits_[i] - borrow; + bigits_[i + offset] = difference & kBigitMask; + borrow = difference >> (kChunkSize - 1); + } + while (borrow != 0) { + Chunk difference = bigits_[i + offset] - borrow; + bigits_[i + offset] = difference & kBigitMask; + borrow = difference >> (kChunkSize - 1); + ++i; + } + Clamp(); +} + + +void Bignum::ShiftLeft(int shift_amount) { + if (used_digits_ == 0) return; + exponent_ += shift_amount / kBigitSize; + int local_shift = shift_amount % kBigitSize; + EnsureCapacity(used_digits_ + 1); + BigitsShiftLeft(local_shift); +} + + +void Bignum::MultiplyByUInt32(uint32_t factor) { + if (factor == 1) return; + if (factor == 0) { + Zero(); + return; + } + if (used_digits_ == 0) return; + + // The product of a bigit with the factor is of size kBigitSize + 32. + // Assert that this number + 1 (for the carry) fits into double chunk. + ASSERT(kDoubleChunkSize >= kBigitSize + 32 + 1); + DoubleChunk carry = 0; + for (int i = 0; i < used_digits_; ++i) { + DoubleChunk product = static_cast(factor) * bigits_[i] + carry; + bigits_[i] = static_cast(product & kBigitMask); + carry = (product >> kBigitSize); + } + while (carry != 0) { + EnsureCapacity(used_digits_ + 1); + bigits_[used_digits_] = carry & kBigitMask; + used_digits_++; + carry >>= kBigitSize; + } +} + + +void Bignum::MultiplyByUInt64(uint64_t factor) { + if (factor == 1) return; + if (factor == 0) { + Zero(); + return; + } + ASSERT(kBigitSize < 32); + uint64_t carry = 0; + uint64_t low = factor & 0xFFFFFFFF; + uint64_t high = factor >> 32; + for (int i = 0; i < used_digits_; ++i) { + uint64_t product_low = low * bigits_[i]; + uint64_t product_high = high * bigits_[i]; + uint64_t tmp = (carry & kBigitMask) + product_low; + bigits_[i] = tmp & kBigitMask; + carry = (carry >> kBigitSize) + (tmp >> kBigitSize) + + (product_high << (32 - kBigitSize)); + } + while (carry != 0) { + EnsureCapacity(used_digits_ + 1); + bigits_[used_digits_] = carry & kBigitMask; + used_digits_++; + carry >>= kBigitSize; + } +} + + +void Bignum::MultiplyByPowerOfTen(int exponent) { + const uint64_t kFive27 = UINT64_2PART_C(0x6765c793, fa10079d); + const uint16_t kFive1 = 5; + const uint16_t kFive2 = kFive1 * 5; + const uint16_t kFive3 = kFive2 * 5; + const uint16_t kFive4 = kFive3 * 5; + const uint16_t kFive5 = kFive4 * 5; + const uint16_t kFive6 = kFive5 * 5; + const uint32_t kFive7 = kFive6 * 5; + const uint32_t kFive8 = kFive7 * 5; + const uint32_t kFive9 = kFive8 * 5; + const uint32_t kFive10 = kFive9 * 5; + const uint32_t kFive11 = kFive10 * 5; + const uint32_t kFive12 = kFive11 * 5; + const uint32_t kFive13 = kFive12 * 5; + const uint32_t kFive1_to_12[] = + { kFive1, kFive2, kFive3, kFive4, kFive5, kFive6, + kFive7, kFive8, kFive9, kFive10, kFive11, kFive12 }; + + ASSERT(exponent >= 0); + if (exponent == 0) return; + if (used_digits_ == 0) return; + + // We shift by exponent at the end just before returning. + int remaining_exponent = exponent; + while (remaining_exponent >= 27) { + MultiplyByUInt64(kFive27); + remaining_exponent -= 27; + } + while (remaining_exponent >= 13) { + MultiplyByUInt32(kFive13); + remaining_exponent -= 13; + } + if (remaining_exponent > 0) { + MultiplyByUInt32(kFive1_to_12[remaining_exponent - 1]); + } + ShiftLeft(exponent); +} + + +void Bignum::Square() { + ASSERT(IsClamped()); + int product_length = 2 * used_digits_; + EnsureCapacity(product_length); + + // Comba multiplication: compute each column separately. + // Example: r = a2a1a0 * b2b1b0. + // r = 1 * a0b0 + + // 10 * (a1b0 + a0b1) + + // 100 * (a2b0 + a1b1 + a0b2) + + // 1000 * (a2b1 + a1b2) + + // 10000 * a2b2 + // + // In the worst case we have to accumulate nb-digits products of digit*digit. + // + // Assert that the additional number of bits in a DoubleChunk are enough to + // sum up used_digits of Bigit*Bigit. + if ((1 << (2 * (kChunkSize - kBigitSize))) <= used_digits_) { + UNIMPLEMENTED(); + } + DoubleChunk accumulator = 0; + // First shift the digits so we don't overwrite them. + int copy_offset = used_digits_; + for (int i = 0; i < used_digits_; ++i) { + bigits_[copy_offset + i] = bigits_[i]; + } + // We have two loops to avoid some 'if's in the loop. + for (int i = 0; i < used_digits_; ++i) { + // Process temporary digit i with power i. + // The sum of the two indices must be equal to i. + int bigit_index1 = i; + int bigit_index2 = 0; + // Sum all of the sub-products. + while (bigit_index1 >= 0) { + Chunk chunk1 = bigits_[copy_offset + bigit_index1]; + Chunk chunk2 = bigits_[copy_offset + bigit_index2]; + accumulator += static_cast(chunk1) * chunk2; + bigit_index1--; + bigit_index2++; + } + bigits_[i] = static_cast(accumulator) & kBigitMask; + accumulator >>= kBigitSize; + } + for (int i = used_digits_; i < product_length; ++i) { + int bigit_index1 = used_digits_ - 1; + int bigit_index2 = i - bigit_index1; + // Invariant: sum of both indices is again equal to i. + // Inner loop runs 0 times on last iteration, emptying accumulator. + while (bigit_index2 < used_digits_) { + Chunk chunk1 = bigits_[copy_offset + bigit_index1]; + Chunk chunk2 = bigits_[copy_offset + bigit_index2]; + accumulator += static_cast(chunk1) * chunk2; + bigit_index1--; + bigit_index2++; + } + // The overwritten bigits_[i] will never be read in further loop iterations, + // because bigit_index1 and bigit_index2 are always greater + // than i - used_digits_. + bigits_[i] = static_cast(accumulator) & kBigitMask; + accumulator >>= kBigitSize; + } + // Since the result was guaranteed to lie inside the number the + // accumulator must be 0 now. + ASSERT(accumulator == 0); + + // Don't forget to update the used_digits and the exponent. + used_digits_ = product_length; + exponent_ *= 2; + Clamp(); +} + + +void Bignum::AssignPowerUInt16(uint16_t base, int power_exponent) { + ASSERT(base != 0); + ASSERT(power_exponent >= 0); + if (power_exponent == 0) { + AssignUInt16(1); + return; + } + Zero(); + int shifts = 0; + // We expect base to be in range 2-32, and most often to be 10. + // It does not make much sense to implement different algorithms for counting + // the bits. + while ((base & 1) == 0) { + base >>= 1; + shifts++; + } + int bit_size = 0; + int tmp_base = base; + while (tmp_base != 0) { + tmp_base >>= 1; + bit_size++; + } + int final_size = bit_size * power_exponent; + // 1 extra bigit for the shifting, and one for rounded final_size. + EnsureCapacity(final_size / kBigitSize + 2); + + // Left to Right exponentiation. + int mask = 1; + while (power_exponent >= mask) mask <<= 1; + + // The mask is now pointing to the bit above the most significant 1-bit of + // power_exponent. + // Get rid of first 1-bit; + mask >>= 2; + uint64_t this_value = base; + + bool delayed_multipliciation = false; + const uint64_t max_32bits = 0xFFFFFFFF; + while (mask != 0 && this_value <= max_32bits) { + this_value = this_value * this_value; + // Verify that there is enough space in this_value to perform the + // multiplication. The first bit_size bits must be 0. + if ((power_exponent & mask) != 0) { + uint64_t base_bits_mask = + ~((static_cast(1) << (64 - bit_size)) - 1); + bool high_bits_zero = (this_value & base_bits_mask) == 0; + if (high_bits_zero) { + this_value *= base; + } else { + delayed_multipliciation = true; + } + } + mask >>= 1; + } + AssignUInt64(this_value); + if (delayed_multipliciation) { + MultiplyByUInt32(base); + } + + // Now do the same thing as a bignum. + while (mask != 0) { + Square(); + if ((power_exponent & mask) != 0) { + MultiplyByUInt32(base); + } + mask >>= 1; + } + + // And finally add the saved shifts. + ShiftLeft(shifts * power_exponent); +} + + +// Precondition: this/other < 16bit. +uint16_t Bignum::DivideModuloIntBignum(const Bignum& other) { + ASSERT(IsClamped()); + ASSERT(other.IsClamped()); + ASSERT(other.used_digits_ > 0); + + // Easy case: if we have less digits than the divisor than the result is 0. + // Note: this handles the case where this == 0, too. + if (BigitLength() < other.BigitLength()) { + return 0; + } + + Align(other); + + uint16_t result = 0; + + // Start by removing multiples of 'other' until both numbers have the same + // number of digits. + while (BigitLength() > other.BigitLength()) { + // This naive approach is extremely inefficient if `this` divided by other + // is big. This function is implemented for doubleToString where + // the result should be small (less than 10). + ASSERT(other.bigits_[other.used_digits_ - 1] >= ((1 << kBigitSize) / 16)); + ASSERT(bigits_[used_digits_ - 1] < 0x10000); + // Remove the multiples of the first digit. + // Example this = 23 and other equals 9. -> Remove 2 multiples. + result += static_cast(bigits_[used_digits_ - 1]); + SubtractTimes(other, bigits_[used_digits_ - 1]); + } + + ASSERT(BigitLength() == other.BigitLength()); + + // Both bignums are at the same length now. + // Since other has more than 0 digits we know that the access to + // bigits_[used_digits_ - 1] is safe. + Chunk this_bigit = bigits_[used_digits_ - 1]; + Chunk other_bigit = other.bigits_[other.used_digits_ - 1]; + + if (other.used_digits_ == 1) { + // Shortcut for easy (and common) case. + int quotient = this_bigit / other_bigit; + bigits_[used_digits_ - 1] = this_bigit - other_bigit * quotient; + ASSERT(quotient < 0x10000); + result += static_cast(quotient); + Clamp(); + return result; + } + + int division_estimate = this_bigit / (other_bigit + 1); + ASSERT(division_estimate < 0x10000); + result += static_cast(division_estimate); + SubtractTimes(other, division_estimate); + + if (other_bigit * (division_estimate + 1) > this_bigit) { + // No need to even try to subtract. Even if other's remaining digits were 0 + // another subtraction would be too much. + return result; + } + + while (LessEqual(other, *this)) { + SubtractBignum(other); + result++; + } + return result; +} + + +template +static int SizeInHexChars(S number) { + ASSERT(number > 0); + int result = 0; + while (number != 0) { + number >>= 4; + result++; + } + return result; +} + + +static char HexCharOfValue(int value) { + ASSERT(0 <= value && value <= 16); + if (value < 10) return static_cast(value + '0'); + return static_cast(value - 10 + 'A'); +} + + +bool Bignum::ToHexString(char* buffer, int buffer_size) const { + ASSERT(IsClamped()); + // Each bigit must be printable as separate hex-character. + ASSERT(kBigitSize % 4 == 0); + const int kHexCharsPerBigit = kBigitSize / 4; + + if (used_digits_ == 0) { + if (buffer_size < 2) return false; + buffer[0] = '0'; + buffer[1] = '\0'; + return true; + } + // We add 1 for the terminating '\0' character. + int needed_chars = (BigitLength() - 1) * kHexCharsPerBigit + + SizeInHexChars(bigits_[used_digits_ - 1]) + 1; + if (needed_chars > buffer_size) return false; + int string_index = needed_chars - 1; + buffer[string_index--] = '\0'; + for (int i = 0; i < exponent_; ++i) { + for (int j = 0; j < kHexCharsPerBigit; ++j) { + buffer[string_index--] = '0'; + } + } + for (int i = 0; i < used_digits_ - 1; ++i) { + Chunk current_bigit = bigits_[i]; + for (int j = 0; j < kHexCharsPerBigit; ++j) { + buffer[string_index--] = HexCharOfValue(current_bigit & 0xF); + current_bigit >>= 4; + } + } + // And finally the last bigit. + Chunk most_significant_bigit = bigits_[used_digits_ - 1]; + while (most_significant_bigit != 0) { + buffer[string_index--] = HexCharOfValue(most_significant_bigit & 0xF); + most_significant_bigit >>= 4; + } + return true; +} + + +Bignum::Chunk Bignum::BigitAt(int index) const { + if (index >= BigitLength()) return 0; + if (index < exponent_) return 0; + return bigits_[index - exponent_]; +} + + +int Bignum::Compare(const Bignum& a, const Bignum& b) { + ASSERT(a.IsClamped()); + ASSERT(b.IsClamped()); + int bigit_length_a = a.BigitLength(); + int bigit_length_b = b.BigitLength(); + if (bigit_length_a < bigit_length_b) return -1; + if (bigit_length_a > bigit_length_b) return +1; + for (int i = bigit_length_a - 1; i >= Min(a.exponent_, b.exponent_); --i) { + Chunk bigit_a = a.BigitAt(i); + Chunk bigit_b = b.BigitAt(i); + if (bigit_a < bigit_b) return -1; + if (bigit_a > bigit_b) return +1; + // Otherwise they are equal up to this digit. Try the next digit. + } + return 0; +} + + +int Bignum::PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c) { + ASSERT(a.IsClamped()); + ASSERT(b.IsClamped()); + ASSERT(c.IsClamped()); + if (a.BigitLength() < b.BigitLength()) { + return PlusCompare(b, a, c); + } + if (a.BigitLength() + 1 < c.BigitLength()) return -1; + if (a.BigitLength() > c.BigitLength()) return +1; + // The exponent encodes 0-bigits. So if there are more 0-digits in 'a' than + // 'b' has digits, then the bigit-length of 'a'+'b' must be equal to the one + // of 'a'. + if (a.exponent_ >= b.BigitLength() && a.BigitLength() < c.BigitLength()) { + return -1; + } + + Chunk borrow = 0; + // Starting at min_exponent all digits are == 0. So no need to compare them. + int min_exponent = Min(Min(a.exponent_, b.exponent_), c.exponent_); + for (int i = c.BigitLength() - 1; i >= min_exponent; --i) { + Chunk chunk_a = a.BigitAt(i); + Chunk chunk_b = b.BigitAt(i); + Chunk chunk_c = c.BigitAt(i); + Chunk sum = chunk_a + chunk_b; + if (sum > chunk_c + borrow) { + return +1; + } else { + borrow = chunk_c + borrow - sum; + if (borrow > 1) return -1; + borrow <<= kBigitSize; + } + } + if (borrow == 0) return 0; + return -1; +} + + +void Bignum::Clamp() { + while (used_digits_ > 0 && bigits_[used_digits_ - 1] == 0) { + used_digits_--; + } + if (used_digits_ == 0) { + // Zero. + exponent_ = 0; + } +} + + +bool Bignum::IsClamped() const { + return used_digits_ == 0 || bigits_[used_digits_ - 1] != 0; +} + + +void Bignum::Zero() { + for (int i = 0; i < used_digits_; ++i) { + bigits_[i] = 0; + } + used_digits_ = 0; + exponent_ = 0; +} + + +void Bignum::Align(const Bignum& other) { + if (exponent_ > other.exponent_) { + // If "X" represents a "hidden" digit (by the exponent) then we are in the + // following case (a == this, b == other): + // a: aaaaaaXXXX or a: aaaaaXXX + // b: bbbbbbX b: bbbbbbbbXX + // We replace some of the hidden digits (X) of a with 0 digits. + // a: aaaaaa000X or a: aaaaa0XX + int zero_digits = exponent_ - other.exponent_; + EnsureCapacity(used_digits_ + zero_digits); + for (int i = used_digits_ - 1; i >= 0; --i) { + bigits_[i + zero_digits] = bigits_[i]; + } + for (int i = 0; i < zero_digits; ++i) { + bigits_[i] = 0; + } + used_digits_ += zero_digits; + exponent_ -= zero_digits; + ASSERT(used_digits_ >= 0); + ASSERT(exponent_ >= 0); + } +} + + +void Bignum::BigitsShiftLeft(int shift_amount) { + ASSERT(shift_amount < kBigitSize); + ASSERT(shift_amount >= 0); + Chunk carry = 0; + for (int i = 0; i < used_digits_; ++i) { + Chunk new_carry = bigits_[i] >> (kBigitSize - shift_amount); + bigits_[i] = ((bigits_[i] << shift_amount) + carry) & kBigitMask; + carry = new_carry; + } + if (carry != 0) { + bigits_[used_digits_] = carry; + used_digits_++; + } +} + + +void Bignum::SubtractTimes(const Bignum& other, int factor) { + ASSERT(exponent_ <= other.exponent_); + if (factor < 3) { + for (int i = 0; i < factor; ++i) { + SubtractBignum(other); + } + return; + } + Chunk borrow = 0; + int exponent_diff = other.exponent_ - exponent_; + for (int i = 0; i < other.used_digits_; ++i) { + DoubleChunk product = static_cast(factor) * other.bigits_[i]; + DoubleChunk remove = borrow + product; + Chunk difference = bigits_[i + exponent_diff] - (remove & kBigitMask); + bigits_[i + exponent_diff] = difference & kBigitMask; + borrow = static_cast((difference >> (kChunkSize - 1)) + + (remove >> kBigitSize)); + } + for (int i = other.used_digits_ + exponent_diff; i < used_digits_; ++i) { + if (borrow == 0) return; + Chunk difference = bigits_[i] - borrow; + bigits_[i] = difference & kBigitMask; + borrow = difference >> (kChunkSize - 1); + } + Clamp(); +} + + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/bignum.h b/contrib/libdouble-conversion/double-conversion/bignum.h new file mode 100644 index 00000000000..c385f2237ba --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/bignum.h @@ -0,0 +1,144 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_BIGNUM_H_ +#define DOUBLE_CONVERSION_BIGNUM_H_ + +#include "utils.h" + +namespace double_conversion { + +class Bignum { + public: + // 3584 = 128 * 28. We can represent 2^3584 > 10^1000 accurately. + // This bignum can encode much bigger numbers, since it contains an + // exponent. + static const int kMaxSignificantBits = 3584; + + Bignum(); + void AssignUInt16(uint16_t value); + void AssignUInt64(uint64_t value); + void AssignBignum(const Bignum& other); + + void AssignDecimalString(Vector value); + void AssignHexString(Vector value); + + void AssignPowerUInt16(uint16_t base, int exponent); + + void AddUInt64(uint64_t operand); + void AddBignum(const Bignum& other); + // Precondition: this >= other. + void SubtractBignum(const Bignum& other); + + void Square(); + void ShiftLeft(int shift_amount); + void MultiplyByUInt32(uint32_t factor); + void MultiplyByUInt64(uint64_t factor); + void MultiplyByPowerOfTen(int exponent); + void Times10() { return MultiplyByUInt32(10); } + // Pseudocode: + // int result = this / other; + // this = this % other; + // In the worst case this function is in O(this/other). + uint16_t DivideModuloIntBignum(const Bignum& other); + + bool ToHexString(char* buffer, int buffer_size) const; + + // Returns + // -1 if a < b, + // 0 if a == b, and + // +1 if a > b. + static int Compare(const Bignum& a, const Bignum& b); + static bool Equal(const Bignum& a, const Bignum& b) { + return Compare(a, b) == 0; + } + static bool LessEqual(const Bignum& a, const Bignum& b) { + return Compare(a, b) <= 0; + } + static bool Less(const Bignum& a, const Bignum& b) { + return Compare(a, b) < 0; + } + // Returns Compare(a + b, c); + static int PlusCompare(const Bignum& a, const Bignum& b, const Bignum& c); + // Returns a + b == c + static bool PlusEqual(const Bignum& a, const Bignum& b, const Bignum& c) { + return PlusCompare(a, b, c) == 0; + } + // Returns a + b <= c + static bool PlusLessEqual(const Bignum& a, const Bignum& b, const Bignum& c) { + return PlusCompare(a, b, c) <= 0; + } + // Returns a + b < c + static bool PlusLess(const Bignum& a, const Bignum& b, const Bignum& c) { + return PlusCompare(a, b, c) < 0; + } + private: + typedef uint32_t Chunk; + typedef uint64_t DoubleChunk; + + static const int kChunkSize = sizeof(Chunk) * 8; + static const int kDoubleChunkSize = sizeof(DoubleChunk) * 8; + // With bigit size of 28 we loose some bits, but a double still fits easily + // into two chunks, and more importantly we can use the Comba multiplication. + static const int kBigitSize = 28; + static const Chunk kBigitMask = (1 << kBigitSize) - 1; + // Every instance allocates kBigitLength chunks on the stack. Bignums cannot + // grow. There are no checks if the stack-allocated space is sufficient. + static const int kBigitCapacity = kMaxSignificantBits / kBigitSize; + + void EnsureCapacity(int size) { + if (size > kBigitCapacity) { + UNREACHABLE(); + } + } + void Align(const Bignum& other); + void Clamp(); + bool IsClamped() const; + void Zero(); + // Requires this to have enough capacity (no tests done). + // Updates used_digits_ if necessary. + // shift_amount must be < kBigitSize. + void BigitsShiftLeft(int shift_amount); + // BigitLength includes the "hidden" digits encoded in the exponent. + int BigitLength() const { return used_digits_ + exponent_; } + Chunk BigitAt(int index) const; + void SubtractTimes(const Bignum& other, int factor); + + Chunk bigits_buffer_[kBigitCapacity]; + // A vector backed by bigits_buffer_. This way accesses to the array are + // checked for out-of-bounds errors. + Vector bigits_; + int used_digits_; + // The Bignum's value equals value(bigits_) * 2^(exponent_ * kBigitSize). + int exponent_; + + DISALLOW_COPY_AND_ASSIGN(Bignum); +}; + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_BIGNUM_H_ diff --git a/contrib/libdouble-conversion/double-conversion/cached-powers.cc b/contrib/libdouble-conversion/double-conversion/cached-powers.cc new file mode 100644 index 00000000000..2b43f064127 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/cached-powers.cc @@ -0,0 +1,175 @@ +// Copyright 2006-2008 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include + +#include "utils.h" + +#include "cached-powers.h" + +namespace double_conversion { + +struct CachedPower { + uint64_t significand; + int16_t binary_exponent; + int16_t decimal_exponent; +}; + +static const CachedPower kCachedPowers[] = { + {UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348}, + {UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340}, + {UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332}, + {UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324}, + {UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316}, + {UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308}, + {UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300}, + {UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292}, + {UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284}, + {UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276}, + {UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268}, + {UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260}, + {UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252}, + {UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244}, + {UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236}, + {UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228}, + {UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220}, + {UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212}, + {UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204}, + {UINT64_2PART_C(0xef340a98, 172aace5), -715, -196}, + {UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188}, + {UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180}, + {UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172}, + {UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164}, + {UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156}, + {UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148}, + {UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140}, + {UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132}, + {UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124}, + {UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116}, + {UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108}, + {UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100}, + {UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92}, + {UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84}, + {UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76}, + {UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68}, + {UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60}, + {UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52}, + {UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44}, + {UINT64_2PART_C(0xaa242499, 697392d3), -183, -36}, + {UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28}, + {UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20}, + {UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12}, + {UINT64_2PART_C(0xd1b71758, e219652c), -77, -4}, + {UINT64_2PART_C(0x9c400000, 00000000), -50, 4}, + {UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12}, + {UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20}, + {UINT64_2PART_C(0x813f3978, f8940984), 30, 28}, + {UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36}, + {UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44}, + {UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52}, + {UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60}, + {UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68}, + {UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76}, + {UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84}, + {UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92}, + {UINT64_2PART_C(0x924d692c, a61be758), 269, 100}, + {UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108}, + {UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116}, + {UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124}, + {UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132}, + {UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140}, + {UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148}, + {UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156}, + {UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164}, + {UINT64_2PART_C(0xa59bc234, db398c25), 508, 172}, + {UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180}, + {UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188}, + {UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196}, + {UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204}, + {UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212}, + {UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220}, + {UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228}, + {UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236}, + {UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244}, + {UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252}, + {UINT64_2PART_C(0xd01fef10, a657842c), 800, 260}, + {UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268}, + {UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276}, + {UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284}, + {UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292}, + {UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300}, + {UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308}, + {UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316}, + {UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324}, + {UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332}, + {UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340}, +}; + +static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent. +static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10) +// Difference between the decimal exponents in the table above. +const int PowersOfTenCache::kDecimalExponentDistance = 8; +const int PowersOfTenCache::kMinDecimalExponent = -348; +const int PowersOfTenCache::kMaxDecimalExponent = 340; + +void PowersOfTenCache::GetCachedPowerForBinaryExponentRange( + int min_exponent, + int max_exponent, + DiyFp* power, + int* decimal_exponent) { + int kQ = DiyFp::kSignificandSize; + double k = ceil((min_exponent + kQ - 1) * kD_1_LOG2_10); + int foo = kCachedPowersOffset; + int index = + (foo + static_cast(k) - 1) / kDecimalExponentDistance + 1; + ASSERT(0 <= index && index < static_cast(ARRAY_SIZE(kCachedPowers))); + CachedPower cached_power = kCachedPowers[index]; + ASSERT(min_exponent <= cached_power.binary_exponent); + (void) max_exponent; // Mark variable as used. + ASSERT(cached_power.binary_exponent <= max_exponent); + *decimal_exponent = cached_power.decimal_exponent; + *power = DiyFp(cached_power.significand, cached_power.binary_exponent); +} + + +void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent, + DiyFp* power, + int* found_exponent) { + ASSERT(kMinDecimalExponent <= requested_exponent); + ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance); + int index = + (requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance; + CachedPower cached_power = kCachedPowers[index]; + *power = DiyFp(cached_power.significand, cached_power.binary_exponent); + *found_exponent = cached_power.decimal_exponent; + ASSERT(*found_exponent <= requested_exponent); + ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance); +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/cached-powers.h b/contrib/libdouble-conversion/double-conversion/cached-powers.h new file mode 100644 index 00000000000..61a50614cf1 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/cached-powers.h @@ -0,0 +1,64 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_CACHED_POWERS_H_ +#define DOUBLE_CONVERSION_CACHED_POWERS_H_ + +#include "diy-fp.h" + +namespace double_conversion { + +class PowersOfTenCache { + public: + + // Not all powers of ten are cached. The decimal exponent of two neighboring + // cached numbers will differ by kDecimalExponentDistance. + static const int kDecimalExponentDistance; + + static const int kMinDecimalExponent; + static const int kMaxDecimalExponent; + + // Returns a cached power-of-ten with a binary exponent in the range + // [min_exponent; max_exponent] (boundaries included). + static void GetCachedPowerForBinaryExponentRange(int min_exponent, + int max_exponent, + DiyFp* power, + int* decimal_exponent); + + // Returns a cached power of ten x ~= 10^k such that + // k <= decimal_exponent < k + kCachedPowersDecimalDistance. + // The given decimal_exponent must satisfy + // kMinDecimalExponent <= requested_exponent, and + // requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance. + static void GetCachedPowerForDecimalExponent(int requested_exponent, + DiyFp* power, + int* found_exponent); +}; + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_CACHED_POWERS_H_ diff --git a/contrib/libdouble-conversion/double-conversion/diy-fp.cc b/contrib/libdouble-conversion/double-conversion/diy-fp.cc new file mode 100644 index 00000000000..ddd1891b168 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/diy-fp.cc @@ -0,0 +1,57 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +#include "diy-fp.h" +#include "utils.h" + +namespace double_conversion { + +void DiyFp::Multiply(const DiyFp& other) { + // Simply "emulates" a 128 bit multiplication. + // However: the resulting number only contains 64 bits. The least + // significant 64 bits are only used for rounding the most significant 64 + // bits. + const uint64_t kM32 = 0xFFFFFFFFU; + uint64_t a = f_ >> 32; + uint64_t b = f_ & kM32; + uint64_t c = other.f_ >> 32; + uint64_t d = other.f_ & kM32; + uint64_t ac = a * c; + uint64_t bc = b * c; + uint64_t ad = a * d; + uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32); + // By adding 1U << 31 to tmp we round the final result. + // Halfway cases will be round up. + tmp += 1U << 31; + uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); + e_ += other.e_ + 64; + f_ = result_f; +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/diy-fp.h b/contrib/libdouble-conversion/double-conversion/diy-fp.h new file mode 100644 index 00000000000..2edf34674ee --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/diy-fp.h @@ -0,0 +1,118 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_DIY_FP_H_ +#define DOUBLE_CONVERSION_DIY_FP_H_ + +#include "utils.h" + +namespace double_conversion { + +// This "Do It Yourself Floating Point" class implements a floating-point number +// with a uint64 significand and an int exponent. Normalized DiyFp numbers will +// have the most significant bit of the significand set. +// Multiplication and Subtraction do not normalize their results. +// DiyFp are not designed to contain special doubles (NaN and Infinity). +class DiyFp { + public: + static const int kSignificandSize = 64; + + DiyFp() : f_(0), e_(0) {} + DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {} + + // this = this - other. + // The exponents of both numbers must be the same and the significand of this + // must be bigger than the significand of other. + // The result will not be normalized. + void Subtract(const DiyFp& other) { + ASSERT(e_ == other.e_); + ASSERT(f_ >= other.f_); + f_ -= other.f_; + } + + // Returns a - b. + // The exponents of both numbers must be the same and this must be bigger + // than other. The result will not be normalized. + static DiyFp Minus(const DiyFp& a, const DiyFp& b) { + DiyFp result = a; + result.Subtract(b); + return result; + } + + + // this = this * other. + void Multiply(const DiyFp& other); + + // returns a * b; + static DiyFp Times(const DiyFp& a, const DiyFp& b) { + DiyFp result = a; + result.Multiply(b); + return result; + } + + void Normalize() { + ASSERT(f_ != 0); + uint64_t significand = f_; + int exponent = e_; + + // This method is mainly called for normalizing boundaries. In general + // boundaries need to be shifted by 10 bits. We thus optimize for this case. + const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); + while ((significand & k10MSBits) == 0) { + significand <<= 10; + exponent -= 10; + } + while ((significand & kUint64MSB) == 0) { + significand <<= 1; + exponent--; + } + f_ = significand; + e_ = exponent; + } + + static DiyFp Normalize(const DiyFp& a) { + DiyFp result = a; + result.Normalize(); + return result; + } + + uint64_t f() const { return f_; } + int e() const { return e_; } + + void set_f(uint64_t new_value) { f_ = new_value; } + void set_e(int new_value) { e_ = new_value; } + + private: + static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); + + uint64_t f_; + int e_; +}; + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_DIY_FP_H_ diff --git a/contrib/libdouble-conversion/double-conversion/double-conversion.cc b/contrib/libdouble-conversion/double-conversion/double-conversion.cc new file mode 100644 index 00000000000..d82150b3504 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/double-conversion.cc @@ -0,0 +1,982 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include +#include + +#include "double-conversion.h" + +#include "bignum-dtoa.h" +#include "fast-dtoa.h" +#include "fixed-dtoa.h" +#include "ieee.h" +#include "strtod.h" +#include "utils.h" + +namespace double_conversion { + +const DoubleToStringConverter& DoubleToStringConverter::EcmaScriptConverter() { + int flags = UNIQUE_ZERO | EMIT_POSITIVE_EXPONENT_SIGN; + static DoubleToStringConverter converter(flags, + "Infinity", + "NaN", + 'e', + -6, 21, + 6, 0); + return converter; +} + + +bool DoubleToStringConverter::HandleSpecialValues( + double value, + StringBuilder* result_builder) const { + Double double_inspect(value); + if (double_inspect.IsInfinite()) { + if (infinity_symbol_ == NULL) return false; + if (value < 0) { + result_builder->AddCharacter('-'); + } + result_builder->AddString(infinity_symbol_); + return true; + } + if (double_inspect.IsNan()) { + if (nan_symbol_ == NULL) return false; + result_builder->AddString(nan_symbol_); + return true; + } + return false; +} + + +void DoubleToStringConverter::CreateExponentialRepresentation( + const char* decimal_digits, + int length, + int exponent, + StringBuilder* result_builder) const { + ASSERT(length != 0); + result_builder->AddCharacter(decimal_digits[0]); + if (length != 1) { + result_builder->AddCharacter('.'); + result_builder->AddSubstring(&decimal_digits[1], length-1); + } + result_builder->AddCharacter(exponent_character_); + if (exponent < 0) { + result_builder->AddCharacter('-'); + exponent = -exponent; + } else { + if ((flags_ & EMIT_POSITIVE_EXPONENT_SIGN) != 0) { + result_builder->AddCharacter('+'); + } + } + if (exponent == 0) { + result_builder->AddCharacter('0'); + return; + } + ASSERT(exponent < 1e4); + const int kMaxExponentLength = 5; + char buffer[kMaxExponentLength + 1]; + buffer[kMaxExponentLength] = '\0'; + int first_char_pos = kMaxExponentLength; + while (exponent > 0) { + buffer[--first_char_pos] = '0' + (exponent % 10); + exponent /= 10; + } + result_builder->AddSubstring(&buffer[first_char_pos], + kMaxExponentLength - first_char_pos); +} + + +void DoubleToStringConverter::CreateDecimalRepresentation( + const char* decimal_digits, + int length, + int decimal_point, + int digits_after_point, + StringBuilder* result_builder) const { + // Create a representation that is padded with zeros if needed. + if (decimal_point <= 0) { + // "0.00000decimal_rep". + result_builder->AddCharacter('0'); + if (digits_after_point > 0) { + result_builder->AddCharacter('.'); + result_builder->AddPadding('0', -decimal_point); + ASSERT(length <= digits_after_point - (-decimal_point)); + result_builder->AddSubstring(decimal_digits, length); + int remaining_digits = digits_after_point - (-decimal_point) - length; + result_builder->AddPadding('0', remaining_digits); + } + } else if (decimal_point >= length) { + // "decimal_rep0000.00000" or "decimal_rep.0000" + result_builder->AddSubstring(decimal_digits, length); + result_builder->AddPadding('0', decimal_point - length); + if (digits_after_point > 0) { + result_builder->AddCharacter('.'); + result_builder->AddPadding('0', digits_after_point); + } + } else { + // "decima.l_rep000" + ASSERT(digits_after_point > 0); + result_builder->AddSubstring(decimal_digits, decimal_point); + result_builder->AddCharacter('.'); + ASSERT(length - decimal_point <= digits_after_point); + result_builder->AddSubstring(&decimal_digits[decimal_point], + length - decimal_point); + int remaining_digits = digits_after_point - (length - decimal_point); + result_builder->AddPadding('0', remaining_digits); + } + if (digits_after_point == 0) { + if ((flags_ & EMIT_TRAILING_DECIMAL_POINT) != 0) { + result_builder->AddCharacter('.'); + } + if ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) { + result_builder->AddCharacter('0'); + } + } +} + + +bool DoubleToStringConverter::ToShortestIeeeNumber( + double value, + StringBuilder* result_builder, + DoubleToStringConverter::DtoaMode mode) const { + ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE); + if (Double(value).IsSpecial()) { + return HandleSpecialValues(value, result_builder); + } + + int decimal_point; + bool sign; + const int kDecimalRepCapacity = kBase10MaximalLength + 1; + char decimal_rep[kDecimalRepCapacity]; + int decimal_rep_length; + + DoubleToAscii(value, mode, 0, decimal_rep, kDecimalRepCapacity, + &sign, &decimal_rep_length, &decimal_point); + + bool unique_zero = (flags_ & UNIQUE_ZERO) != 0; + if (sign && (value != 0.0 || !unique_zero)) { + result_builder->AddCharacter('-'); + } + + int exponent = decimal_point - 1; + if ((decimal_in_shortest_low_ <= exponent) && + (exponent < decimal_in_shortest_high_)) { + CreateDecimalRepresentation(decimal_rep, decimal_rep_length, + decimal_point, + Max(0, decimal_rep_length - decimal_point), + result_builder); + } else { + CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent, + result_builder); + } + return true; +} + + +bool DoubleToStringConverter::ToFixed(double value, + int requested_digits, + StringBuilder* result_builder) const { + ASSERT(kMaxFixedDigitsBeforePoint == 60); + const double kFirstNonFixed = 1e60; + + if (Double(value).IsSpecial()) { + return HandleSpecialValues(value, result_builder); + } + + if (requested_digits > kMaxFixedDigitsAfterPoint) return false; + if (value >= kFirstNonFixed || value <= -kFirstNonFixed) return false; + + // Find a sufficiently precise decimal representation of n. + int decimal_point; + bool sign; + // Add space for the '\0' byte. + const int kDecimalRepCapacity = + kMaxFixedDigitsBeforePoint + kMaxFixedDigitsAfterPoint + 1; + char decimal_rep[kDecimalRepCapacity]; + int decimal_rep_length; + DoubleToAscii(value, FIXED, requested_digits, + decimal_rep, kDecimalRepCapacity, + &sign, &decimal_rep_length, &decimal_point); + + bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); + if (sign && (value != 0.0 || !unique_zero)) { + result_builder->AddCharacter('-'); + } + + CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point, + requested_digits, result_builder); + return true; +} + + +bool DoubleToStringConverter::ToExponential( + double value, + int requested_digits, + StringBuilder* result_builder) const { + if (Double(value).IsSpecial()) { + return HandleSpecialValues(value, result_builder); + } + + if (requested_digits < -1) return false; + if (requested_digits > kMaxExponentialDigits) return false; + + int decimal_point; + bool sign; + // Add space for digit before the decimal point and the '\0' character. + const int kDecimalRepCapacity = kMaxExponentialDigits + 2; + ASSERT(kDecimalRepCapacity > kBase10MaximalLength); + char decimal_rep[kDecimalRepCapacity]; + int decimal_rep_length; + + if (requested_digits == -1) { + DoubleToAscii(value, SHORTEST, 0, + decimal_rep, kDecimalRepCapacity, + &sign, &decimal_rep_length, &decimal_point); + } else { + DoubleToAscii(value, PRECISION, requested_digits + 1, + decimal_rep, kDecimalRepCapacity, + &sign, &decimal_rep_length, &decimal_point); + ASSERT(decimal_rep_length <= requested_digits + 1); + + for (int i = decimal_rep_length; i < requested_digits + 1; ++i) { + decimal_rep[i] = '0'; + } + decimal_rep_length = requested_digits + 1; + } + + bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); + if (sign && (value != 0.0 || !unique_zero)) { + result_builder->AddCharacter('-'); + } + + int exponent = decimal_point - 1; + CreateExponentialRepresentation(decimal_rep, + decimal_rep_length, + exponent, + result_builder); + return true; +} + + +bool DoubleToStringConverter::ToPrecision(double value, + int precision, + StringBuilder* result_builder) const { + if (Double(value).IsSpecial()) { + return HandleSpecialValues(value, result_builder); + } + + if (precision < kMinPrecisionDigits || precision > kMaxPrecisionDigits) { + return false; + } + + // Find a sufficiently precise decimal representation of n. + int decimal_point; + bool sign; + // Add one for the terminating null character. + const int kDecimalRepCapacity = kMaxPrecisionDigits + 1; + char decimal_rep[kDecimalRepCapacity]; + int decimal_rep_length; + + DoubleToAscii(value, PRECISION, precision, + decimal_rep, kDecimalRepCapacity, + &sign, &decimal_rep_length, &decimal_point); + ASSERT(decimal_rep_length <= precision); + + bool unique_zero = ((flags_ & UNIQUE_ZERO) != 0); + if (sign && (value != 0.0 || !unique_zero)) { + result_builder->AddCharacter('-'); + } + + // The exponent if we print the number as x.xxeyyy. That is with the + // decimal point after the first digit. + int exponent = decimal_point - 1; + + int extra_zero = ((flags_ & EMIT_TRAILING_ZERO_AFTER_POINT) != 0) ? 1 : 0; + if ((-decimal_point + 1 > max_leading_padding_zeroes_in_precision_mode_) || + (decimal_point - precision + extra_zero > + max_trailing_padding_zeroes_in_precision_mode_)) { + // Fill buffer to contain 'precision' digits. + // Usually the buffer is already at the correct length, but 'DoubleToAscii' + // is allowed to return less characters. + for (int i = decimal_rep_length; i < precision; ++i) { + decimal_rep[i] = '0'; + } + + CreateExponentialRepresentation(decimal_rep, + precision, + exponent, + result_builder); + } else { + CreateDecimalRepresentation(decimal_rep, decimal_rep_length, decimal_point, + Max(0, precision - decimal_point), + result_builder); + } + return true; +} + + +static BignumDtoaMode DtoaToBignumDtoaMode( + DoubleToStringConverter::DtoaMode dtoa_mode) { + switch (dtoa_mode) { + case DoubleToStringConverter::SHORTEST: return BIGNUM_DTOA_SHORTEST; + case DoubleToStringConverter::SHORTEST_SINGLE: + return BIGNUM_DTOA_SHORTEST_SINGLE; + case DoubleToStringConverter::FIXED: return BIGNUM_DTOA_FIXED; + case DoubleToStringConverter::PRECISION: return BIGNUM_DTOA_PRECISION; + default: + UNREACHABLE(); + } +} + + +void DoubleToStringConverter::DoubleToAscii(double v, + DtoaMode mode, + int requested_digits, + char* buffer, + int buffer_length, + bool* sign, + int* length, + int* point) { + Vector vector(buffer, buffer_length); + ASSERT(!Double(v).IsSpecial()); + ASSERT(mode == SHORTEST || mode == SHORTEST_SINGLE || requested_digits >= 0); + + if (Double(v).Sign() < 0) { + *sign = true; + v = -v; + } else { + *sign = false; + } + + if (mode == PRECISION && requested_digits == 0) { + vector[0] = '\0'; + *length = 0; + return; + } + + if (v == 0) { + vector[0] = '0'; + vector[1] = '\0'; + *length = 1; + *point = 1; + return; + } + + bool fast_worked; + switch (mode) { + case SHORTEST: + fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, vector, length, point); + break; + case SHORTEST_SINGLE: + fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST_SINGLE, 0, + vector, length, point); + break; + case FIXED: + fast_worked = FastFixedDtoa(v, requested_digits, vector, length, point); + break; + case PRECISION: + fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, + vector, length, point); + break; + default: + fast_worked = false; + UNREACHABLE(); + } + if (fast_worked) return; + + // If the fast dtoa didn't succeed use the slower bignum version. + BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode); + BignumDtoa(v, bignum_mode, requested_digits, vector, length, point); + vector[*length] = '\0'; +} + + +// Consumes the given substring from the iterator. +// Returns false, if the substring does not match. +template +static bool ConsumeSubString(Iterator* current, + Iterator end, + const char* substring) { + ASSERT(**current == *substring); + for (substring++; *substring != '\0'; substring++) { + ++*current; + if (*current == end || **current != *substring) return false; + } + ++*current; + return true; +} + + +// Maximum number of significant digits in decimal representation. +// The longest possible double in decimal representation is +// (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 +// (768 digits). If we parse a number whose first digits are equal to a +// mean of 2 adjacent doubles (that could have up to 769 digits) the result +// must be rounded to the bigger one unless the tail consists of zeros, so +// we don't need to preserve all the digits. +const int kMaxSignificantDigits = 772; + + +static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 }; +static const int kWhitespaceTable7Length = ARRAY_SIZE(kWhitespaceTable7); + + +static const uc16 kWhitespaceTable16[] = { + 160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195, + 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279 +}; +static const int kWhitespaceTable16Length = ARRAY_SIZE(kWhitespaceTable16); + + +static bool isWhitespace(int x) { + if (x < 128) { + for (int i = 0; i < kWhitespaceTable7Length; i++) { + if (kWhitespaceTable7[i] == x) return true; + } + } else { + for (int i = 0; i < kWhitespaceTable16Length; i++) { + if (kWhitespaceTable16[i] == x) return true; + } + } + return false; +} + + +// Returns true if a nonspace found and false if the end has reached. +template +static inline bool AdvanceToNonspace(Iterator* current, Iterator end) { + while (*current != end) { + if (!isWhitespace(**current)) return true; + ++*current; + } + return false; +} + + +static bool isDigit(int x, int radix) { + return (x >= '0' && x <= '9' && x < '0' + radix) + || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) + || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); +} + + +static double SignedZero(bool sign) { + return sign ? -0.0 : 0.0; +} + + +// Returns true if 'c' is a decimal digit that is valid for the given radix. +// +// The function is small and could be inlined, but VS2012 emitted a warning +// because it constant-propagated the radix and concluded that the last +// condition was always true. By moving it into a separate function the +// compiler wouldn't warn anymore. +#if _MSC_VER +#pragma optimize("",off) +static bool IsDecimalDigitForRadix(int c, int radix) { + return '0' <= c && c <= '9' && (c - '0') < radix; +} +#pragma optimize("",on) +#else +static bool inline IsDecimalDigitForRadix(int c, int radix) { + return '0' <= c && c <= '9' && (c - '0') < radix; +} +#endif +// Returns true if 'c' is a character digit that is valid for the given radix. +// The 'a_character' should be 'a' or 'A'. +// +// The function is small and could be inlined, but VS2012 emitted a warning +// because it constant-propagated the radix and concluded that the first +// condition was always false. By moving it into a separate function the +// compiler wouldn't warn anymore. +static bool IsCharacterDigitForRadix(int c, int radix, char a_character) { + return radix > 10 && c >= a_character && c < a_character + radix - 10; +} + + +// Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. +template +static double RadixStringToIeee(Iterator* current, + Iterator end, + bool sign, + bool allow_trailing_junk, + double junk_string_value, + bool read_as_double, + bool* result_is_junk) { + ASSERT(*current != end); + + const int kDoubleSize = Double::kSignificandSize; + const int kSingleSize = Single::kSignificandSize; + const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize; + + *result_is_junk = true; + + // Skip leading 0s. + while (**current == '0') { + ++(*current); + if (*current == end) { + *result_is_junk = false; + return SignedZero(sign); + } + } + + int64_t number = 0; + int exponent = 0; + const int radix = (1 << radix_log_2); + + do { + int digit; + if (IsDecimalDigitForRadix(**current, radix)) { + digit = static_cast(**current) - '0'; + } else if (IsCharacterDigitForRadix(**current, radix, 'a')) { + digit = static_cast(**current) - 'a' + 10; + } else if (IsCharacterDigitForRadix(**current, radix, 'A')) { + digit = static_cast(**current) - 'A' + 10; + } else { + if (allow_trailing_junk || !AdvanceToNonspace(current, end)) { + break; + } else { + return junk_string_value; + } + } + + number = number * radix + digit; + int overflow = static_cast(number >> kSignificandSize); + if (overflow != 0) { + // Overflow occurred. Need to determine which direction to round the + // result. + int overflow_bits_count = 1; + while (overflow > 1) { + overflow_bits_count++; + overflow >>= 1; + } + + int dropped_bits_mask = ((1 << overflow_bits_count) - 1); + int dropped_bits = static_cast(number) & dropped_bits_mask; + number >>= overflow_bits_count; + exponent = overflow_bits_count; + + bool zero_tail = true; + for (;;) { + ++(*current); + if (*current == end || !isDigit(**current, radix)) break; + zero_tail = zero_tail && **current == '0'; + exponent += radix_log_2; + } + + if (!allow_trailing_junk && AdvanceToNonspace(current, end)) { + return junk_string_value; + } + + int middle_value = (1 << (overflow_bits_count - 1)); + if (dropped_bits > middle_value) { + number++; // Rounding up. + } else if (dropped_bits == middle_value) { + // Rounding to even to consistency with decimals: half-way case rounds + // up if significant part is odd and down otherwise. + if ((number & 1) != 0 || !zero_tail) { + number++; // Rounding up. + } + } + + // Rounding up may cause overflow. + if ((number & ((int64_t)1 << kSignificandSize)) != 0) { + exponent++; + number >>= 1; + } + break; + } + ++(*current); + } while (*current != end); + + ASSERT(number < ((int64_t)1 << kSignificandSize)); + ASSERT(static_cast(static_cast(number)) == number); + + *result_is_junk = false; + + if (exponent == 0) { + if (sign) { + if (number == 0) return -0.0; + number = -number; + } + return static_cast(number); + } + + ASSERT(number != 0); + return Double(DiyFp(number, exponent)).value(); +} + + +template +double StringToDoubleConverter::StringToIeee( + Iterator input, + int length, + bool read_as_double, + int* processed_characters_count) const { + Iterator current = input; + Iterator end = input + length; + + *processed_characters_count = 0; + + const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0; + const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0; + const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0; + const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0; + + // To make sure that iterator dereferencing is valid the following + // convention is used: + // 1. Each '++current' statement is followed by check for equality to 'end'. + // 2. If AdvanceToNonspace returned false then current == end. + // 3. If 'current' becomes equal to 'end' the function returns or goes to + // 'parsing_done'. + // 4. 'current' is not dereferenced after the 'parsing_done' label. + // 5. Code before 'parsing_done' may rely on 'current != end'. + if (current == end) return empty_string_value_; + + if (allow_leading_spaces || allow_trailing_spaces) { + if (!AdvanceToNonspace(¤t, end)) { + *processed_characters_count = static_cast(current - input); + return empty_string_value_; + } + if (!allow_leading_spaces && (input != current)) { + // No leading spaces allowed, but AdvanceToNonspace moved forward. + return junk_string_value_; + } + } + + // The longest form of simplified number is: "-.1eXXX\0". + const int kBufferSize = kMaxSignificantDigits + 10; + char buffer[kBufferSize]; // NOLINT: size is known at compile time. + int buffer_pos = 0; + + // Exponent will be adjusted if insignificant digits of the integer part + // or insignificant leading zeros of the fractional part are dropped. + int exponent = 0; + int significant_digits = 0; + int insignificant_digits = 0; + bool nonzero_digit_dropped = false; + + bool sign = false; + + if (*current == '+' || *current == '-') { + sign = (*current == '-'); + ++current; + Iterator next_non_space = current; + // Skip following spaces (if allowed). + if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_; + if (!allow_spaces_after_sign && (current != next_non_space)) { + return junk_string_value_; + } + current = next_non_space; + } + + if (infinity_symbol_ != NULL) { + if (*current == infinity_symbol_[0]) { + if (!ConsumeSubString(¤t, end, infinity_symbol_)) { + return junk_string_value_; + } + + if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { + return junk_string_value_; + } + if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { + return junk_string_value_; + } + + ASSERT(buffer_pos == 0); + *processed_characters_count = static_cast(current - input); + return sign ? -Double::Infinity() : Double::Infinity(); + } + } + + if (nan_symbol_ != NULL) { + if (*current == nan_symbol_[0]) { + if (!ConsumeSubString(¤t, end, nan_symbol_)) { + return junk_string_value_; + } + + if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { + return junk_string_value_; + } + if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { + return junk_string_value_; + } + + ASSERT(buffer_pos == 0); + *processed_characters_count = static_cast(current - input); + return sign ? -Double::NaN() : Double::NaN(); + } + } + + bool leading_zero = false; + if (*current == '0') { + ++current; + if (current == end) { + *processed_characters_count = static_cast(current - input); + return SignedZero(sign); + } + + leading_zero = true; + + // It could be hexadecimal value. + if ((flags_ & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { + ++current; + if (current == end || !isDigit(*current, 16)) { + return junk_string_value_; // "0x". + } + + bool result_is_junk; + double result = RadixStringToIeee<4>(¤t, + end, + sign, + allow_trailing_junk, + junk_string_value_, + read_as_double, + &result_is_junk); + if (!result_is_junk) { + if (allow_trailing_spaces) AdvanceToNonspace(¤t, end); + *processed_characters_count = static_cast(current - input); + } + return result; + } + + // Ignore leading zeros in the integer part. + while (*current == '0') { + ++current; + if (current == end) { + *processed_characters_count = static_cast(current - input); + return SignedZero(sign); + } + } + } + + bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0; + + // Copy significant digits of the integer part (if any) to the buffer. + while (*current >= '0' && *current <= '9') { + if (significant_digits < kMaxSignificantDigits) { + ASSERT(buffer_pos < kBufferSize); + buffer[buffer_pos++] = static_cast(*current); + significant_digits++; + // Will later check if it's an octal in the buffer. + } else { + insignificant_digits++; // Move the digit into the exponential part. + nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; + } + octal = octal && *current < '8'; + ++current; + if (current == end) goto parsing_done; + } + + if (significant_digits == 0) { + octal = false; + } + + if (*current == '.') { + if (octal && !allow_trailing_junk) return junk_string_value_; + if (octal) goto parsing_done; + + ++current; + if (current == end) { + if (significant_digits == 0 && !leading_zero) { + return junk_string_value_; + } else { + goto parsing_done; + } + } + + if (significant_digits == 0) { + // octal = false; + // Integer part consists of 0 or is absent. Significant digits start after + // leading zeros (if any). + while (*current == '0') { + ++current; + if (current == end) { + *processed_characters_count = static_cast(current - input); + return SignedZero(sign); + } + exponent--; // Move this 0 into the exponent. + } + } + + // There is a fractional part. + // We don't emit a '.', but adjust the exponent instead. + while (*current >= '0' && *current <= '9') { + if (significant_digits < kMaxSignificantDigits) { + ASSERT(buffer_pos < kBufferSize); + buffer[buffer_pos++] = static_cast(*current); + significant_digits++; + exponent--; + } else { + // Ignore insignificant digits in the fractional part. + nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; + } + ++current; + if (current == end) goto parsing_done; + } + } + + if (!leading_zero && exponent == 0 && significant_digits == 0) { + // If leading_zeros is true then the string contains zeros. + // If exponent < 0 then string was [+-]\.0*... + // If significant_digits != 0 the string is not equal to 0. + // Otherwise there are no digits in the string. + return junk_string_value_; + } + + // Parse exponential part. + if (*current == 'e' || *current == 'E') { + if (octal && !allow_trailing_junk) return junk_string_value_; + if (octal) goto parsing_done; + ++current; + if (current == end) { + if (allow_trailing_junk) { + goto parsing_done; + } else { + return junk_string_value_; + } + } + char exponen_sign = '+'; + if (*current == '+' || *current == '-') { + exponen_sign = static_cast(*current); + ++current; + if (current == end) { + if (allow_trailing_junk) { + goto parsing_done; + } else { + return junk_string_value_; + } + } + } + + if (current == end || *current < '0' || *current > '9') { + if (allow_trailing_junk) { + goto parsing_done; + } else { + return junk_string_value_; + } + } + + const int max_exponent = INT_MAX / 2; + ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2); + int num = 0; + do { + // Check overflow. + int digit = *current - '0'; + if (num >= max_exponent / 10 + && !(num == max_exponent / 10 && digit <= max_exponent % 10)) { + num = max_exponent; + } else { + num = num * 10 + digit; + } + ++current; + } while (current != end && *current >= '0' && *current <= '9'); + + exponent += (exponen_sign == '-' ? -num : num); + } + + if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { + return junk_string_value_; + } + if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { + return junk_string_value_; + } + if (allow_trailing_spaces) { + AdvanceToNonspace(¤t, end); + } + + parsing_done: + exponent += insignificant_digits; + + if (octal) { + double result; + bool result_is_junk; + char* start = buffer; + result = RadixStringToIeee<3>(&start, + buffer + buffer_pos, + sign, + allow_trailing_junk, + junk_string_value_, + read_as_double, + &result_is_junk); + ASSERT(!result_is_junk); + *processed_characters_count = static_cast(current - input); + return result; + } + + if (nonzero_digit_dropped) { + buffer[buffer_pos++] = '1'; + exponent--; + } + + ASSERT(buffer_pos < kBufferSize); + buffer[buffer_pos] = '\0'; + + double converted; + if (read_as_double) { + converted = Strtod(Vector(buffer, buffer_pos), exponent); + } else { + converted = Strtof(Vector(buffer, buffer_pos), exponent); + } + *processed_characters_count = static_cast(current - input); + return sign? -converted: converted; +} + + +double StringToDoubleConverter::StringToDouble( + const char* buffer, + int length, + int* processed_characters_count) const { + return StringToIeee(buffer, length, true, processed_characters_count); +} + + +double StringToDoubleConverter::StringToDouble( + const uc16* buffer, + int length, + int* processed_characters_count) const { + return StringToIeee(buffer, length, true, processed_characters_count); +} + + +float StringToDoubleConverter::StringToFloat( + const char* buffer, + int length, + int* processed_characters_count) const { + return static_cast(StringToIeee(buffer, length, false, + processed_characters_count)); +} + + +float StringToDoubleConverter::StringToFloat( + const uc16* buffer, + int length, + int* processed_characters_count) const { + return static_cast(StringToIeee(buffer, length, false, + processed_characters_count)); +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/double-conversion.h b/contrib/libdouble-conversion/double-conversion/double-conversion.h new file mode 100644 index 00000000000..6bdfa8d25d7 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/double-conversion.h @@ -0,0 +1,543 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ +#define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ + +#include "utils.h" + +namespace double_conversion { + +class DoubleToStringConverter { + public: + // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint + // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the + // function returns false. + static const int kMaxFixedDigitsBeforePoint = 60; + static const int kMaxFixedDigitsAfterPoint = 60; + + // When calling ToExponential with a requested_digits + // parameter > kMaxExponentialDigits then the function returns false. + static const int kMaxExponentialDigits = 120; + + // When calling ToPrecision with a requested_digits + // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits + // then the function returns false. + static const int kMinPrecisionDigits = 1; + static const int kMaxPrecisionDigits = 120; + + enum Flags { + NO_FLAGS = 0, + EMIT_POSITIVE_EXPONENT_SIGN = 1, + EMIT_TRAILING_DECIMAL_POINT = 2, + EMIT_TRAILING_ZERO_AFTER_POINT = 4, + UNIQUE_ZERO = 8 + }; + + // Flags should be a bit-or combination of the possible Flags-enum. + // - NO_FLAGS: no special flags. + // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent + // form, emits a '+' for positive exponents. Example: 1.2e+2. + // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is + // converted into decimal format then a trailing decimal point is appended. + // Example: 2345.0 is converted to "2345.". + // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point + // emits a trailing '0'-character. This flag requires the + // EXMIT_TRAILING_DECIMAL_POINT flag. + // Example: 2345.0 is converted to "2345.0". + // - UNIQUE_ZERO: "-0.0" is converted to "0.0". + // + // Infinity symbol and nan_symbol provide the string representation for these + // special values. If the string is NULL and the special value is encountered + // then the conversion functions return false. + // + // The exponent_character is used in exponential representations. It is + // usually 'e' or 'E'. + // + // When converting to the shortest representation the converter will + // represent input numbers in decimal format if they are in the interval + // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ + // (lower boundary included, greater boundary excluded). + // Example: with decimal_in_shortest_low = -6 and + // decimal_in_shortest_high = 21: + // ToShortest(0.000001) -> "0.000001" + // ToShortest(0.0000001) -> "1e-7" + // ToShortest(111111111111111111111.0) -> "111111111111111110000" + // ToShortest(100000000000000000000.0) -> "100000000000000000000" + // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" + // + // When converting to precision mode the converter may add + // max_leading_padding_zeroes before returning the number in exponential + // format. + // Example with max_leading_padding_zeroes_in_precision_mode = 6. + // ToPrecision(0.0000012345, 2) -> "0.0000012" + // ToPrecision(0.00000012345, 2) -> "1.2e-7" + // Similarily the converter may add up to + // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid + // returning an exponential representation. A zero added by the + // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. + // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: + // ToPrecision(230.0, 2) -> "230" + // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. + // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. + DoubleToStringConverter(int flags, + const char* infinity_symbol, + const char* nan_symbol, + char exponent_character, + int decimal_in_shortest_low, + int decimal_in_shortest_high, + int max_leading_padding_zeroes_in_precision_mode, + int max_trailing_padding_zeroes_in_precision_mode) + : flags_(flags), + infinity_symbol_(infinity_symbol), + nan_symbol_(nan_symbol), + exponent_character_(exponent_character), + decimal_in_shortest_low_(decimal_in_shortest_low), + decimal_in_shortest_high_(decimal_in_shortest_high), + max_leading_padding_zeroes_in_precision_mode_( + max_leading_padding_zeroes_in_precision_mode), + max_trailing_padding_zeroes_in_precision_mode_( + max_trailing_padding_zeroes_in_precision_mode) { + // When 'trailing zero after the point' is set, then 'trailing point' + // must be set too. + ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || + !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); + } + + // Returns a converter following the EcmaScript specification. + static const DoubleToStringConverter& EcmaScriptConverter(); + + // Computes the shortest string of digits that correctly represent the input + // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high + // (see constructor) it then either returns a decimal representation, or an + // exponential representation. + // Example with decimal_in_shortest_low = -6, + // decimal_in_shortest_high = 21, + // EMIT_POSITIVE_EXPONENT_SIGN activated, and + // EMIT_TRAILING_DECIMAL_POINT deactived: + // ToShortest(0.000001) -> "0.000001" + // ToShortest(0.0000001) -> "1e-7" + // ToShortest(111111111111111111111.0) -> "111111111111111110000" + // ToShortest(100000000000000000000.0) -> "100000000000000000000" + // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" + // + // Note: the conversion may round the output if the returned string + // is accurate enough to uniquely identify the input-number. + // For example the most precise representation of the double 9e59 equals + // "899999999999999918767229449717619953810131273674690656206848", but + // the converter will return the shorter (but still correct) "9e59". + // + // Returns true if the conversion succeeds. The conversion always succeeds + // except when the input value is special and no infinity_symbol or + // nan_symbol has been given to the constructor. + bool ToShortest(double value, StringBuilder* result_builder) const { + return ToShortestIeeeNumber(value, result_builder, SHORTEST); + } + + // Same as ToShortest, but for single-precision floats. + bool ToShortestSingle(float value, StringBuilder* result_builder) const { + return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); + } + + + // Computes a decimal representation with a fixed number of digits after the + // decimal point. The last emitted digit is rounded. + // + // Examples: + // ToFixed(3.12, 1) -> "3.1" + // ToFixed(3.1415, 3) -> "3.142" + // ToFixed(1234.56789, 4) -> "1234.5679" + // ToFixed(1.23, 5) -> "1.23000" + // ToFixed(0.1, 4) -> "0.1000" + // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" + // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" + // ToFixed(0.1, 17) -> "0.10000000000000001" + // + // If requested_digits equals 0, then the tail of the result depends on + // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. + // Examples, for requested_digits == 0, + // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be + // - false and false: then 123.45 -> 123 + // 0.678 -> 1 + // - true and false: then 123.45 -> 123. + // 0.678 -> 1. + // - true and true: then 123.45 -> 123.0 + // 0.678 -> 1.0 + // + // Returns true if the conversion succeeds. The conversion always succeeds + // except for the following cases: + // - the input value is special and no infinity_symbol or nan_symbol has + // been provided to the constructor, + // - 'value' > 10^kMaxFixedDigitsBeforePoint, or + // - 'requested_digits' > kMaxFixedDigitsAfterPoint. + // The last two conditions imply that the result will never contain more than + // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters + // (one additional character for the sign, and one for the decimal point). + bool ToFixed(double value, + int requested_digits, + StringBuilder* result_builder) const; + + // Computes a representation in exponential format with requested_digits + // after the decimal point. The last emitted digit is rounded. + // If requested_digits equals -1, then the shortest exponential representation + // is computed. + // + // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and + // exponent_character set to 'e'. + // ToExponential(3.12, 1) -> "3.1e0" + // ToExponential(5.0, 3) -> "5.000e0" + // ToExponential(0.001, 2) -> "1.00e-3" + // ToExponential(3.1415, -1) -> "3.1415e0" + // ToExponential(3.1415, 4) -> "3.1415e0" + // ToExponential(3.1415, 3) -> "3.142e0" + // ToExponential(123456789000000, 3) -> "1.235e14" + // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" + // ToExponential(1000000000000000019884624838656.0, 32) -> + // "1.00000000000000001988462483865600e30" + // ToExponential(1234, 0) -> "1e3" + // + // Returns true if the conversion succeeds. The conversion always succeeds + // except for the following cases: + // - the input value is special and no infinity_symbol or nan_symbol has + // been provided to the constructor, + // - 'requested_digits' > kMaxExponentialDigits. + // The last condition implies that the result will never contain more than + // kMaxExponentialDigits + 8 characters (the sign, the digit before the + // decimal point, the decimal point, the exponent character, the + // exponent's sign, and at most 3 exponent digits). + bool ToExponential(double value, + int requested_digits, + StringBuilder* result_builder) const; + + // Computes 'precision' leading digits of the given 'value' and returns them + // either in exponential or decimal format, depending on + // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the + // constructor). + // The last computed digit is rounded. + // + // Example with max_leading_padding_zeroes_in_precision_mode = 6. + // ToPrecision(0.0000012345, 2) -> "0.0000012" + // ToPrecision(0.00000012345, 2) -> "1.2e-7" + // Similarily the converter may add up to + // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid + // returning an exponential representation. A zero added by the + // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. + // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: + // ToPrecision(230.0, 2) -> "230" + // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. + // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. + // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no + // EMIT_TRAILING_ZERO_AFTER_POINT: + // ToPrecision(123450.0, 6) -> "123450" + // ToPrecision(123450.0, 5) -> "123450" + // ToPrecision(123450.0, 4) -> "123500" + // ToPrecision(123450.0, 3) -> "123000" + // ToPrecision(123450.0, 2) -> "1.2e5" + // + // Returns true if the conversion succeeds. The conversion always succeeds + // except for the following cases: + // - the input value is special and no infinity_symbol or nan_symbol has + // been provided to the constructor, + // - precision < kMinPericisionDigits + // - precision > kMaxPrecisionDigits + // The last condition implies that the result will never contain more than + // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the + // exponent character, the exponent's sign, and at most 3 exponent digits). + bool ToPrecision(double value, + int precision, + StringBuilder* result_builder) const; + + enum DtoaMode { + // Produce the shortest correct representation. + // For example the output of 0.299999999999999988897 is (the less accurate + // but correct) 0.3. + SHORTEST, + // Same as SHORTEST, but for single-precision floats. + SHORTEST_SINGLE, + // Produce a fixed number of digits after the decimal point. + // For instance fixed(0.1, 4) becomes 0.1000 + // If the input number is big, the output will be big. + FIXED, + // Fixed number of digits (independent of the decimal point). + PRECISION + }; + + // The maximal number of digits that are needed to emit a double in base 10. + // A higher precision can be achieved by using more digits, but the shortest + // accurate representation of any double will never use more digits than + // kBase10MaximalLength. + // Note that DoubleToAscii null-terminates its input. So the given buffer + // should be at least kBase10MaximalLength + 1 characters long. + static const int kBase10MaximalLength = 17; + + // Converts the given double 'v' to ascii. 'v' must not be NaN, +Infinity, or + // -Infinity. In SHORTEST_SINGLE-mode this restriction also applies to 'v' + // after it has been casted to a single-precision float. That is, in this + // mode static_cast(v) must not be NaN, +Infinity or -Infinity. + // + // The result should be interpreted as buffer * 10^(point-length). + // + // The output depends on the given mode: + // - SHORTEST: produce the least amount of digits for which the internal + // identity requirement is still satisfied. If the digits are printed + // (together with the correct exponent) then reading this number will give + // 'v' again. The buffer will choose the representation that is closest to + // 'v'. If there are two at the same distance, than the one farther away + // from 0 is chosen (halfway cases - ending with 5 - are rounded up). + // In this mode the 'requested_digits' parameter is ignored. + // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. + // - FIXED: produces digits necessary to print a given number with + // 'requested_digits' digits after the decimal point. The produced digits + // might be too short in which case the caller has to fill the remainder + // with '0's. + // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. + // Halfway cases are rounded towards +/-Infinity (away from 0). The call + // toFixed(0.15, 2) thus returns buffer="2", point=0. + // The returned buffer may contain digits that would be truncated from the + // shortest representation of the input. + // - PRECISION: produces 'requested_digits' where the first digit is not '0'. + // Even though the length of produced digits usually equals + // 'requested_digits', the function is allowed to return fewer digits, in + // which case the caller has to fill the missing digits with '0's. + // Halfway cases are again rounded away from 0. + // DoubleToAscii expects the given buffer to be big enough to hold all + // digits and a terminating null-character. In SHORTEST-mode it expects a + // buffer of at least kBase10MaximalLength + 1. In all other modes the + // requested_digits parameter and the padding-zeroes limit the size of the + // output. Don't forget the decimal point, the exponent character and the + // terminating null-character when computing the maximal output size. + // The given length is only used in debug mode to ensure the buffer is big + // enough. + static void DoubleToAscii(double v, + DtoaMode mode, + int requested_digits, + char* buffer, + int buffer_length, + bool* sign, + int* length, + int* point); + + private: + // Implementation for ToShortest and ToShortestSingle. + bool ToShortestIeeeNumber(double value, + StringBuilder* result_builder, + DtoaMode mode) const; + + // If the value is a special value (NaN or Infinity) constructs the + // corresponding string using the configured infinity/nan-symbol. + // If either of them is NULL or the value is not special then the + // function returns false. + bool HandleSpecialValues(double value, StringBuilder* result_builder) const; + // Constructs an exponential representation (i.e. 1.234e56). + // The given exponent assumes a decimal point after the first decimal digit. + void CreateExponentialRepresentation(const char* decimal_digits, + int length, + int exponent, + StringBuilder* result_builder) const; + // Creates a decimal representation (i.e 1234.5678). + void CreateDecimalRepresentation(const char* decimal_digits, + int length, + int decimal_point, + int digits_after_point, + StringBuilder* result_builder) const; + + const int flags_; + const char* const infinity_symbol_; + const char* const nan_symbol_; + const char exponent_character_; + const int decimal_in_shortest_low_; + const int decimal_in_shortest_high_; + const int max_leading_padding_zeroes_in_precision_mode_; + const int max_trailing_padding_zeroes_in_precision_mode_; + + DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); +}; + + +class StringToDoubleConverter { + public: + // Enumeration for allowing octals and ignoring junk when converting + // strings to numbers. + enum Flags { + NO_FLAGS = 0, + ALLOW_HEX = 1, + ALLOW_OCTALS = 2, + ALLOW_TRAILING_JUNK = 4, + ALLOW_LEADING_SPACES = 8, + ALLOW_TRAILING_SPACES = 16, + ALLOW_SPACES_AFTER_SIGN = 32 + }; + + // Flags should be a bit-or combination of the possible Flags-enum. + // - NO_FLAGS: no special flags. + // - ALLOW_HEX: recognizes the prefix "0x". Hex numbers may only be integers. + // Ex: StringToDouble("0x1234") -> 4660.0 + // In StringToDouble("0x1234.56") the characters ".56" are trailing + // junk. The result of the call is hence dependent on + // the ALLOW_TRAILING_JUNK flag and/or the junk value. + // With this flag "0x" is a junk-string. Even with ALLOW_TRAILING_JUNK, + // the string will not be parsed as "0" followed by junk. + // + // - ALLOW_OCTALS: recognizes the prefix "0" for octals: + // If a sequence of octal digits starts with '0', then the number is + // read as octal integer. Octal numbers may only be integers. + // Ex: StringToDouble("01234") -> 668.0 + // StringToDouble("012349") -> 12349.0 // Not a sequence of octal + // // digits. + // In StringToDouble("01234.56") the characters ".56" are trailing + // junk. The result of the call is hence dependent on + // the ALLOW_TRAILING_JUNK flag and/or the junk value. + // In StringToDouble("01234e56") the characters "e56" are trailing + // junk, too. + // - ALLOW_TRAILING_JUNK: ignore trailing characters that are not part of + // a double literal. + // - ALLOW_LEADING_SPACES: skip over leading whitespace, including spaces, + // new-lines, and tabs. + // - ALLOW_TRAILING_SPACES: ignore trailing whitespace. + // - ALLOW_SPACES_AFTER_SIGN: ignore whitespace after the sign. + // Ex: StringToDouble("- 123.2") -> -123.2. + // StringToDouble("+ 123.2") -> 123.2 + // + // empty_string_value is returned when an empty string is given as input. + // If ALLOW_LEADING_SPACES or ALLOW_TRAILING_SPACES are set, then a string + // containing only spaces is converted to the 'empty_string_value', too. + // + // junk_string_value is returned when + // a) ALLOW_TRAILING_JUNK is not set, and a junk character (a character not + // part of a double-literal) is found. + // b) ALLOW_TRAILING_JUNK is set, but the string does not start with a + // double literal. + // + // infinity_symbol and nan_symbol are strings that are used to detect + // inputs that represent infinity and NaN. They can be null, in which case + // they are ignored. + // The conversion routine first reads any possible signs. Then it compares the + // following character of the input-string with the first character of + // the infinity, and nan-symbol. If either matches, the function assumes, that + // a match has been found, and expects the following input characters to match + // the remaining characters of the special-value symbol. + // This means that the following restrictions apply to special-value symbols: + // - they must not start with signs ('+', or '-'), + // - they must not have the same first character. + // - they must not start with digits. + // + // Examples: + // flags = ALLOW_HEX | ALLOW_TRAILING_JUNK, + // empty_string_value = 0.0, + // junk_string_value = NaN, + // infinity_symbol = "infinity", + // nan_symbol = "nan": + // StringToDouble("0x1234") -> 4660.0. + // StringToDouble("0x1234K") -> 4660.0. + // StringToDouble("") -> 0.0 // empty_string_value. + // StringToDouble(" ") -> NaN // junk_string_value. + // StringToDouble(" 1") -> NaN // junk_string_value. + // StringToDouble("0x") -> NaN // junk_string_value. + // StringToDouble("-123.45") -> -123.45. + // StringToDouble("--123.45") -> NaN // junk_string_value. + // StringToDouble("123e45") -> 123e45. + // StringToDouble("123E45") -> 123e45. + // StringToDouble("123e+45") -> 123e45. + // StringToDouble("123E-45") -> 123e-45. + // StringToDouble("123e") -> 123.0 // trailing junk ignored. + // StringToDouble("123e-") -> 123.0 // trailing junk ignored. + // StringToDouble("+NaN") -> NaN // NaN string literal. + // StringToDouble("-infinity") -> -inf. // infinity literal. + // StringToDouble("Infinity") -> NaN // junk_string_value. + // + // flags = ALLOW_OCTAL | ALLOW_LEADING_SPACES, + // empty_string_value = 0.0, + // junk_string_value = NaN, + // infinity_symbol = NULL, + // nan_symbol = NULL: + // StringToDouble("0x1234") -> NaN // junk_string_value. + // StringToDouble("01234") -> 668.0. + // StringToDouble("") -> 0.0 // empty_string_value. + // StringToDouble(" ") -> 0.0 // empty_string_value. + // StringToDouble(" 1") -> 1.0 + // StringToDouble("0x") -> NaN // junk_string_value. + // StringToDouble("0123e45") -> NaN // junk_string_value. + // StringToDouble("01239E45") -> 1239e45. + // StringToDouble("-infinity") -> NaN // junk_string_value. + // StringToDouble("NaN") -> NaN // junk_string_value. + StringToDoubleConverter(int flags, + double empty_string_value, + double junk_string_value, + const char* infinity_symbol, + const char* nan_symbol) + : flags_(flags), + empty_string_value_(empty_string_value), + junk_string_value_(junk_string_value), + infinity_symbol_(infinity_symbol), + nan_symbol_(nan_symbol) { + } + + // Performs the conversion. + // The output parameter 'processed_characters_count' is set to the number + // of characters that have been processed to read the number. + // Spaces than are processed with ALLOW_{LEADING|TRAILING}_SPACES are included + // in the 'processed_characters_count'. Trailing junk is never included. + double StringToDouble(const char* buffer, + int length, + int* processed_characters_count) const; + + // Same as StringToDouble above but for 16 bit characters. + double StringToDouble(const uc16* buffer, + int length, + int* processed_characters_count) const; + + // Same as StringToDouble but reads a float. + // Note that this is not equivalent to static_cast(StringToDouble(...)) + // due to potential double-rounding. + float StringToFloat(const char* buffer, + int length, + int* processed_characters_count) const; + + // Same as StringToFloat above but for 16 bit characters. + float StringToFloat(const uc16* buffer, + int length, + int* processed_characters_count) const; + + private: + const int flags_; + const double empty_string_value_; + const double junk_string_value_; + const char* const infinity_symbol_; + const char* const nan_symbol_; + + template + double StringToIeee(Iterator start_pointer, + int length, + bool read_as_double, + int* processed_characters_count) const; + + DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter); +}; + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ diff --git a/contrib/libdouble-conversion/double-conversion/fast-dtoa.cc b/contrib/libdouble-conversion/double-conversion/fast-dtoa.cc new file mode 100644 index 00000000000..61350383a95 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/fast-dtoa.cc @@ -0,0 +1,665 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "fast-dtoa.h" + +#include "cached-powers.h" +#include "diy-fp.h" +#include "ieee.h" + +namespace double_conversion { + +// The minimal and maximal target exponent define the range of w's binary +// exponent, where 'w' is the result of multiplying the input by a cached power +// of ten. +// +// A different range might be chosen on a different platform, to optimize digit +// generation, but a smaller range requires more powers of ten to be cached. +static const int kMinimalTargetExponent = -60; +static const int kMaximalTargetExponent = -32; + + +// Adjusts the last digit of the generated number, and screens out generated +// solutions that may be inaccurate. A solution may be inaccurate if it is +// outside the safe interval, or if we cannot prove that it is closer to the +// input than a neighboring representation of the same length. +// +// Input: * buffer containing the digits of too_high / 10^kappa +// * the buffer's length +// * distance_too_high_w == (too_high - w).f() * unit +// * unsafe_interval == (too_high - too_low).f() * unit +// * rest = (too_high - buffer * 10^kappa).f() * unit +// * ten_kappa = 10^kappa * unit +// * unit = the common multiplier +// Output: returns true if the buffer is guaranteed to contain the closest +// representable number to the input. +// Modifies the generated digits in the buffer to approach (round towards) w. +static bool RoundWeed(Vector buffer, + int length, + uint64_t distance_too_high_w, + uint64_t unsafe_interval, + uint64_t rest, + uint64_t ten_kappa, + uint64_t unit) { + uint64_t small_distance = distance_too_high_w - unit; + uint64_t big_distance = distance_too_high_w + unit; + // Let w_low = too_high - big_distance, and + // w_high = too_high - small_distance. + // Note: w_low < w < w_high + // + // The real w (* unit) must lie somewhere inside the interval + // ]w_low; w_high[ (often written as "(w_low; w_high)") + + // Basically the buffer currently contains a number in the unsafe interval + // ]too_low; too_high[ with too_low < w < too_high + // + // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // ^v 1 unit ^ ^ ^ ^ + // boundary_high --------------------- . . . . + // ^v 1 unit . . . . + // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . . + // . . ^ . . + // . big_distance . . . + // . . . . rest + // small_distance . . . . + // v . . . . + // w_high - - - - - - - - - - - - - - - - - - . . . . + // ^v 1 unit . . . . + // w ---------------------------------------- . . . . + // ^v 1 unit v . . . + // w_low - - - - - - - - - - - - - - - - - - - - - . . . + // . . v + // buffer --------------------------------------------------+-------+-------- + // . . + // safe_interval . + // v . + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - . + // ^v 1 unit . + // boundary_low ------------------------- unsafe_interval + // ^v 1 unit v + // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // + // + // Note that the value of buffer could lie anywhere inside the range too_low + // to too_high. + // + // boundary_low, boundary_high and w are approximations of the real boundaries + // and v (the input number). They are guaranteed to be precise up to one unit. + // In fact the error is guaranteed to be strictly less than one unit. + // + // Anything that lies outside the unsafe interval is guaranteed not to round + // to v when read again. + // Anything that lies inside the safe interval is guaranteed to round to v + // when read again. + // If the number inside the buffer lies inside the unsafe interval but not + // inside the safe interval then we simply do not know and bail out (returning + // false). + // + // Similarly we have to take into account the imprecision of 'w' when finding + // the closest representation of 'w'. If we have two potential + // representations, and one is closer to both w_low and w_high, then we know + // it is closer to the actual value v. + // + // By generating the digits of too_high we got the largest (closest to + // too_high) buffer that is still in the unsafe interval. In the case where + // w_high < buffer < too_high we try to decrement the buffer. + // This way the buffer approaches (rounds towards) w. + // There are 3 conditions that stop the decrementation process: + // 1) the buffer is already below w_high + // 2) decrementing the buffer would make it leave the unsafe interval + // 3) decrementing the buffer would yield a number below w_high and farther + // away than the current number. In other words: + // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high + // Instead of using the buffer directly we use its distance to too_high. + // Conceptually rest ~= too_high - buffer + // We need to do the following tests in this order to avoid over- and + // underflows. + ASSERT(rest <= unsafe_interval); + while (rest < small_distance && // Negated condition 1 + unsafe_interval - rest >= ten_kappa && // Negated condition 2 + (rest + ten_kappa < small_distance || // buffer{-1} > w_high + small_distance - rest >= rest + ten_kappa - small_distance)) { + buffer[length - 1]--; + rest += ten_kappa; + } + + // We have approached w+ as much as possible. We now test if approaching w- + // would require changing the buffer. If yes, then we have two possible + // representations close to w, but we cannot decide which one is closer. + if (rest < big_distance && + unsafe_interval - rest >= ten_kappa && + (rest + ten_kappa < big_distance || + big_distance - rest > rest + ten_kappa - big_distance)) { + return false; + } + + // Weeding test. + // The safe interval is [too_low + 2 ulp; too_high - 2 ulp] + // Since too_low = too_high - unsafe_interval this is equivalent to + // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp] + // Conceptually we have: rest ~= too_high - buffer + return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit); +} + + +// Rounds the buffer upwards if the result is closer to v by possibly adding +// 1 to the buffer. If the precision of the calculation is not sufficient to +// round correctly, return false. +// The rounding might shift the whole buffer in which case the kappa is +// adjusted. For example "99", kappa = 3 might become "10", kappa = 4. +// +// If 2*rest > ten_kappa then the buffer needs to be round up. +// rest can have an error of +/- 1 unit. This function accounts for the +// imprecision and returns false, if the rounding direction cannot be +// unambiguously determined. +// +// Precondition: rest < ten_kappa. +static bool RoundWeedCounted(Vector buffer, + int length, + uint64_t rest, + uint64_t ten_kappa, + uint64_t unit, + int* kappa) { + ASSERT(rest < ten_kappa); + // The following tests are done in a specific order to avoid overflows. They + // will work correctly with any uint64 values of rest < ten_kappa and unit. + // + // If the unit is too big, then we don't know which way to round. For example + // a unit of 50 means that the real number lies within rest +/- 50. If + // 10^kappa == 40 then there is no way to tell which way to round. + if (unit >= ten_kappa) return false; + // Even if unit is just half the size of 10^kappa we are already completely + // lost. (And after the previous test we know that the expression will not + // over/underflow.) + if (ten_kappa - unit <= unit) return false; + // If 2 * (rest + unit) <= 10^kappa we can safely round down. + if ((ten_kappa - rest > rest) && (ten_kappa - 2 * rest >= 2 * unit)) { + return true; + } + // If 2 * (rest - unit) >= 10^kappa, then we can safely round up. + if ((rest > unit) && (ten_kappa - (rest - unit) <= (rest - unit))) { + // Increment the last digit recursively until we find a non '9' digit. + buffer[length - 1]++; + for (int i = length - 1; i > 0; --i) { + if (buffer[i] != '0' + 10) break; + buffer[i] = '0'; + buffer[i - 1]++; + } + // If the first digit is now '0'+ 10 we had a buffer with all '9's. With the + // exception of the first digit all digits are now '0'. Simply switch the + // first digit to '1' and adjust the kappa. Example: "99" becomes "10" and + // the power (the kappa) is increased. + if (buffer[0] == '0' + 10) { + buffer[0] = '1'; + (*kappa) += 1; + } + return true; + } + return false; +} + +// Returns the biggest power of ten that is less than or equal to the given +// number. We furthermore receive the maximum number of bits 'number' has. +// +// Returns power == 10^(exponent_plus_one-1) such that +// power <= number < power * 10. +// If number_bits == 0 then 0^(0-1) is returned. +// The number of bits must be <= 32. +// Precondition: number < (1 << (number_bits + 1)). + +// Inspired by the method for finding an integer log base 10 from here: +// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 +static unsigned int const kSmallPowersOfTen[] = + {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, + 1000000000}; + +static void BiggestPowerTen(uint32_t number, + int number_bits, + uint32_t* power, + int* exponent_plus_one) { + ASSERT(number < (1u << (number_bits + 1))); + // 1233/4096 is approximately 1/lg(10). + int exponent_plus_one_guess = ((number_bits + 1) * 1233 >> 12); + // We increment to skip over the first entry in the kPowersOf10 table. + // Note: kPowersOf10[i] == 10^(i-1). + exponent_plus_one_guess++; + // We don't have any guarantees that 2^number_bits <= number. + if (number < kSmallPowersOfTen[exponent_plus_one_guess]) { + exponent_plus_one_guess--; + } + *power = kSmallPowersOfTen[exponent_plus_one_guess]; + *exponent_plus_one = exponent_plus_one_guess; +} + +// Generates the digits of input number w. +// w is a floating-point number (DiyFp), consisting of a significand and an +// exponent. Its exponent is bounded by kMinimalTargetExponent and +// kMaximalTargetExponent. +// Hence -60 <= w.e() <= -32. +// +// Returns false if it fails, in which case the generated digits in the buffer +// should not be used. +// Preconditions: +// * low, w and high are correct up to 1 ulp (unit in the last place). That +// is, their error must be less than a unit of their last digits. +// * low.e() == w.e() == high.e() +// * low < w < high, and taking into account their error: low~ <= high~ +// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent +// Postconditions: returns false if procedure fails. +// otherwise: +// * buffer is not null-terminated, but len contains the number of digits. +// * buffer contains the shortest possible decimal digit-sequence +// such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the +// correct values of low and high (without their error). +// * if more than one decimal representation gives the minimal number of +// decimal digits then the one closest to W (where W is the correct value +// of w) is chosen. +// Remark: this procedure takes into account the imprecision of its input +// numbers. If the precision is not enough to guarantee all the postconditions +// then false is returned. This usually happens rarely (~0.5%). +// +// Say, for the sake of example, that +// w.e() == -48, and w.f() == 0x1234567890abcdef +// w's value can be computed by w.f() * 2^w.e() +// We can obtain w's integral digits by simply shifting w.f() by -w.e(). +// -> w's integral part is 0x1234 +// w's fractional part is therefore 0x567890abcdef. +// Printing w's integral part is easy (simply print 0x1234 in decimal). +// In order to print its fraction we repeatedly multiply the fraction by 10 and +// get each digit. Example the first digit after the point would be computed by +// (0x567890abcdef * 10) >> 48. -> 3 +// The whole thing becomes slightly more complicated because we want to stop +// once we have enough digits. That is, once the digits inside the buffer +// represent 'w' we can stop. Everything inside the interval low - high +// represents w. However we have to pay attention to low, high and w's +// imprecision. +static bool DigitGen(DiyFp low, + DiyFp w, + DiyFp high, + Vector buffer, + int* length, + int* kappa) { + ASSERT(low.e() == w.e() && w.e() == high.e()); + ASSERT(low.f() + 1 <= high.f() - 1); + ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent); + // low, w and high are imprecise, but by less than one ulp (unit in the last + // place). + // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that + // the new numbers are outside of the interval we want the final + // representation to lie in. + // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield + // numbers that are certain to lie in the interval. We will use this fact + // later on. + // We will now start by generating the digits within the uncertain + // interval. Later we will weed out representations that lie outside the safe + // interval and thus _might_ lie outside the correct interval. + uint64_t unit = 1; + DiyFp too_low = DiyFp(low.f() - unit, low.e()); + DiyFp too_high = DiyFp(high.f() + unit, high.e()); + // too_low and too_high are guaranteed to lie outside the interval we want the + // generated number in. + DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low); + // We now cut the input number into two parts: the integral digits and the + // fractionals. We will not write any decimal separator though, but adapt + // kappa instead. + // Reminder: we are currently computing the digits (stored inside the buffer) + // such that: too_low < buffer * 10^kappa < too_high + // We use too_high for the digit_generation and stop as soon as possible. + // If we stop early we effectively round down. + DiyFp one = DiyFp(static_cast(1) << -w.e(), w.e()); + // Division by one is a shift. + uint32_t integrals = static_cast(too_high.f() >> -one.e()); + // Modulo by one is an and. + uint64_t fractionals = too_high.f() & (one.f() - 1); + uint32_t divisor; + int divisor_exponent_plus_one; + BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), + &divisor, &divisor_exponent_plus_one); + *kappa = divisor_exponent_plus_one; + *length = 0; + // Loop invariant: buffer = too_high / 10^kappa (integer division) + // The invariant holds for the first iteration: kappa has been initialized + // with the divisor exponent + 1. And the divisor is the biggest power of ten + // that is smaller than integrals. + while (*kappa > 0) { + int digit = integrals / divisor; + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + integrals %= divisor; + (*kappa)--; + // Note that kappa now equals the exponent of the divisor and that the + // invariant thus holds again. + uint64_t rest = + (static_cast(integrals) << -one.e()) + fractionals; + // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e()) + // Reminder: unsafe_interval.e() == one.e() + if (rest < unsafe_interval.f()) { + // Rounding down (by not emitting the remaining digits) yields a number + // that lies within the unsafe interval. + return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(), + unsafe_interval.f(), rest, + static_cast(divisor) << -one.e(), unit); + } + divisor /= 10; + } + + // The integrals have been generated. We are at the point of the decimal + // separator. In the following loop we simply multiply the remaining digits by + // 10 and divide by one. We just need to pay attention to multiply associated + // data (like the interval or 'unit'), too. + // Note that the multiplication by 10 does not overflow, because w.e >= -60 + // and thus one.e >= -60. + ASSERT(one.e() >= -60); + ASSERT(fractionals < one.f()); + ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); + for (;;) { + fractionals *= 10; + unit *= 10; + unsafe_interval.set_f(unsafe_interval.f() * 10); + // Integer division by one. + int digit = static_cast(fractionals >> -one.e()); + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + fractionals &= one.f() - 1; // Modulo by one. + (*kappa)--; + if (fractionals < unsafe_interval.f()) { + return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit, + unsafe_interval.f(), fractionals, one.f(), unit); + } + } +} + + + +// Generates (at most) requested_digits digits of input number w. +// w is a floating-point number (DiyFp), consisting of a significand and an +// exponent. Its exponent is bounded by kMinimalTargetExponent and +// kMaximalTargetExponent. +// Hence -60 <= w.e() <= -32. +// +// Returns false if it fails, in which case the generated digits in the buffer +// should not be used. +// Preconditions: +// * w is correct up to 1 ulp (unit in the last place). That +// is, its error must be strictly less than a unit of its last digit. +// * kMinimalTargetExponent <= w.e() <= kMaximalTargetExponent +// +// Postconditions: returns false if procedure fails. +// otherwise: +// * buffer is not null-terminated, but length contains the number of +// digits. +// * the representation in buffer is the most precise representation of +// requested_digits digits. +// * buffer contains at most requested_digits digits of w. If there are less +// than requested_digits digits then some trailing '0's have been removed. +// * kappa is such that +// w = buffer * 10^kappa + eps with |eps| < 10^kappa / 2. +// +// Remark: This procedure takes into account the imprecision of its input +// numbers. If the precision is not enough to guarantee all the postconditions +// then false is returned. This usually happens rarely, but the failure-rate +// increases with higher requested_digits. +static bool DigitGenCounted(DiyFp w, + int requested_digits, + Vector buffer, + int* length, + int* kappa) { + ASSERT(kMinimalTargetExponent <= w.e() && w.e() <= kMaximalTargetExponent); + ASSERT(kMinimalTargetExponent >= -60); + ASSERT(kMaximalTargetExponent <= -32); + // w is assumed to have an error less than 1 unit. Whenever w is scaled we + // also scale its error. + uint64_t w_error = 1; + // We cut the input number into two parts: the integral digits and the + // fractional digits. We don't emit any decimal separator, but adapt kappa + // instead. Example: instead of writing "1.2" we put "12" into the buffer and + // increase kappa by 1. + DiyFp one = DiyFp(static_cast(1) << -w.e(), w.e()); + // Division by one is a shift. + uint32_t integrals = static_cast(w.f() >> -one.e()); + // Modulo by one is an and. + uint64_t fractionals = w.f() & (one.f() - 1); + uint32_t divisor; + int divisor_exponent_plus_one; + BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()), + &divisor, &divisor_exponent_plus_one); + *kappa = divisor_exponent_plus_one; + *length = 0; + + // Loop invariant: buffer = w / 10^kappa (integer division) + // The invariant holds for the first iteration: kappa has been initialized + // with the divisor exponent + 1. And the divisor is the biggest power of ten + // that is smaller than 'integrals'. + while (*kappa > 0) { + int digit = integrals / divisor; + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + requested_digits--; + integrals %= divisor; + (*kappa)--; + // Note that kappa now equals the exponent of the divisor and that the + // invariant thus holds again. + if (requested_digits == 0) break; + divisor /= 10; + } + + if (requested_digits == 0) { + uint64_t rest = + (static_cast(integrals) << -one.e()) + fractionals; + return RoundWeedCounted(buffer, *length, rest, + static_cast(divisor) << -one.e(), w_error, + kappa); + } + + // The integrals have been generated. We are at the point of the decimal + // separator. In the following loop we simply multiply the remaining digits by + // 10 and divide by one. We just need to pay attention to multiply associated + // data (the 'unit'), too. + // Note that the multiplication by 10 does not overflow, because w.e >= -60 + // and thus one.e >= -60. + ASSERT(one.e() >= -60); + ASSERT(fractionals < one.f()); + ASSERT(UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF) / 10 >= one.f()); + while (requested_digits > 0 && fractionals > w_error) { + fractionals *= 10; + w_error *= 10; + // Integer division by one. + int digit = static_cast(fractionals >> -one.e()); + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + requested_digits--; + fractionals &= one.f() - 1; // Modulo by one. + (*kappa)--; + } + if (requested_digits != 0) return false; + return RoundWeedCounted(buffer, *length, fractionals, one.f(), w_error, + kappa); +} + + +// Provides a decimal representation of v. +// Returns true if it succeeds, otherwise the result cannot be trusted. +// There will be *length digits inside the buffer (not null-terminated). +// If the function returns true then +// v == (double) (buffer * 10^decimal_exponent). +// The digits in the buffer are the shortest representation possible: no +// 0.09999999999999999 instead of 0.1. The shorter representation will even be +// chosen even if the longer one would be closer to v. +// The last digit will be closest to the actual v. That is, even if several +// digits might correctly yield 'v' when read again, the closest will be +// computed. +static bool Grisu3(double v, + FastDtoaMode mode, + Vector buffer, + int* length, + int* decimal_exponent) { + DiyFp w = Double(v).AsNormalizedDiyFp(); + // boundary_minus and boundary_plus are the boundaries between v and its + // closest floating-point neighbors. Any number strictly between + // boundary_minus and boundary_plus will round to v when convert to a double. + // Grisu3 will never output representations that lie exactly on a boundary. + DiyFp boundary_minus, boundary_plus; + if (mode == FAST_DTOA_SHORTEST) { + Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus); + } else { + ASSERT(mode == FAST_DTOA_SHORTEST_SINGLE); + float single_v = static_cast(v); + Single(single_v).NormalizedBoundaries(&boundary_minus, &boundary_plus); + } + ASSERT(boundary_plus.e() == w.e()); + DiyFp ten_mk; // Cached power of ten: 10^-k + int mk; // -k + int ten_mk_minimal_binary_exponent = + kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); + int ten_mk_maximal_binary_exponent = + kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); + PowersOfTenCache::GetCachedPowerForBinaryExponentRange( + ten_mk_minimal_binary_exponent, + ten_mk_maximal_binary_exponent, + &ten_mk, &mk); + ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + + DiyFp::kSignificandSize) && + (kMaximalTargetExponent >= w.e() + ten_mk.e() + + DiyFp::kSignificandSize)); + // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a + // 64 bit significand and ten_mk is thus only precise up to 64 bits. + + // The DiyFp::Times procedure rounds its result, and ten_mk is approximated + // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now + // off by a small amount. + // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. + // In other words: let f = scaled_w.f() and e = scaled_w.e(), then + // (f-1) * 2^e < w*10^k < (f+1) * 2^e + DiyFp scaled_w = DiyFp::Times(w, ten_mk); + ASSERT(scaled_w.e() == + boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize); + // In theory it would be possible to avoid some recomputations by computing + // the difference between w and boundary_minus/plus (a power of 2) and to + // compute scaled_boundary_minus/plus by subtracting/adding from + // scaled_w. However the code becomes much less readable and the speed + // enhancements are not terriffic. + DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk); + DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk); + + // DigitGen will generate the digits of scaled_w. Therefore we have + // v == (double) (scaled_w * 10^-mk). + // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an + // integer than it will be updated. For instance if scaled_w == 1.23 then + // the buffer will be filled with "123" und the decimal_exponent will be + // decreased by 2. + int kappa; + bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus, + buffer, length, &kappa); + *decimal_exponent = -mk + kappa; + return result; +} + + +// The "counted" version of grisu3 (see above) only generates requested_digits +// number of digits. This version does not generate the shortest representation, +// and with enough requested digits 0.1 will at some point print as 0.9999999... +// Grisu3 is too imprecise for real halfway cases (1.5 will not work) and +// therefore the rounding strategy for halfway cases is irrelevant. +static bool Grisu3Counted(double v, + int requested_digits, + Vector buffer, + int* length, + int* decimal_exponent) { + DiyFp w = Double(v).AsNormalizedDiyFp(); + DiyFp ten_mk; // Cached power of ten: 10^-k + int mk; // -k + int ten_mk_minimal_binary_exponent = + kMinimalTargetExponent - (w.e() + DiyFp::kSignificandSize); + int ten_mk_maximal_binary_exponent = + kMaximalTargetExponent - (w.e() + DiyFp::kSignificandSize); + PowersOfTenCache::GetCachedPowerForBinaryExponentRange( + ten_mk_minimal_binary_exponent, + ten_mk_maximal_binary_exponent, + &ten_mk, &mk); + ASSERT((kMinimalTargetExponent <= w.e() + ten_mk.e() + + DiyFp::kSignificandSize) && + (kMaximalTargetExponent >= w.e() + ten_mk.e() + + DiyFp::kSignificandSize)); + // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a + // 64 bit significand and ten_mk is thus only precise up to 64 bits. + + // The DiyFp::Times procedure rounds its result, and ten_mk is approximated + // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now + // off by a small amount. + // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. + // In other words: let f = scaled_w.f() and e = scaled_w.e(), then + // (f-1) * 2^e < w*10^k < (f+1) * 2^e + DiyFp scaled_w = DiyFp::Times(w, ten_mk); + + // We now have (double) (scaled_w * 10^-mk). + // DigitGen will generate the first requested_digits digits of scaled_w and + // return together with a kappa such that scaled_w ~= buffer * 10^kappa. (It + // will not always be exactly the same since DigitGenCounted only produces a + // limited number of digits.) + int kappa; + bool result = DigitGenCounted(scaled_w, requested_digits, + buffer, length, &kappa); + *decimal_exponent = -mk + kappa; + return result; +} + + +bool FastDtoa(double v, + FastDtoaMode mode, + int requested_digits, + Vector buffer, + int* length, + int* decimal_point) { + ASSERT(v > 0); + ASSERT(!Double(v).IsSpecial()); + + bool result = false; + int decimal_exponent = 0; + switch (mode) { + case FAST_DTOA_SHORTEST: + case FAST_DTOA_SHORTEST_SINGLE: + result = Grisu3(v, mode, buffer, length, &decimal_exponent); + break; + case FAST_DTOA_PRECISION: + result = Grisu3Counted(v, requested_digits, + buffer, length, &decimal_exponent); + break; + default: + UNREACHABLE(); + } + if (result) { + *decimal_point = *length + decimal_exponent; + buffer[*length] = '\0'; + } + return result; +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/fast-dtoa.h b/contrib/libdouble-conversion/double-conversion/fast-dtoa.h new file mode 100644 index 00000000000..5f1e8eee5e5 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/fast-dtoa.h @@ -0,0 +1,88 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_FAST_DTOA_H_ +#define DOUBLE_CONVERSION_FAST_DTOA_H_ + +#include "utils.h" + +namespace double_conversion { + +enum FastDtoaMode { + // Computes the shortest representation of the given input. The returned + // result will be the most accurate number of this length. Longer + // representations might be more accurate. + FAST_DTOA_SHORTEST, + // Same as FAST_DTOA_SHORTEST but for single-precision floats. + FAST_DTOA_SHORTEST_SINGLE, + // Computes a representation where the precision (number of digits) is + // given as input. The precision is independent of the decimal point. + FAST_DTOA_PRECISION +}; + +// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not +// include the terminating '\0' character. +static const int kFastDtoaMaximalLength = 17; +// Same for single-precision numbers. +static const int kFastDtoaMaximalSingleLength = 9; + +// Provides a decimal representation of v. +// The result should be interpreted as buffer * 10^(point - length). +// +// Precondition: +// * v must be a strictly positive finite double. +// +// Returns true if it succeeds, otherwise the result can not be trusted. +// There will be *length digits inside the buffer followed by a null terminator. +// If the function returns true and mode equals +// - FAST_DTOA_SHORTEST, then +// the parameter requested_digits is ignored. +// The result satisfies +// v == (double) (buffer * 10^(point - length)). +// The digits in the buffer are the shortest representation possible. E.g. +// if 0.099999999999 and 0.1 represent the same double then "1" is returned +// with point = 0. +// The last digit will be closest to the actual v. That is, even if several +// digits might correctly yield 'v' when read again, the buffer will contain +// the one closest to v. +// - FAST_DTOA_PRECISION, then +// the buffer contains requested_digits digits. +// the difference v - (buffer * 10^(point-length)) is closest to zero for +// all possible representations of requested_digits digits. +// If there are two values that are equally close, then FastDtoa returns +// false. +// For both modes the buffer must be large enough to hold the result. +bool FastDtoa(double d, + FastDtoaMode mode, + int requested_digits, + Vector buffer, + int* length, + int* decimal_point); + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_FAST_DTOA_H_ diff --git a/contrib/libdouble-conversion/double-conversion/fixed-dtoa.cc b/contrib/libdouble-conversion/double-conversion/fixed-dtoa.cc new file mode 100644 index 00000000000..aef65fdc211 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/fixed-dtoa.cc @@ -0,0 +1,404 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include + +#include "fixed-dtoa.h" +#include "ieee.h" + +namespace double_conversion { + +// Represents a 128bit type. This class should be replaced by a native type on +// platforms that support 128bit integers. +class UInt128 { + public: + UInt128() : high_bits_(0), low_bits_(0) { } + UInt128(uint64_t high, uint64_t low) : high_bits_(high), low_bits_(low) { } + + void Multiply(uint32_t multiplicand) { + uint64_t accumulator; + + accumulator = (low_bits_ & kMask32) * multiplicand; + uint32_t part = static_cast(accumulator & kMask32); + accumulator >>= 32; + accumulator = accumulator + (low_bits_ >> 32) * multiplicand; + low_bits_ = (accumulator << 32) + part; + accumulator >>= 32; + accumulator = accumulator + (high_bits_ & kMask32) * multiplicand; + part = static_cast(accumulator & kMask32); + accumulator >>= 32; + accumulator = accumulator + (high_bits_ >> 32) * multiplicand; + high_bits_ = (accumulator << 32) + part; + ASSERT((accumulator >> 32) == 0); + } + + void Shift(int shift_amount) { + ASSERT(-64 <= shift_amount && shift_amount <= 64); + if (shift_amount == 0) { + return; + } else if (shift_amount == -64) { + high_bits_ = low_bits_; + low_bits_ = 0; + } else if (shift_amount == 64) { + low_bits_ = high_bits_; + high_bits_ = 0; + } else if (shift_amount <= 0) { + high_bits_ <<= -shift_amount; + high_bits_ += low_bits_ >> (64 + shift_amount); + low_bits_ <<= -shift_amount; + } else { + low_bits_ >>= shift_amount; + low_bits_ += high_bits_ << (64 - shift_amount); + high_bits_ >>= shift_amount; + } + } + + // Modifies *this to *this MOD (2^power). + // Returns *this DIV (2^power). + int DivModPowerOf2(int power) { + if (power >= 64) { + int result = static_cast(high_bits_ >> (power - 64)); + high_bits_ -= static_cast(result) << (power - 64); + return result; + } else { + uint64_t part_low = low_bits_ >> power; + uint64_t part_high = high_bits_ << (64 - power); + int result = static_cast(part_low + part_high); + high_bits_ = 0; + low_bits_ -= part_low << power; + return result; + } + } + + bool IsZero() const { + return high_bits_ == 0 && low_bits_ == 0; + } + + int BitAt(int position) { + if (position >= 64) { + return static_cast(high_bits_ >> (position - 64)) & 1; + } else { + return static_cast(low_bits_ >> position) & 1; + } + } + + private: + static const uint64_t kMask32 = 0xFFFFFFFF; + // Value == (high_bits_ << 64) + low_bits_ + uint64_t high_bits_; + uint64_t low_bits_; +}; + + +static const int kDoubleSignificandSize = 53; // Includes the hidden bit. + + +static void FillDigits32FixedLength(uint32_t number, int requested_length, + Vector buffer, int* length) { + for (int i = requested_length - 1; i >= 0; --i) { + buffer[(*length) + i] = '0' + number % 10; + number /= 10; + } + *length += requested_length; +} + + +static void FillDigits32(uint32_t number, Vector buffer, int* length) { + int number_length = 0; + // We fill the digits in reverse order and exchange them afterwards. + while (number != 0) { + int digit = number % 10; + number /= 10; + buffer[(*length) + number_length] = static_cast('0' + digit); + number_length++; + } + // Exchange the digits. + int i = *length; + int j = *length + number_length - 1; + while (i < j) { + char tmp = buffer[i]; + buffer[i] = buffer[j]; + buffer[j] = tmp; + i++; + j--; + } + *length += number_length; +} + + +static void FillDigits64FixedLength(uint64_t number, + Vector buffer, int* length) { + const uint32_t kTen7 = 10000000; + // For efficiency cut the number into 3 uint32_t parts, and print those. + uint32_t part2 = static_cast(number % kTen7); + number /= kTen7; + uint32_t part1 = static_cast(number % kTen7); + uint32_t part0 = static_cast(number / kTen7); + + FillDigits32FixedLength(part0, 3, buffer, length); + FillDigits32FixedLength(part1, 7, buffer, length); + FillDigits32FixedLength(part2, 7, buffer, length); +} + + +static void FillDigits64(uint64_t number, Vector buffer, int* length) { + const uint32_t kTen7 = 10000000; + // For efficiency cut the number into 3 uint32_t parts, and print those. + uint32_t part2 = static_cast(number % kTen7); + number /= kTen7; + uint32_t part1 = static_cast(number % kTen7); + uint32_t part0 = static_cast(number / kTen7); + + if (part0 != 0) { + FillDigits32(part0, buffer, length); + FillDigits32FixedLength(part1, 7, buffer, length); + FillDigits32FixedLength(part2, 7, buffer, length); + } else if (part1 != 0) { + FillDigits32(part1, buffer, length); + FillDigits32FixedLength(part2, 7, buffer, length); + } else { + FillDigits32(part2, buffer, length); + } +} + + +static void RoundUp(Vector buffer, int* length, int* decimal_point) { + // An empty buffer represents 0. + if (*length == 0) { + buffer[0] = '1'; + *decimal_point = 1; + *length = 1; + return; + } + // Round the last digit until we either have a digit that was not '9' or until + // we reached the first digit. + buffer[(*length) - 1]++; + for (int i = (*length) - 1; i > 0; --i) { + if (buffer[i] != '0' + 10) { + return; + } + buffer[i] = '0'; + buffer[i - 1]++; + } + // If the first digit is now '0' + 10, we would need to set it to '0' and add + // a '1' in front. However we reach the first digit only if all following + // digits had been '9' before rounding up. Now all trailing digits are '0' and + // we simply switch the first digit to '1' and update the decimal-point + // (indicating that the point is now one digit to the right). + if (buffer[0] == '0' + 10) { + buffer[0] = '1'; + (*decimal_point)++; + } +} + + +// The given fractionals number represents a fixed-point number with binary +// point at bit (-exponent). +// Preconditions: +// -128 <= exponent <= 0. +// 0 <= fractionals * 2^exponent < 1 +// The buffer holds the result. +// The function will round its result. During the rounding-process digits not +// generated by this function might be updated, and the decimal-point variable +// might be updated. If this function generates the digits 99 and the buffer +// already contained "199" (thus yielding a buffer of "19999") then a +// rounding-up will change the contents of the buffer to "20000". +static void FillFractionals(uint64_t fractionals, int exponent, + int fractional_count, Vector buffer, + int* length, int* decimal_point) { + ASSERT(-128 <= exponent && exponent <= 0); + // 'fractionals' is a fixed-point number, with binary point at bit + // (-exponent). Inside the function the non-converted remainder of fractionals + // is a fixed-point number, with binary point at bit 'point'. + if (-exponent <= 64) { + // One 64 bit number is sufficient. + ASSERT(fractionals >> 56 == 0); + int point = -exponent; + for (int i = 0; i < fractional_count; ++i) { + if (fractionals == 0) break; + // Instead of multiplying by 10 we multiply by 5 and adjust the point + // location. This way the fractionals variable will not overflow. + // Invariant at the beginning of the loop: fractionals < 2^point. + // Initially we have: point <= 64 and fractionals < 2^56 + // After each iteration the point is decremented by one. + // Note that 5^3 = 125 < 128 = 2^7. + // Therefore three iterations of this loop will not overflow fractionals + // (even without the subtraction at the end of the loop body). At this + // time point will satisfy point <= 61 and therefore fractionals < 2^point + // and any further multiplication of fractionals by 5 will not overflow. + fractionals *= 5; + point--; + int digit = static_cast(fractionals >> point); + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + fractionals -= static_cast(digit) << point; + } + // If the first bit after the point is set we have to round up. + if (((fractionals >> (point - 1)) & 1) == 1) { + RoundUp(buffer, length, decimal_point); + } + } else { // We need 128 bits. + ASSERT(64 < -exponent && -exponent <= 128); + UInt128 fractionals128 = UInt128(fractionals, 0); + fractionals128.Shift(-exponent - 64); + int point = 128; + for (int i = 0; i < fractional_count; ++i) { + if (fractionals128.IsZero()) break; + // As before: instead of multiplying by 10 we multiply by 5 and adjust the + // point location. + // This multiplication will not overflow for the same reasons as before. + fractionals128.Multiply(5); + point--; + int digit = fractionals128.DivModPowerOf2(point); + ASSERT(digit <= 9); + buffer[*length] = static_cast('0' + digit); + (*length)++; + } + if (fractionals128.BitAt(point - 1) == 1) { + RoundUp(buffer, length, decimal_point); + } + } +} + + +// Removes leading and trailing zeros. +// If leading zeros are removed then the decimal point position is adjusted. +static void TrimZeros(Vector buffer, int* length, int* decimal_point) { + while (*length > 0 && buffer[(*length) - 1] == '0') { + (*length)--; + } + int first_non_zero = 0; + while (first_non_zero < *length && buffer[first_non_zero] == '0') { + first_non_zero++; + } + if (first_non_zero != 0) { + for (int i = first_non_zero; i < *length; ++i) { + buffer[i - first_non_zero] = buffer[i]; + } + *length -= first_non_zero; + *decimal_point -= first_non_zero; + } +} + + +bool FastFixedDtoa(double v, + int fractional_count, + Vector buffer, + int* length, + int* decimal_point) { + const uint32_t kMaxUInt32 = 0xFFFFFFFF; + uint64_t significand = Double(v).Significand(); + int exponent = Double(v).Exponent(); + // v = significand * 2^exponent (with significand a 53bit integer). + // If the exponent is larger than 20 (i.e. we may have a 73bit number) then we + // don't know how to compute the representation. 2^73 ~= 9.5*10^21. + // If necessary this limit could probably be increased, but we don't need + // more. + if (exponent > 20) return false; + if (fractional_count > 20) return false; + *length = 0; + // At most kDoubleSignificandSize bits of the significand are non-zero. + // Given a 64 bit integer we have 11 0s followed by 53 potentially non-zero + // bits: 0..11*..0xxx..53*..xx + if (exponent + kDoubleSignificandSize > 64) { + // The exponent must be > 11. + // + // We know that v = significand * 2^exponent. + // And the exponent > 11. + // We simplify the task by dividing v by 10^17. + // The quotient delivers the first digits, and the remainder fits into a 64 + // bit number. + // Dividing by 10^17 is equivalent to dividing by 5^17*2^17. + const uint64_t kFive17 = UINT64_2PART_C(0xB1, A2BC2EC5); // 5^17 + uint64_t divisor = kFive17; + int divisor_power = 17; + uint64_t dividend = significand; + uint32_t quotient; + uint64_t remainder; + // Let v = f * 2^e with f == significand and e == exponent. + // Then need q (quotient) and r (remainder) as follows: + // v = q * 10^17 + r + // f * 2^e = q * 10^17 + r + // f * 2^e = q * 5^17 * 2^17 + r + // If e > 17 then + // f * 2^(e-17) = q * 5^17 + r/2^17 + // else + // f = q * 5^17 * 2^(17-e) + r/2^e + if (exponent > divisor_power) { + // We only allow exponents of up to 20 and therefore (17 - e) <= 3 + dividend <<= exponent - divisor_power; + quotient = static_cast(dividend / divisor); + remainder = (dividend % divisor) << divisor_power; + } else { + divisor <<= divisor_power - exponent; + quotient = static_cast(dividend / divisor); + remainder = (dividend % divisor) << exponent; + } + FillDigits32(quotient, buffer, length); + FillDigits64FixedLength(remainder, buffer, length); + *decimal_point = *length; + } else if (exponent >= 0) { + // 0 <= exponent <= 11 + significand <<= exponent; + FillDigits64(significand, buffer, length); + *decimal_point = *length; + } else if (exponent > -kDoubleSignificandSize) { + // We have to cut the number. + uint64_t integrals = significand >> -exponent; + uint64_t fractionals = significand - (integrals << -exponent); + if (integrals > kMaxUInt32) { + FillDigits64(integrals, buffer, length); + } else { + FillDigits32(static_cast(integrals), buffer, length); + } + *decimal_point = *length; + FillFractionals(fractionals, exponent, fractional_count, + buffer, length, decimal_point); + } else if (exponent < -128) { + // This configuration (with at most 20 digits) means that all digits must be + // 0. + ASSERT(fractional_count <= 20); + buffer[0] = '\0'; + *length = 0; + *decimal_point = -fractional_count; + } else { + *decimal_point = 0; + FillFractionals(significand, exponent, fractional_count, + buffer, length, decimal_point); + } + TrimZeros(buffer, length, decimal_point); + buffer[*length] = '\0'; + if ((*length) == 0) { + // The string is empty and the decimal_point thus has no importance. Mimick + // Gay's dtoa and and set it to -fractional_count. + *decimal_point = -fractional_count; + } + return true; +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/fixed-dtoa.h b/contrib/libdouble-conversion/double-conversion/fixed-dtoa.h new file mode 100644 index 00000000000..3bdd08e21f5 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/fixed-dtoa.h @@ -0,0 +1,56 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_FIXED_DTOA_H_ +#define DOUBLE_CONVERSION_FIXED_DTOA_H_ + +#include "utils.h" + +namespace double_conversion { + +// Produces digits necessary to print a given number with +// 'fractional_count' digits after the decimal point. +// The buffer must be big enough to hold the result plus one terminating null +// character. +// +// The produced digits might be too short in which case the caller has to fill +// the gaps with '0's. +// Example: FastFixedDtoa(0.001, 5, ...) is allowed to return buffer = "1", and +// decimal_point = -2. +// Halfway cases are rounded towards +/-Infinity (away from 0). The call +// FastFixedDtoa(0.15, 2, ...) thus returns buffer = "2", decimal_point = 0. +// The returned buffer may contain digits that would be truncated from the +// shortest representation of the input. +// +// This method only works for some parameters. If it can't handle the input it +// returns false. The output is null-terminated when the function succeeds. +bool FastFixedDtoa(double v, int fractional_count, + Vector buffer, int* length, int* decimal_point); + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_FIXED_DTOA_H_ diff --git a/contrib/libdouble-conversion/double-conversion/ieee.h b/contrib/libdouble-conversion/double-conversion/ieee.h new file mode 100644 index 00000000000..661141d1a8d --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/ieee.h @@ -0,0 +1,402 @@ +// Copyright 2012 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_DOUBLE_H_ +#define DOUBLE_CONVERSION_DOUBLE_H_ + +#include "diy-fp.h" + +namespace double_conversion { + +// We assume that doubles and uint64_t have the same endianness. +static uint64_t double_to_uint64(double d) { return BitCast(d); } +static double uint64_to_double(uint64_t d64) { return BitCast(d64); } +static uint32_t float_to_uint32(float f) { return BitCast(f); } +static float uint32_to_float(uint32_t d32) { return BitCast(d32); } + +// Helper functions for doubles. +class Double { + public: + static const uint64_t kSignMask = UINT64_2PART_C(0x80000000, 00000000); + static const uint64_t kExponentMask = UINT64_2PART_C(0x7FF00000, 00000000); + static const uint64_t kSignificandMask = UINT64_2PART_C(0x000FFFFF, FFFFFFFF); + static const uint64_t kHiddenBit = UINT64_2PART_C(0x00100000, 00000000); + static const int kPhysicalSignificandSize = 52; // Excludes the hidden bit. + static const int kSignificandSize = 53; + + Double() : d64_(0) {} + explicit Double(double d) : d64_(double_to_uint64(d)) {} + explicit Double(uint64_t d64) : d64_(d64) {} + explicit Double(DiyFp diy_fp) + : d64_(DiyFpToUint64(diy_fp)) {} + + // The value encoded by this Double must be greater or equal to +0.0. + // It must not be special (infinity, or NaN). + DiyFp AsDiyFp() const { + ASSERT(Sign() > 0); + ASSERT(!IsSpecial()); + return DiyFp(Significand(), Exponent()); + } + + // The value encoded by this Double must be strictly greater than 0. + DiyFp AsNormalizedDiyFp() const { + ASSERT(value() > 0.0); + uint64_t f = Significand(); + int e = Exponent(); + + // The current double could be a denormal. + while ((f & kHiddenBit) == 0) { + f <<= 1; + e--; + } + // Do the final shifts in one go. + f <<= DiyFp::kSignificandSize - kSignificandSize; + e -= DiyFp::kSignificandSize - kSignificandSize; + return DiyFp(f, e); + } + + // Returns the double's bit as uint64. + uint64_t AsUint64() const { + return d64_; + } + + // Returns the next greater double. Returns +infinity on input +infinity. + double NextDouble() const { + if (d64_ == kInfinity) return Double(kInfinity).value(); + if (Sign() < 0 && Significand() == 0) { + // -0.0 + return 0.0; + } + if (Sign() < 0) { + return Double(d64_ - 1).value(); + } else { + return Double(d64_ + 1).value(); + } + } + + double PreviousDouble() const { + if (d64_ == (kInfinity | kSignMask)) return -Double::Infinity(); + if (Sign() < 0) { + return Double(d64_ + 1).value(); + } else { + if (Significand() == 0) return -0.0; + return Double(d64_ - 1).value(); + } + } + + int Exponent() const { + if (IsDenormal()) return kDenormalExponent; + + uint64_t d64 = AsUint64(); + int biased_e = + static_cast((d64 & kExponentMask) >> kPhysicalSignificandSize); + return biased_e - kExponentBias; + } + + uint64_t Significand() const { + uint64_t d64 = AsUint64(); + uint64_t significand = d64 & kSignificandMask; + if (!IsDenormal()) { + return significand + kHiddenBit; + } else { + return significand; + } + } + + // Returns true if the double is a denormal. + bool IsDenormal() const { + uint64_t d64 = AsUint64(); + return (d64 & kExponentMask) == 0; + } + + // We consider denormals not to be special. + // Hence only Infinity and NaN are special. + bool IsSpecial() const { + uint64_t d64 = AsUint64(); + return (d64 & kExponentMask) == kExponentMask; + } + + bool IsNan() const { + uint64_t d64 = AsUint64(); + return ((d64 & kExponentMask) == kExponentMask) && + ((d64 & kSignificandMask) != 0); + } + + bool IsInfinite() const { + uint64_t d64 = AsUint64(); + return ((d64 & kExponentMask) == kExponentMask) && + ((d64 & kSignificandMask) == 0); + } + + int Sign() const { + uint64_t d64 = AsUint64(); + return (d64 & kSignMask) == 0? 1: -1; + } + + // Precondition: the value encoded by this Double must be greater or equal + // than +0.0. + DiyFp UpperBoundary() const { + ASSERT(Sign() > 0); + return DiyFp(Significand() * 2 + 1, Exponent() - 1); + } + + // Computes the two boundaries of this. + // The bigger boundary (m_plus) is normalized. The lower boundary has the same + // exponent as m_plus. + // Precondition: the value encoded by this Double must be greater than 0. + void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { + ASSERT(value() > 0.0); + DiyFp v = this->AsDiyFp(); + DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); + DiyFp m_minus; + if (LowerBoundaryIsCloser()) { + m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); + } else { + m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); + } + m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); + m_minus.set_e(m_plus.e()); + *out_m_plus = m_plus; + *out_m_minus = m_minus; + } + + bool LowerBoundaryIsCloser() const { + // The boundary is closer if the significand is of the form f == 2^p-1 then + // the lower boundary is closer. + // Think of v = 1000e10 and v- = 9999e9. + // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but + // at a distance of 1e8. + // The only exception is for the smallest normal: the largest denormal is + // at the same distance as its successor. + // Note: denormals have the same exponent as the smallest normals. + bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0); + return physical_significand_is_zero && (Exponent() != kDenormalExponent); + } + + double value() const { return uint64_to_double(d64_); } + + // Returns the significand size for a given order of magnitude. + // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude. + // This function returns the number of significant binary digits v will have + // once it's encoded into a double. In almost all cases this is equal to + // kSignificandSize. The only exceptions are denormals. They start with + // leading zeroes and their effective significand-size is hence smaller. + static int SignificandSizeForOrderOfMagnitude(int order) { + if (order >= (kDenormalExponent + kSignificandSize)) { + return kSignificandSize; + } + if (order <= kDenormalExponent) return 0; + return order - kDenormalExponent; + } + + static double Infinity() { + return Double(kInfinity).value(); + } + + static double NaN() { + return Double(kNaN).value(); + } + + private: + static const int kExponentBias = 0x3FF + kPhysicalSignificandSize; + static const int kDenormalExponent = -kExponentBias + 1; + static const int kMaxExponent = 0x7FF - kExponentBias; + static const uint64_t kInfinity = UINT64_2PART_C(0x7FF00000, 00000000); + static const uint64_t kNaN = UINT64_2PART_C(0x7FF80000, 00000000); + + const uint64_t d64_; + + static uint64_t DiyFpToUint64(DiyFp diy_fp) { + uint64_t significand = diy_fp.f(); + int exponent = diy_fp.e(); + while (significand > kHiddenBit + kSignificandMask) { + significand >>= 1; + exponent++; + } + if (exponent >= kMaxExponent) { + return kInfinity; + } + if (exponent < kDenormalExponent) { + return 0; + } + while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) { + significand <<= 1; + exponent--; + } + uint64_t biased_exponent; + if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) { + biased_exponent = 0; + } else { + biased_exponent = static_cast(exponent + kExponentBias); + } + return (significand & kSignificandMask) | + (biased_exponent << kPhysicalSignificandSize); + } + + DISALLOW_COPY_AND_ASSIGN(Double); +}; + +class Single { + public: + static const uint32_t kSignMask = 0x80000000; + static const uint32_t kExponentMask = 0x7F800000; + static const uint32_t kSignificandMask = 0x007FFFFF; + static const uint32_t kHiddenBit = 0x00800000; + static const int kPhysicalSignificandSize = 23; // Excludes the hidden bit. + static const int kSignificandSize = 24; + + Single() : d32_(0) {} + explicit Single(float f) : d32_(float_to_uint32(f)) {} + explicit Single(uint32_t d32) : d32_(d32) {} + + // The value encoded by this Single must be greater or equal to +0.0. + // It must not be special (infinity, or NaN). + DiyFp AsDiyFp() const { + ASSERT(Sign() > 0); + ASSERT(!IsSpecial()); + return DiyFp(Significand(), Exponent()); + } + + // Returns the single's bit as uint64. + uint32_t AsUint32() const { + return d32_; + } + + int Exponent() const { + if (IsDenormal()) return kDenormalExponent; + + uint32_t d32 = AsUint32(); + int biased_e = + static_cast((d32 & kExponentMask) >> kPhysicalSignificandSize); + return biased_e - kExponentBias; + } + + uint32_t Significand() const { + uint32_t d32 = AsUint32(); + uint32_t significand = d32 & kSignificandMask; + if (!IsDenormal()) { + return significand + kHiddenBit; + } else { + return significand; + } + } + + // Returns true if the single is a denormal. + bool IsDenormal() const { + uint32_t d32 = AsUint32(); + return (d32 & kExponentMask) == 0; + } + + // We consider denormals not to be special. + // Hence only Infinity and NaN are special. + bool IsSpecial() const { + uint32_t d32 = AsUint32(); + return (d32 & kExponentMask) == kExponentMask; + } + + bool IsNan() const { + uint32_t d32 = AsUint32(); + return ((d32 & kExponentMask) == kExponentMask) && + ((d32 & kSignificandMask) != 0); + } + + bool IsInfinite() const { + uint32_t d32 = AsUint32(); + return ((d32 & kExponentMask) == kExponentMask) && + ((d32 & kSignificandMask) == 0); + } + + int Sign() const { + uint32_t d32 = AsUint32(); + return (d32 & kSignMask) == 0? 1: -1; + } + + // Computes the two boundaries of this. + // The bigger boundary (m_plus) is normalized. The lower boundary has the same + // exponent as m_plus. + // Precondition: the value encoded by this Single must be greater than 0. + void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const { + ASSERT(value() > 0.0); + DiyFp v = this->AsDiyFp(); + DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1)); + DiyFp m_minus; + if (LowerBoundaryIsCloser()) { + m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2); + } else { + m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1); + } + m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e())); + m_minus.set_e(m_plus.e()); + *out_m_plus = m_plus; + *out_m_minus = m_minus; + } + + // Precondition: the value encoded by this Single must be greater or equal + // than +0.0. + DiyFp UpperBoundary() const { + ASSERT(Sign() > 0); + return DiyFp(Significand() * 2 + 1, Exponent() - 1); + } + + bool LowerBoundaryIsCloser() const { + // The boundary is closer if the significand is of the form f == 2^p-1 then + // the lower boundary is closer. + // Think of v = 1000e10 and v- = 9999e9. + // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but + // at a distance of 1e8. + // The only exception is for the smallest normal: the largest denormal is + // at the same distance as its successor. + // Note: denormals have the same exponent as the smallest normals. + bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0); + return physical_significand_is_zero && (Exponent() != kDenormalExponent); + } + + float value() const { return uint32_to_float(d32_); } + + static float Infinity() { + return Single(kInfinity).value(); + } + + static float NaN() { + return Single(kNaN).value(); + } + + private: + static const int kExponentBias = 0x7F + kPhysicalSignificandSize; + static const int kDenormalExponent = -kExponentBias + 1; + static const int kMaxExponent = 0xFF - kExponentBias; + static const uint32_t kInfinity = 0x7F800000; + static const uint32_t kNaN = 0x7FC00000; + + const uint32_t d32_; + + DISALLOW_COPY_AND_ASSIGN(Single); +}; + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_DOUBLE_H_ diff --git a/contrib/libdouble-conversion/double-conversion/strtod.cc b/contrib/libdouble-conversion/double-conversion/strtod.cc new file mode 100644 index 00000000000..17abcbb2a55 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/strtod.cc @@ -0,0 +1,555 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include +#include + +#include "strtod.h" +#include "bignum.h" +#include "cached-powers.h" +#include "ieee.h" + +namespace double_conversion { + +// 2^53 = 9007199254740992. +// Any integer with at most 15 decimal digits will hence fit into a double +// (which has a 53bit significand) without loss of precision. +static const int kMaxExactDoubleIntegerDecimalDigits = 15; +// 2^64 = 18446744073709551616 > 10^19 +static const int kMaxUint64DecimalDigits = 19; + +// Max double: 1.7976931348623157 x 10^308 +// Min non-zero double: 4.9406564584124654 x 10^-324 +// Any x >= 10^309 is interpreted as +infinity. +// Any x <= 10^-324 is interpreted as 0. +// Note that 2.5e-324 (despite being smaller than the min double) will be read +// as non-zero (equal to the min non-zero double). +static const int kMaxDecimalPower = 309; +static const int kMinDecimalPower = -324; + +// 2^64 = 18446744073709551616 +static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF); + + +static const double exact_powers_of_ten[] = { + 1.0, // 10^0 + 10.0, + 100.0, + 1000.0, + 10000.0, + 100000.0, + 1000000.0, + 10000000.0, + 100000000.0, + 1000000000.0, + 10000000000.0, // 10^10 + 100000000000.0, + 1000000000000.0, + 10000000000000.0, + 100000000000000.0, + 1000000000000000.0, + 10000000000000000.0, + 100000000000000000.0, + 1000000000000000000.0, + 10000000000000000000.0, + 100000000000000000000.0, // 10^20 + 1000000000000000000000.0, + // 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22 + 10000000000000000000000.0 +}; +static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten); + +// Maximum number of significant digits in the decimal representation. +// In fact the value is 772 (see conversions.cc), but to give us some margin +// we round up to 780. +static const int kMaxSignificantDecimalDigits = 780; + +static Vector TrimLeadingZeros(Vector buffer) { + for (int i = 0; i < buffer.length(); i++) { + if (buffer[i] != '0') { + return buffer.SubVector(i, buffer.length()); + } + } + return Vector(buffer.start(), 0); +} + + +static Vector TrimTrailingZeros(Vector buffer) { + for (int i = buffer.length() - 1; i >= 0; --i) { + if (buffer[i] != '0') { + return buffer.SubVector(0, i + 1); + } + } + return Vector(buffer.start(), 0); +} + + +static void CutToMaxSignificantDigits(Vector buffer, + int exponent, + char* significant_buffer, + int* significant_exponent) { + for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) { + significant_buffer[i] = buffer[i]; + } + // The input buffer has been trimmed. Therefore the last digit must be + // different from '0'. + ASSERT(buffer[buffer.length() - 1] != '0'); + // Set the last digit to be non-zero. This is sufficient to guarantee + // correct rounding. + significant_buffer[kMaxSignificantDecimalDigits - 1] = '1'; + *significant_exponent = + exponent + (buffer.length() - kMaxSignificantDecimalDigits); +} + + +// Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits. +// If possible the input-buffer is reused, but if the buffer needs to be +// modified (due to cutting), then the input needs to be copied into the +// buffer_copy_space. +static void TrimAndCut(Vector buffer, int exponent, + char* buffer_copy_space, int space_size, + Vector* trimmed, int* updated_exponent) { + Vector left_trimmed = TrimLeadingZeros(buffer); + Vector right_trimmed = TrimTrailingZeros(left_trimmed); + exponent += left_trimmed.length() - right_trimmed.length(); + if (right_trimmed.length() > kMaxSignificantDecimalDigits) { + (void) space_size; // Mark variable as used. + ASSERT(space_size >= kMaxSignificantDecimalDigits); + CutToMaxSignificantDigits(right_trimmed, exponent, + buffer_copy_space, updated_exponent); + *trimmed = Vector(buffer_copy_space, + kMaxSignificantDecimalDigits); + } else { + *trimmed = right_trimmed; + *updated_exponent = exponent; + } +} + + +// Reads digits from the buffer and converts them to a uint64. +// Reads in as many digits as fit into a uint64. +// When the string starts with "1844674407370955161" no further digit is read. +// Since 2^64 = 18446744073709551616 it would still be possible read another +// digit if it was less or equal than 6, but this would complicate the code. +static uint64_t ReadUint64(Vector buffer, + int* number_of_read_digits) { + uint64_t result = 0; + int i = 0; + while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) { + int digit = buffer[i++] - '0'; + ASSERT(0 <= digit && digit <= 9); + result = 10 * result + digit; + } + *number_of_read_digits = i; + return result; +} + + +// Reads a DiyFp from the buffer. +// The returned DiyFp is not necessarily normalized. +// If remaining_decimals is zero then the returned DiyFp is accurate. +// Otherwise it has been rounded and has error of at most 1/2 ulp. +static void ReadDiyFp(Vector buffer, + DiyFp* result, + int* remaining_decimals) { + int read_digits; + uint64_t significand = ReadUint64(buffer, &read_digits); + if (buffer.length() == read_digits) { + *result = DiyFp(significand, 0); + *remaining_decimals = 0; + } else { + // Round the significand. + if (buffer[read_digits] >= '5') { + significand++; + } + // Compute the binary exponent. + int exponent = 0; + *result = DiyFp(significand, exponent); + *remaining_decimals = buffer.length() - read_digits; + } +} + + +static bool DoubleStrtod(Vector trimmed, + int exponent, + double* result) { +#if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS) + // On x86 the floating-point stack can be 64 or 80 bits wide. If it is + // 80 bits wide (as is the case on Linux) then double-rounding occurs and the + // result is not accurate. + // We know that Windows32 uses 64 bits and is therefore accurate. + // Note that the ARM simulator is compiled for 32bits. It therefore exhibits + // the same problem. + return false; +#endif + if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) { + int read_digits; + // The trimmed input fits into a double. + // If the 10^exponent (resp. 10^-exponent) fits into a double too then we + // can compute the result-double simply by multiplying (resp. dividing) the + // two numbers. + // This is possible because IEEE guarantees that floating-point operations + // return the best possible approximation. + if (exponent < 0 && -exponent < kExactPowersOfTenSize) { + // 10^-exponent fits into a double. + *result = static_cast(ReadUint64(trimmed, &read_digits)); + ASSERT(read_digits == trimmed.length()); + *result /= exact_powers_of_ten[-exponent]; + return true; + } + if (0 <= exponent && exponent < kExactPowersOfTenSize) { + // 10^exponent fits into a double. + *result = static_cast(ReadUint64(trimmed, &read_digits)); + ASSERT(read_digits == trimmed.length()); + *result *= exact_powers_of_ten[exponent]; + return true; + } + int remaining_digits = + kMaxExactDoubleIntegerDecimalDigits - trimmed.length(); + if ((0 <= exponent) && + (exponent - remaining_digits < kExactPowersOfTenSize)) { + // The trimmed string was short and we can multiply it with + // 10^remaining_digits. As a result the remaining exponent now fits + // into a double too. + *result = static_cast(ReadUint64(trimmed, &read_digits)); + ASSERT(read_digits == trimmed.length()); + *result *= exact_powers_of_ten[remaining_digits]; + *result *= exact_powers_of_ten[exponent - remaining_digits]; + return true; + } + } + return false; +} + + +// Returns 10^exponent as an exact DiyFp. +// The given exponent must be in the range [1; kDecimalExponentDistance[. +static DiyFp AdjustmentPowerOfTen(int exponent) { + ASSERT(0 < exponent); + ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance); + // Simply hardcode the remaining powers for the given decimal exponent + // distance. + ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8); + switch (exponent) { + case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60); + case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57); + case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54); + case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50); + case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47); + case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44); + case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40); + default: + UNREACHABLE(); + } +} + + +// If the function returns true then the result is the correct double. +// Otherwise it is either the correct double or the double that is just below +// the correct double. +static bool DiyFpStrtod(Vector buffer, + int exponent, + double* result) { + DiyFp input; + int remaining_decimals; + ReadDiyFp(buffer, &input, &remaining_decimals); + // Since we may have dropped some digits the input is not accurate. + // If remaining_decimals is different than 0 than the error is at most + // .5 ulp (unit in the last place). + // We don't want to deal with fractions and therefore keep a common + // denominator. + const int kDenominatorLog = 3; + const int kDenominator = 1 << kDenominatorLog; + // Move the remaining decimals into the exponent. + exponent += remaining_decimals; + uint64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2); + + int old_e = input.e(); + input.Normalize(); + error <<= old_e - input.e(); + + ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent); + if (exponent < PowersOfTenCache::kMinDecimalExponent) { + *result = 0.0; + return true; + } + DiyFp cached_power; + int cached_decimal_exponent; + PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent, + &cached_power, + &cached_decimal_exponent); + + if (cached_decimal_exponent != exponent) { + int adjustment_exponent = exponent - cached_decimal_exponent; + DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent); + input.Multiply(adjustment_power); + if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) { + // The product of input with the adjustment power fits into a 64 bit + // integer. + ASSERT(DiyFp::kSignificandSize == 64); + } else { + // The adjustment power is exact. There is hence only an error of 0.5. + error += kDenominator / 2; + } + } + + input.Multiply(cached_power); + // The error introduced by a multiplication of a*b equals + // error_a + error_b + error_a*error_b/2^64 + 0.5 + // Substituting a with 'input' and b with 'cached_power' we have + // error_b = 0.5 (all cached powers have an error of less than 0.5 ulp), + // error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64 + int error_b = kDenominator / 2; + int error_ab = (error == 0 ? 0 : 1); // We round up to 1. + int fixed_error = kDenominator / 2; + error += error_b + error_ab + fixed_error; + + old_e = input.e(); + input.Normalize(); + error <<= old_e - input.e(); + + // See if the double's significand changes if we add/subtract the error. + int order_of_magnitude = DiyFp::kSignificandSize + input.e(); + int effective_significand_size = + Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude); + int precision_digits_count = + DiyFp::kSignificandSize - effective_significand_size; + if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) { + // This can only happen for very small denormals. In this case the + // half-way multiplied by the denominator exceeds the range of an uint64. + // Simply shift everything to the right. + int shift_amount = (precision_digits_count + kDenominatorLog) - + DiyFp::kSignificandSize + 1; + input.set_f(input.f() >> shift_amount); + input.set_e(input.e() + shift_amount); + // We add 1 for the lost precision of error, and kDenominator for + // the lost precision of input.f(). + error = (error >> shift_amount) + 1 + kDenominator; + precision_digits_count -= shift_amount; + } + // We use uint64_ts now. This only works if the DiyFp uses uint64_ts too. + ASSERT(DiyFp::kSignificandSize == 64); + ASSERT(precision_digits_count < 64); + uint64_t one64 = 1; + uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1; + uint64_t precision_bits = input.f() & precision_bits_mask; + uint64_t half_way = one64 << (precision_digits_count - 1); + precision_bits *= kDenominator; + half_way *= kDenominator; + DiyFp rounded_input(input.f() >> precision_digits_count, + input.e() + precision_digits_count); + if (precision_bits >= half_way + error) { + rounded_input.set_f(rounded_input.f() + 1); + } + // If the last_bits are too close to the half-way case than we are too + // inaccurate and round down. In this case we return false so that we can + // fall back to a more precise algorithm. + + *result = Double(rounded_input).value(); + if (half_way - error < precision_bits && precision_bits < half_way + error) { + // Too imprecise. The caller will have to fall back to a slower version. + // However the returned number is guaranteed to be either the correct + // double, or the next-lower double. + return false; + } else { + return true; + } +} + + +// Returns +// - -1 if buffer*10^exponent < diy_fp. +// - 0 if buffer*10^exponent == diy_fp. +// - +1 if buffer*10^exponent > diy_fp. +// Preconditions: +// buffer.length() + exponent <= kMaxDecimalPower + 1 +// buffer.length() + exponent > kMinDecimalPower +// buffer.length() <= kMaxDecimalSignificantDigits +static int CompareBufferWithDiyFp(Vector buffer, + int exponent, + DiyFp diy_fp) { + ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1); + ASSERT(buffer.length() + exponent > kMinDecimalPower); + ASSERT(buffer.length() <= kMaxSignificantDecimalDigits); + // Make sure that the Bignum will be able to hold all our numbers. + // Our Bignum implementation has a separate field for exponents. Shifts will + // consume at most one bigit (< 64 bits). + // ln(10) == 3.3219... + ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits); + Bignum buffer_bignum; + Bignum diy_fp_bignum; + buffer_bignum.AssignDecimalString(buffer); + diy_fp_bignum.AssignUInt64(diy_fp.f()); + if (exponent >= 0) { + buffer_bignum.MultiplyByPowerOfTen(exponent); + } else { + diy_fp_bignum.MultiplyByPowerOfTen(-exponent); + } + if (diy_fp.e() > 0) { + diy_fp_bignum.ShiftLeft(diy_fp.e()); + } else { + buffer_bignum.ShiftLeft(-diy_fp.e()); + } + return Bignum::Compare(buffer_bignum, diy_fp_bignum); +} + + +// Returns true if the guess is the correct double. +// Returns false, when guess is either correct or the next-lower double. +static bool ComputeGuess(Vector trimmed, int exponent, + double* guess) { + if (trimmed.length() == 0) { + *guess = 0.0; + return true; + } + if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) { + *guess = Double::Infinity(); + return true; + } + if (exponent + trimmed.length() <= kMinDecimalPower) { + *guess = 0.0; + return true; + } + + if (DoubleStrtod(trimmed, exponent, guess) || + DiyFpStrtod(trimmed, exponent, guess)) { + return true; + } + if (*guess == Double::Infinity()) { + return true; + } + return false; +} + +double Strtod(Vector buffer, int exponent) { + char copy_buffer[kMaxSignificantDecimalDigits]; + Vector trimmed; + int updated_exponent; + TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, + &trimmed, &updated_exponent); + exponent = updated_exponent; + + double guess; + bool is_correct = ComputeGuess(trimmed, exponent, &guess); + if (is_correct) return guess; + + DiyFp upper_boundary = Double(guess).UpperBoundary(); + int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); + if (comparison < 0) { + return guess; + } else if (comparison > 0) { + return Double(guess).NextDouble(); + } else if ((Double(guess).Significand() & 1) == 0) { + // Round towards even. + return guess; + } else { + return Double(guess).NextDouble(); + } +} + +float Strtof(Vector buffer, int exponent) { + char copy_buffer[kMaxSignificantDecimalDigits]; + Vector trimmed; + int updated_exponent; + TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits, + &trimmed, &updated_exponent); + exponent = updated_exponent; + + double double_guess; + bool is_correct = ComputeGuess(trimmed, exponent, &double_guess); + + float float_guess = static_cast(double_guess); + if (float_guess == double_guess) { + // This shortcut triggers for integer values. + return float_guess; + } + + // We must catch double-rounding. Say the double has been rounded up, and is + // now a boundary of a float, and rounds up again. This is why we have to + // look at previous too. + // Example (in decimal numbers): + // input: 12349 + // high-precision (4 digits): 1235 + // low-precision (3 digits): + // when read from input: 123 + // when rounded from high precision: 124. + // To do this we simply look at the neigbors of the correct result and see + // if they would round to the same float. If the guess is not correct we have + // to look at four values (since two different doubles could be the correct + // double). + + double double_next = Double(double_guess).NextDouble(); + double double_previous = Double(double_guess).PreviousDouble(); + + float f1 = static_cast(double_previous); + float f2 = float_guess; + float f3 = static_cast(double_next); + float f4; + if (is_correct) { + f4 = f3; + } else { + double double_next2 = Double(double_next).NextDouble(); + f4 = static_cast(double_next2); + } + (void) f2; // Mark variable as used. + ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4); + + // If the guess doesn't lie near a single-precision boundary we can simply + // return its float-value. + if (f1 == f4) { + return float_guess; + } + + ASSERT((f1 != f2 && f2 == f3 && f3 == f4) || + (f1 == f2 && f2 != f3 && f3 == f4) || + (f1 == f2 && f2 == f3 && f3 != f4)); + + // guess and next are the two possible canditates (in the same way that + // double_guess was the lower candidate for a double-precision guess). + float guess = f1; + float next = f4; + DiyFp upper_boundary; + if (guess == 0.0f) { + float min_float = 1e-45f; + upper_boundary = Double(static_cast(min_float) / 2).AsDiyFp(); + } else { + upper_boundary = Single(guess).UpperBoundary(); + } + int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary); + if (comparison < 0) { + return guess; + } else if (comparison > 0) { + return next; + } else if ((Single(guess).Significand() & 1) == 0) { + // Round towards even. + return guess; + } else { + return next; + } +} + +} // namespace double_conversion diff --git a/contrib/libdouble-conversion/double-conversion/strtod.h b/contrib/libdouble-conversion/double-conversion/strtod.h new file mode 100644 index 00000000000..ed0293b8f54 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/strtod.h @@ -0,0 +1,45 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_STRTOD_H_ +#define DOUBLE_CONVERSION_STRTOD_H_ + +#include "utils.h" + +namespace double_conversion { + +// The buffer must only contain digits in the range [0-9]. It must not +// contain a dot or a sign. It must not start with '0', and must not be empty. +double Strtod(Vector buffer, int exponent); + +// The buffer must only contain digits in the range [0-9]. It must not +// contain a dot or a sign. It must not start with '0', and must not be empty. +float Strtof(Vector buffer, int exponent); + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_STRTOD_H_ diff --git a/contrib/libdouble-conversion/double-conversion/utils.h b/contrib/libdouble-conversion/double-conversion/utils.h new file mode 100644 index 00000000000..51d5e61e311 --- /dev/null +++ b/contrib/libdouble-conversion/double-conversion/utils.h @@ -0,0 +1,341 @@ +// Copyright 2010 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef DOUBLE_CONVERSION_UTILS_H_ +#define DOUBLE_CONVERSION_UTILS_H_ + +#include +#include + +#include +#ifndef ASSERT +#define ASSERT(condition) \ + assert(condition); +#endif +#ifndef UNIMPLEMENTED +#define UNIMPLEMENTED() (abort()) +#endif +#ifndef DOUBLE_CONVERSION_NO_RETURN +#ifdef _MSC_VER +#define DOUBLE_CONVERSION_NO_RETURN __declspec(noreturn) +#else +#define DOUBLE_CONVERSION_NO_RETURN __attribute__((noreturn)) +#endif +#endif +#ifndef UNREACHABLE +#ifdef _MSC_VER +void DOUBLE_CONVERSION_NO_RETURN abort_noreturn(); +inline void abort_noreturn() { abort(); } +#define UNREACHABLE() (abort_noreturn()) +#else +#define UNREACHABLE() (abort()) +#endif +#endif + + +// Double operations detection based on target architecture. +// Linux uses a 80bit wide floating point stack on x86. This induces double +// rounding, which in turn leads to wrong results. +// An easy way to test if the floating-point operations are correct is to +// evaluate: 89255.0/1e22. If the floating-point stack is 64 bits wide then +// the result is equal to 89255e-22. +// The best way to test this, is to create a division-function and to compare +// the output of the division with the expected result. (Inlining must be +// disabled.) +// On Linux,x86 89255e-22 != Div_double(89255.0/1e22) +#if defined(_M_X64) || defined(__x86_64__) || \ + defined(__ARMEL__) || defined(__avr32__) || \ + defined(__hppa__) || defined(__ia64__) || \ + defined(__mips__) || \ + defined(__powerpc__) || defined(__ppc__) || defined(__ppc64__) || \ + defined(_POWER) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \ + defined(__sparc__) || defined(__sparc) || defined(__s390__) || \ + defined(__SH4__) || defined(__alpha__) || \ + defined(_MIPS_ARCH_MIPS32R2) || \ + defined(__AARCH64EL__) || defined(__aarch64__) +#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1 +#elif defined(__mc68000__) +#undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS +#elif defined(_M_IX86) || defined(__i386__) || defined(__i386) +#if defined(_WIN32) +// Windows uses a 64bit wide floating point stack. +#define DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS 1 +#else +#undef DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS +#endif // _WIN32 +#else +#error Target architecture was not detected as supported by Double-Conversion. +#endif + +#if defined(__GNUC__) +#define DOUBLE_CONVERSION_UNUSED __attribute__((unused)) +#else +#define DOUBLE_CONVERSION_UNUSED +#endif + +#if defined(_WIN32) && !defined(__MINGW32__) + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; // NOLINT +typedef unsigned short uint16_t; // NOLINT +typedef int int32_t; +typedef unsigned int uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +// intptr_t and friends are defined in crtdefs.h through stdio.h. + +#else + +#include + +#endif + +typedef uint16_t uc16; + +// The following macro works on both 32 and 64-bit platforms. +// Usage: instead of writing 0x1234567890123456 +// write UINT64_2PART_C(0x12345678,90123456); +#define UINT64_2PART_C(a, b) (((static_cast(a) << 32) + 0x##b##u)) + + +// The expression ARRAY_SIZE(a) is a compile-time constant of type +// size_t which represents the number of elements of the given +// array. You should only use ARRAY_SIZE on statically allocated +// arrays. +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(a) \ + ((sizeof(a) / sizeof(*(a))) / \ + static_cast(!(sizeof(a) % sizeof(*(a))))) +#endif + +// A macro to disallow the evil copy constructor and operator= functions +// This should be used in the private: declarations for a class +#ifndef DISALLOW_COPY_AND_ASSIGN +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + void operator=(const TypeName&) +#endif + +// A macro to disallow all the implicit constructors, namely the +// default constructor, copy constructor and operator= functions. +// +// This should be used in the private: declarations for a class +// that wants to prevent anyone from instantiating it. This is +// especially useful for classes containing only static methods. +#ifndef DISALLOW_IMPLICIT_CONSTRUCTORS +#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName(); \ + DISALLOW_COPY_AND_ASSIGN(TypeName) +#endif + +namespace double_conversion { + +static const int kCharSize = sizeof(char); + +// Returns the maximum of the two parameters. +template +static T Max(T a, T b) { + return a < b ? b : a; +} + + +// Returns the minimum of the two parameters. +template +static T Min(T a, T b) { + return a < b ? a : b; +} + + +inline int StrLength(const char* string) { + size_t length = strlen(string); + ASSERT(length == static_cast(static_cast(length))); + return static_cast(length); +} + +// This is a simplified version of V8's Vector class. +template +class Vector { + public: + Vector() : start_(NULL), length_(0) {} + Vector(T* data, int len) : start_(data), length_(len) { + ASSERT(len == 0 || (len > 0 && data != NULL)); + } + + // Returns a vector using the same backing storage as this one, + // spanning from and including 'from', to but not including 'to'. + Vector SubVector(int from, int to) { + ASSERT(to <= length_); + ASSERT(from < to); + ASSERT(0 <= from); + return Vector(start() + from, to - from); + } + + // Returns the length of the vector. + int length() const { return length_; } + + // Returns whether or not the vector is empty. + bool is_empty() const { return length_ == 0; } + + // Returns the pointer to the start of the data in the vector. + T* start() const { return start_; } + + // Access individual vector elements - checks bounds in debug mode. + T& operator[](int index) const { + ASSERT(0 <= index && index < length_); + return start_[index]; + } + + T& first() { return start_[0]; } + + T& last() { return start_[length_ - 1]; } + + private: + T* start_; + int length_; +}; + + +// Helper class for building result strings in a character buffer. The +// purpose of the class is to use safe operations that checks the +// buffer bounds on all operations in debug mode. +class StringBuilder { + public: + StringBuilder(char* buffer, int buffer_size) + : buffer_(buffer, buffer_size), position_(0) { } + + ~StringBuilder() { if (!is_finalized()) Finalize(); } + + int size() const { return buffer_.length(); } + + // Get the current position in the builder. + int position() const { + ASSERT(!is_finalized()); + return position_; + } + + // Reset the position. + void Reset() { position_ = 0; } + + // Add a single character to the builder. It is not allowed to add + // 0-characters; use the Finalize() method to terminate the string + // instead. + void AddCharacter(char c) { + ASSERT(c != '\0'); + ASSERT(!is_finalized() && position_ < buffer_.length()); + buffer_[position_++] = c; + } + + // Add an entire string to the builder. Uses strlen() internally to + // compute the length of the input string. + void AddString(const char* s) { + AddSubstring(s, StrLength(s)); + } + + // Add the first 'n' characters of the given string 's' to the + // builder. The input string must have enough characters. + void AddSubstring(const char* s, int n) { + ASSERT(!is_finalized() && position_ + n < buffer_.length()); + ASSERT(static_cast(n) <= strlen(s)); + memmove(&buffer_[position_], s, n * kCharSize); + position_ += n; + } + + + // Add character padding to the builder. If count is non-positive, + // nothing is added to the builder. + void AddPadding(char c, int count) { + for (int i = 0; i < count; i++) { + AddCharacter(c); + } + } + + // Finalize the string by 0-terminating it and returning the buffer. + char* Finalize() { + ASSERT(!is_finalized() && position_ < buffer_.length()); + buffer_[position_] = '\0'; + // Make sure nobody managed to add a 0-character to the + // buffer while building the string. + ASSERT(strlen(buffer_.start()) == static_cast(position_)); + position_ = -1; + ASSERT(is_finalized()); + return buffer_.start(); + } + + private: + Vector buffer_; + int position_; + + bool is_finalized() const { return position_ < 0; } + + DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); +}; + +// The type-based aliasing rule allows the compiler to assume that pointers of +// different types (for some definition of different) never alias each other. +// Thus the following code does not work: +// +// float f = foo(); +// int fbits = *(int*)(&f); +// +// The compiler 'knows' that the int pointer can't refer to f since the types +// don't match, so the compiler may cache f in a register, leaving random data +// in fbits. Using C++ style casts makes no difference, however a pointer to +// char data is assumed to alias any other pointer. This is the 'memcpy +// exception'. +// +// Bit_cast uses the memcpy exception to move the bits from a variable of one +// type of a variable of another type. Of course the end result is likely to +// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) +// will completely optimize BitCast away. +// +// There is an additional use for BitCast. +// Recent gccs will warn when they see casts that may result in breakage due to +// the type-based aliasing rule. If you have checked that there is no breakage +// you can use BitCast to cast one pointer type to another. This confuses gcc +// enough that it can no longer see that you have cast one pointer type to +// another thus avoiding the warning. +template +inline Dest BitCast(const Source& source) { + // Compile time assertion: sizeof(Dest) == sizeof(Source) + // A compile error here means your Dest and Source have different sizes. + DOUBLE_CONVERSION_UNUSED + typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]; + + Dest dest; + memmove(&dest, &source, sizeof(dest)); + return dest; +} + +template +inline Dest BitCast(Source* source) { + return BitCast(reinterpret_cast(source)); +} + +} // namespace double_conversion + +#endif // DOUBLE_CONVERSION_UTILS_H_ diff --git a/contrib/libfarmhash/CMakeLists.txt b/contrib/libfarmhash/CMakeLists.txt new file mode 100644 index 00000000000..170fb17ac53 --- /dev/null +++ b/contrib/libfarmhash/CMakeLists.txt @@ -0,0 +1,5 @@ +include_directories (${CMAKE_CURRENT_BINARY_DIR}) + +add_library(farmhash + farmhash.cc + farmhash.h) diff --git a/contrib/libfarmhash/COPYING b/contrib/libfarmhash/COPYING new file mode 100644 index 00000000000..8176e66198b --- /dev/null +++ b/contrib/libfarmhash/COPYING @@ -0,0 +1,19 @@ +// Copyright (c) 2014 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. diff --git a/contrib/libfarmhash/README b/contrib/libfarmhash/README new file mode 100644 index 00000000000..f19bb5e7b46 --- /dev/null +++ b/contrib/libfarmhash/README @@ -0,0 +1,8 @@ +Original URL: https://code.google.com/p/farmhash/ +Created from version: + +commit afdb2e459d030afd1f3b6116545397509326385e +Author: gpike@google.com +Date: Sun Mar 1 20:04:23 2015 +0000 + + Various additions, updates, and fixes for FarmHash 1.1. diff --git a/contrib/libfarmhash/farmhash.cc b/contrib/libfarmhash/farmhash.cc new file mode 100644 index 00000000000..78dc5242fe9 --- /dev/null +++ b/contrib/libfarmhash/farmhash.cc @@ -0,0 +1,11854 @@ +// Copyright (c) 2014 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// FarmHash, by Geoff Pike + +#include "farmhash.h" +// FARMHASH ASSUMPTIONS: Modify as needed, or use -DFARMHASH_ASSUME_SSE42 etc. +// Note that if you use -DFARMHASH_ASSUME_SSE42 you likely need -msse42 +// (or its equivalent for your compiler); if you use -DFARMHASH_ASSUME_AESNI +// you likely need -maes (or its equivalent for your compiler). + +#ifdef FARMHASH_ASSUME_SSSE3 +#undef FARMHASH_ASSUME_SSSE3 +#define FARMHASH_ASSUME_SSSE3 1 +#endif + +#ifdef FARMHASH_ASSUME_SSE41 +#undef FARMHASH_ASSUME_SSE41 +#define FARMHASH_ASSUME_SSE41 1 +#endif + +#ifdef FARMHASH_ASSUME_SSE42 +#undef FARMHASH_ASSUME_SSE42 +#define FARMHASH_ASSUME_SSE42 1 +#endif + +#ifdef FARMHASH_ASSUME_AESNI +#undef FARMHASH_ASSUME_AESNI +#define FARMHASH_ASSUME_AESNI 1 +#endif + +#ifdef FARMHASH_ASSUME_AVX +#undef FARMHASH_ASSUME_AVX +#define FARMHASH_ASSUME_AVX 1 +#endif + +#if !defined(FARMHASH_CAN_USE_CXX11) && defined(LANG_CXX11) +#define FARMHASH_CAN_USE_CXX11 1 +#else +#undef FARMHASH_CAN_USE_CXX11 +#define FARMHASH_CAN_USE_CXX11 0 +#endif + +// FARMHASH PORTABILITY LAYER: Runtime error if misconfigured + +#ifndef FARMHASH_DIE_IF_MISCONFIGURED +#define FARMHASH_DIE_IF_MISCONFIGURED do { *(char*)(len % 17) = 0; } while (0) +#endif + +// FARMHASH PORTABILITY LAYER: "static inline" or similar + +#ifndef STATIC_INLINE +#define STATIC_INLINE static inline +#endif + +// FARMHASH PORTABILITY LAYER: LIKELY and UNLIKELY + +#if !defined(LIKELY) +#if defined(FARMHASH_NO_BUILTIN_EXPECT) || (defined(FARMHASH_OPTIONAL_BUILTIN_EXPECT) && !defined(HAVE_BUILTIN_EXPECT)) +#define LIKELY(x) (x) +#else +#define LIKELY(x) (__builtin_expect(!!(x), 1)) +#endif +#endif + +#undef UNLIKELY +#define UNLIKELY(x) !LIKELY(!(x)) + +// FARMHASH PORTABILITY LAYER: endianness and byteswapping functions + +#ifdef WORDS_BIGENDIAN +#undef FARMHASH_BIG_ENDIAN +#define FARMHASH_BIG_ENDIAN 1 +#endif + +#if defined(FARMHASH_LITTLE_ENDIAN) && defined(FARMHASH_BIG_ENDIAN) +#error +#endif + +#if !defined(FARMHASH_LITTLE_ENDIAN) && !defined(FARMHASH_BIG_ENDIAN) +#define FARMHASH_UNKNOWN_ENDIAN 1 +#endif + +#if !defined(bswap_32) || !defined(bswap_64) +#undef bswap_32 +#undef bswap_64 + +#if defined(HAVE_BUILTIN_BSWAP) || defined(__clang__) || \ + (defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || \ + __GNUC__ >= 5)) +// Easy case for bswap: no header file needed. +#define bswap_32(x) __builtin_bswap32(x) +#define bswap_64(x) __builtin_bswap64(x) +#endif + +#endif + +#if defined(FARMHASH_UNKNOWN_ENDIAN) || !defined(bswap_64) + +#ifdef _MSC_VER + +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) _byteswap_ulong(x) +#define bswap_64(x) _byteswap_uint64(x) + +#elif defined(__APPLE__) + +// Mac OS X / Darwin features +#include +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) OSSwapInt32(x) +#define bswap_64(x) OSSwapInt64(x) + +#elif defined(__sun) || defined(sun) + +#include +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) BSWAP_32(x) +#define bswap_64(x) BSWAP_64(x) + +#elif defined(__FreeBSD__) + +#include +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) bswap32(x) +#define bswap_64(x) bswap64(x) + +#elif defined(__OpenBSD__) + +#include +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) swap32(x) +#define bswap_64(x) swap64(x) + +#elif defined(__NetBSD__) + +#include +#include +#if defined(__BSWAP_RENAME) && !defined(__bswap_32) +#undef bswap_32 +#undef bswap_64 +#define bswap_32(x) bswap32(x) +#define bswap_64(x) bswap64(x) +#endif + +#else + +#undef bswap_32 +#undef bswap_64 +#include + +#endif + +#ifdef WORDS_BIGENDIAN +#define FARMHASH_BIG_ENDIAN 1 +#endif + +#endif + +#ifdef FARMHASH_BIG_ENDIAN +#define uint32_in_expected_order(x) (bswap_32(x)) +#define uint64_in_expected_order(x) (bswap_64(x)) +#else +#define uint32_in_expected_order(x) (x) +#define uint64_in_expected_order(x) (x) +#endif + +namespace NAMESPACE_FOR_HASH_FUNCTIONS { + +STATIC_INLINE uint64_t Fetch64(const char *p) { + uint64_t result; + memcpy(&result, p, sizeof(result)); + return uint64_in_expected_order(result); +} + +STATIC_INLINE uint32_t Fetch32(const char *p) { + uint32_t result; + memcpy(&result, p, sizeof(result)); + return uint32_in_expected_order(result); +} + +STATIC_INLINE uint32_t Bswap32(uint32_t val) { return bswap_32(val); } +STATIC_INLINE uint64_t Bswap64(uint64_t val) { return bswap_64(val); } + +// FARMHASH PORTABILITY LAYER: bitwise rot + +STATIC_INLINE uint32_t BasicRotate32(uint32_t val, int shift) { + // Avoid shifting by 32: doing so yields an undefined result. + return shift == 0 ? val : ((val >> shift) | (val << (32 - shift))); +} + +STATIC_INLINE uint64_t BasicRotate64(uint64_t val, int shift) { + // Avoid shifting by 64: doing so yields an undefined result. + return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); +} + +#if defined(_MSC_VER) && defined(FARMHASH_ROTR) + +STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) { + return sizeof(unsigned long) == sizeof(val) ? + _lrotr(val, shift) : + BasicRotate32(val, shift); +} + +STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) { + return sizeof(unsigned long) == sizeof(val) ? + _lrotr(val, shift) : + BasicRotate64(val, shift); +} + +#else + +STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) { + return BasicRotate32(val, shift); +} +STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) { + return BasicRotate64(val, shift); +} + +#endif + +} // namespace NAMESPACE_FOR_HASH_FUNCTIONS + +// FARMHASH PORTABILITY LAYER: debug mode or max speed? +// One may use -DFARMHASH_DEBUG=1 or -DFARMHASH_DEBUG=0 to force the issue. + +#if !defined(FARMHASH_DEBUG) && (!defined(NDEBUG) || defined(_DEBUG)) +#define FARMHASH_DEBUG 1 +#endif + +#undef debug_mode +#if FARMHASH_DEBUG +#define debug_mode 1 +#else +#define debug_mode 0 +#endif + +// PLATFORM-SPECIFIC FUNCTIONS AND MACROS + +#undef x86_64 +#if defined (__x86_64) || defined (__x86_64__) +#define x86_64 1 +#else +#define x86_64 0 +#endif + +#undef x86 +#if defined(__i386__) || defined(__i386) || defined(__X86__) +#define x86 1 +#else +#define x86 x86_64 +#endif + +#if !defined(is_64bit) +#define is_64bit (x86_64 || (sizeof(void*) == 8)) +#endif + +#undef can_use_ssse3 +#if defined(__SSSE3__) || defined(FARMHASH_ASSUME_SSSE3) + +#include +#define can_use_ssse3 1 +// Now we can use _mm_hsub_epi16 and so on. + +#else +#define can_use_ssse3 0 +#endif + +#undef can_use_sse41 +#if defined(__SSE4_1__) || defined(FARMHASH_ASSUME_SSE41) + +#include +#define can_use_sse41 1 +// Now we can use _mm_insert_epi64 and so on. + +#else +#define can_use_sse41 0 +#endif + +#undef can_use_sse42 +#if defined(__SSE4_2__) || defined(FARMHASH_ASSUME_SSE42) + +#include +#define can_use_sse42 1 +// Now we can use _mm_crc32_u{32,16,8}. And on 64-bit platforms, _mm_crc32_u64. + +#else +#define can_use_sse42 0 +#endif + +#undef can_use_aesni +#if defined(__AES__) || defined(FARMHASH_ASSUME_AESNI) + +#include +#define can_use_aesni 1 +// Now we can use _mm_aesimc_si128 and so on. + +#else +#define can_use_aesni 0 +#endif + +#undef can_use_avx +#if defined(__AVX__) || defined(FARMHASH_ASSUME_AVX) + +#include +#define can_use_avx 1 + +#else +#define can_use_avx 0 +#endif + +#if can_use_ssse3 || can_use_sse41 || can_use_sse42 || can_use_aesni || can_use_avx +STATIC_INLINE __m128i Fetch128(const char* s) { + return _mm_loadu_si128(reinterpret_cast(s)); +} +#endif +// Building blocks for hash functions + +// std::swap() was in but is in from C++11 on. +#if !FARMHASH_CAN_USE_CXX11 +#include +#endif + +#undef PERMUTE3 +#define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0) + +namespace NAMESPACE_FOR_HASH_FUNCTIONS { + +// Some primes between 2^63 and 2^64 for various uses. +static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; +static const uint64_t k1 = 0xb492b66fbe98f273ULL; +static const uint64_t k2 = 0x9ae16a3b2f90404fULL; + +// Magic numbers for 32-bit hashing. Copied from Murmur3. +static const uint32_t c1 = 0xcc9e2d51; +static const uint32_t c2 = 0x1b873593; + +// A 32-bit to 32-bit integer hash copied from Murmur3. +STATIC_INLINE uint32_t fmix(uint32_t h) +{ + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; +} + +STATIC_INLINE uint32_t Mur(uint32_t a, uint32_t h) { + // Helper from Murmur3 for combining two 32-bit values. + a *= c1; + a = Rotate32(a, 17); + a *= c2; + h ^= a; + h = Rotate32(h, 19); + return h * 5 + 0xe6546b64; +} + +template STATIC_INLINE T DebugTweak(T x) { + if (debug_mode) { + if (sizeof(x) == 4) { + x = ~Bswap32(x * c1); + } else { + x = ~Bswap64(x * k1); + } + } + return x; +} + +template <> uint128_t DebugTweak(uint128_t x) { + if (debug_mode) { + uint64_t y = DebugTweak(Uint128Low64(x)); + uint64_t z = DebugTweak(Uint128High64(x)); + y += z; + z += y; + x = Uint128(y, z * k1); + } + return x; +} + +} // namespace NAMESPACE_FOR_HASH_FUNCTIONS + +using namespace std; +using namespace NAMESPACE_FOR_HASH_FUNCTIONS; +namespace farmhashna { +#undef Fetch +#define Fetch Fetch64 + +#undef Rotate +#define Rotate Rotate64 + +#undef Bswap +#define Bswap Bswap64 + +STATIC_INLINE uint64_t ShiftMix(uint64_t val) { + return val ^ (val >> 47); +} + +STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) { + return Hash128to64(Uint128(u, v)); +} + +STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) { + // Murmur-inspired hashing. + uint64_t a = (u ^ v) * mul; + a ^= (a >> 47); + uint64_t b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; +} + +STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) { + if (len >= 8) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch(s) + k2; + uint64_t b = Fetch(s + len - 8); + uint64_t c = Rotate(b, 37) * mul + a; + uint64_t d = (Rotate(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + if (len >= 4) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch32(s); + return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul); + } + if (len > 0) { + uint8_t a = s[0]; + uint8_t b = s[len >> 1]; + uint8_t c = s[len - 1]; + uint32_t y = static_cast(a) + (static_cast(b) << 8); + uint32_t z = len + (static_cast(c) << 2); + return ShiftMix(y * k2 ^ z * k0) * k2; + } + return k2; +} + +// This probably works well for 16-byte strings as well, but it may be overkill +// in that case. +STATIC_INLINE uint64_t HashLen17to32(const char *s, size_t len) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch(s) * k1; + uint64_t b = Fetch(s + 8); + uint64_t c = Fetch(s + len - 8) * mul; + uint64_t d = Fetch(s + len - 16) * k2; + return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d, + a + Rotate(b + k2, 18) + c, mul); +} + +// Return a 16-byte hash for 48 bytes. Quick and dirty. +// Callers do best to use "random-looking" values for a and b. +STATIC_INLINE pair WeakHashLen32WithSeeds( + uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) { + a += w; + b = Rotate(b + a + z, 21); + uint64_t c = a; + a += x; + a += y; + b += Rotate(a, 44); + return make_pair(a + z, b + c); +} + +// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. +STATIC_INLINE pair WeakHashLen32WithSeeds( + const char* s, uint64_t a, uint64_t b) { + return WeakHashLen32WithSeeds(Fetch(s), + Fetch(s + 8), + Fetch(s + 16), + Fetch(s + 24), + a, + b); +} + +// Return an 8-byte hash for 33 to 64 bytes. +STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch(s) * k2; + uint64_t b = Fetch(s + 8); + uint64_t c = Fetch(s + len - 8) * mul; + uint64_t d = Fetch(s + len - 16) * k2; + uint64_t y = Rotate(a + b, 43) + Rotate(c, 30) + d; + uint64_t z = HashLen16(y, a + Rotate(b + k2, 18) + c, mul); + uint64_t e = Fetch(s + 16) * mul; + uint64_t f = Fetch(s + 24); + uint64_t g = (y + Fetch(s + len - 32)) * mul; + uint64_t h = (z + Fetch(s + len - 24)) * mul; + return HashLen16(Rotate(e + f, 43) + Rotate(g, 30) + h, + e + Rotate(f + a, 18) + g, mul); +} + +uint64_t Hash64(const char *s, size_t len) { + const uint64_t seed = 81; + if (len <= 32) { + if (len <= 16) { + return HashLen0to16(s, len); + } else { + return HashLen17to32(s, len); + } + } else if (len <= 64) { + return HashLen33to64(s, len); + } + + // For strings over 64 bytes we loop. Internal state consists of + // 56 bytes: v, w, x, y, and z. + uint64_t x = seed; + uint64_t y = seed * k1 + 113; + uint64_t z = ShiftMix(y * k2 + 113) * k2; + pair v = make_pair(0, 0); + pair w = make_pair(0, 0); + x = x * k2 + Fetch(s); + + // Set end so that after the loop we have 1 to 64 bytes left to process. + const char* end = s + ((len - 1) / 64) * 64; + const char* last64 = end + ((len - 1) & 63) - 63; + assert(s + len - 64 == last64); + do { + x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)); + std::swap(z, x); + s += 64; + } while (s != end); + uint64_t mul = k1 + ((z & 0xff) << 1); + // Make s point to the last 64 bytes of input. + s = last64; + w.first += ((len - 1) & 63); + v.first += w.first; + w.first += v.first; + x = Rotate(x + y + v.first + Fetch(s + 8), 37) * mul; + y = Rotate(y + v.second + Fetch(s + 48), 42) * mul; + x ^= w.second * 9; + y += v.first * 9 + Fetch(s + 40); + z = Rotate(z + w.first, 33) * mul; + v = WeakHashLen32WithSeeds(s, v.second * mul, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)); + std::swap(z, x); + return HashLen16(HashLen16(v.first, w.first, mul) + ShiftMix(y) * k0 + z, + HashLen16(v.second, w.second, mul) + x, + mul); +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1); + +uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) { + return Hash64WithSeeds(s, len, k2, seed); +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) { + return HashLen16(Hash64(s, len) - seed0, seed1); +} +} // namespace farmhashna +namespace farmhashuo { +#undef Fetch +#define Fetch Fetch64 + +#undef Rotate +#define Rotate Rotate64 + +STATIC_INLINE uint64_t H(uint64_t x, uint64_t y, uint64_t mul, int r) { + uint64_t a = (x ^ y) * mul; + a ^= (a >> 47); + uint64_t b = (y ^ a) * mul; + return Rotate(b, r) * mul; +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, + uint64_t seed0, uint64_t seed1) { + if (len <= 64) { + return farmhashna::Hash64WithSeeds(s, len, seed0, seed1); + } + + // For strings over 64 bytes we loop. Internal state consists of + // 64 bytes: u, v, w, x, y, and z. + uint64_t x = seed0; + uint64_t y = seed1 * k2 + 113; + uint64_t z = farmhashna::ShiftMix(y * k2) * k2; + pair v = make_pair(seed0, seed1); + pair w = make_pair(0, 0); + uint64_t u = x - z; + x *= k2; + uint64_t mul = k2 + (u & 0x82); + + // Set end so that after the loop we have 1 to 64 bytes left to process. + const char* end = s + ((len - 1) / 64) * 64; + const char* last64 = end + ((len - 1) & 63) - 63; + assert(s + len - 64 == last64); + do { + uint64_t a0 = Fetch(s); + uint64_t a1 = Fetch(s + 8); + uint64_t a2 = Fetch(s + 16); + uint64_t a3 = Fetch(s + 24); + uint64_t a4 = Fetch(s + 32); + uint64_t a5 = Fetch(s + 40); + uint64_t a6 = Fetch(s + 48); + uint64_t a7 = Fetch(s + 56); + x += a0 + a1; + y += a2; + z += a3; + v.first += a4; + v.second += a5 + a1; + w.first += a6; + w.second += a7; + + x = Rotate(x, 26); + x *= 9; + y = Rotate(y, 29); + z *= mul; + v.first = Rotate(v.first, 33); + v.second = Rotate(v.second, 30); + w.first ^= x; + w.first *= 9; + z = Rotate(z, 32); + z += w.second; + w.second += z; + z *= 9; + std::swap(u, y); + + z += a0 + a6; + v.first += a2; + v.second += a3; + w.first += a4; + w.second += a5 + a6; + x += a1; + y += a7; + + y += v.first; + v.first += x - y; + v.second += w.first; + w.first += v.second; + w.second += x - y; + x += w.second; + w.second = Rotate(w.second, 34); + std::swap(u, z); + s += 64; + } while (s != end); + // Make s point to the last 64 bytes of input. + s = last64; + u *= 9; + v.second = Rotate(v.second, 28); + v.first = Rotate(v.first, 20); + w.first += ((len - 1) & 63); + u += y; + y += u; + x = Rotate(y - x + v.first + Fetch(s + 8), 37) * mul; + y = Rotate(y ^ v.second ^ Fetch(s + 48), 42) * mul; + x ^= w.second * 9; + y += v.first + Fetch(s + 40); + z = Rotate(z + w.first, 33) * mul; + v = farmhashna::WeakHashLen32WithSeeds(s, v.second * mul, x + w.first); + w = farmhashna::WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)); + return H(farmhashna::HashLen16(v.first + x, w.first ^ y, mul) + z - u, + H(v.second + y, w.second + z, k2, 30) ^ x, + k2, + 31); +} + +uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) { + return len <= 64 ? farmhashna::Hash64WithSeed(s, len, seed) : + Hash64WithSeeds(s, len, 0, seed); +} + +uint64_t Hash64(const char *s, size_t len) { + return len <= 64 ? farmhashna::Hash64(s, len) : + Hash64WithSeeds(s, len, 81, 0); +} +} // namespace farmhashuo +namespace farmhashxo { +#undef Fetch +#define Fetch Fetch64 + +#undef Rotate +#define Rotate Rotate64 + +STATIC_INLINE uint64_t H32(const char *s, size_t len, uint64_t mul, + uint64_t seed0 = 0, uint64_t seed1 = 0) { + uint64_t a = Fetch(s) * k1; + uint64_t b = Fetch(s + 8); + uint64_t c = Fetch(s + len - 8) * mul; + uint64_t d = Fetch(s + len - 16) * k2; + uint64_t u = Rotate(a + b, 43) + Rotate(c, 30) + d + seed0; + uint64_t v = a + Rotate(b + k2, 18) + c + seed1; + a = farmhashna::ShiftMix((u ^ v) * mul); + b = farmhashna::ShiftMix((v ^ a) * mul); + return b; +} + +// Return an 8-byte hash for 33 to 64 bytes. +STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) { + uint64_t mul0 = k2 - 30; + uint64_t mul1 = k2 - 30 + 2 * len; + uint64_t h0 = H32(s, 32, mul0); + uint64_t h1 = H32(s + len - 32, 32, mul1); + return ((h1 * mul1) + h0) * mul1; +} + +// Return an 8-byte hash for 65 to 96 bytes. +STATIC_INLINE uint64_t HashLen65to96(const char *s, size_t len) { + uint64_t mul0 = k2 - 114; + uint64_t mul1 = k2 - 114 + 2 * len; + uint64_t h0 = H32(s, 32, mul0); + uint64_t h1 = H32(s + 32, 32, mul1); + uint64_t h2 = H32(s + len - 32, 32, mul1, h0, h1); + return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1; +} + +uint64_t Hash64(const char *s, size_t len) { + if (len <= 32) { + if (len <= 16) { + return farmhashna::HashLen0to16(s, len); + } else { + return farmhashna::HashLen17to32(s, len); + } + } else if (len <= 64) { + return HashLen33to64(s, len); + } else if (len <= 96) { + return HashLen65to96(s, len); + } else if (len <= 256) { + return farmhashna::Hash64(s, len); + } else { + return farmhashuo::Hash64(s, len); + } +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) { + return farmhashuo::Hash64WithSeeds(s, len, seed0, seed1); +} + +uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) { + return farmhashuo::Hash64WithSeed(s, len, seed); +} +} // namespace farmhashxo +namespace farmhashte { +#if !can_use_sse41 || !x86_64 + +uint64_t Hash64(const char *s, size_t len) { + FARMHASH_DIE_IF_MISCONFIGURED; + return s == NULL ? 0 : len; +} + +uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) { + FARMHASH_DIE_IF_MISCONFIGURED; + return seed + Hash64(s, len); +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, + uint64_t seed0, uint64_t seed1) { + FARMHASH_DIE_IF_MISCONFIGURED; + return seed0 + seed1 + Hash64(s, len); +} + +#else + +#undef Fetch +#define Fetch Fetch64 + +#undef Rotate +#define Rotate Rotate64 + +#undef Bswap +#define Bswap Bswap64 + +// Helpers for data-parallel operations (1x 128 bits or 2x 64 or 4x 32). +STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi64(x, y); } +STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); } +STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); } +STATIC_INLINE __m128i Shuf(__m128i x, __m128i y) { return _mm_shuffle_epi8(y, x); } + +// Requires n >= 256. Requires SSE4.1. Should be slightly faster if the +// compiler uses AVX instructions (e.g., use the -mavx flag with GCC). +STATIC_INLINE uint64_t Hash64Long(const char* s, size_t n, + uint64_t seed0, uint64_t seed1) { + const __m128i kShuf = + _mm_set_epi8(4, 11, 10, 5, 8, 15, 6, 9, 12, 2, 14, 13, 0, 7, 3, 1); + const __m128i kMult = + _mm_set_epi8(0xbd, 0xd6, 0x33, 0x39, 0x45, 0x54, 0xfa, 0x03, + 0x34, 0x3e, 0x33, 0xed, 0xcc, 0x9e, 0x2d, 0x51); + uint64_t seed2 = (seed0 + 113) * (seed1 + 9); + uint64_t seed3 = (Rotate(seed0, 23) + 27) * (Rotate(seed1, 30) + 111); + __m128i d0 = _mm_cvtsi64_si128(seed0); + __m128i d1 = _mm_cvtsi64_si128(seed1); + __m128i d2 = Shuf(kShuf, d0); + __m128i d3 = Shuf(kShuf, d1); + __m128i d4 = Xor(d0, d1); + __m128i d5 = Xor(d1, d2); + __m128i d6 = Xor(d2, d4); + __m128i d7 = _mm_set1_epi32(seed2 >> 32); + __m128i d8 = Mul(kMult, d2); + __m128i d9 = _mm_set1_epi32(seed3 >> 32); + __m128i d10 = _mm_set1_epi32(seed3); + __m128i d11 = Add(d2, _mm_set1_epi32(seed2)); + const char* end = s + (n & ~static_cast(255)); + do { + __m128i z; + z = Fetch128(s); + d0 = Add(d0, z); + d1 = Shuf(kShuf, d1); + d2 = Xor(d2, d0); + d4 = Xor(d4, z); + d4 = Xor(d4, d1); + std::swap(d0, d6); + z = Fetch128(s + 16); + d5 = Add(d5, z); + d6 = Shuf(kShuf, d6); + d8 = Shuf(kShuf, d8); + d7 = Xor(d7, d5); + d0 = Xor(d0, z); + d0 = Xor(d0, d6); + std::swap(d5, d11); + z = Fetch128(s + 32); + d1 = Add(d1, z); + d2 = Shuf(kShuf, d2); + d4 = Shuf(kShuf, d4); + d5 = Xor(d5, z); + d5 = Xor(d5, d2); + std::swap(d10, d4); + z = Fetch128(s + 48); + d6 = Add(d6, z); + d7 = Shuf(kShuf, d7); + d0 = Shuf(kShuf, d0); + d8 = Xor(d8, d6); + d1 = Xor(d1, z); + d1 = Add(d1, d7); + z = Fetch128(s + 64); + d2 = Add(d2, z); + d5 = Shuf(kShuf, d5); + d4 = Add(d4, d2); + d6 = Xor(d6, z); + d6 = Xor(d6, d11); + std::swap(d8, d2); + z = Fetch128(s + 80); + d7 = Xor(d7, z); + d8 = Shuf(kShuf, d8); + d1 = Shuf(kShuf, d1); + d0 = Add(d0, d7); + d2 = Add(d2, z); + d2 = Add(d2, d8); + std::swap(d1, d7); + z = Fetch128(s + 96); + d4 = Shuf(kShuf, d4); + d6 = Shuf(kShuf, d6); + d8 = Mul(kMult, d8); + d5 = Xor(d5, d11); + d7 = Xor(d7, z); + d7 = Add(d7, d4); + std::swap(d6, d0); + z = Fetch128(s + 112); + d8 = Add(d8, z); + d0 = Shuf(kShuf, d0); + d2 = Shuf(kShuf, d2); + d1 = Xor(d1, d8); + d10 = Xor(d10, z); + d10 = Xor(d10, d0); + std::swap(d11, d5); + z = Fetch128(s + 128); + d4 = Add(d4, z); + d5 = Shuf(kShuf, d5); + d7 = Shuf(kShuf, d7); + d6 = Add(d6, d4); + d8 = Xor(d8, z); + d8 = Xor(d8, d5); + std::swap(d4, d10); + z = Fetch128(s + 144); + d0 = Add(d0, z); + d1 = Shuf(kShuf, d1); + d2 = Add(d2, d0); + d4 = Xor(d4, z); + d4 = Xor(d4, d1); + z = Fetch128(s + 160); + d5 = Add(d5, z); + d6 = Shuf(kShuf, d6); + d8 = Shuf(kShuf, d8); + d7 = Xor(d7, d5); + d0 = Xor(d0, z); + d0 = Xor(d0, d6); + std::swap(d2, d8); + z = Fetch128(s + 176); + d1 = Add(d1, z); + d2 = Shuf(kShuf, d2); + d4 = Shuf(kShuf, d4); + d5 = Mul(kMult, d5); + d5 = Xor(d5, z); + d5 = Xor(d5, d2); + std::swap(d7, d1); + z = Fetch128(s + 192); + d6 = Add(d6, z); + d7 = Shuf(kShuf, d7); + d0 = Shuf(kShuf, d0); + d8 = Add(d8, d6); + d1 = Xor(d1, z); + d1 = Xor(d1, d7); + std::swap(d0, d6); + z = Fetch128(s + 208); + d2 = Add(d2, z); + d5 = Shuf(kShuf, d5); + d4 = Xor(d4, d2); + d6 = Xor(d6, z); + d6 = Xor(d6, d9); + std::swap(d5, d11); + z = Fetch128(s + 224); + d7 = Add(d7, z); + d8 = Shuf(kShuf, d8); + d1 = Shuf(kShuf, d1); + d0 = Xor(d0, d7); + d2 = Xor(d2, z); + d2 = Xor(d2, d8); + std::swap(d10, d4); + z = Fetch128(s + 240); + d3 = Add(d3, z); + d4 = Shuf(kShuf, d4); + d6 = Shuf(kShuf, d6); + d7 = Mul(kMult, d7); + d5 = Add(d5, d3); + d7 = Xor(d7, z); + d7 = Xor(d7, d4); + std::swap(d3, d9); + s += 256; + } while (s != end); + d6 = Add(Mul(kMult, d6), _mm_cvtsi64_si128(n)); + if (n % 256 != 0) { + d7 = Add(_mm_shuffle_epi32(d8, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0)), d7); + d8 = Add(Mul(kMult, d8), _mm_cvtsi64_si128(farmhashxo::Hash64(s, n % 256))); + } + __m128i t[8]; + d0 = Mul(kMult, Shuf(kShuf, Mul(kMult, d0))); + d3 = Mul(kMult, Shuf(kShuf, Mul(kMult, d3))); + d9 = Mul(kMult, Shuf(kShuf, Mul(kMult, d9))); + d1 = Mul(kMult, Shuf(kShuf, Mul(kMult, d1))); + d0 = Add(d11, d0); + d3 = Xor(d7, d3); + d9 = Add(d8, d9); + d1 = Add(d10, d1); + d4 = Add(d3, d4); + d5 = Add(d9, d5); + d6 = Xor(d1, d6); + d2 = Add(d0, d2); + t[0] = d0; + t[1] = d3; + t[2] = d9; + t[3] = d1; + t[4] = d4; + t[5] = d5; + t[6] = d6; + t[7] = d2; + return farmhashxo::Hash64(reinterpret_cast(t), sizeof(t)); +} + +uint64_t Hash64(const char *s, size_t len) { + // Empirically, farmhashxo seems faster until length 512. + return len >= 512 ? Hash64Long(s, len, k2, k1) : farmhashxo::Hash64(s, len); +} + +uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) { + return len >= 512 ? Hash64Long(s, len, k1, seed) : + farmhashxo::Hash64WithSeed(s, len, seed); +} + +uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) { + return len >= 512 ? Hash64Long(s, len, seed0, seed1) : + farmhashxo::Hash64WithSeeds(s, len, seed0, seed1); +} + +#endif +} // namespace farmhashte +namespace farmhashnt { +#if !can_use_sse41 || !x86_64 + +uint32_t Hash32(const char *s, size_t len) { + FARMHASH_DIE_IF_MISCONFIGURED; + return s == NULL ? 0 : len; +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + FARMHASH_DIE_IF_MISCONFIGURED; + return seed + Hash32(s, len); +} + +#else + +uint32_t Hash32(const char *s, size_t len) { + return static_cast(farmhashte::Hash64(s, len)); +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + return static_cast(farmhashte::Hash64WithSeed(s, len, seed)); +} + +#endif +} // namespace farmhashnt +namespace farmhashmk { +#undef Fetch +#define Fetch Fetch32 + +#undef Rotate +#define Rotate Rotate32 + +#undef Bswap +#define Bswap Bswap32 + +STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len, uint32_t seed = 0) { + uint32_t a = Fetch(s - 4 + (len >> 1)); + uint32_t b = Fetch(s + 4); + uint32_t c = Fetch(s + len - 8); + uint32_t d = Fetch(s + (len >> 1)); + uint32_t e = Fetch(s); + uint32_t f = Fetch(s + len - 4); + uint32_t h = d * c1 + len + seed; + a = Rotate(a, 12) + f; + h = Mur(c, h) + a; + a = Rotate(a, 3) + c; + h = Mur(e, h) + a; + a = Rotate(a + f, 12) + d; + h = Mur(b ^ seed, h) + a; + return fmix(h); +} + +STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len, uint32_t seed = 0) { + uint32_t b = seed; + uint32_t c = 9; + for (size_t i = 0; i < len; i++) { + signed char v = s[i]; + b = b * c1 + v; + c ^= b; + } + return fmix(Mur(b, Mur(len, c))); +} + +STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len, uint32_t seed = 0) { + uint32_t a = len, b = len * 5, c = 9, d = b + seed; + a += Fetch(s); + b += Fetch(s + len - 4); + c += Fetch(s + ((len >> 1) & 4)); + return fmix(seed ^ Mur(c, Mur(b, Mur(a, d)))); +} + +uint32_t Hash32(const char *s, size_t len) { + if (len <= 24) { + return len <= 12 ? + (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) : + Hash32Len13to24(s, len); + } + + // len > 24 + uint32_t h = len, g = c1 * len, f = g; + uint32_t a0 = Rotate(Fetch(s + len - 4) * c1, 17) * c2; + uint32_t a1 = Rotate(Fetch(s + len - 8) * c1, 17) * c2; + uint32_t a2 = Rotate(Fetch(s + len - 16) * c1, 17) * c2; + uint32_t a3 = Rotate(Fetch(s + len - 12) * c1, 17) * c2; + uint32_t a4 = Rotate(Fetch(s + len - 20) * c1, 17) * c2; + h ^= a0; + h = Rotate(h, 19); + h = h * 5 + 0xe6546b64; + h ^= a2; + h = Rotate(h, 19); + h = h * 5 + 0xe6546b64; + g ^= a1; + g = Rotate(g, 19); + g = g * 5 + 0xe6546b64; + g ^= a3; + g = Rotate(g, 19); + g = g * 5 + 0xe6546b64; + f += a4; + f = Rotate(f, 19) + 113; + size_t iters = (len - 1) / 20; + do { + uint32_t a = Fetch(s); + uint32_t b = Fetch(s + 4); + uint32_t c = Fetch(s + 8); + uint32_t d = Fetch(s + 12); + uint32_t e = Fetch(s + 16); + h += a; + g += b; + f += c; + h = Mur(d, h) + e; + g = Mur(c, g) + a; + f = Mur(b + e * c1, f) + d; + f += g; + g += f; + s += 20; + } while (--iters != 0); + g = Rotate(g, 11) * c1; + g = Rotate(g, 17) * c1; + f = Rotate(f, 11) * c1; + f = Rotate(f, 17) * c1; + h = Rotate(h + g, 19); + h = h * 5 + 0xe6546b64; + h = Rotate(h, 17) * c1; + h = Rotate(h + f, 19); + h = h * 5 + 0xe6546b64; + h = Rotate(h, 17) * c1; + return h; +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + if (len <= 24) { + if (len >= 13) return Hash32Len13to24(s, len, seed * c1); + else if (len >= 5) return Hash32Len5to12(s, len, seed); + else return Hash32Len0to4(s, len, seed); + } + uint32_t h = Hash32Len13to24(s, 24, seed ^ len); + return Mur(Hash32(s + 24, len - 24) + seed, h); +} +} // namespace farmhashmk +namespace farmhashsu { +#if !can_use_sse42 || !can_use_aesni + +uint32_t Hash32(const char *s, size_t len) { + FARMHASH_DIE_IF_MISCONFIGURED; + return s == NULL ? 0 : len; +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + FARMHASH_DIE_IF_MISCONFIGURED; + return seed + Hash32(s, len); +} + +#else + +#undef Fetch +#define Fetch Fetch32 + +#undef Rotate +#define Rotate Rotate32 + +#undef Bswap +#define Bswap Bswap32 + +// Helpers for data-parallel operations (4x 32-bits). +STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi32(x, y); } +STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); } +STATIC_INLINE __m128i Or(__m128i x, __m128i y) { return _mm_or_si128(x, y); } +STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); } +STATIC_INLINE __m128i Mul5(__m128i x) { return Add(x, _mm_slli_epi32(x, 2)); } +STATIC_INLINE __m128i RotateLeft(__m128i x, int c) { + return Or(_mm_slli_epi32(x, c), + _mm_srli_epi32(x, 32 - c)); +} +STATIC_INLINE __m128i Rol17(__m128i x) { return RotateLeft(x, 17); } +STATIC_INLINE __m128i Rol19(__m128i x) { return RotateLeft(x, 19); } +STATIC_INLINE __m128i Shuffle0321(__m128i x) { + return _mm_shuffle_epi32(x, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0)); +} + +uint32_t Hash32(const char *s, size_t len) { + const uint32_t seed = 81; + if (len <= 24) { + return len <= 12 ? + (len <= 4 ? + farmhashmk::Hash32Len0to4(s, len) : + farmhashmk::Hash32Len5to12(s, len)) : + farmhashmk::Hash32Len13to24(s, len); + } + + if (len < 40) { + uint32_t a = len, b = seed * c2, c = a + b; + a += Fetch(s + len - 4); + b += Fetch(s + len - 20); + c += Fetch(s + len - 16); + uint32_t d = a; + a = NAMESPACE_FOR_HASH_FUNCTIONS::Rotate32(a, 21); + a = Mur(a, Mur(b, _mm_crc32_u32(c, d))); + a += Fetch(s + len - 12); + b += Fetch(s + len - 8); + d += a; + a += d; + b = Mur(b, d) * c2; + a = _mm_crc32_u32(a, b + c); + return farmhashmk::Hash32Len13to24(s, (len + 1) / 2, a) + b; + } + +#undef Mulc1 +#define Mulc1(x) Mul((x), cc1) + +#undef Mulc2 +#define Mulc2(x) Mul((x), cc2) + +#undef Murk +#define Murk(a, h) \ + Add(k, \ + Mul5( \ + Rol19( \ + Xor( \ + Mulc2( \ + Rol17( \ + Mulc1(a))), \ + (h))))) + + const __m128i cc1 = _mm_set1_epi32(c1); + const __m128i cc2 = _mm_set1_epi32(c2); + __m128i h = _mm_set1_epi32(seed); + __m128i g = _mm_set1_epi32(c1 * seed); + __m128i f = g; + __m128i k = _mm_set1_epi32(0xe6546b64); + __m128i q; + if (len < 80) { + __m128i a = Fetch128(s); + __m128i b = Fetch128(s + 16); + __m128i c = Fetch128(s + (len - 15) / 2); + __m128i d = Fetch128(s + len - 32); + __m128i e = Fetch128(s + len - 16); + h = Add(h, a); + g = Add(g, b); + q = g; + g = Shuffle0321(g); + f = Add(f, c); + __m128i be = Add(b, Mulc1(e)); + h = Add(h, f); + f = Add(f, h); + h = Add(Murk(d, h), e); + k = Xor(k, _mm_shuffle_epi8(g, f)); + g = Add(Xor(c, g), a); + f = Add(Xor(be, f), d); + k = Add(k, be); + k = Add(k, _mm_shuffle_epi8(f, h)); + f = Add(f, g); + g = Add(g, f); + g = Add(_mm_set1_epi32(len), Mulc1(g)); + } else { + // len >= 80 + // The following is loosely modelled after farmhashmk::Hash32. + size_t iters = (len - 1) / 80; + len -= iters * 80; + +#undef Chunk +#define Chunk() do { \ + __m128i a = Fetch128(s); \ + __m128i b = Fetch128(s + 16); \ + __m128i c = Fetch128(s + 32); \ + __m128i d = Fetch128(s + 48); \ + __m128i e = Fetch128(s + 64); \ + h = Add(h, a); \ + g = Add(g, b); \ + g = Shuffle0321(g); \ + f = Add(f, c); \ + __m128i be = Add(b, Mulc1(e)); \ + h = Add(h, f); \ + f = Add(f, h); \ + h = Add(h, d); \ + q = Add(q, e); \ + h = Rol17(h); \ + h = Mulc1(h); \ + k = Xor(k, _mm_shuffle_epi8(g, f)); \ + g = Add(Xor(c, g), a); \ + f = Add(Xor(be, f), d); \ + std::swap(f, q); \ + q = _mm_aesimc_si128(q); \ + k = Add(k, be); \ + k = Add(k, _mm_shuffle_epi8(f, h)); \ + f = Add(f, g); \ + g = Add(g, f); \ + f = Mulc1(f); \ +} while (0) + + q = g; + while (iters-- != 0) { + Chunk(); + s += 80; + } + + if (len != 0) { + h = Add(h, _mm_set1_epi32(len)); + s = s + len - 80; + Chunk(); + } + } + + g = Shuffle0321(g); + k = Xor(k, g); + k = Xor(k, q); + h = Xor(h, q); + f = Mulc1(f); + k = Mulc2(k); + g = Mulc1(g); + h = Mulc2(h); + k = Add(k, _mm_shuffle_epi8(g, f)); + h = Add(h, f); + f = Add(f, h); + g = Add(g, k); + k = Add(k, g); + k = Xor(k, _mm_shuffle_epi8(f, h)); + __m128i buf[4]; + buf[0] = f; + buf[1] = g; + buf[2] = k; + buf[3] = h; + s = reinterpret_cast(buf); + uint32_t x = Fetch(s); + uint32_t y = Fetch(s+4); + uint32_t z = Fetch(s+8); + x = _mm_crc32_u32(x, Fetch(s+12)); + y = _mm_crc32_u32(y, Fetch(s+16)); + z = _mm_crc32_u32(z * c1, Fetch(s+20)); + x = _mm_crc32_u32(x, Fetch(s+24)); + y = _mm_crc32_u32(y * c1, Fetch(s+28)); + uint32_t o = y; + z = _mm_crc32_u32(z, Fetch(s+32)); + x = _mm_crc32_u32(x * c1, Fetch(s+36)); + y = _mm_crc32_u32(y, Fetch(s+40)); + z = _mm_crc32_u32(z * c1, Fetch(s+44)); + x = _mm_crc32_u32(x, Fetch(s+48)); + y = _mm_crc32_u32(y * c1, Fetch(s+52)); + z = _mm_crc32_u32(z, Fetch(s+56)); + x = _mm_crc32_u32(x, Fetch(s+60)); + return (o - x + y - z) * c1; +} + +#undef Chunk +#undef Murk +#undef Mulc2 +#undef Mulc1 + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + if (len <= 24) { + if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * c1); + else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed); + else return farmhashmk::Hash32Len0to4(s, len, seed); + } + uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len); + return _mm_crc32_u32(Hash32(s + 24, len - 24) + seed, h); +} + +#endif +} // namespace farmhashsu +namespace farmhashsa { +#if !can_use_sse42 + +uint32_t Hash32(const char *s, size_t len) { + FARMHASH_DIE_IF_MISCONFIGURED; + return s == NULL ? 0 : len; +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + FARMHASH_DIE_IF_MISCONFIGURED; + return seed + Hash32(s, len); +} + +#else + +#undef Fetch +#define Fetch Fetch32 + +#undef Rotate +#define Rotate Rotate32 + +#undef Bswap +#define Bswap Bswap32 + +// Helpers for data-parallel operations (4x 32-bits). +STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi32(x, y); } +STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); } +STATIC_INLINE __m128i Or(__m128i x, __m128i y) { return _mm_or_si128(x, y); } +STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); } +STATIC_INLINE __m128i Mul5(__m128i x) { return Add(x, _mm_slli_epi32(x, 2)); } +STATIC_INLINE __m128i Rotate(__m128i x, int c) { + return Or(_mm_slli_epi32(x, c), + _mm_srli_epi32(x, 32 - c)); +} +STATIC_INLINE __m128i Rot17(__m128i x) { return Rotate(x, 17); } +STATIC_INLINE __m128i Rot19(__m128i x) { return Rotate(x, 19); } +STATIC_INLINE __m128i Shuffle0321(__m128i x) { + return _mm_shuffle_epi32(x, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0)); +} + +uint32_t Hash32(const char *s, size_t len) { + const uint32_t seed = 81; + if (len <= 24) { + return len <= 12 ? + (len <= 4 ? + farmhashmk::Hash32Len0to4(s, len) : + farmhashmk::Hash32Len5to12(s, len)) : + farmhashmk::Hash32Len13to24(s, len); + } + + if (len < 40) { + uint32_t a = len, b = seed * c2, c = a + b; + a += Fetch(s + len - 4); + b += Fetch(s + len - 20); + c += Fetch(s + len - 16); + uint32_t d = a; + a = NAMESPACE_FOR_HASH_FUNCTIONS::Rotate32(a, 21); + a = Mur(a, Mur(b, Mur(c, d))); + a += Fetch(s + len - 12); + b += Fetch(s + len - 8); + d += a; + a += d; + b = Mur(b, d) * c2; + a = _mm_crc32_u32(a, b + c); + return farmhashmk::Hash32Len13to24(s, (len + 1) / 2, a) + b; + } + +#undef Mulc1 +#define Mulc1(x) Mul((x), cc1) + +#undef Mulc2 +#define Mulc2(x) Mul((x), cc2) + +#undef Murk +#define Murk(a, h) \ + Add(k, \ + Mul5( \ + Rot19( \ + Xor( \ + Mulc2( \ + Rot17( \ + Mulc1(a))), \ + (h))))) + + const __m128i cc1 = _mm_set1_epi32(c1); + const __m128i cc2 = _mm_set1_epi32(c2); + __m128i h = _mm_set1_epi32(seed); + __m128i g = _mm_set1_epi32(c1 * seed); + __m128i f = g; + __m128i k = _mm_set1_epi32(0xe6546b64); + if (len < 80) { + __m128i a = Fetch128(s); + __m128i b = Fetch128(s + 16); + __m128i c = Fetch128(s + (len - 15) / 2); + __m128i d = Fetch128(s + len - 32); + __m128i e = Fetch128(s + len - 16); + h = Add(h, a); + g = Add(g, b); + g = Shuffle0321(g); + f = Add(f, c); + __m128i be = Add(b, Mulc1(e)); + h = Add(h, f); + f = Add(f, h); + h = Add(Murk(d, h), e); + k = Xor(k, _mm_shuffle_epi8(g, f)); + g = Add(Xor(c, g), a); + f = Add(Xor(be, f), d); + k = Add(k, be); + k = Add(k, _mm_shuffle_epi8(f, h)); + f = Add(f, g); + g = Add(g, f); + g = Add(_mm_set1_epi32(len), Mulc1(g)); + } else { + // len >= 80 + // The following is loosely modelled after farmhashmk::Hash32. + size_t iters = (len - 1) / 80; + len -= iters * 80; + +#undef Chunk +#define Chunk() do { \ + __m128i a = Fetch128(s); \ + __m128i b = Fetch128(s + 16); \ + __m128i c = Fetch128(s + 32); \ + __m128i d = Fetch128(s + 48); \ + __m128i e = Fetch128(s + 64); \ + h = Add(h, a); \ + g = Add(g, b); \ + g = Shuffle0321(g); \ + f = Add(f, c); \ + __m128i be = Add(b, Mulc1(e)); \ + h = Add(h, f); \ + f = Add(f, h); \ + h = Add(Murk(d, h), e); \ + k = Xor(k, _mm_shuffle_epi8(g, f)); \ + g = Add(Xor(c, g), a); \ + f = Add(Xor(be, f), d); \ + k = Add(k, be); \ + k = Add(k, _mm_shuffle_epi8(f, h)); \ + f = Add(f, g); \ + g = Add(g, f); \ + f = Mulc1(f); \ +} while (0) + + while (iters-- != 0) { + Chunk(); + s += 80; + } + + if (len != 0) { + h = Add(h, _mm_set1_epi32(len)); + s = s + len - 80; + Chunk(); + } + } + + g = Shuffle0321(g); + k = Xor(k, g); + f = Mulc1(f); + k = Mulc2(k); + g = Mulc1(g); + h = Mulc2(h); + k = Add(k, _mm_shuffle_epi8(g, f)); + h = Add(h, f); + f = Add(f, h); + g = Add(g, k); + k = Add(k, g); + k = Xor(k, _mm_shuffle_epi8(f, h)); + __m128i buf[4]; + buf[0] = f; + buf[1] = g; + buf[2] = k; + buf[3] = h; + s = reinterpret_cast(buf); + uint32_t x = Fetch(s); + uint32_t y = Fetch(s+4); + uint32_t z = Fetch(s+8); + x = _mm_crc32_u32(x, Fetch(s+12)); + y = _mm_crc32_u32(y, Fetch(s+16)); + z = _mm_crc32_u32(z * c1, Fetch(s+20)); + x = _mm_crc32_u32(x, Fetch(s+24)); + y = _mm_crc32_u32(y * c1, Fetch(s+28)); + uint32_t o = y; + z = _mm_crc32_u32(z, Fetch(s+32)); + x = _mm_crc32_u32(x * c1, Fetch(s+36)); + y = _mm_crc32_u32(y, Fetch(s+40)); + z = _mm_crc32_u32(z * c1, Fetch(s+44)); + x = _mm_crc32_u32(x, Fetch(s+48)); + y = _mm_crc32_u32(y * c1, Fetch(s+52)); + z = _mm_crc32_u32(z, Fetch(s+56)); + x = _mm_crc32_u32(x, Fetch(s+60)); + return (o - x + y - z) * c1; +} + +#undef Chunk +#undef Murk +#undef Mulc2 +#undef Mulc1 + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + if (len <= 24) { + if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * c1); + else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed); + else return farmhashmk::Hash32Len0to4(s, len, seed); + } + uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len); + return _mm_crc32_u32(Hash32(s + 24, len - 24) + seed, h); +} + +#endif +} // namespace farmhashsa +namespace farmhashcc { +// This file provides a 32-bit hash equivalent to CityHash32 (v1.1.1) +// and a 128-bit hash equivalent to CityHash128 (v1.1.1). It also provides +// a seeded 32-bit hash function similar to CityHash32. + +#undef Fetch +#define Fetch Fetch32 + +#undef Rotate +#define Rotate Rotate32 + +#undef Bswap +#define Bswap Bswap32 + +STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len) { + uint32_t a = Fetch(s - 4 + (len >> 1)); + uint32_t b = Fetch(s + 4); + uint32_t c = Fetch(s + len - 8); + uint32_t d = Fetch(s + (len >> 1)); + uint32_t e = Fetch(s); + uint32_t f = Fetch(s + len - 4); + uint32_t h = len; + + return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h))))))); +} + +STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len) { + uint32_t b = 0; + uint32_t c = 9; + for (size_t i = 0; i < len; i++) { + signed char v = s[i]; + b = b * c1 + v; + c ^= b; + } + return fmix(Mur(b, Mur(len, c))); +} + +STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len) { + uint32_t a = len, b = len * 5, c = 9, d = b; + a += Fetch(s); + b += Fetch(s + len - 4); + c += Fetch(s + ((len >> 1) & 4)); + return fmix(Mur(c, Mur(b, Mur(a, d)))); +} + +uint32_t Hash32(const char *s, size_t len) { + if (len <= 24) { + return len <= 12 ? + (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) : + Hash32Len13to24(s, len); + } + + // len > 24 + uint32_t h = len, g = c1 * len, f = g; + uint32_t a0 = Rotate(Fetch(s + len - 4) * c1, 17) * c2; + uint32_t a1 = Rotate(Fetch(s + len - 8) * c1, 17) * c2; + uint32_t a2 = Rotate(Fetch(s + len - 16) * c1, 17) * c2; + uint32_t a3 = Rotate(Fetch(s + len - 12) * c1, 17) * c2; + uint32_t a4 = Rotate(Fetch(s + len - 20) * c1, 17) * c2; + h ^= a0; + h = Rotate(h, 19); + h = h * 5 + 0xe6546b64; + h ^= a2; + h = Rotate(h, 19); + h = h * 5 + 0xe6546b64; + g ^= a1; + g = Rotate(g, 19); + g = g * 5 + 0xe6546b64; + g ^= a3; + g = Rotate(g, 19); + g = g * 5 + 0xe6546b64; + f += a4; + f = Rotate(f, 19); + f = f * 5 + 0xe6546b64; + size_t iters = (len - 1) / 20; + do { + uint32_t a0 = Rotate(Fetch(s) * c1, 17) * c2; + uint32_t a1 = Fetch(s + 4); + uint32_t a2 = Rotate(Fetch(s + 8) * c1, 17) * c2; + uint32_t a3 = Rotate(Fetch(s + 12) * c1, 17) * c2; + uint32_t a4 = Fetch(s + 16); + h ^= a0; + h = Rotate(h, 18); + h = h * 5 + 0xe6546b64; + f += a1; + f = Rotate(f, 19); + f = f * c1; + g += a2; + g = Rotate(g, 18); + g = g * 5 + 0xe6546b64; + h ^= a3 + a1; + h = Rotate(h, 19); + h = h * 5 + 0xe6546b64; + g ^= a4; + g = Bswap(g) * 5; + h += a4 * 5; + h = Bswap(h); + f += a0; + PERMUTE3(f, h, g); + s += 20; + } while (--iters != 0); + g = Rotate(g, 11) * c1; + g = Rotate(g, 17) * c1; + f = Rotate(f, 11) * c1; + f = Rotate(f, 17) * c1; + h = Rotate(h + g, 19); + h = h * 5 + 0xe6546b64; + h = Rotate(h, 17) * c1; + h = Rotate(h + f, 19); + h = h * 5 + 0xe6546b64; + h = Rotate(h, 17) * c1; + return h; +} + +uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) { + if (len <= 24) { + if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * c1); + else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed); + else return farmhashmk::Hash32Len0to4(s, len, seed); + } + uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len); + return Mur(Hash32(s + 24, len - 24) + seed, h); +} + +#undef Fetch +#define Fetch Fetch64 + +#undef Rotate +#define Rotate Rotate64 + +#undef Bswap +#define Bswap Bswap64 + +STATIC_INLINE uint64_t ShiftMix(uint64_t val) { + return val ^ (val >> 47); +} + +STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) { + return Hash128to64(Uint128(u, v)); +} + +STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) { + // Murmur-inspired hashing. + uint64_t a = (u ^ v) * mul; + a ^= (a >> 47); + uint64_t b = (v ^ a) * mul; + b ^= (b >> 47); + b *= mul; + return b; +} + +STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) { + if (len >= 8) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch(s) + k2; + uint64_t b = Fetch(s + len - 8); + uint64_t c = Rotate(b, 37) * mul + a; + uint64_t d = (Rotate(a, 25) + b) * mul; + return HashLen16(c, d, mul); + } + if (len >= 4) { + uint64_t mul = k2 + len * 2; + uint64_t a = Fetch32(s); + return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul); + } + if (len > 0) { + uint8_t a = s[0]; + uint8_t b = s[len >> 1]; + uint8_t c = s[len - 1]; + uint32_t y = static_cast(a) + (static_cast(b) << 8); + uint32_t z = len + (static_cast(c) << 2); + return ShiftMix(y * k2 ^ z * k0) * k2; + } + return k2; +} + +// Return a 16-byte hash for 48 bytes. Quick and dirty. +// Callers do best to use "random-looking" values for a and b. +STATIC_INLINE pair WeakHashLen32WithSeeds( + uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) { + a += w; + b = Rotate(b + a + z, 21); + uint64_t c = a; + a += x; + a += y; + b += Rotate(a, 44); + return make_pair(a + z, b + c); +} + +// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. +STATIC_INLINE pair WeakHashLen32WithSeeds( + const char* s, uint64_t a, uint64_t b) { + return WeakHashLen32WithSeeds(Fetch(s), + Fetch(s + 8), + Fetch(s + 16), + Fetch(s + 24), + a, + b); +} + + + +// A subroutine for CityHash128(). Returns a decent 128-bit hash for strings +// of any length representable in signed long. Based on City and Murmur. +STATIC_INLINE uint128_t CityMurmur(const char *s, size_t len, uint128_t seed) { + uint64_t a = Uint128Low64(seed); + uint64_t b = Uint128High64(seed); + uint64_t c = 0; + uint64_t d = 0; + signed long l = len - 16; + if (l <= 0) { // len <= 16 + a = ShiftMix(a * k1) * k1; + c = b * k1 + HashLen0to16(s, len); + d = ShiftMix(a + (len >= 8 ? Fetch(s) : c)); + } else { // len > 16 + c = HashLen16(Fetch(s + len - 8) + k1, a); + d = HashLen16(b + len, c + Fetch(s + len - 16)); + a += d; + do { + a ^= ShiftMix(Fetch(s) * k1) * k1; + a *= k1; + b ^= a; + c ^= ShiftMix(Fetch(s + 8) * k1) * k1; + c *= k1; + d ^= c; + s += 16; + l -= 16; + } while (l > 0); + } + a = HashLen16(a, c); + b = HashLen16(d, b); + return uint128_t(a ^ b, HashLen16(b, a)); +} + +uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed) { + if (len < 128) { + return CityMurmur(s, len, seed); + } + + // We expect len >= 128 to be the common case. Keep 56 bytes of state: + // v, w, x, y, and z. + pair v, w; + uint64_t x = Uint128Low64(seed); + uint64_t y = Uint128High64(seed); + uint64_t z = len * k1; + v.first = Rotate(y ^ k1, 49) * k1 + Fetch(s); + v.second = Rotate(v.first, 42) * k1 + Fetch(s + 8); + w.first = Rotate(y + z, 35) * k1 + x; + w.second = Rotate(x + Fetch(s + 88), 53) * k1; + + // This is the same inner loop as CityHash64(), manually unrolled. + do { + x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)); + std::swap(z, x); + s += 64; + x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1; + y = Rotate(y + v.second + Fetch(s + 48), 42) * k1; + x ^= w.second; + y += v.first + Fetch(s + 40); + z = Rotate(z + w.first, 33) * k1; + v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first); + w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)); + std::swap(z, x); + s += 64; + len -= 128; + } while (LIKELY(len >= 128)); + x += Rotate(v.first + z, 49) * k0; + y = y * k0 + Rotate(w.second, 37); + z = z * k0 + Rotate(w.first, 27); + w.first *= 9; + v.first *= k0; + // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. + for (size_t tail_done = 0; tail_done < len; ) { + tail_done += 32; + y = Rotate(x + y, 42) * k0 + v.second; + w.first += Fetch(s + len - tail_done + 16); + x = x * k0 + w.first; + z += w.second + Fetch(s + len - tail_done); + w.second += v.first; + v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second); + v.first *= k0; + } + // At this point our 56 bytes of state should contain more than + // enough information for a strong 128-bit hash. We use two + // different 56-byte-to-8-byte hashes to get a 16-byte final result. + x = HashLen16(x, v.first); + y = HashLen16(y + z, w.first); + return uint128_t(HashLen16(x + v.second, w.second) + y, + HashLen16(x + w.second, y + v.second)); +} + +STATIC_INLINE uint128_t CityHash128(const char *s, size_t len) { + return len >= 16 ? + CityHash128WithSeed(s + 16, len - 16, + uint128_t(Fetch(s), Fetch(s + 8) + k0)) : + CityHash128WithSeed(s, len, uint128_t(k0, k1)); +} + +uint128_t Fingerprint128(const char* s, size_t len) { + return CityHash128(s, len); +} +} // namespace farmhashcc +namespace NAMESPACE_FOR_HASH_FUNCTIONS { + +// BASIC STRING HASHING + +// Hash function for a byte array. See also Hash(), below. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint32_t Hash32(const char* s, size_t len) { + return DebugTweak( + (can_use_sse41 & x86_64) ? farmhashnt::Hash32(s, len) : + (can_use_sse42 & can_use_aesni) ? farmhashsu::Hash32(s, len) : + can_use_sse42 ? farmhashsa::Hash32(s, len) : + farmhashmk::Hash32(s, len)); +} + +// Hash function for a byte array. For convenience, a 32-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed) { + return DebugTweak( + (can_use_sse41 & x86_64) ? farmhashnt::Hash32WithSeed(s, len, seed) : + (can_use_sse42 & can_use_aesni) ? farmhashsu::Hash32WithSeed(s, len, seed) : + can_use_sse42 ? farmhashsa::Hash32WithSeed(s, len, seed) : + farmhashmk::Hash32WithSeed(s, len, seed)); +} + +// Hash function for a byte array. For convenience, a 64-bit seed is also +// hashed into the result. See also Hash(), below. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64(const char* s, size_t len) { + return DebugTweak( + (can_use_sse42 & x86_64) ? + farmhashte::Hash64(s, len) : + farmhashxo::Hash64(s, len)); +} + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +size_t Hash(const char* s, size_t len) { + return sizeof(size_t) == 8 ? Hash64(s, len) : Hash32(s, len); +} + +// Hash function for a byte array. For convenience, a 64-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed) { + return DebugTweak(farmhashna::Hash64WithSeed(s, len, seed)); +} + +// Hash function for a byte array. For convenience, two seeds are also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1) { + return DebugTweak(farmhashna::Hash64WithSeeds(s, len, seed0, seed1)); +} + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint128_t Hash128(const char* s, size_t len) { + return DebugTweak(farmhashcc::Fingerprint128(s, len)); +} + +// Hash function for a byte array. For convenience, a 128-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint128_t Hash128WithSeed(const char* s, size_t len, uint128_t seed) { + return DebugTweak(farmhashcc::CityHash128WithSeed(s, len, seed)); +} + +// BASIC NON-STRING HASHING + +// FINGERPRINTING (i.e., good, portable, forever-fixed hash functions) + +// Fingerprint function for a byte array. Most useful in 32-bit binaries. +uint32_t Fingerprint32(const char* s, size_t len) { + return farmhashmk::Hash32(s, len); +} + +// Fingerprint function for a byte array. +uint64_t Fingerprint64(const char* s, size_t len) { + return farmhashna::Hash64(s, len); +} + +// Fingerprint function for a byte array. +uint128_t Fingerprint128(const char* s, size_t len) { + return farmhashcc::Fingerprint128(s, len); +} + +// Older and still available but perhaps not as fast as the above: +// farmhashns::Hash32{,WithSeed}() + +} // namespace NAMESPACE_FOR_HASH_FUNCTIONS + +#if FARMHASHSELFTEST + +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashccTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +4223616069u, +3696677242u, +1039179260u, 1690343979u, 1018511555u, 2464489001u, +20368522u, 2663783964u, 175201532u, 1619210592u, +4081014168u, +2576519988u, +3285042206u, 502478099u, 739479538u, 1500332790u, +13754768u, 3789353455u, 3473868058u, 1909255088u, +2212771159u, +1112731063u, +826915357u, 2893489933u, 118369799u, 1848668220u, +1308219822u, 249416982u, 64306364u, 4221800195u, +1020067935u, +3955445564u, +563346294u, 550236731u, 2339016688u, 1826259714u, +3872358639u, 2295981050u, 1870005390u, 4015628802u, +1451961420u, +653440099u, +1292493871u, 164377749u, 1717712483u, 463414587u, +3924343675u, 1050492084u, 3566618804u, 2046983362u, +31917516u, +2957164615u, +230718965u, 999595115u, 3534822176u, 2175709186u, +965707431u, 441796222u, 2481718051u, 1827777486u, +2590087362u, +3879448744u, +3515079898u, 1601433082u, 982764532u, 254808716u, +1293372530u, 4205605817u, 947001462u, 1138890052u, +176305566u, +2447367541u, +2973802542u, 4123621138u, 3083865840u, 1706367795u, +792114347u, 2880110657u, 440613768u, 195054868u, +1359016305u, +3363804638u, +649488537u, 1624045597u, 1441938215u, 3147758996u, +3199173578u, 2597283203u, 2191333609u, 3763129144u, +1117290165u, +1062549743u, +2565615889u, 1046361554u, 1581968261u, 1058773671u, +1123053168u, 3807622275u, 1486749916u, 3900816089u, +2437877004u, +1894455839u, +1912520953u, 1914997013u, 561048608u, 1643267444u, +3671572006u, 194811086u, 1468911468u, 2179206286u, +673206794u, +3486923651u, +3741426466u, 3292160512u, 697001377u, 1900763774u, +3726097344u, 629282039u, 3578723715u, 2868028489u, +3269862919u, +2303349487u, +3643953525u, 2307255916u, 849996280u, 732080434u, +909961480u, 3542445214u, 2628347095u, 4236856917u, +1380660650u, +2631821908u, +2007289004u, 3509705198u, 3788541675u, 789457322u, +3090670546u, 638977894u, 3503881773u, 947102987u, +1525325287u, +1816697045u, +2706647405u, 288763142u, 3505438495u, 481308609u, +2882636782u, 3745162621u, 3503467033u, 428247823u, +176408838u, +333551502u, +1001068721u, 1681483651u, 75380831u, 4191469679u, +3627361839u, 2736617386u, 3120737438u, 1297502456u, +864896482u, +85674920u, +2886047255u, 4119881331u, 2496990525u, 3442502055u, +1806582817u, 3186345024u, 4099591287u, 2560171465u, +3489229104u, +3065015872u, +2755089808u, 3098442882u, 378524719u, 2664097023u, +1771960725u, 2901182183u, 55258521u, 1266621443u, +581644891u, +37790450u, +1800731704u, 3601350920u, 53428754u, 2759476837u, +3391093099u, 1496510311u, 2511119507u, 2636877410u, +631613207u, +1573846064u, +260484875u, 1088212603u, 2369525206u, 322522428u, +3191396600u, 2076543340u, 1552496658u, 2739811558u, +3867875546u, +2051584261u, +2126250818u, 901517871u, 3651631165u, 1323139145u, +1521111765u, 477802997u, 3508559783u, 383954241u, +3804516756u, +4250206331u, +2655954340u, 2484996477u, 1417544845u, 1520282298u, +2745204366u, 2869345147u, 1872738335u, 2592877343u, +1619744564u, +1804962124u, +3458679890u, 423948620u, 273645618u, 4187865426u, +376057175u, 2943431463u, 3581950599u, 1035398331u, +1088213445u, +861988903u, +1323370244u, 777069428u, 506235917u, 369720851u, +2789995854u, 230915180u, 1505086948u, 940361236u, +3727873235u, +1159167499u, +1860302871u, 3456858862u, 3923555152u, 2131072714u, +2910461068u, 3671950363u, 2010742682u, 4088068851u, +3616470388u, +2087714788u, +221675509u, 1230154072u, 3450704646u, 1463226695u, +1998357699u, 266026801u, 619568740u, 3560427266u, +4148162586u, +3150417316u, +1356375822u, 2056097622u, 627905802u, 3881675638u, +2309738053u, 971916703u, 3447805361u, 1673575328u, +673084328u, +3317849401u, +2836362782u, 2377208890u, 3275350588u, 158350552u, +2553241779u, 2497264995u, 3262882649u, 3897937187u, +1598963653u, +3068514414u, +601541505u, 374517071u, 3380795976u, 235752573u, +284670003u, 2990192160u, 904937105u, 2306579150u, +2117362589u, +1635274830u, +3355572906u, 170799903u, 1226685528u, 664567688u, +413219134u, 878324258u, 4026159448u, 3620649295u, +1823625377u, +3175888439u, +1759344347u, 2640637095u, 3549558u, 2192984935u, +978623493u, 804017880u, 3877562323u, 3843116489u, +1641748342u, +1853539444u, +3001178468u, 3443560727u, 2685426077u, 1653064722u, +349231508u, 2726789654u, 3136215581u, 768402830u, +269384321u, +531936536u, +2592883487u, 1343156334u, 3628619802u, 1477143570u, +4269458419u, 3285611028u, 959104925u, 2712290710u, +3480237248u, +835796333u, +2020636251u, 1191914589u, 126521603u, 4288023938u, +3731699932u, 2136758855u, 985780142u, 193807575u, +1850544433u, +653947619u, +3929316796u, 381871169u, 950486363u, 1787262279u, +360480382u, 1800636585u, 1039258631u, 3682073259u, +1262819303u, +1786000319u, +1570627191u, 893065837u, 301304916u, 1478469809u, +623018819u, 2742232545u, 2058913014u, 1706060059u, +2421125401u, +1315829592u, +3208766775u, 1805586156u, 575853086u, 3085025513u, +4010908260u, 2344058256u, 3814407434u, 1458485673u, +2474514786u, +3581895658u, +2710719679u, 190812706u, 2135454262u, 2620080728u, +3400757986u, 1669914857u, 1559978393u, 1629811331u, +3096616493u, +1391424435u, +4158376003u, 1015657076u, 794783832u, 479952178u, +1150290207u, 2497437906u, 231815090u, 755078067u, +3832053281u, +63649475u, +2415822606u, 4105027719u, 1706992318u, 1106598740u, +3941945667u, 1271300761u, 505882259u, 760186809u, +2657183368u, +1925422058u, +1039773764u, 880219458u, 4275949176u, 1556833823u, +925882132u, 4216310340u, 757497522u, 461833914u, +3884002070u, +2790957660u, +2100050089u, 651959176u, 1380301291u, 1289124125u, +452314403u, 226156280u, 3306924715u, 1750807758u, +2290180542u, +1953760569u, +2253069096u, 3960924806u, 1786291620u, 60736185u, +2569018293u, 3870479674u, 2247005661u, 2239850953u, +4261808536u, +3282975782u, +780945879u, 3349849383u, 1579362556u, 2265045884u, +905088740u, 725212379u, 3156479246u, 2501620391u, +3062836263u, +4070422690u, +996797869u, 4082582315u, 976105756u, 303983602u, +1862104804u, 3864508254u, 3383979677u, 2835500286u, +2798364010u, +519359476u, +3447342725u, 194373889u, 3313466630u, 232399983u, +2841787856u, 1672751454u, 3345183154u, 1805381384u, +2226129336u, +2847829057u, +2350774567u, 2838540121u, 2757948482u, 1017002062u, +2329150951u, 2171488196u, 3668619047u, 3874977844u, +3287966998u, +262346753u, +2493054715u, 2298644430u, 2926101182u, 1528457638u, +598656233u, 2615845874u, 989110727u, 820441411u, +253617372u, +2201077208u, +2047569338u, 3114356329u, 3335563734u, 2967673540u, +768438341u, 1417708203u, 3873718246u, 1538441843u, +1279167650u, +3917966776u, +2218481734u, 1015935150u, 1957845042u, 1318150213u, +3146423971u, 4218994877u, 1162470863u, 1519718292u, +2594658906u, +665870414u, +3430347817u, 3933868731u, 1597041394u, 3138684682u, +3398212027u, 1064647658u, 1576321132u, 14792918u, +224938029u, +3706456050u, +847274786u, 2645698692u, 1743374687u, 2343133224u, +3066596790u, 2857270120u, 200596308u, 452055528u, +2319312082u, +3488655402u, +4146865894u, 608206438u, 2699777051u, 3687240713u, +327957508u, 3664730153u, 568134564u, 2993484554u, +4159860363u, +4274533921u, +1079994063u, 2360220210u, 3609597760u, 3639708902u, +2836180437u, 1069910270u, 1892427666u, 1874729790u, +1267712826u, +121886940u, +3572289214u, 2475945610u, 783779452u, 588827737u, +1531395014u, 2085084212u, 2219189792u, 3981444548u, +2218885336u, +1691622694u, +2053232885u, 1386558530u, 2182946189u, 2365247285u, +1871081313u, 2935751853u, 38413723u, 543465863u, +900691890u, +2899905665u, +575120562u, 93133904u, 457154948u, 2983705792u, +4232229200u, 2038565963u, 614693984u, 3405328302u, +4083090010u, +2088004171u, +244031209u, 1861889294u, 2417109253u, 3299562328u, +4158642443u, 4199064449u, 3161611046u, 885015950u, +3677904099u, +2969861785u, +772348805u, 1712263832u, 3219357614u, 484271305u, +3645706114u, 2059620251u, 409557488u, 2278896731u, +224475749u, +3523022952u, +2057140088u, 449131785u, 1149879244u, 4255363996u, +3602720135u, 1690010854u, 2503998822u, 2750828466u, +3340671802u, +1447583863u, +2649684943u, 2764747249u, 3046070595u, 3441726138u, +3840332559u, 3156747501u, 1288666680u, 1472744459u, +3452391933u, +1617542784u, +217869690u, 3718469527u, 348639731u, 590532355u, +43789787u, 22606314u, 1621559290u, 2231743261u, +2234620879u, +544748955u, +3169387920u, 203343594u, 3272552527u, 1078282365u, +809576321u, 854207584u, 3625491053u, 1193737267u, +1628966807u, +2661421060u, +2433442061u, 3886639039u, 2149304418u, 303000565u, +1432830882u, 137378235u, 1135974068u, 318705754u, +2491227157u, +2627534472u, +3520352233u, 2488397682u, 3969194920u, 3843962181u, +2135981459u, 2611933220u, 799460731u, 2300968851u, +3412851628u, +3070914013u, +3555224260u, 4125937572u, 240359903u, 722496673u, +2061023600u, 3843919221u, 2759960043u, 1191155322u, +1504041490u, +3735253656u, +1773124736u, 101110011u, 1627699578u, 2645634551u, +263603947u, 1388368439u, 677146538u, 1644201982u, +2625699644u, +2403862553u, +2426069017u, 3613511705u, 915141802u, 2981654265u, +3474818167u, 2611101773u, 627891434u, 762754924u, +2143021902u, +51067670u, +4017746573u, 2269879853u, 3037857950u, 2388899692u, +582729171u, 1886116725u, 2281219772u, 264704948u, +3509984037u, +4078683368u, +2172959411u, 1807195632u, 3357092302u, 2253764928u, +2320369390u, 3076335959u, 2623583210u, 168378015u, +1435562650u, +1100977467u, +3160490319u, 2550328495u, 2396855930u, 1347823908u, +1617990918u, 3849653099u, 3224111576u, 1681539821u, +4171542880u, +552200045u, +3562947778u, 1676237880u, 3747732307u, 2453332913u, +865530667u, 3566636849u, 3485502777u, 336779723u, +2535942410u, +1685000184u, +820545711u, 1893670486u, 1273910461u, 1193758569u, +970365241u, 381205962u, 3612810852u, 1160577445u, +541488143u, +4005031080u, +2333965236u, 2419855455u, 3484533538u, 3073937876u, +908466956u, 661391539u, 2342122412u, 1467049112u, +1785800827u, +135343033u, +139643209u, 2438375667u, 974654058u, 3216478230u, +3807620420u, 779043363u, 2812846449u, 333254784u, +1025244024u, +2242303095u, +2476683742u, 350018683u, 174652916u, 933097576u, +826905896u, 559603581u, 2777181260u, 164915169u, +4070353203u, +1459055748u, +297303985u, 3103837241u, 3812514233u, 232265137u, +2032819099u, 1523091376u, 3531238208u, 1403510182u, +2886832080u, +2599705941u, +2789695716u, 68437968u, 3823813791u, 1040994569u, +3024194990u, 2461740520u, 3735391266u, 2042207153u, +2461678616u, +3519231840u, +1344224923u, 411442756u, 1179779351u, 7661528u, +778352196u, 3288808867u, 589356197u, 2627504511u, +3374744599u, +3312172905u, +357423007u, 3539567796u, 4044452215u, 1445118403u, +2937983820u, 184089910u, 346201845u, 2427295202u, +1345448010u, +2884434843u, +3085001879u, 2640105409u, 315310640u, 3530289798u, +3362974764u, 963602652u, 75228477u, 3509381180u, +4012777756u, +2380345941u, +1073137836u, 2083960378u, 1220315185u, 3628720934u, +3508867818u, 67148343u, 3558085158u, 1753943368u, +863309561u, +2844713625u, +441921850u, 854732254u, 816793316u, 2555428747u, +3440623414u, 1707304366u, 3189874375u, 1623229221u, +1220335976u, +806745430u, +3909262947u, 1680369031u, 2926179486u, 3410391660u, +3991630434u, 2876458763u, 1179167079u, 536360759u, +1592117159u, +1514343977u, +1032622306u, 2057494855u, 784938958u, 178402996u, +1152907972u, 2326185495u, 2939973666u, 4181120253u, +552831733u, +664251856u, +1297139539u, 1969357631u, 1474065957u, 3055419017u, +3395829380u, 3316562752u, 2168409017u, 614624786u, +3585854336u, +668291094u, +1162889217u, 3773171307u, 2263271126u, 355089668u, +3195850578u, 3396793277u, 3519870267u, 527857605u, +3972392320u, +2224315010u, +4047225561u, 3271434798u, 3192704713u, 2798505213u, +3932215896u, 3792924012u, 3796843756u, 453872975u, +4050552799u, +1056432676u, +928166947u, 121311642u, 930989547u, 2087070683u, +1288978057u, 1556325239u, 1812435626u, 1682385724u, +1214364933u, +904760776u, +3957045528u, 3949822847u, 2411065880u, 3716420732u, +3424837835u, 3833550693u, 1799375326u, 2012368921u, +2768764136u, +1786111037u, +4055479315u, 3751639533u, 2808224623u, 3492656387u, +1306824780u, 2624000170u, 3134795218u, 1778409297u, +3900821801u, +593336325u, +2772069220u, 2980873673u, 3574497158u, 3994780459u, +4246519854u, 3482758570u, 4228015183u, 33101083u, +1769887734u, +4158035314u, +3690638998u, 1119035482u, 4134969651u, 2483207353u, +3932823321u, 285829887u, 3485140138u, 1304815138u, +995608264u, +3133997465u, +1195477617u, 2147693728u, 3506673112u, 4234467492u, +1183174337u, 1395340482u, 769199343u, 193262308u, +2798920256u, +3827889422u, +3399695609u, 3036045724u, 2999477386u, 3567001759u, +2682864314u, 1414023907u, 3699872975u, 3369870701u, +2662284872u, +2179640019u, +2485080099u, 3234415609u, 3755915606u, 1339453220u, +1567403399u, 2076272391u, 293946298u, 3861962750u, +1291949822u, +2916864995u, +132642326u, 2215117062u, 2205863575u, 2488805750u, +405632860u, 3248129390u, 2952606864u, 896734759u, +2047417173u, +3865951392u, +657296855u, 1328547532u, 3966511825u, 3959682388u, +4171801020u, 2981416957u, 1868896247u, 790081075u, +3143666398u, +2950766549u, +2065854887u, 2737081890u, 995061774u, 1510712611u, +2865954809u, 565044286u, 1565631102u, 1500654931u, +494822108u, +2803515503u, +1058154996u, 3506280187u, 856885925u, 4204610546u, +800905649u, 1130711562u, 558146282u, 2053400666u, +449794061u, +2643520245u, +2101248725u, 3123292429u, 3583524041u, 983372394u, +1587743780u, 672870813u, 444833475u, 100741452u, +366232251u, +1717951248u, +524144122u, 1362432726u, 1304947719u, 674306020u, +405665887u, 4081931036u, 1580408204u, 2343242778u, +3901654006u, +2627173567u, +3015148205u, 814686701u, 1327920712u, 1346494176u, +2468632605u, 2259795544u, 2519278184u, 2129281928u, +2860266380u, +4001619412u, +1154910973u, 2841022216u, 1199925485u, 1372200293u, +2713179055u, 3609776550u, 2896463880u, 1056406892u, +177413841u, +40180172u, +3274788406u, 660921784u, 1686225028u, 4003382965u, +2532691887u, 4256809101u, 1186018983u, 667359096u, +2375266493u, +2760222015u, +745187078u, 312264012u, 396822261u, 2588536966u, +2026998998u, 1766454365u, 3218807676u, 3915487497u, +2630550356u, +4130063378u, +4231937074u, 752212123u, 3085144349u, 3267186363u, +4103872100u, 4193207863u, 1306401710u, 3014853131u, +1067760598u, +2306188342u, +2437881506u, 4258185052u, 2506507580u, 130876929u, +1076894205u, 4106981702u, 2799540844u, 945747327u, +1436722291u, +2499772225u, +2571537041u, 2038830635u, 2066826058u, 2892892912u, +524875858u, 3392572161u, 2869992096u, 1308273341u, +923668994u, +1980407857u, +2275009652u, 240598096u, 2658376530u, 3505603048u, +1022603789u, 582423424u, 846379327u, 4092636095u, +4177298326u, +1004173023u, +2154027018u, 2993634669u, 1098364089u, 3035642175u, +1335688126u, 1376393415u, 1252369770u, 3815033328u, +1999309358u, +1234054757u, +1388595255u, 2859334775u, 366532860u, 3453410395u, +4226967708u, 1321729870u, 2078463405u, 156766592u, +3157683394u, +3549293384u, +3348214547u, 2879648344u, 1144813399u, 2758966254u, +647753581u, 813615926u, 2035441590u, 1961053117u, +600168686u, +2192833387u, +3156481401u, 3627320321u, 383550248u, 81209584u, +2339331745u, 1284116690u, 1980144976u, 2955724163u, +789301728u, +3842040415u, +1115881490u, 965249078u, 4098663322u, 1870257033u, +2923150701u, 4217108433u, 183816559u, 2104089285u, +2640095343u, +3173757052u, +927847464u, 2383114981u, 4287174363u, 1886129652u, +70635161u, 1182924521u, 1121440038u, 4246220730u, +3890583049u, +975913757u, +2436253031u, 1074894869u, 1301280627u, 992471939u, +735658128u, 244441856u, 1541612456u, 3457776165u, +3503534059u, +1931651133u, +349142786u, 3669028584u, 1828812038u, 99128389u, +1364272849u, 1963678455u, 3971963311u, 2316950886u, +1308901796u, +2789591580u, +1460494965u, 2380227479u, 1577190651u, 1755822080u, +2911014607u, 859387544u, 13023113u, 2319243370u, +2522582211u, +2299110490u, +3342378874u, 2589323490u, 1884430765u, 3739058655u, +2419330954u, 355389916u, 273950915u, 3670136553u, +410946824u, +3174041420u, +2609010298u, 3059091350u, 2300275014u, 725729828u, +2548380995u, 1738849964u, 1257081412u, 79430455u, +810321297u, +3246190593u, +1007937684u, 912115394u, 40880059u, 3450073327u, +4289832174u, 2253485111u, 1065639151u, 2953189309u, +124779113u, +654299738u, +115760833u, 1250932069u, 884995826u, 3998908281u, +1382882981u, 1134187162u, 3202324501u, 487502928u, +3032756345u, +4057517628u, +933197381u, 2319223127u, 2044528655u, 2554572663u, +4049450620u, 1620812836u, 2832905391u, 2273005481u, +1913090121u, +1055456023u, +510593296u, 3285343192u, 2912822536u, 1645225063u, +638418430u, 452701300u, 1025483165u, 1639370512u, +167948643u, +2809842730u, +2983135664u, 407521332u, 1543756616u, 3949773145u, +4283462892u, 659962275u, 3878013463u, 1000748756u, +4053212051u, +4099239406u, +3467581965u, 354635541u, 21301844u, 3831212473u, +3189450571u, 2264401966u, 4096484849u, 1736448515u, +3976926096u, +3727194724u, +2243487039u, 585209095u, 3143046007u, 969558123u, +3037113502u, 3594170243u, 2835860223u, 3775493975u, +2787220812u, +2274252217u, +2915380701u, 3077533278u, 1252871826u, 1519790952u, +205297661u, 2950557658u, 3956882191u, 2724439401u, +3694608025u, +124028038u, +216019153u, 1533010676u, 2259986336u, 2014061617u, +2068617849u, 3078123052u, 2692046098u, 1582812948u, +396916232u, +1470894001u, +1694309312u, 300268215u, 1553892743u, 671176040u, +1544988994u, 2793402821u, 4194972569u, 2296476154u, +748354332u, +3491325898u, +4261053291u, 1104998242u, 797816835u, 243564059u, +2197717393u, 299029458u, 1675252188u, 3139770041u, +583018574u, +2532106100u, +2099391658u, 3760526730u, 3422719327u, 3556917689u, +2374009285u, 2130865894u, 3710563151u, 1437538307u, +3938030842u, +2006930694u, +2151243336u, 1939741287u, 1957068175u, 2135147479u, +649553342u, 1713643042u, 4188696599u, 1698739939u, +3549427584u, +1016382174u, +322644378u, 2476164549u, 2037263020u, 88036019u, +2548960923u, 539867919u, 2871157727u, 4031659929u, +754087252u, +972656559u, +4246379429u, 3877308578u, 2059459630u, 3614934323u, +1410565271u, 2102980459u, 215395636u, 1083393481u, +3775523015u, +2062750105u, +2475645882u, 3041186774u, 3534315423u, 758607219u, +1686100614u, 180500983u, 1155581185u, 1476664671u, +2918661695u, +3812731350u, +4003853737u, 4148884881u, 1468469436u, 3278880418u, +1045838071u, 1049161262u, 360450415u, 3158065524u, +814443735u, +3391401707u, +729968410u, 738771593u, 3662738792u, 1672830580u, +4199496163u, 188487238u, 219098233u, 2141731267u, +3890250614u, +2988780375u, +4026279523u, 3489429375u, 2468433807u, 1178270701u, +2685094218u, 2716621497u, 3718335529u, 2273344755u, +701110882u, +1925717409u, +1515176562u, 2325460593u, 3954798930u, 784566105u, +3769422266u, 1641530321u, 2703876862u, 2907480267u, +1828076455u, +1805635221u, +3883381245u, 1476756210u, 2072514392u, 3658557081u, +2003610746u, 2556845550u, 729594004u, 3303898266u, +1968227254u, +423204951u, +231828688u, 4223697811u, 698619045u, 3636824418u, +2738779239u, 2333529003u, 2833158642u, 580285428u, +3038148234u, +1012378004u, +1113647298u, 1424593483u, 4053247723u, 1167152941u, +2677383578u, 3419485379u, 2135673840u, 440478166u, +1682229112u, +3226724137u, +1217439806u, 3828726923u, 3636576271u, 3467643156u, +2005614908u, 2655346461u, 2345488441u, 1027557096u, +3594084220u, +1372306343u, +2342583762u, 4291342905u, 4094931814u, 3254771759u, +821978248u, 2404930117u, 1143937655u, 3156949255u, +3460606610u, +449701786u, +3474906110u, 1932585294u, 2283357584u, 1808481478u, +3522851029u, 3040164731u, 1530172182u, 2950426149u, +1402416557u, +756419859u, +4132576145u, 724994790u, 2852015871u, 2177908339u, +899914731u, 139675671u, 1423281870u, 3198458070u, +807581308u, +2021611521u, +1801452575u, 1425984297u, 2833835949u, 1536827865u, +3902351840u, 164546042u, 1872840974u, 3986194780u, +792156290u, +3378681896u, +941547959u, 3931328334u, 3661060482u, 2386420777u, +3920146272u, 3458621279u, 3348500844u, 2269586542u, +797371473u, +3188953649u, +80514771u, 2913333490u, 1246325623u, 3253846094u, +1723906239u, 1606413555u, 587500718u, 1412413859u, +2310046829u, +2113313263u, +3855635608u, 47271944u, 1112281934u, 3440228404u, +2633519166u, 425094457u, 307659635u, 67338587u, +2412987939u, +2363930989u, +2853008596u, 2844637339u, 922568813u, 130379293u, +2825204405u, 2904442145u, 1176875333u, 1511685505u, +599177514u, +1872681372u, +682394826u, 1888849790u, 3635304282u, 1761257265u, +1571292431u, 355247075u, 1177210823u, 1691529530u, +3629531121u, +3760474006u, +1129340625u, 868116266u, 3908237785u, 1942124366u, +1266630014u, 3214841995u, 334023850u, 1110037019u, +369650727u, +1288666741u, +70535706u, 20230114u, 4284225520u, 727856157u, +293696779u, 1244943770u, 3976592462u, 560421917u, +4171688499u, +2438786950u, +1218144639u, 3809125983u, 1302395746u, 534542359u, +2121993015u, 2899519374u, 3192177626u, 1761707794u, +3101683464u, +1555403906u, +3225675390u, 1875263768u, 4278894569u, 651707603u, +2111591484u, 3802716028u, 2900262228u, 1181469202u, +3254743797u, +1822684466u, +860641829u, 3046128268u, 1284833012u, 1125261608u, +461384524u, 2331344566u, 1274400010u, 990498321u, +3462536298u, +3796842585u, +2346607194u, 279495949u, 3951194590u, 3522664971u, +3169688303u, 726831706u, 1123875117u, 1816166599u, +3759808754u, +2918558151u, +3713203220u, 3369939267u, 466047109u, 384042536u, +587271104u, 2191634696u, 2449929095u, 1157932232u, +2084466674u, +841370485u, +3241372562u, 4277738486u, 2150836793u, 1173569449u, +778768930u, 2594706485u, 3065269405u, 3019263663u, +2660146610u, +2789946230u, +77056913u, 728174395u, 3647185904u, 804562358u, +2697276483u, 881311175u, 1178696435u, 2059173891u, +2308303791u, +221481230u, +50241451u, 3689414100u, 1969074761u, 2732071529u, +1900890356u, 840789500u, 2100609300u, 985565597u, +1220850414u, +2456636259u, +223607678u, 1016310244u, 1937434395u, 85717256u, +275058190u, 3712011133u, 171916016u, 2389569096u, +3679765802u, +3575358777u, +3481108261u, 3178286380u, 2489642395u, 2931039055u, +3086601621u, 3079518902u, 3027718495u, 2506894644u, +2976869602u, +2134336365u, +2420172217u, 918054427u, 661522682u, 1403791357u, +3587174388u, 2623673551u, 1355661457u, 4159477684u, +1109013587u, +3112183488u, +2217849279u, 3500291996u, 2419603731u, 2929886201u, +3854470013u, 1358382103u, 1357666555u, 21053566u, +2716621233u, +3094836862u, +3309729704u, 57086558u, 839187419u, 2757944838u, +3651040558u, 3607536716u, 3691257732u, 2312878285u, +1202511724u, +183479927u, +2509829803u, 109313218u, 478173887u, 2072044014u, +190631406u, 2495604975u, 1010416260u, 3679857586u, +726566957u, +258500881u, +1805873908u, 3081447051u, 2352101327u, 534922207u, +1584552873u, 813470716u, 255914637u, 249169434u, +3193498057u, +1038802706u, +2590158653u, 3147907290u, 663060128u, 1156177857u, +634616100u, 312879189u, 1545020368u, 2054634247u, +3271451914u, +3438291534u, +2181454946u, 3864535432u, 2398586877u, 896491075u, +2810631478u, 2770357487u, 3372930052u, 898070638u, +2051007323u, +392959778u, +36645539u, 3743556044u, 4134529680u, 4124451188u, +566806297u, 2936523982u, 1304761965u, 537399498u, +1940818842u, +40862381u, +36288410u, 3063605629u, 2826611650u, 3961972098u, +1871578006u, 2392095486u, 1136931591u, 513864488u, +173276451u, +3039055682u, +3543322032u, 1943592006u, 657217094u, 1751698246u, +2969618445u, 456616022u, 900309519u, 113892716u, +1126392103u, +1235651045u, +1882073852u, 2136610853u, 2353639710u, 2819956700u, +3980083530u, 828773559u, 224069850u, 902434120u, +2802008036u, +94358995u, +2777723394u, 2812641403u, 2525832595u, 4157388110u, +4235563782u, 937800324u, 141690749u, 568062536u, +550123849u, +1330316521u, +1949488696u, 2296431366u, 1958465262u, 3564751729u, +3748252207u, 120455129u, 1607318832u, 2525729790u, +2640987481u, +2332096657u, +1775969159u, 1555085077u, 2913525137u, 1347085183u, +2376253113u, 3194050574u, 1806090610u, 678641356u, +1499146713u, +383849715u, +3299835823u, 2284860330u, 2614269636u, 3913628844u, +2761334210u, 1959484587u, 529797021u, 239966995u, +3102194829u, +3602307804u, +1122192627u, 3577510006u, 164486066u, 1680137310u, +1473396395u, 1467801424u, 903493660u, 1185943071u, +2798556505u, +2306744492u, +3167201310u, 3577947177u, 3067592134u, 2905506289u, +1210366329u, 204484056u, 2347778932u, 3862374472u, +3277439508u, +4187414621u, +1646699310u, 621385800u, 3934869089u, 3975491588u, +3580085916u, 1925674500u, 2436305348u, 3983301539u, +2739439523u, +3291507446u, +3395637920u, 3753389171u, 2955202032u, 2654255623u, +3771089254u, 2140443405u, 2779834738u, 3261942805u, +3526889244u, +1842009139u, +4048484340u, 2106218403u, 2161244271u, 772152700u, +1158647659u, 3776791619u, 3882186721u, 699525237u, +2954670460u, +1007105869u, +3359152025u, 1146388699u, 1401550303u, 2326582541u, +4181783540u, 1085644043u, 1942143795u, 1038368308u, +1526153809u, +4042547244u, +1891441000u, 2573991874u, 1281441253u, 3635098284u, +1980545715u, 825985487u, 3934748116u, 4228386979u, +1480870944u, +1042194545u, +2397771642u, 2248490001u, 3817869868u, 878654626u, +3785629484u, 1672470870u, 3229367873u, 1894538933u, +1010692731u, +1733824268u, +656620328u, 3048283803u, 3353340056u, 2324965120u, +4192585951u, 2284524675u, 3483884368u, 1510168293u, +1554942691u, +1309709396u, +1241133168u, 3162179280u, 4046378054u, 3171681593u, +1165297136u, 3496703563u, 150437903u, 1948622072u, +1076332463u, +2292479143u, +1464229958u, 3479738093u, 2328067598u, 2334503110u, +833324834u, 3981605747u, 3002629155u, 2854644186u, +2832201336u, +95796957u, +3269249397u, 2358313329u, 3411860910u, 4283292480u, +2802208697u, 1305947955u, 2156803420u, 1991340283u, +189678024u, +447602599u, +1055411517u, 1531748363u, 1555852656u, 412402681u, +3774988152u, 20597551u, 2925024131u, 1423989620u, +3749428061u, +1541439448u, +112270416u, 1936224776u, 132162941u, 3772011507u, +3814102518u, 1908807815u, 444154079u, 823765347u, +3362275567u, +3419047430u, +2108287005u, 2315102125u, 658593738u, 3195094029u, +3721937534u, 3176229204u, 3398835373u, 1271898712u, +1142546577u, +3185986817u, +3562705803u, 2046119567u, 912990621u, 1829977672u, +3459576979u, 1118045834u, 1369529376u, 3320601076u, +3954988953u, +4002467635u, +3359456351u, 1314849568u, 1766750942u, 2998874853u, +1181800239u, 707328036u, 3314954697u, 2066721120u, +598194215u, +1124451278u, +3156679616u, 3742684743u, 2960199690u, 2683497915u, +2566077529u, 937014607u, 102095219u, 4262922475u, +3132264275u, +1262099830u, +862722905u, 2717653494u, 3245583534u, 3427209989u, +3220278124u, 85457091u, 2222333500u, 3513997967u, +3522324951u, +2830855552u, +2215004781u, 3482411840u, 4227160614u, 2030964411u, +1741393851u, 2643723748u, 942813508u, 403442675u, +3112048748u, +530556423u, +3817755244u, 3543286628u, 2247276090u, 1532920842u, +4101962711u, 1446540991u, 3297821473u, 1861255389u, +1984398u, +2366525138u, +377589481u, 3549193828u, 1427765914u, 506831657u, +277278988u, 1447652775u, 3214362239u, 3142198690u, +2843087541u, +468915015u, +807895062u, 2198723907u, 4031145069u, 2417156212u, +4027298697u, 637175947u, 1229254212u, 1773257887u, +1659444818u, +451148891u, +2099741368u, 735351990u, 2534775713u, 3261804619u, +712519954u, 3527962772u, 3758642738u, 4245823575u, +1281314264u, +1167866160u, +1489546151u, 1197354389u, 1043278102u, 2563326586u, +371937794u, 2320164817u, 3189512691u, 573685198u, +4108603513u, +3758899588u, +3507030163u, 2947201212u, 2529492585u, 578234375u, +3362349842u, 3318878925u, 3611203517u, 3059253190u, +4270755916u, +4291274625u, +4237586791u, 4137422245u, 2927218651u, 2444687041u, +797128811u, 2043057612u, 396533859u, 2665256178u, +3346510674u, +1779586176u, +3076562062u, 1882746214u, 921095362u, 2026988397u, +514514911u, 3886379478u, 4218272420u, 1480386793u, +3900160816u, +2292273451u, +1276138356u, 1125461821u, 1912885715u, 3365266013u, +1333211627u, 4085009861u, 1390530102u, 3347984752u, +2721771301u, +1419492325u, +4066766256u, 3250852311u, 820111852u, 1382201318u, +2366036798u, 938032241u, 3100979439u, 487048687u, +2292851045u, +3241399180u, +3912670510u, 2416437067u, 2973194517u, 3507707986u, +1935099406u, 2533441488u, 104616731u, 2892622820u, +3801190339u, +4239188808u, +807238241u, 3300121546u, 2249406147u, 4032114017u, +3713738189u, 3324425575u, 4275607376u, 3663120298u, +4173658372u, +3984289690u, +1827636846u, 3264588778u, 3297165529u, 558623533u, +2728945672u, 1566297318u, 3447249966u, 481719551u, +1596842050u, +1838185946u, +265271620u, 1050246315u, 4046655705u, 1844193138u, +3807563245u, 1075384804u, 1292554949u, 1506525927u, +2921816148u, +2051885269u, +1930534041u, 3872721086u, 1564489377u, 2272482181u, +2849358683u, 589618304u, 2262072443u, 290363051u, +299168363u, +3867603931u, +2868688756u, 2545263115u, 1092098533u, 3885725603u, +2352430409u, 1981595469u, 2047946646u, 1332642839u, +793806516u, +214858837u, +1061484659u, 3192394476u, 1115054785u, 3690637234u, +996792368u, 2023479706u, 3046498231u, 4205835102u, +3870714754u, +257472875u, +3549864599u, 2040276129u, 2414778670u, 812235477u, +2674248196u, 1864096101u, 2257492689u, 1332556794u, +1079540713u, +465530720u, +2304763972u, 830724724u, 3354588920u, 2510713652u, +3103749409u, 468835585u, 1707620787u, 3038024846u, +1000303198u, +3462270146u, +2748698899u, 2100348093u, 511537258u, 1237187486u, +102049383u, 2268226698u, 3162251739u, 4219404629u, +838822407u, +1481440623u, +2989224077u, 2676681975u, 3246551821u, 3812079906u, +370572963u, 2283154352u, 3084789986u, 1961085583u, +1955640586u, +2409348147u, +2284780581u, 1634818716u, 4018221729u, 2320761377u, +3566831899u, 1799560520u, 91431959u, 1754113747u, +1459430477u, +3613658517u, +924489906u, 3406317699u, 866289774u, 3924821603u, +1265394945u, 1870668109u, 151949856u, 2747006534u, +3111906201u, +64039467u, +2314447545u, 2600195638u, 4095795204u, 4162096026u, +1026756826u, 2460047982u, 52686887u, 823198739u, +1518045160u, +2867527376u, +566410761u, 2200433819u, 2114146405u, 2893790965u, +881504901u, 974783212u, 490815659u, 937300283u, +1523735309u, +2511976468u, +2634644947u, 355119367u, 1373773092u, 309232995u, +3088671051u, 787126032u, 3442836843u, 4289194567u, +2177850062u, +1174136430u, +3248982914u, 3129039732u, 1166851580u, 2196451882u, +469595580u, 2130837700u, 3783349021u, 3745262548u, +1236930515u, +3032131496u, +1525591437u, 1823628217u, 1939019255u, 1950270463u, +3659899927u, 3688643445u, 3004399289u, 1155199552u, +357547234u, +2213110526u, +3122658210u, 2667800490u, 2718690333u, 3512372076u, +1098611683u, 2657518392u, 4248458835u, 3109874532u, +1592908438u, +2864927516u, +3635248840u, 1251777186u, 3797340158u, 3508496870u, +303354834u, 1482394062u, 2087100120u, 1595931912u, +608574156u, +723367884u, +907938402u, 3357047807u, 1619629851u, 3092082995u, +89030300u, 916336992u, 1861180168u, 3436334155u, +1375000544u, +3472936241u, +1321217853u, 791356402u, 2872410224u, 2326250297u, +2657644088u, 1748314108u, 4146771421u, 2913114440u, +2924821844u, +2101101496u, +3268017251u, 2109603066u, 690665520u, 1830067573u, +951427661u, 2982533150u, 3884512506u, 2358657479u, +2833210784u, +3419798214u, +3785893994u, 2103940206u, 86759766u, 4031230616u, +3745237192u, 2739453927u, 497038072u, 3303159408u, +1251537249u, +1993408196u, +3185905715u, 2885948408u, 3154277110u, 2444150313u, +2505582079u, 2120610195u, 3266465773u, 1814611964u, +3080050407u, +1079915522u, +1819346505u, 2529946763u, 892097374u, 3740257161u, +3618100441u, 1079900094u, 3607172225u, 737863389u, +360704560u, +3341993089u, +1139047381u, 3132219631u, 1248981859u, 1109338159u, +2004908615u, 4022302594u, 4166640860u, 2959140950u, +3949235962u, +2832278473u, +2200524012u, 2634933043u, 2495844522u, 2613799818u, +4034096813u, 683271795u, 1673546817u, 1363163726u, +1805395136u, +511749501u, +1231032599u, 2305979751u, 345737783u, 3339868854u, +2931857933u, 2323251738u, 1332068477u, 51846558u, +3927238177u, +1387182179u, +1701238601u, 1419275173u, 2580882268u, 3357874599u, +1726558907u, 1292901039u, 1371322339u, 1311713044u, +3526735232u, +4017884184u, +3366093428u, 77140994u, 2128996229u, 1357915765u, +4019691901u, 483989024u, 2390311750u, 2766065288u, +3938587520u, +3064810344u, +1054589198u, 1274997019u, 4040589616u, 1277751144u, +2274907047u, 4170399945u, 2886368209u, 4168922115u, +3901237033u, +3252972311u, +2205185840u, 3403097556u, 3385493699u, 2809751370u, +555319628u, 399539034u, 2998971454u, 1521596214u, +178870216u, +1471733541u, +519629198u, 514159209u, 1500582242u, 1928616587u, +2686427928u, 4133138798u, 1225914083u, 1432713584u, +3559310915u, +3925489366u, +1055613123u, 4126676029u, 2723867653u, 3290604111u, +1377022957u, 2373608155u, 3615237379u, 594338683u, +2645257602u, +2408427260u, +917033274u, 750455097u, 625657657u, 121713200u, +2191273413u, 4043949724u, 3293146785u, 3809297972u, +3947296919u, +115456894u, +1529576616u, 1459278275u, 2157117997u, 1747859293u, +4106665903u, 996939232u, 2007976332u, 4274649009u, +1017725787u, +4244666096u, +1219631331u, 3072426253u, 3547691720u, 1620822012u, +1397717508u, 2031597325u, 3345983430u, 2459068000u, +3645130467u, +2308642742u, +359955852u, 1348467968u, 1133123059u, 2435919062u, +2800365907u, 4213217210u, 4056565603u, 2811666556u, +2318007236u, +3823652401u, +3654086429u, 1273260424u, 1591610446u, 943349350u, +3441227678u, 3779964757u, 233818224u, 3469971032u, +3764095096u, +4009204587u, +678472092u, 1990559652u, 2583121088u, 2978143652u, +2496370864u, 2139539656u, 4287972050u, 295832576u, +3536742861u, +2257466133u, +2738052161u, 1988611898u, 2466189642u, 3294419573u, +2311186273u, 474374532u, 3081964174u, 2515138278u, +835731677u, +1178182694u, +3352119543u, 2884763225u, 3462399574u, 2900817210u, +1993698511u, 2868445043u, 2746444849u, 1205258179u, +2353442946u, +4079040070u, +3624133102u, 2907136076u, 2902521697u, 426813211u, +1418185512u, 3711189488u, 1351506552u, 1934749519u, +46595543u, +401688809u, +3514602124u, 1396852607u, 1951477943u, 2502249173u, +3199695820u, 2890250638u, 4205072507u, 1715623846u, +3266686789u, +3218688128u, +1697759742u, 851227671u, 2358709645u, 4174233268u, +500583683u, 3805940955u, 736234120u, 2710563712u, +1949664540u, +3139414003u, +4293073253u, 1284406972u, 1785182449u, 1051548274u, +2994248357u, 2499882522u, 717208669u, 2039517285u, +518424929u, +143136433u, +2303774671u, 1272930860u, 2286410920u, 788459311u, +273225293u, 2439291703u, 2254505236u, 3446287701u, +3655156558u, +1546628787u, +340081500u, 3285722006u, 1324810435u, 1053980860u, +1779472859u, 2700355724u, 686005017u, 3762376315u, +3963193100u, +1370881135u, +661300087u, 1152753704u, 2349891598u, 3910051187u, +2109444785u, 1311123870u, 2639837565u, 1896770931u, +1081414128u, +869877586u, +4284220400u, 63045374u, 235968615u, 184451062u, +1271099822u, 1319179857u, 3274963209u, 4172272710u, +3388797445u, +2965973320u, +3793110097u, 3327241723u, 2991804005u, 1199544355u, +771553759u, 2031749842u, 2596517372u, 1199888213u, +858347951u, +3340178832u, +2903875412u, 763490382u, 76949161u, 2056544406u, +1145227689u, 998233136u, 2354530024u, 427713587u, +3537837347u, +604661755u, +923986833u, 1023730418u, 798294227u, 432557449u, +801802449u, 1861313429u, 3899128441u, 4068407979u, +2352677083u, +3783539925u, +10731973u, 3390767975u, 3949540249u, 1920121661u, +3248580201u, 641956426u, 2104847395u, 604835744u, +1491663404u, +4255204651u, +1520970746u, 2845653368u, 3247412938u, 3730629005u, +855569514u, 3073294700u, 2429691698u, 3818342476u, +3938869985u, +2731201328u, +2335202643u, 778117742u, 13298408u, 228780590u, +2871715314u, 3253688653u, 4150999702u, 3846220408u, +930808u, +1397128726u, +1964216488u, 2781092828u, 116285375u, 2271239476u, +3724347554u, 2931203895u, 3893169206u, 1883912528u, +2093892660u, +3658787024u, +3095016046u, 1094059199u, 3640239610u, 558564267u, +2102812456u, 464734873u, 925262247u, 1609838036u, +588364741u, +1731409233u, +1576165139u, 3933979268u, 375316394u, 4247099643u, +3670508019u, 4080496835u, 2371248533u, 183762693u, +2078935389u, +2699810414u, +1491815683u, 2999180789u, 1831158425u, 1603373553u, +2006136905u, 3210230591u, 416748595u, 1536971415u, +3271869367u, +1266062739u, +2138414557u, 3337114778u, 1634586826u, 36472629u, +4482244u, 568009609u, 2721216780u, 4037289545u, +2235138807u, +1789351460u, +4067539527u, 1323062829u, 3864620647u, 4192026301u, +4278901241u, 1399025382u, 2826652805u, 1363860382u, +1801770651u, +1613381526u, +1165249276u, 4046576622u, 2535596946u, 3260388176u, +1078898578u, 2259750862u, 643387587u, 237144235u, +4199571427u, +3440917581u, +3067939258u, 2018625455u, 1460528353u, 3138629939u, +1666223528u, 3841139376u, 2528281125u, 885565193u, +2609492686u, +2517257479u, +560864620u, 2261471820u, 3491559165u, 1329620416u, +622383582u, 1759597655u, 2877873893u, 584692817u, +1901728399u, +2599000260u, +3169771644u, 296332336u, 774719455u, 4175920823u, +2287316070u, 4115615023u, 1073335619u, 4240292725u, +1359158837u, +1960974237u, +3173724597u, 1619084286u, 2876340752u, 4065675347u, +480741335u, 1237329941u, 701055566u, 3729009837u, +1314736422u, +4003180069u, +3118519317u, 3035354420u, 3380357671u, 4020909015u, +253958714u, 3545798863u, 3008185002u, 2624719888u, +3219955575u, +3060719376u, +573101682u, 1580316843u, 2610493412u, 3490983536u, +3601975611u, 851470366u, 635384901u, 3427048824u, +1470002757u, +3592460087u, +2265226856u, 4124282457u, 2106385486u, 3334305617u, +4208282753u, 3798749815u, 225396466u, 118791182u, +2523395972u, +194595464u, +2563824631u, 2521301383u, 4224409406u, 468670274u, +1761966400u, 1300908277u, 2570709228u, 1847901526u, +1470099163u, +2690466752u, +1472536718u, 2399279735u, 4150607803u, 1775080054u, +2082537685u, 4080034578u, 1256001880u, 392967725u, +2055838940u, +3349115816u, +1745947263u, 2213925887u, 1836572741u, 2417722792u, +636223705u, 2423329294u, 3960951311u, 1543591052u, +1547914361u, +2760945653u, +3519014111u, 313543871u, 4119598884u, 1071003714u, +2192556597u, 1526995535u, 3929839778u, 536388591u, +3040873792u, +3752682932u, +1640614237u, 2432794021u, 385337403u, 2794410617u, +2386128075u, 1055206708u, 1422747714u, 3759330929u, +2533597496u, +30440955u, +1482899460u, 3350385050u, 616259409u, 3980103795u, +1211364140u, 1040071544u, 594746920u, 1645973936u, +2547331531u, +1097726368u, +700666526u, 2976247482u, 1144906608u, 996506677u, +1997130756u, 800321417u, 1392942823u, 1601662248u, +2079778663u, +529512908u, +2925120134u, 4106433085u, 630221833u, 2423086156u, +1119859778u, 1726827981u, 1870859181u, 2559832707u, +1792284257u, +2059356387u, +3572353364u, 3229407475u, 575621095u, 3221893291u, +2372428048u, 2020123035u, 961449593u, 2243824063u, +3803906611u, +3735348189u, +2981620804u, 4180681078u, 1555330629u, 230736535u, +2075526640u, 749652975u, 713664372u, 2152096659u, +2142067223u, +3322302242u, +1421646830u, 2092832615u, 1213735101u, 3192136753u, +1106723940u, 3455398230u, 2541685524u, 2529956739u, +3789430647u, +1950084508u, +2157395621u, 850457360u, 2758902426u, 2848030169u, +6506379u, 1162213157u, 2981459221u, 272690871u, +3059420255u, +4242691285u, +588065598u, 1206949936u, 3968214184u, 566348532u, +126142880u, 1480567086u, 2959621988u, 2050218418u, +2242731195u, +3833514449u, +1898070331u, 3687399477u, 3891859374u, 868185955u, +2335308774u, 3676335246u, 3871121805u, 2189032743u, +3275728647u, +860492892u, +1590764344u, 4130384758u, 262871548u, 3004764525u, +2685542071u, 991231482u, 435122019u, 3031116998u, +2898921700u, +2917932604u, +4238665148u, 2459072654u, 3444612545u, 4207731740u, +1808564313u, 2798532269u, 3944553556u, 3926395409u, +1633200670u, +4138335224u, +2524878605u, 4184292650u, 3563398268u, 4288943552u, +3802121210u, 957502058u, 2410820887u, 4227117506u, +4018625153u, +4284329158u, +530216712u, 2978986531u, 863452221u, 1910162118u, +4088211378u, 4091971261u, 3150811451u, 4200871487u, +3794038652u, +3041564310u, +2045287082u, 887805614u, 2889167251u, 4120352181u, +1699912580u, 3478922097u, 3211994687u, 3136177842u, +1500806861u, +3211881347u, +2147976385u, 3342722260u, 3359650541u, 4197378460u, +781354073u, 1533623029u, 2204677828u, 3228172832u, +3248592437u, +3355841359u, +560815159u, 1144951236u, 4027015711u, 2882625391u, +339363613u, 2354572719u, 1769831876u, 4238589331u, +1519732871u, +2185834614u, +1601096831u, 129709881u, 39655633u, 367604993u, +1737681770u, 3259114599u, 2767070452u, 872365177u, +1574125529u, +3405020189u, +4181346685u, 1134030380u, 403769171u, 2193351164u, +1426232618u, 2885309450u, 3033612627u, 924948363u, +935514094u, +3202053329u, +912294839u, 1618472324u, 4159158431u, 3744999487u, +777064358u, 3974213124u, 1990246048u, 309725290u, +2449849392u, +1943692420u, +2288635750u, 2433793635u, 2168904061u, 683315308u, +3081493019u, 3477759434u, 3815496269u, 2823504699u, +586945121u, +3088963200u, +3492287335u, 636875049u, 1111206944u, 2037346120u, +1282050044u, 1409681512u, 1786128584u, 755810950u, +2332676758u, +2178142310u, +957827166u, 1014983590u, 1888800725u, 3608595803u, +3200072714u, 2534008478u, 659336139u, 1281728287u, +4060560529u, +2915575125u, +3521503774u, 2926487340u, 1096297674u, 653489861u, +2352326980u, 2561136777u, 1224141198u, 1250479629u, +1297625391u, +2409997371u, +1942483722u, 2481835750u, 1394715707u, 1673070941u, +2456039704u, 3980558014u, 3547934764u, 1882038812u, +1078160498u, +2488279087u, +1848235245u, 1211914722u, 2264928765u, 2807773070u, +270145554u, 583747883u, 3826009010u, 2996618216u, +425727157u, +992726957u, +3384462280u, 726650661u, 1955043265u, 1923879512u, +1854693773u, 2987614542u, 2660044993u, 2457260810u, +426299370u, +2671892900u, +1827308087u, 3083953443u, 1791749638u, 3265087416u, +2119752201u, 2547122538u, 3990783236u, 1912713468u, +3688865211u, +1815780016u, +303699291u, 2416763742u, 2690891610u, 1535193548u, +1107803989u, 1504143133u, 2235270371u, 2545884083u, +2276278682u, +411724404u, +3416925704u, 2565792091u, 3383911757u, 546058824u, +3374654444u, 2364630415u, 2693473470u, 2622125691u, +261864817u, +55682470u, +857617568u, 141304067u, 1885488541u, 155368182u, +1281949051u, 3384522408u, 3254816901u, 1959816782u, +1452224057u, +2830267691u, +3709231247u, 58988202u, 4218130458u, 2984061349u, +1888707848u, 4223605071u, 4241442486u, 375269213u, +3208327038u, +2199916493u, +550337252u, 2855061437u, 276088636u, 114362204u, +2321163647u, 2127813633u, 3289403024u, 2686973202u, +2717376797u, +3593428039u, +3648831666u, 890925902u, 3289404818u, 3289516821u, +4248913260u, 1858916580u, 3303932308u, 1752797086u, +1628149686u, +3245893605u, +1568537311u, 2844194502u, 1593855770u, 2408174109u, +124797514u, 2085649512u, 3188565660u, 2264996276u, +1926696513u, +3053957740u, +2238806881u, 2189050973u, 203685243u, 379855590u, +3920271562u, 1058600179u, 3698061923u, 4255106849u, +608401664u, +1598041932u, +3318266418u, 2535016555u, 852760884u, 1918098822u, +2200437599u, 1532285043u, 3425662132u, 3561293706u, +2231633206u, +4108785088u, +3359152801u, 173534780u, 208383607u, 2862988169u, +2406642243u, 426814583u, 2777335795u, 3322703596u, +954190623u, +615093090u, +4179102978u, 2452847930u, 100239619u, 42471741u, +818352432u, 2190624654u, 504379960u, 3631619975u, +633412456u, +1018421783u, +842645419u, 711808707u, 3424580813u, 2132457941u, +1158335882u, 3567952480u, 2302183699u, 1145788151u, +3474264138u, +3105085243u, +3115506027u, 2783713015u, 3871785309u, 539583269u, +1400252405u, 3857849984u, 4231186588u, 1278653799u, +1760227022u, +761044088u, +3838185417u, 2439542532u, 585283357u, 2055995220u, +937117124u, 3831944855u, 1823586038u, 3287917855u, +485082427u, +3209172809u, +1984570176u, 2818337297u, 2691869057u, 3790476953u, +839035557u, 3203129010u, 669981176u, 4121157385u, +3519870450u, +3792633352u, +3017650322u, 1603459507u, 4225677666u, 376555451u, +473780127u, 2018786277u, 3299822439u, 1010254499u, +2383887565u, +3155009499u, +3108110655u, 2641738274u, 3684908622u, 1606463047u, +3311068174u, 52708046u, 754181455u, 1018079176u, +3915670272u, +3366999425u, +1012880204u, 1339439715u, 466437962u, 1402662350u, +2504046911u, 736323938u, 2037800124u, 1725908589u, +716341840u, +1750123474u, +3366342464u, 1743666195u, 2975303189u, 3821364027u, +3253707772u, 3635548377u, 3840413796u, 1955642085u, +1018315169u, +1258092848u, +2095540656u, 1076256607u, 117289557u, 1311658655u, +2118301000u, 68721550u, 2886814107u, 2712432819u, +4201862886u, +753807148u, +1940229047u, 731347296u, 1068901393u, 3873155894u, +2852787666u, 1973464853u, 79735652u, 3966380587u, +3245740712u, +2525773438u, +734938109u, 3045656416u, 3335746354u, 4099732691u, +1911896517u, 1697006473u, 1145487066u, 1605663299u, +3053606724u, +2386289465u, +3821211369u, 1006215345u, 1256304829u, 1053001668u, +1289194958u, 118761054u, 1853688730u, 2803418011u, +188650809u, +3763686458u, +1006829556u, 2961984133u, 3390525025u, 2061199893u, +141792681u, 2439893463u, 2652982650u, 1804942682u, +1546510005u, +1246961405u, +2407577046u, 565772575u, 3751844810u, 2943166103u, +3750052451u, 3022527280u, 25162928u, 397381043u, +1818337632u, +3447363730u, +3936437150u, 2569420703u, 2215592390u, 2171555672u, +3665571006u, 4021712412u, 2939158353u, 4057813172u, +1823237318u, +103999245u, +3251978010u, 3591914940u, 3582495283u, 2519035265u, +3905726135u, 3180393349u, 2743117123u, 55247368u, +3325286701u, +705195946u, +1857526853u, 1480518550u, 3809990433u, 1398189338u, +3126362926u, 3959531492u, 1503658285u, 1977847740u, +3043964489u, +2613086143u, +1518119282u, 4238434900u, 3905746486u, 3064949667u, +1028122931u, 3309119457u, 4071194920u, 3096098907u, +4137180520u, +494467959u, +1231408687u, 1691606157u, 1793452569u, 2722196118u, +3478603952u, 1059665738u, 2282032278u, 3990268388u, +1719514651u, +4248311578u, +3799146721u, 898026304u, 3367808954u, 4162472815u, +170495870u, 1308116609u, 3428285344u, 1714716475u, +395576794u, +4153638621u, +2999745812u, 3483315953u, 304980828u, 595337120u, +3486516729u, 2331563143u, 2583609459u, 1885928417u, +3834283777u, +979337825u, +932057378u, 3124081189u, 1930356777u, 3865887996u, +4178282217u, 4214219408u, 3669465884u, 1472413856u, +3356866587u, +1012769806u, +3043639963u, 996996396u, 207308216u, 982967331u, +2991319933u, 318066902u, 721489670u, 1249967713u, +749240921u, +591392325u, +2379365192u, 2250868849u, 2163259329u, 143191325u, +3778285606u, 982149096u, 3536906200u, 2244353244u, +1443862317u, +3161549210u, +2183127464u, 2015409516u, 547003700u, 2032484282u, +523677821u, 4275663308u, 3827205526u, 3903778273u, +2444530525u, +2543645801u, +1173958423u, 784740616u, 2878693675u, 3127696736u, +3832037316u, 3161002398u, 4084166400u, 4213346853u, +223390424u, +4273380883u, +2130315482u, 3429606032u, 3367732613u, 1912357694u, +422632590u, 1266957023u, 3437535648u, 736404240u, +2281709372u, +415859912u, +212948797u, 351612650u, 3920561440u, 112963586u, +2230727543u, 2851076612u, 1990662634u, 2264296857u, +3131463650u, +2704034623u, +3541637839u, 2954232792u, 533986918u, 4158757533u, +65174248u, 4232639593u, 865906667u, 1948225652u, +779656112u, +3873989249u, +2372984749u, 2346988193u, 1104345713u, 1165654138u, +4045762610u, 3588205178u, 461363991u, 1111215752u, +1389675192u, +2404325151u, +2152228101u, 3808973622u, 1901235912u, 3458690696u, +314513238u, 2539459143u, 2847998873u, 952026138u, +2325705328u, +407844712u, +3727960715u, 2996448351u, 2374336760u, 3138756390u, +2600015243u, 539980418u, 1876285352u, 1670330799u, +1709360377u, +2868531654u, +494777964u, 2773053597u, 599486162u, 3962209577u, +1871328846u, 2171933018u, 110279472u, 384074780u, +4147021936u, +2333589647u, +4251778066u, 40493468u, 3099342316u, 4108779767u, +2812424588u, 954542332u, 2040682331u, 2251152306u, +45915516u, +259525626u, +1045384743u, 4134656562u, 749389261u, 874399445u, +616549904u, 2200447504u, 436024539u, 78972290u, +3210485762u, +1907985531u, +3013721395u, 4214533685u, 4198804243u, 534879265u, +1517190881u, 3756787754u, 1152563554u, 1718750948u, +777737463u, +1402478860u, +1824562784u, 1879401449u, 3515818786u, 513165201u, +1423491227u, 2103067918u, 2291777410u, 1097943000u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; IsAlive(farmhashcc::Hash32WithSeed(data, len++, SEED)); IsAlive(farmhashcc::Hash32(data, len++)); { uint128_t u = farmhashcc::Fingerprint128(data, len++); uint64_t h = Uint128Low64(u); IsAlive(h >> 32); IsAlive((h << 32) >> 32); h = Uint128High64(u); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } len -= 3; return alive > 0; } +Check(farmhashcc::Hash32WithSeed(data + offset, len, SEED)); +Check(farmhashcc::Hash32(data + offset, len)); +{ uint128_t u = farmhashcc::Fingerprint128(data + offset, len); uint64_t h = Uint128Low64(u); Check(h >> 32); Check((h << 32) >> 32); h = Uint128High64(u); Check(h >> 32); Check((h << 32) >> 32); } +{ uint128_t u = farmhashcc::CityHash128WithSeed(data + offset, len, Uint128(SEED0, SEED1)); uint64_t h = Uint128Low64(u); Check(h >> 32); Check((h << 32) >> 32); h = Uint128High64(u); Check(h >> 32); Check((h << 32) >> 32); } + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashccTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +cout << farmhashcc::Hash32WithSeed(data + offset, len, SEED) << "u," << endl; +cout << farmhashcc::Hash32(data + offset, len) << "u," << endl; +{ uint128_t u = farmhashcc::Fingerprint128(data + offset, len); uint64_t h = Uint128Low64(u); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u, "; h = Uint128High64(u); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint128_t u = farmhashcc::CityHash128WithSeed(data + offset, len, Uint128(SEED0, SEED1)); uint64_t h = Uint128Low64(u); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u, "; h = Uint128High64(u); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashccTest + +#if TESTING + +static int farmhashccTestResult = farmhashccTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashccTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashccTest::Dump(0, i); + } + farmhashccTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashmkTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +4223616069u, +3696677242u, +4081014168u, +2576519988u, +2212771159u, +1112731063u, +1020067935u, +3955445564u, +1451961420u, +653440099u, +31917516u, +2957164615u, +2590087362u, +3879448744u, +176305566u, +2447367541u, +1359016305u, +3363804638u, +1117290165u, +1062549743u, +2437877004u, +1894455839u, +673206794u, +3486923651u, +3269862919u, +2303349487u, +1380660650u, +595525107u, +1525325287u, +2025609358u, +176408838u, +1592885012u, +864896482u, +2101378090u, +3489229104u, +2118965695u, +581644891u, +2718789079u, +631613207u, +4228658372u, +3867875546u, +3531368319u, +3804516756u, +3317755099u, +1619744564u, +2884717286u, +1088213445u, +2667691076u, +3727873235u, +2330406762u, +3616470388u, +967660719u, +4148162586u, +315219121u, +673084328u, +3047602355u, +1598963653u, +1267826661u, +2117362589u, +2861192253u, +1823625377u, +1380350078u, +1641748342u, +1176094482u, +269384321u, +2178982315u, +3480237248u, +2660755208u, +1850544433u, +3429699438u, +1262819303u, +640556464u, +2421125401u, +2188368608u, +2612932825u, +1474432581u, +173790449u, +2124882189u, +831272654u, +622960146u, +4238751051u, +3250317967u, +2120810248u, +1948231495u, +1389029321u, +2200398357u, +2134232963u, +2948072329u, +617717625u, +681164587u, +114859387u, +430545646u, +57239089u, +3163338012u, +3482496399u, +557662576u, +1102441413u, +2670159360u, +991116729u, +846014240u, +4233741566u, +1802317242u, +3129528802u, +1459456375u, +1305643039u, +3258671612u, +1578285833u, +868590079u, +1631034517u, +1695432937u, +561078856u, +1004115553u, +3086090507u, +3818348650u, +731596645u, +780926790u, +2544205955u, +158479164u, +3983514188u, +2004735250u, +3436218400u, +673684751u, +1463431419u, +2880490219u, +3223748024u, +2218318859u, +1474466194u, +2636437533u, +2206794961u, +140995728u, +1186394086u, +1805716888u, +1640037724u, +3942729099u, +1944727013u, +918951560u, +498666871u, +3486974657u, +2967205462u, +1167253804u, +1884281041u, +2866015002u, +4158319270u, +2627220079u, +3733319624u, +3317092271u, +438323662u, +3195868065u, +3426606709u, +360708338u, +1905491012u, +650004803u, +1351266252u, +3133279000u, +3722811115u, +2722412434u, +918432408u, +3678271248u, +269599647u, +621514057u, +3117077855u, +1545425390u, +2597567410u, +1221437820u, +3493254589u, +102787342u, +918861168u, +348795089u, +3439883229u, +2353641807u, +2209585469u, +4035884492u, +2686995435u, +1649888022u, +3852893848u, +3042700028u, +314103172u, +726977769u, +2489830276u, +2872753660u, +1316214989u, +1488801501u, +1811420390u, +639581627u, +2362837215u, +3634581834u, +3648576802u, +1257314182u, +762118371u, +4268447045u, +730167096u, +755561509u, +882614845u, +3696972894u, +228263661u, +1478636142u, +2767751651u, +1532617116u, +3838657661u, +1944359935u, +1401102137u, +3772933173u, +1050098254u, +1658079354u, +1846025728u, +2204244794u, +2017217424u, +1275162853u, +1429816745u, +2175565479u, +1716109139u, +1187506761u, +2434641075u, +2725597783u, +1795687662u, +1393312782u, +3511565397u, +627885430u, +4145733164u, +2519005353u, +231414775u, +1242015635u, +2760723497u, +2185540568u, +727314436u, +2358790354u, +1186393454u, +4234795645u, +350567813u, +866773875u, +3145590392u, +1158374055u, +3903123687u, +1862119793u, +2204587556u, +4266276976u, +4151548555u, +915250402u, +2874695320u, +2360311410u, +1099212769u, +1271542714u, +3473148363u, +1637325418u, +1807795989u, +2493819794u, +3800917924u, +4001205856u, +2582153621u, +3365872040u, +2890146216u, +2626363824u, +3133351295u, +4046827296u, +3053118771u, +4113026751u, +884356716u, +3828347401u, +10608262u, +830987972u, +1841080500u, +3202717763u, +3561778749u, +1906000052u, +3058284660u, +1432904514u, +2567431677u, +2550162530u, +665557986u, +936887821u, +2101205308u, +4253535847u, +1662043545u, +1253611611u, +2091370094u, +2635077370u, +2602176041u, +3624115809u, +748442714u, +2709749154u, +1023493343u, +860291012u, +3924715584u, +1536436740u, +2551145800u, +2391782865u, +1467705048u, +2583909796u, +3616666170u, +1162857372u, +4228631071u, +1510132376u, +2739165009u, +2656606142u, +3454996358u, +3155038853u, +1022087316u, +100044110u, +494208296u, +2746186477u, +4216782431u, +225448834u, +3728320521u, +335282866u, +3148194874u, +953503703u, +1293353960u, +202372387u, +1326119870u, +4045123735u, +3819994846u, +1629004186u, +1081099186u, +3591584153u, +1670825804u, +3404257979u, +3262192301u, +2572846095u, +3714992543u, +4264142572u, +529616678u, +2882154574u, +3006354178u, +3865969421u, +2007174907u, +308283107u, +2629833703u, +3159124075u, +1146492131u, +494104332u, +493149727u, +1342910585u, +521642387u, +2201695937u, +2517980959u, +2426821287u, +777374655u, +2228189792u, +4027055486u, +228976000u, +3842083468u, +1723920223u, +1192126094u, +787744493u, +2740368380u, +2284153001u, +2773829458u, +442000614u, +387830783u, +2169780670u, +2253144627u, +3532502484u, +1969684059u, +1165351416u, +3055056536u, +3582324253u, +231419363u, +770979865u, +3213983597u, +3690452836u, +935794639u, +3230602762u, +2841762457u, +407598927u, +1164479891u, +3721799696u, +354738136u, +1801566618u, +3206038542u, +2621379981u, +1943487262u, +3534745636u, +1074424589u, +1304517521u, +4133400969u, +2339317978u, +2135116860u, +4180643791u, +2415309340u, +1855926417u, +3418648630u, +1968113037u, +597304222u, +3668824865u, +3810008716u, +3014702569u, +3151212026u, +156057449u, +373134533u, +2068234004u, +191580563u, +3832754488u, +2924104199u, +2026044494u, +4065780435u, +122565840u, +4194985167u, +2744823717u, +2494098735u, +3753793370u, +1885739217u, +2488161225u, +3643797615u, +2653367310u, +2494061477u, +189968132u, +899646597u, +392100396u, +4012318310u, +3855777086u, +3566860954u, +2698574996u, +2414249905u, +1330623339u, +1263222732u, +1277741760u, +2194959402u, +1629656136u, +120494320u, +1072368005u, +1084245077u, +4011372748u, +1366613353u, +3108643228u, +3332219532u, +2114746095u, +3964007334u, +371687128u, +1084813876u, +126459896u, +4292782331u, +321283184u, +398168499u, +3604983506u, +560701543u, +2073961354u, +4240841868u, +4151211362u, +1338986875u, +4093476832u, +2269279497u, +3500846299u, +2510225147u, +598000444u, +1330391422u, +1432533385u, +4171226231u, +426821154u, +2932270996u, +3378981077u, +2217871549u, +1619647984u, +4051608043u, +3180237819u, +12919578u, +1375401767u, +371320427u, +2986640571u, +2336669859u, +3796464715u, +1892383284u, +306814912u, +2125823211u, +1863678891u, +3249703818u, +3840225752u, +281579900u, +264680257u, +4266359110u, +4182229890u, +2239659703u, +3627947372u, +2373929191u, +224082765u, +4053639058u, +1862360303u, +3187739624u, +3392706679u, +948039509u, +817505760u, +1215842393u, +3462222651u, +536021853u, +182346832u, +2731944883u, +2346674384u, +2640961678u, +3446695687u, +2271722179u, +1301069656u, +2803881468u, +2832614405u, +1691544398u, +698756814u, +3980620906u, +3565421410u, +754769376u, +4115923404u, +3909962218u, +2747614077u, +2888289845u, +1016920862u, +2790946178u, +3067070960u, +3173251481u, +1572132982u, +255048203u, +2996538818u, +3405398987u, +136106013u, +3581605228u, +4277437977u, +2147300534u, +3728426265u, +3483629996u, +1478452694u, +20756076u, +2774992067u, +432987927u, +1516771026u, +3511588664u, +2130994978u, +509385406u, +873090347u, +2163904107u, +4192239086u, +2532489989u, +1090772651u, +3910797408u, +3710882132u, +155010959u, +1369823531u, +1599664937u, +4035593587u, +1212746925u, +795822552u, +116689518u, +3674240941u, +1135576664u, +756750261u, +1027431362u, +390555140u, +2228460216u, +1506940482u, +3733857700u, +3048762971u, +2511703196u, +548609887u, +1607354252u, +659053982u, +259884450u, +1793130460u, +4083364495u, +3148555881u, +1764350138u, +2436485683u, +4031563025u, +3261860724u, +2475833430u, +2101726086u, +3191176464u, +2646658847u, +2127042126u, +771316100u, +2115922959u, +3208515045u, +2355437783u, +3621147793u, +1580163615u, +3211555675u, +3299188490u, +191613920u, +466733956u, +2939029038u, +1509152039u, +130591314u, +1892874677u, +1646908044u, +3452406523u, +3998376606u, +1199243832u, +2187108812u, +3189230066u, +4161151481u, +3371454980u, +3681788646u, +180842187u, +3685022399u, +3058749895u, +3250165163u, +2895367943u, +2627101723u, +771755098u, +1332921024u, +3638871848u, +514215135u, +3591227378u, +2300310870u, +3689533503u, +851607114u, +114330368u, +2709027386u, +1743034877u, +1013693860u, +288169008u, +3545190686u, +1052165084u, +3995862307u, +96902755u, +1097819851u, +2645431442u, +2184148618u, +2151206566u, +350979797u, +3467920900u, +421116779u, +1246252u, +4057835428u, +329324407u, +4104482417u, +844624570u, +3306265806u, +3787625025u, +4263241191u, +3251413927u, +2921204431u, +2931915325u, +992134330u, +3986338354u, +1327895216u, +1458363596u, +1480608532u, +728594368u, +3804366693u, +794404223u, +1643240863u, +793417255u, +4167916443u, +2683488959u, +3124925324u, +4184843652u, +3750971752u, +308509829u, +1054550805u, +2797511972u, +4043123412u, +1587158240u, +4050518606u, +3030062190u, +2589912753u, +603440067u, +937013191u, +1071662315u, +2100661456u, +2602005741u, +435516078u, +2260470147u, +1256268350u, +3612035u, +3368856141u, +151516099u, +3081868591u, +3363755681u, +2049963149u, +2885320434u, +84682005u, +2411758308u, +2695174275u, +3099904644u, +1787308684u, +1132379308u, +564634346u, +510236510u, +2804443681u, +3931864252u, +2064427949u, +1893979229u, +2916544974u, +1885887717u, +2978018250u, +494192125u, +2642662373u, +901112508u, +636035003u, +1658643797u, +172746975u, +517504890u, +3440019372u, +4144498044u, +1854755456u, +3672653905u, +4176892856u, +382159097u, +282871690u, +3629300472u, +2500754041u, +1677659759u, +1067175061u, +161654075u, +1672575536u, +346120493u, +2730229631u, +203466442u, +1244549529u, +199761971u, +2744895408u, +3195315331u, +2124618519u, +3261045496u, +985339699u, +3385585455u, +1545740710u, +3636652160u, +2167020081u, +1207897204u, +28752417u, +2895834146u, +3640845375u, +3750293073u, +548997850u, +4207814196u, +4183030708u, +2462810989u, +3929965401u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; IsAlive(farmhashmk::Hash32WithSeed(data, len++, SEED)); IsAlive(farmhashmk::Hash32(data, len++)); IsAlive(farmhashmk::Hash32(data, len++)); len -= 3; return alive > 0; } +Check(farmhashmk::Hash32WithSeed(data + offset, len, SEED)); +Check(farmhashmk::Hash32(data + offset, len)); + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashmkTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +cout << farmhashmk::Hash32WithSeed(data + offset, len, SEED) << "u," << endl; +cout << farmhashmk::Hash32(data + offset, len) << "u," << endl; +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashmkTest + +#if TESTING + +static int farmhashmkTestResult = farmhashmkTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashmkTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashmkTest::Dump(0, i); + } + farmhashmkTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashnaTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +1140953930u, 861465670u, +3277735313u, 2681724312u, +2598464059u, 797982799u, +890626835u, 800175912u, +2603993599u, 921001710u, +1410420968u, 2134990486u, +3283896453u, 1867689945u, +2914424215u, 2244477846u, +255297188u, 2992121793u, +1110588164u, 4186314283u, +161451183u, 3943596029u, +4019337850u, 452431531u, +283198166u, 2741341286u, +3379021470u, 2557197665u, +299850021u, 2532580744u, +452473466u, 1706958772u, +1298374911u, 3099673830u, +2199864459u, 3696623795u, +236935126u, 2976578695u, +4055299123u, 3281581178u, +1053458494u, 1882212500u, +2305012065u, 2169731866u, +3456121707u, 275903667u, +458884671u, 3033004529u, +3058973506u, 2379411653u, +1898235244u, 1402319660u, +2700149065u, 2699376854u, +147814787u, 720739346u, +2433714046u, 4222949502u, +4220361840u, 1712034059u, +3425469811u, 3690733394u, +4148372108u, 1330324210u, +594028478u, 2921867846u, +1635026870u, 192883107u, +780716741u, 1728752234u, +3280331829u, 326029180u, +3969463346u, 1436364519u, +393215742u, 3349570000u, +3824583307u, 1612122221u, +2859809759u, 3808705738u, +1379537552u, 1646032583u, +2233466664u, 1432476832u, +4023053163u, 2650381482u, +2052294713u, 3552092450u, +1628777059u, 1499109081u, +3476440786u, 3829307897u, +2960536756u, 1554038301u, +1145519619u, 3190844552u, +2902102606u, 3600725550u, +237495366u, 540224401u, +65721842u, 489963606u, +1448662590u, 397635823u, +1596489240u, 1562872448u, +1790705123u, 2128624475u, +180854224u, 2604346966u, +1435705557u, 1262831810u, +155445229u, 1672724608u, +1669465176u, 1341975128u, +663607706u, 2077310004u, +3610042449u, 1911523866u, +1043692997u, 1454396064u, +2563776023u, 294527927u, +1099072299u, 1389770549u, +703505868u, 678706990u, +2952353448u, 2026137563u, +3603803785u, 629449419u, +1933894405u, 3043213226u, +226132789u, 2489287368u, +1552847036u, 645684964u, +3828089804u, 3632594520u, +187883449u, 230403464u, +3151491850u, 3272648435u, +3729087873u, 1303930448u, +2002861219u, 165370827u, +916494250u, 1230085527u, +3103338579u, 3064290191u, +3807265751u, 3628174014u, +231181488u, 851743255u, +2295806711u, 1781190011u, +2988893883u, 1554380634u, +1142264800u, 3667013118u, +1968445277u, 315203929u, +2638023604u, 2290487377u, +732137533u, 1909203251u, +440398219u, 1891630171u, +1380301172u, 1498556724u, +4072067757u, 4165088768u, +4204318635u, 441430649u, +3931792696u, 197618179u, +956300927u, 914413116u, +3010839769u, 2837339569u, +2148126371u, 1913303225u, +3074915312u, 3117299654u, +4139181436u, 2993479124u, +3178848746u, 1357272220u, +1438494951u, 507436733u, +667183474u, 2084369203u, +3854939912u, 1413396341u, +126024219u, 146044391u, +1016656857u, 3022024459u, +3254014218u, 429095991u, +165589978u, 1578546616u, +985653208u, 1718653828u, +623071693u, 366414107u, +249776086u, 1207522198u, +3047342438u, 2991127487u, +3120876698u, 1684583131u, +46987739u, 1157614300u, +863214540u, 1087193030u, +199124911u, 520792961u, +3614377032u, 586863115u, +3331828431u, 1013201099u, +1716848157u, 4033596884u, +1164298657u, 4140791139u, +1146169032u, 1434258493u, +3824360466u, 3242407770u, +3725511003u, 232064808u, +872586426u, 762243036u, +2736953692u, 816692935u, +512845449u, 3748861010u, +2266795890u, 3781899767u, +4290630595u, 517646945u, +22638523u, 648000590u, +959214578u, 558910384u, +1283799121u, 3047062993u, +1024246061u, 4027776454u, +3544509313u, 622325861u, +834785312u, 382936554u, +411505255u, 1973395102u, +1825135056u, 2725923798u, +580988377u, 2826990641u, +3474970689u, 1029055034u, +812546227u, 2506885666u, +2584372201u, 1758123094u, +589567754u, 325737734u, +345313518u, 2022370576u, +3886113119u, 3338548567u, +257578986u, 3698087965u, +1776047957u, 1771384107u, +3604937815u, 3198590202u, +2305332220u, 191910725u, +4232136669u, 427759438u, +4244322689u, 542201663u, +3315355162u, 2135941665u, +556609672u, 45845311u, +1175961330u, 3948351189u, +23075771u, 3252374102u, +1634635545u, 4151937410u, +713127376u, 1467786451u, +663013031u, 3444053918u, +2638154051u, 810082938u, +3077742128u, 1062268187u, +2115441882u, 4081398201u, +3735739145u, 2794294783u, +2335576331u, 2560479831u, +1379288194u, 4225182569u, +2442302747u, 3948961926u, +3958366652u, 3067277639u, +3667516477u, 1709989541u, +1516711748u, 2339636583u, +4188504038u, 59581167u, +2725013602u, 3639843023u, +2658147000u, 2643979752u, +3758739543u, 4189944477u, +2470483982u, 877580602u, +2995362413u, 118817200u, +3252925478u, 2062343506u, +3981838403u, 3762572073u, +1231633714u, 4168280671u, +2931588131u, 3284356565u, +1129162571u, 732225574u, +4173605289u, 1407328702u, +1677744031u, 3532596884u, +3232041815u, 1652884780u, +2256541290u, 3459463480u, +3740979556u, 259034107u, +2227121257u, 1426140634u, +3606709555u, 3424793077u, +315836068u, 3200749877u, +1386256573u, 24035717u, +2982018998u, 1811050648u, +234531934u, 1115203611u, +1598686658u, 3146815575u, +1603559457u, 323296368u, +2632963283u, 1778459926u, +739944537u, 579625482u, +3486330348u, 492621815u, +1231665285u, 2457048126u, +3903349120u, 389846205u, +3355404249u, 3275550588u, +1052645068u, 862072556u, +2834153464u, 1481069623u, +2657392572u, 4279236653u, +1688445808u, 701920051u, +3740748788u, 3388062747u, +1873358321u, 2152785640u, +883382081u, 1005815394u, +1020177209u, 734239551u, +2371453141u, 100326520u, +3488500412u, 1279682138u, +2610427744u, 49703572u, +3026361211u, 605900428u, +302392721u, 2509302188u, +1416453607u, 2815915291u, +1862819968u, 519710058u, +2450888314u, 4017598378u, +937074653u, 3035635454u, +1590230729u, 3268013438u, +2710029305u, 12886044u, +3711259084u, 2627383582u, +3895772404u, 648534979u, +260307902u, 855990313u, +3669691805u, 263366740u, +2938543471u, 414331688u, +3080542944u, 3405007814u, +3565059103u, 1190977418u, +390836981u, 1606450012u, +2649808239u, 2514169310u, +2747519432u, 4129538640u, +1721522849u, 492099164u, +792990594u, 3625507637u, +2271095827u, 2993032712u, +2302363854u, 4013112951u, +1111617969u, 2183845740u, +795918276u, 1116991810u, +3110898804u, 3963062126u, +2737064702u, 462795667u, +937372240u, 1343017609u, +1091041189u, 2790555455u, +277024217u, 25485284u, +1166522068u, 1623631848u, +241727183u, 2836158787u, +3112996740u, 573836428u, +2721658101u, 1937681565u, +4175169209u, 3190765433u, +1970000788u, 1668258120u, +114616703u, 954762543u, +199237753u, 4094644498u, +2522281978u, 732086117u, +1756889687u, 2936126607u, +2437031370u, 4103143808u, +3883389541u, 3171090854u, +2483004780u, 1927385370u, +2360538162u, 2740855009u, +4241185118u, 1492209542u, +1672737098u, 2148675559u, +1789864670u, 2434313103u, +2319172611u, 2760941207u, +2636210123u, 1338083267u, +1128080590u, 822806371u, +1199583556u, 314727461u, +1335160250u, 2084630531u, +1156261526u, 316766066u, +112090465u, 3129033323u, +2746885618u, 636616055u, +2582210744u, 1721064910u, +3468394263u, 470463518u, +2076016059u, 408721884u, +2121041886u, 378460278u, +1915948002u, 357324860u, +2301682622u, 2691859523u, +1869756364u, 2429314418u, +2193146527u, 1185564327u, +2614088922u, 1975527044u, +919067651u, 2855948894u, +3662539576u, 1943802836u, +3529473373u, 1490330107u, +366036094u, 3384241033u, +4276268604u, 448403661u, +4271796078u, 1910401882u, +3077107698u, 299427366u, +2035665349u, 3201262636u, +3738454258u, 2554452696u, +3588997135u, 3363895827u, +1267505995u, 1852004679u, +2237827073u, 2803250686u, +3468044908u, 2143572850u, +1728158656u, 1022551180u, +1996680960u, 839529273u, +2400647871u, 2201096054u, +3606433628u, 2597259793u, +3544595875u, 3909443124u, +819278607u, 3447346709u, +806136613u, 2711436388u, +3656063205u, 837475154u, +694525336u, 4070212073u, +4011303412u, 1068395209u, +438095290u, 484603494u, +2673730227u, 737767009u, +642310823u, 3914002299u, +308425103u, 268427550u, +1334387085u, 4069797497u, +4280783219u, 2914011058u, +4243643405u, 2849988118u, +2504230175u, 1817156623u, +2804200483u, 3406991497u, +2948254999u, 2102063419u, +1071272117u, 514889942u, +571972433u, 1246595599u, +1735616066u, 1539151988u, +1230831543u, 277987182u, +4269526481u, 991511607u, +95237878u, 2005032160u, +1291113144u, 626619670u, +3560835907u, 164940926u, +1433635018u, 116647396u, +3039097112u, 2868163232u, +1141645918u, 1764165478u, +881378302u, 2159170082u, +2953647681u, 1011320066u, +184856151u, 1723308975u, +336034862u, 2017579106u, +1476681709u, 147523618u, +3896252223u, 2264728166u, +944743644u, 1694443528u, +2690700128u, 1947321519u, +735478508u, 4058183171u, +260177668u, 505662155u, +2391691262u, 1920739747u, +3216960415u, 1898176786u, +3722741628u, 1511077569u, +449636564u, 983350414u, +2580237367u, 2055059789u, +1103819072u, 2089123665u, +3873755579u, 2718467458u, +3124338704u, 3204250304u, +2475035432u, 1120017626u, +3873758287u, 1982999824u, +2950794582u, 780634378u, +2842141483u, 4029205195u, +1656892865u, 3330993377u, +80890710u, 1953796601u, +3873078673u, 136118734u, +2317676604u, 4199091610u, +1864448181u, 3063437608u, +1699452298u, 1403506686u, +1513069466u, 2348491299u, +4273657745u, 4055855649u, +1805475756u, 2562064338u, +973124563u, 4197091358u, +172861513u, 2858726767u, +4271866024u, 3071338162u, +3590386266u, 2328277259u, +1096050703u, 1189614342u, +459509140u, 771592405u, +817999971u, 3740825152u, +520400189u, 1941874618u, +185232757u, 4032960199u, +3928245258u, 3527721294u, +1301118856u, 752188080u, +3512945009u, 308584855u, +2105373972u, 752872278u, +3823368815u, 3760952096u, +4250142168u, 2565680167u, +3646354146u, 1259957455u, +1085857127u, 3471066607u, +38924274u, 3770488806u, +1083869477u, 3312508103u, +71956383u, 3738784936u, +3099963860u, 1255084262u, +4286969992u, 3621849251u, +1190908967u, 1831557743u, +2363435042u, 54945052u, +4059585566u, 4023974274u, +1788578453u, 3442180039u, +2534883189u, 2432427547u, +3909757989u, 731996369u, +4168347425u, 1356028512u, +2741583197u, 1280920000u, +312887059u, 3259015297u, +3946278527u, 4135481831u, +1281043691u, 1121403845u, +3312292477u, 1819941269u, +1741932545u, 3293015483u, +2127558730u, 713121337u, +2635469238u, 486003418u, +4015067527u, 2976737859u, +2108187161u, 927011680u, +1970188338u, 4177613234u, +1799789551u, 2118505126u, +4134691985u, 1958963937u, +1929210029u, 2555835851u, +2768832862u, 910892050u, +2567532373u, 4075249328u, +86689814u, 3726640307u, +1392137718u, 1240000030u, +4104757832u, 3026358429u, +313797689u, 1435798509u, +3101500919u, 1241665335u, +3573008472u, 3615577014u, +3767659003u, 3134294021u, +4063565523u, 2296824134u, +1541946015u, 3087190425u, +2693152531u, 2199672572u, +2123763822u, 1034244398u, +857839960u, 2515339233u, +2228007483u, 1628096047u, +2116502287u, 2502657424u, +2809830736u, 460237542u, +450205998u, 3646921704u, +3818199357u, 1808504491u, +1950698961u, 2069753399u, +3657033172u, 3734547671u, +4067859590u, 3292597295u, +1106466069u, 356742959u, +2469567432u, 3495418823u, +183440071u, 3248055817u, +3662626864u, 1750561299u, +3926138664u, 4088592524u, +567122118u, 3810297651u, +992181339u, 3384018814u, +3272124369u, 3177596743u, +320086295u, 2316548367u, +100741310u, 451656820u, +4086604273u, 3759628395u, +2553391092u, 1745659881u, +3650357479u, 2390172694u, +330172533u, 767377322u, +526742034u, 4102497288u, +2088767754u, 164402616u, +2482632320u, 2352347393u, +1873658044u, 3861555476u, +2751052984u, 1767810825u, +20037241u, 545143220u, +2594532522u, 472304191u, +3441135892u, 3323383489u, +258785117u, 2977745165u, +2781737565u, 2963590112u, +2756998822u, 207428029u, +2581558559u, 3824717027u, +1258619503u, 3472047571u, +2648427775u, 2360400900u, +2393763818u, 2332399088u, +3932701729u, 884421165u, +1396468647u, 1377764574u, +4061795938u, 1559119087u, +3343596838u, 3604258095u, +1435134775u, 1099809675u, +908163739u, 1418405656u, +368446627u, 3741651161u, +3374512975u, 3542220540u, +3244772570u, 200009340u, +3198975081u, 2521038253u, +4081637863u, 337070226u, +3235259030u, 3897262827u, +736956644u, 641040550u, +644850146u, 1306761320u, +4219448634u, 193750500u, +3293278106u, 1383997679u, +1242645122u, 4109252858u, +450747727u, 3716617561u, +362725793u, 2252520167u, +3377483696u, 1788337208u, +8130777u, 3226734120u, +759239140u, 1012411364u, +1658628529u, 2911512007u, +1002580201u, 1681898320u, +3039016929u, 4294520281u, +367022558u, 3071359622u, +3205848570u, 152989999u, +3839042136u, 2357687350u, +4273132307u, 3898950547u, +1176841812u, 1314157485u, +75443951u, 1027027239u, +1858986613u, 2040551642u, +36574105u, 2603059541u, +3456147251u, 2137668425u, +4077477194u, 3565689036u, +491832241u, 363703593u, +2579177168u, 3589545214u, +265993036u, 1864569342u, +4149035573u, 3189253455u, +1072259310u, 3153745937u, +923017956u, 490608221u, +855846773u, 845706553u, +1018226240u, 1604548872u, +3833372385u, 3287246572u, +2757959551u, 2452872151u, +1553870564u, 1713154780u, +2649450292u, 500120236u, +84251717u, 661869670u, +1444911517u, 2489716881u, +2810524030u, 1561519055u, +3884088359u, 2509890699u, +4247155916u, 1005636939u, +3224066062u, 2774151984u, +2035978240u, 2514910366u, +1478837908u, 3144450144u, +2107011431u, 96459446u, +3587732908u, 2389230590u, +3287635953u, 250533792u, +1235983679u, 4237425634u, +3704645833u, 3882376657u, +2976369049u, 1187061987u, +276949224u, 4100839753u, +1698347543u, 1629662314u, +1556151829u, 3784939568u, +427484362u, 4246879223u, +3155311770u, 4285163791u, +1693376813u, 124492786u, +1858777639u, 3476334357u, +1941442701u, 1121980173u, +3485932087u, 820852908u, +358032121u, 2511026735u, +1873607283u, 2556067450u, +2248275536u, 1528632094u, +1535473864u, 556796152u, +1499201704u, 1472623890u, +1526518503u, 3692729434u, +1476438092u, 2913077464u, +335109599u, 2167614601u, +4121131078u, 3158127917u, +3051522276u, 4046477658u, +2857717851u, 1863977403u, +1341023343u, 692059110u, +1802040304u, 990407433u, +3285847572u, 319814144u, +561105582u, 1540183799u, +4052924496u, 2926590471u, +2244539806u, 439121871u, +3317903224u, 3178387550u, +4265214507u, 82077489u, +1978918971u, 4279668976u, +128732476u, 2853224222u, +464407878u, 4190838199u, +997819001u, 3250520802u, +2330081301u, 4095846095u, +733509243u, 1583801700u, +722314527u, 3552883023u, +1403784280u, 432327540u, +1877837196u, 3912423882u, +505219998u, 696031431u, +908238873u, 4189387259u, +8759461u, 2540185277u, +3385159748u, 381355877u, +2519951681u, 1679786240u, +2019419351u, 4051584612u, +1933923923u, 3768201861u, +1670133081u, 3454981037u, +700836153u, 1675560450u, +371560700u, 338262316u, +847351840u, 2222395828u, +3130433948u, 405251683u, +3037574880u, 184098830u, +453340528u, 1385561439u, +2224044848u, 4071581802u, +1431235296u, 5570097u, +570114376u, 2287305551u, +2272418128u, 803575837u, +3943113491u, 414959787u, +708083137u, 2452657767u, +4019147902u, 3841480082u, +3791794715u, 2965956183u, +2763690963u, 2350937598u, +3424361375u, 779434428u, +1274947212u, 686105485u, +3426668051u, 3692865672u, +3057021940u, 2285701422u, +349809124u, 1379278508u, +3623750518u, 215970497u, +1783152480u, 823305654u, +216118434u, 1787189830u, +3692048450u, 2272612521u, +3032187389u, 4159715581u, +1388133148u, 1611772864u, +2544383526u, 552925303u, +3420960112u, 3198900547u, +3503230228u, 2603352423u, +2318375898u, 4064071435u, +3006227299u, 4194096960u, +1283392422u, 1510460996u, +174272138u, 3671038966u, +1775955687u, 1719108984u, +1763892006u, 1385029063u, +4083790740u, 406757708u, +684087286u, 531310503u, +3329923157u, 3492083607u, +1059031410u, 3037314475u, +3105682208u, 3382290593u, +2292208503u, 426380557u, +97373678u, 3842309471u, +777173623u, 3241407531u, +303065016u, 1477104583u, +4234905200u, 2512514774u, +2649684057u, 1397502982u, +1802596032u, 3973022223u, +2543566442u, 3139578968u, +3193669211u, 811750340u, +4013496209u, 567361887u, +4169410406u, 3622282782u, +3403136990u, 2540585554u, +895210040u, 3862229802u, +1145435213u, 4146963980u, +784952939u, 943914610u, +573034522u, 464420660u, +2356867109u, 3054347639u, +3985088434u, 1911188923u, +583391304u, 176468511u, +2990150068u, 2338031599u, +519948041u, 3181425568u, +496106033u, 4110294665u, +2736756930u, 1196757691u, +1089679033u, 240953857u, +3399092928u, 4040779538u, +2843673626u, 240495962u, +3017658263u, 3828377737u, +4243717901u, 2448373688u, +2759616657u, 2246245780u, +308018483u, 4262383425u, +2731780771u, 328023017u, +2884443148u, 841480070u, +3188015819u, 4051263539u, +2298178908u, 2944209234u, +1372958390u, 4164532914u, +4074952232u, 1683612329u, +2155036654u, 1872815858u, +2041174279u, 2368092311u, +206775997u, 2283918569u, +645945606u, 115406202u, +4206471368u, 3923500892u, +2217060665u, 350160869u, +706531239u, 2824302286u, +509981657u, 1469342315u, +140980u, 1891558063u, +164887091u, 3094962711u, +3437115622u, 13327420u, +422986366u, 330624974u, +3630863408u, 2425505046u, +824008515u, 3543885677u, +918718096u, 376390582u, +3224043675u, 3724791476u, +1837192976u, 2968738516u, +3424344721u, 3187805406u, +1550978788u, 1743089918u, +4251270061u, 645016762u, +3855037968u, 1928519266u, +1373803416u, 2289007286u, +1889218686u, 1610271373u, +3059200728u, 2108753646u, +582042641u, 812347242u, +3188172418u, 191994904u, +1343511943u, 2247006571u, +463291708u, 2697254095u, +1534175504u, 1106275740u, +622521957u, 917121602u, +4095777215u, 3955972648u, +3852234638u, 2845309942u, +3299763344u, 2864033668u, +2554947496u, 799569078u, +2551629074u, 1102873346u, +2661022773u, 2006922227u, +2900438444u, 1448194126u, +1321567432u, 1983773590u, +1237256330u, 3449066284u, +1691553115u, 3274671549u, +4271625619u, 2741371614u, +3285899651u, 786322314u, +1586632825u, 564385522u, +2530557509u, 2974240289u, +1244759631u, 3263135197u, +3592389776u, 3570296884u, +2749873561u, 521432811u, +987586766u, 3206261120u, +1327840078u, 4078716491u, +1753812954u, 976892272u, +1827135136u, 1781944746u, +1328622957u, 1015377974u, +3439601008u, 2209584557u, +2482286699u, 1109175923u, +874877499u, 2036083451u, +483570344u, 1091877599u, +4190721328u, 1129462471u, +640035849u, 1867372700u, +920761165u, 3273688770u, +1623777358u, 3389003793u, +3241132743u, 2734783008u, +696674661u, 2502161880u, +1646071378u, 1164309901u, +350411888u, 1978005963u, +2253937037u, 7371540u, +989577914u, 3626554867u, +3214796883u, 531343826u, +398899695u, 1145247203u, +1516846461u, 3656006011u, +529303412u, 3318455811u, +3062828129u, 1696355359u, +3698796465u, 3155218919u, +1457595996u, 3191404246u, +1395609912u, 2917345728u, +1237411891u, 1854985978u, +1091884675u, 3504488111u, +3109924189u, 1628881950u, +3939149151u, 878608872u, +778235395u, 1052990614u, +903730231u, 2069566979u, +2437686324u, 3163786257u, +2257884264u, 2123173186u, +939764916u, 2933010098u, +1235300371u, 1256485167u, +1950274665u, 2180372319u, +2648400302u, 122035049u, +1883344352u, 2083771672u, +3712110541u, 321199441u, +1896357377u, 508560958u, +3066325351u, 2770847216u, +3177982504u, 296902736u, +1486926688u, 456842861u, +601221482u, 3992583643u, +2794121515u, 1533934172u, +1706465470u, 4281971893u, +2557027816u, 900741486u, +227175484u, 550595824u, +690918144u, 2825943628u, +90375300u, 300318232u, +1985329734u, 1440763373u, +3670603707u, 2533900859u, +3253901179u, 542270815u, +3677388841u, 307706478u, +2570910669u, 3320103693u, +1273768482u, 1216399252u, +1652924805u, 1043647584u, +1120323676u, 639941430u, +325675502u, 3652676161u, +4241680335u, 1545838362u, +1991398008u, 4100211814u, +1097584090u, 3262252593u, +2254324292u, 1765019121u, +4060211241u, 2315856188u, +3704419305u, 411263051u, +238929055u, 3540688404u, +3094544537u, 3250435765u, +3460621305u, 1967599860u, +2016157366u, 847389916u, +1659615591u, 4020453639u, +901109753u, 2682611693u, +1661364280u, 177155177u, +3210561911u, 3802058181u, +797089608u, 3286110054u, +2110358240u, 1353279028u, +2479975820u, 471725410u, +2219863904u, 3623364733u, +3167128228u, 1052188336u, +3656587111u, 721788662u, +3061255808u, 1615375832u, +924941453u, 2547780700u, +3328169224u, 1310964134u, +2701956286u, 4145497671u, +1421461094u, 1221397398u, +1589183618u, 1492533854u, +449740816u, 2686506989u, +3035198924u, 1682886232u, +2529760244u, 3342031659u, +1235084019u, 2151665147u, +2315686577u, 3282027660u, +1140138691u, 2754346599u, +2091754612u, 1178454681u, +4226896579u, 2942520471u, +2122168506u, 3751680858u, +3213794286u, 2601416506u, +4142747914u, 3951404257u, +4243249649u, 748595836u, +4004834921u, 238887261u, +1927321047u, 2217148444u, +205977665u, 1885975275u, +186020771u, 2367569534u, +2941662631u, 2608559272u, +3342096731u, 741809437u, +1962659444u, 3539886328u, +3036596491u, 2282550094u, +2366462727u, 2748286642u, +2144472852u, 1390394371u, +1257385924u, 2205425874u, +2119055686u, 46865323u, +3597555910u, 3188438773u, +2372320753u, 3641116924u, +3116286108u, 2680722658u, +3371014971u, 2058751609u, +2966943726u, 2345078707u, +2330535244u, 4013841927u, +1169588594u, 857915866u, +1875260989u, 3175831309u, +3193475664u, 1955181430u, +923161569u, 4068653043u, +776445899u, 954196929u, +61509556u, 4248237857u, +3808667664u, 581227317u, +2893240187u, 4159497403u, +4212264930u, 3973886195u, +2077539039u, 851579036u, +2957587591u, 772351886u, +1173659554u, 946748363u, +2794103714u, 2094375930u, +4234750213u, 3671645488u, +2614250782u, 2620465358u, +3122317317u, 2365436865u, +3393973390u, 523513960u, +3645735309u, 2766686992u, +2023960931u, 2312244996u, +1875932218u, 3253711056u, +3622416881u, 3274929205u, +612094988u, 1555465129u, +2114270406u, 3553762793u, +1832633644u, 1087551556u, +3306195841u, 1702313921u, +3675066046u, 1735998785u, +1690923980u, 1482649756u, +1171351291u, 2043136409u, +1962596992u, 461214626u, +3278253346u, 1392428048u, +3744621107u, 1028502697u, +3991171462u, 1014064003u, +3642345425u, 3186995039u, +6114625u, 3359104346u, +414856965u, 2814387514u, +3583605071u, 2497896367u, +1024572712u, 1927582962u, +2892797583u, 845302635u, +328548052u, 1523379748u, +3392622118u, 1347167673u, +1012316581u, 37767602u, +2647726017u, 1070326065u, +2075035198u, 4202817168u, +2502924707u, 2612406822u, +2187115553u, 1180137213u, +701024148u, 1481965992u, +3223787553u, 2083541843u, +203230202u, 3876887380u, +1334816273u, 2870251538u, +2186205850u, 3985213979u, +333533378u, 806507642u, +1010064531u, 713520765u, +3084131515u, 2637421459u, +1703168933u, 1517562266u, +4089081247u, 3231042924u, +3079916123u, 3154574447u, +2253948262u, 1725190035u, +2452539325u, 1343734533u, +213706059u, 2519409656u, +108055211u, 2916327746u, +587001593u, 1917607088u, +4202913084u, 926304016u, +469255411u, 4042080256u, +3498936874u, 246692543u, +495780578u, 438717281u, +2259272650u, 4011324645u, +2836854664u, 2317249321u, +946828752u, 1280403658u, +1905648354u, 2034241661u, +774652981u, 1285694082u, +2200307766u, 2158671727u, +1135162148u, 232040752u, +397012087u, 1717527689u, +1720414106u, 918797022u, +2580119304u, 3568069742u, +2904461070u, 3893453420u, +973817938u, 667499332u, +3785870412u, 2088861715u, +1565179401u, 600903026u, +591806775u, 3512242245u, +997964515u, 2339605347u, +1134342772u, 3234226304u, +4084179455u, 302315791u, +2445626811u, 2590372496u, +345572299u, 2274770442u, +3600587867u, 3706939009u, +1430507980u, 2656330434u, +1079209397u, 2122849632u, +1423705223u, 3826321888u, +3683385276u, 1057038163u, +1242840526u, 3987000643u, +2398253089u, 1538190921u, +1295898647u, 3570196893u, +3065138774u, 3111336863u, +2524949549u, 4203895425u, +3025864372u, 968800353u, +1023721001u, 3763083325u, +526350786u, 635552097u, +2308118370u, 2166472723u, +2196937373u, 2643841788u, +3040011470u, 4010301879u, +2782379560u, 3474682856u, +4201389782u, 4223278891u, +1457302296u, 2251842132u, +1090062008u, 3188219189u, +292733931u, 1424229089u, +1590782640u, 1365212370u, +3975957073u, 3982969588u, +2927147928u, 1048291071u, +2766680094u, 884908196u, +35237839u, 2221180633u, +2490333812u, 4098360768u, +4029081103u, 3490831871u, +2392516272u, 3455379186u, +3948800722u, 335456628u, +2105117968u, 4181629008u, +1044201772u, 3335754111u, +540133451u, 3313113759u, +3786107905u, 2627207327u, +3540337875u, 3473113388u, +3430536378u, 2514123129u, +2124531276u, 3872633376u, +3272957388u, 3501994650u, +2418881542u, 487365389u, +3877672368u, 1512866656u, +3486531087u, 2102955203u, +1136054817u, 3004241477u, +1549075351u, 1302002008u, +3936430045u, 2258587644u, +4109233936u, 3679809321u, +3467083076u, 2484463221u, +1594979755u, 529218470u, +3527024461u, 1147434678u, +106799023u, 1823161970u, +1704656738u, 1675883700u, +3308746763u, 1875093248u, +1352868568u, 1898561846u, +2508994984u, 3177750780u, +4217929592u, 400784472u, +80090315u, 3564414786u, +3841585648u, 3379293868u, +160353261u, 2413172925u, +2378499279u, 673436726u, +1505702418u, 1330977363u, +1853298225u, 3201741245u, +2135714208u, 4069554166u, +3715612384u, 3692488887u, +3680311316u, 4274382900u, +914186796u, 2264886523u, +3869634032u, 1254199592u, +1131020455u, 194781179u, +429923922u, 2763792336u, +2052895198u, 3997373194u, +3440090658u, 2165746386u, +1575500242u, 3463310191u, +2064974716u, 3779513671u, +3106421434u, 880320527u, +3281914119u, 286569042u, +3909096631u, 122359727u, +1429837716u, 252230074u, +4111461225u, 762273136u, +93658514u, 2766407143u, +3623657004u, 3869801679u, +3925695921u, 2390397316u, +2499025338u, 2741806539u, +2507199021u, 1659221866u, +361292116u, 4048761557u, +3797133396u, 1517903247u, +3121647246u, 3884308578u, +1697201500u, 1558800262u, +4150812360u, 3161302278u, +2610217849u, 641564641u, +183814518u, 2075245419u, +611996508u, 2223461433u, +329123979u, 121860586u, +860985829u, 1137889144u, +4018949439u, 2904348960u, +947795261u, 1992594155u, +4255427501u, 2281583851u, +2892637604u, 1478186924u, +3050771207u, 2767035539u, +373510582u, 1963520320u, +3763848370u, 3756817798u, +627269409u, 1806905031u, +1814444610u, 3646665053u, +1822693920u, 278515794u, +584050483u, 4142579188u, +2149745808u, 3193071606u, +1179706341u, 2693495182u, +3259749808u, 644172091u, +880509048u, 3340630542u, +3365160815u, 2384445068u, +3053081915u, 2840648309u, +1986990122u, 1084703471u, +2370410550u, 1627743573u, +2244943480u, 4057483496u, +2611595995u, 2470013639u, +4024732359u, 3987190386u, +873421687u, 2447660175u, +3226583022u, 767655877u, +2528024413u, 1962070688u, +1233635843u, 2163464207u, +659054446u, 854207134u, +258410943u, 4197831420u, +2515400215u, 3100476924u, +1961549594u, 2219491151u, +3997658851u, 163850514u, +470325051u, 2598261204u, +3052145580u, 59836528u, +1376188597u, 966733415u, +850667549u, 3622479237u, +1083731990u, 1525777459u, +4005126532u, 1428155540u, +2781907007u, 943739431u, +1493961005u, 2839096988u, +2000057832u, 1941829603u, +1901484772u, 939810041u, +3377407371u, 3090115837u, +3310840540u, 2068409688u, +3261383939u, 2212130277u, +2594774045u, 2912652418u, +4179816101u, 3534504531u, +3349254805u, 2796552902u, +1385421283u, 4259908631u, +3714780837u, 3070073945u, +3372846298u, 3835884044u, +3047965714u, 3009018735u, +744091167u, 1861124263u, +2764936304u, 1338171648u, +4222019554u, 1395200692u, +1371426007u, 3338031581u, +2525665319u, 4196233786u, +2332743921u, 1474702008u, +2274266301u, 4255175517u, +2290169528u, 1793910997u, +2188254024u, 354202001u, +3864458796u, 4280290498u, +1554419340u, 1733094688u, +2010552302u, 1561807039u, +664313606u, 2548990879u, +1084699349u, 3233936866u, +973895284u, 2386881969u, +1831995860u, 2961465052u, +1428704144u, 3269904970u, +231648253u, 2602483763u, +4125013173u, 3319187387u, +3347011944u, 1892898231u, +4019114049u, 868879116u, +4085937045u, 2378411019u, +1072588531u, 3547435717u, +2208070766u, 1069899078u, +3142980597u, 2337088907u, +1593338562u, 919414554u, +688077849u, 3625708135u, +1472447348u, 1947711896u, +3953006207u, 877438080u, +845995820u, 3150361443u, +3053496713u, 2484577841u, +224271045u, 2914958001u, +2682612949u, 806655563u, +2436224507u, 1907729235u, +2920583824u, 1251814062u, +2070814520u, 4034325578u, +497847539u, 2714317144u, +385182008u, 640855184u, +1327075087u, 1062468773u, +1757405994u, 1374270191u, +4263183176u, 3041193150u, +1037871524u, 3633173991u, +4231821821u, 2830131945u, +3505072908u, 2830570613u, +4195208715u, 575398021u, +3992840257u, 3691788221u, +1949847968u, 2999344380u, +3183782163u, 3723754342u, +759716128u, 3284107364u, +1714496583u, 15918244u, +820509475u, 2553936299u, +2201876606u, 4237151697u, +2605688266u, 3253705097u, +1008333207u, 712158730u, +1722280252u, 1933868287u, +4152736859u, 2097020806u, +584426382u, 2836501956u, +2522777566u, 1996172430u, +2122199776u, 1069285218u, +1474209360u, 690831894u, +107482532u, 3695525410u, +670591796u, 768977505u, +2412057331u, 3647886687u, +3110327607u, 1072658422u, +379861934u, 1557579480u, +4124127129u, 2271365865u, +3880613089u, 739218494u, +547346027u, 388559045u, +3147335977u, 176230425u, +3094853730u, 2554321205u, +1495176194u, 4093461535u, +3521297827u, 4108148413u, +1913727929u, 1177947623u, +1911655402u, 1053371241u, +3265708874u, 1266515850u, +1045540427u, 3194420196u, +3717104621u, 1144474110u, +1464392345u, 52070157u, +4144237690u, 3350490823u, +4166253320u, 2747410691u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; { uint64_t h = farmhashna::Hash64WithSeeds(data, len++, SEED0, SEED1); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashna::Hash64WithSeed(data, len++, SEED); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashna::Hash64(data, len++); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } len -= 3; return alive > 0; } +{ uint64_t h = farmhashna::Hash64WithSeeds(data + offset, len, SEED0, SEED1); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashna::Hash64WithSeed(data + offset, len, SEED); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashna::Hash64(data + offset, len); Check(h >> 32); Check((h << 32) >> 32); } + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashnaTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +{ uint64_t h = farmhashna::Hash64WithSeeds(data + offset, len, SEED0, SEED1); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashna::Hash64WithSeed(data + offset, len, SEED); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashna::Hash64(data + offset, len); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashnaTest + +#if TESTING + +static int farmhashnaTestResult = farmhashnaTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashnaTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashnaTest::Dump(0, i); + } + farmhashnaTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashntTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +2681724312u, +797982799u, +921001710u, +2134990486u, +2244477846u, +2992121793u, +3943596029u, +452431531u, +2557197665u, +2532580744u, +3099673830u, +3696623795u, +3281581178u, +1882212500u, +275903667u, +3033004529u, +1402319660u, +2699376854u, +4222949502u, +1712034059u, +1330324210u, +2921867846u, +1728752234u, +326029180u, +3349570000u, +1612122221u, +1646032583u, +1432476832u, +3552092450u, +1499109081u, +1554038301u, +3190844552u, +540224401u, +489963606u, +1562872448u, +2128624475u, +1262831810u, +1672724608u, +2077310004u, +1911523866u, +294527927u, +1389770549u, +2026137563u, +629449419u, +2489287368u, +645684964u, +230403464u, +3272648435u, +165370827u, +1230085527u, +3628174014u, +851743255u, +1554380634u, +3667013118u, +2290487377u, +1909203251u, +1498556724u, +4165088768u, +197618179u, +914413116u, +1913303225u, +3117299654u, +1357272220u, +507436733u, +1413396341u, +146044391u, +429095991u, +3056862311u, +366414107u, +2293458109u, +1684583131u, +1170404994u, +520792961u, +1577421232u, +4033596884u, +4229339322u, +3242407770u, +2649785113u, +816692935u, +3555213933u, +517646945u, +2180594090u, +3047062993u, +2391606125u, +382936554u, +788479970u, +2826990641u, +3167748333u, +1758123094u, +389974094u, +3338548567u, +2583576230u, +3198590202u, +4155628142u, +542201663u, +2856634168u, +3948351189u, +4194218315u, +1467786451u, +2743592929u, +1062268187u, +3810665822u, +2560479831u, +997658837u, +3067277639u, +1211737169u, +59581167u, +1389679610u, +4189944477u, +100876854u, +2062343506u, +3088828656u, +3284356565u, +3130054947u, +3532596884u, +3887208531u, +259034107u, +3233195759u, +3200749877u, +760633989u, +1115203611u, +1516407838u, +1778459926u, +2146672889u, +2457048126u, +2217471853u, +862072556u, +3745267835u, +701920051u, +581695350u, +1410111809u, +3326135446u, +2187968410u, +4267859263u, +479241367u, +2868987960u, +704325635u, +1418509533u, +735688735u, +3283299459u, +813690332u, +1439630796u, +3195309868u, +1616408198u, +3254795114u, +2799925823u, +3929484338u, +1798536177u, +4205965408u, +1499475160u, +4247675634u, +3779953975u, +785893184u, +2778575413u, +1160134629u, +823113169u, +4116162021u, +4167766971u, +2487440590u, +4004655503u, +4044418876u, +1462554406u, +2011102035u, +4265993528u, +576405853u, +4038839101u, +2425317635u, +1401013391u, +3062418115u, +3167030094u, +2602636307u, +4264167741u, +4017058800u, +1029665228u, +4036354071u, +2670703363u, +688472265u, +1054670286u, +338058159u, +1539305024u, +146827036u, +4060134777u, +2502815838u, +1603444633u, +2448966429u, +3891353218u, +1082330589u, +201837927u, +2848283092u, +883849006u, +1982110346u, +541496720u, +133643215u, +3847827123u, +4015671361u, +2849988118u, +3452457457u, +2102063419u, +3281002516u, +1539151988u, +1147951686u, +2005032160u, +2415262714u, +116647396u, +1029284767u, +2159170082u, +1919171906u, +2017579106u, +2473524405u, +1694443528u, +3671562289u, +505662155u, +1019936943u, +1511077569u, +773792826u, +2089123665u, +484732447u, +1120017626u, +2809286837u, +4029205195u, +1097806406u, +136118734u, +4017075736u, +1403506686u, +1516736273u, +2562064338u, +2984955003u, +3071338162u, +1923531348u, +771592405u, +2586632018u, +4032960199u, +2687561076u, +308584855u, +1692079268u, +2565680167u, +3674576684u, +3770488806u, +69201295u, +1255084262u, +3593730713u, +54945052u, +1939595371u, +2432427547u, +2295501078u, +1280920000u, +82177963u, +1121403845u, +2889101923u, +713121337u, +1747052377u, +927011680u, +4142246789u, +1958963937u, +1636932722u, +4075249328u, +2025886508u, +3026358429u, +1845587644u, +3615577014u, +1363253259u, +3087190425u, +341851980u, +2515339233u, +1276595523u, +460237542u, +4198897105u, +2069753399u, +4278599955u, +356742959u, +3735275001u, +1750561299u, +668829411u, +3384018814u, +4233785523u, +451656820u, +107312677u, +2390172694u, +1216645846u, +164402616u, +1689811113u, +1767810825u, +1397772514u, +3323383489u, +2986430557u, +207428029u, +2260498180u, +2360400900u, +1263709570u, +1377764574u, +4252610345u, +1099809675u, +2776960536u, +3542220540u, +3752806924u, +337070226u, +3267551635u, +1306761320u, +2220373824u, +4109252858u, +896322512u, +1788337208u, +1336556841u, +2911512007u, +3712582785u, +3071359622u, +2561488770u, +3898950547u, +536047554u, +2040551642u, +3528794619u, +3565689036u, +1197100813u, +1864569342u, +3329594980u, +490608221u, +1174785921u, +3287246572u, +2163330264u, +500120236u, +2520062970u, +1561519055u, +4042710240u, +2774151984u, +3160666939u, +96459446u, +1878067032u, +4237425634u, +2952135524u, +4100839753u, +1265237690u, +4246879223u, +834830418u, +3476334357u, +4277111759u, +2511026735u, +3065234219u, +556796152u, +198182691u, +2913077464u, +1535115487u, +4046477658u, +140762681u, +990407433u, +2198985327u, +2926590471u, +559702706u, +82077489u, +1096697687u, +4190838199u, +3046872820u, +1583801700u, +2185339100u, +3912423882u, +3703603898u, +2540185277u, +1446869792u, +4051584612u, +2719373510u, +1675560450u, +1996164093u, +405251683u, +2864244470u, +4071581802u, +2028708916u, +803575837u, +557660441u, +3841480082u, +255451671u, +779434428u, +3452203069u, +2285701422u, +1568745354u, +823305654u, +3184047862u, +4159715581u, +3160134214u, +3198900547u, +1566527339u, +4194096960u, +1496132623u, +1719108984u, +2584236470u, +531310503u, +3456882941u, +3382290593u, +467441309u, +3241407531u, +2540270567u, +1397502982u, +3348545480u, +811750340u, +1017047954u, +2540585554u, +3531646869u, +943914610u, +1903578924u, +1911188923u, +241574049u, +3181425568u, +3529565564u, +240953857u, +2964595704u, +3828377737u, +4260564140u, +4262383425u, +383233885u, +4051263539u, +919677938u, +1683612329u, +4204155962u, +2283918569u, +4153726847u, +350160869u, +1387233546u, +1891558063u, +740563169u, +330624974u, +2948665536u, +376390582u, +3799363969u, +3187805406u, +2263421398u, +1928519266u, +2746577402u, +2108753646u, +768287270u, +2247006571u, +212490675u, +917121602u, +2549835613u, +2864033668u, +3738062408u, +2006922227u, +2616619070u, +3449066284u, +431292293u, +786322314u, +1415970351u, +3263135197u, +2954777083u, +3206261120u, +2287507921u, +1781944746u, +4081586725u, +1109175923u, +1813855658u, +1129462471u, +1037031473u, +3389003793u, +3122687303u, +1164309901u, +3193251135u, +3626554867u, +3071568023u, +3656006011u, +1167681812u, +3155218919u, +2704165015u, +1854985978u, +1712976649u, +878608872u, +4155949943u, +3163786257u, +1626463554u, +1256485167u, +582664250u, +2083771672u, +804336148u, +2770847216u, +1674051445u, +3992583643u, +2966108111u, +900741486u, +4014551783u, +300318232u, +3517585534u, +542270815u, +760762191u, +1216399252u, +643179562u, +3652676161u, +2990167340u, +3262252593u, +2134299399u, +411263051u, +1342880802u, +1967599860u, +853593042u, +2682611693u, +850464484u, +3286110054u, +3842907484u, +3623364733u, +3693536939u, +1615375832u, +2318423400u, +4145497671u, +1728968857u, +2686506989u, +1502282913u, +2151665147u, +3651607391u, +1178454681u, +4146839064u, +2601416506u, +1448097974u, +238887261u, +4093725287u, +2367569534u, +679517009u, +3539886328u, +3086277222u, +1390394371u, +119173722u, +1766260771u, +751439914u, +215917713u, +2656990891u, +1570750352u, +3533987737u, +3576119563u, +963183826u, +3796810515u, +136547246u, +2592925324u, +427154472u, +1228758574u, +1464255968u, +2984611177u, +2001585786u, +1525438381u, +1348536411u, +2861338018u, +764077711u, +3785343245u, +457568934u, +4104954272u, +2381948487u, +3148473363u, +2180270337u, +1387729170u, +951677556u, +2721005055u, +66786703u, +1149351924u, +1895026827u, +3711056516u, +3638638708u, +2263003308u, +3448840877u, +225333538u, +3797521928u, +3262952567u, +2078619498u, +1178073973u, +3288261538u, +1496966875u, +2481012988u, +114945840u, +1632780103u, +2087949619u, +3787017905u, +2575395164u, +2971726178u, +3642087909u, +3894199764u, +203853421u, +425935223u, +3565833278u, +1748785729u, +580966986u, +2124704694u, +1107045577u, +1067532701u, +1406028344u, +18613994u, +3476683808u, +3762914298u, +1844996900u, +904215228u, +1118521573u, +3657647605u, +3136157065u, +2287683323u, +126005630u, +3555092974u, +49515858u, +1010661841u, +1902040126u, +1400735275u, +2771676666u, +2225229957u, +3454177594u, +2883475137u, +4144472319u, +1051332394u, +542648229u, +1669710469u, +553041029u, +584127807u, +2993670925u, +3587959456u, +1745399498u, +1404723176u, +1334333531u, +3239516985u, +1275954779u, +367320647u, +3684418197u, +4030809053u, +484559105u, +4255931645u, +4271715616u, +3171911678u, +928543347u, +2159512867u, +313902234u, +647086234u, +577214736u, +1130129573u, +995791646u, +1645086060u, +4122335794u, +1064648931u, +2752145076u, +3312498873u, +4238535494u, +1471227427u, +633688562u, +1959779970u, +766642813u, +1380896111u, +3647601207u, +1733961041u, +521947915u, +189164145u, +486382294u, +3770038872u, +3235740744u, +1912506671u, +2276864677u, +1588060152u, +2504457929u, +1471020554u, +3623212998u, +3026631806u, +2342164722u, +1674890530u, +3011542850u, +3549160092u, +4290680005u, +3943068002u, +2273781461u, +2127663659u, +1646681121u, +447810651u, +2366308558u, +970504950u, +2008155560u, +2695940969u, +3444688454u, +1739318893u, +2683090634u, +2774816580u, +437560100u, +512012738u, +3305170944u, +665292744u, +3580039116u, +1579404983u, +3397891494u, +710590371u, +2514565805u, +3624609754u, +3516075816u, +1314000850u, +1935166880u, +3257747610u, +3776931214u, +3183054185u, +675129307u, +3333261712u, +1154611403u, +2759854023u, +1963228038u, +505138315u, +1803966773u, +4032705384u, +798395739u, +3473799845u, +476400898u, +602972493u, +3289878097u, +2520311409u, +3214794876u, +748160407u, +1326769504u, +902775872u, +1372805534u, +1213925114u, +3009384989u, +3781981134u, +2835608783u, +2716786748u, +1669490957u, +1089334066u, +250756920u, +4041016629u, +2495807367u, +2008251381u, +106212622u, +1927268995u, +2251978818u, +3788056262u, +3678660147u, +2656772270u, +1997584981u, +2668998785u, +2954162084u, +845687881u, +776018378u, +2066910012u, +918315064u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; IsAlive(farmhashnt::Hash32WithSeed(data, len++, SEED)); IsAlive(farmhashnt::Hash32(data, len++)); IsAlive(farmhashnt::Hash32(data, len++)); len -= 3; return alive > 0; } +Check(farmhashnt::Hash32WithSeed(data + offset, len, SEED)); +Check(farmhashnt::Hash32(data + offset, len)); + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashntTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +cout << farmhashnt::Hash32WithSeed(data + offset, len, SEED) << "u," << endl; +cout << farmhashnt::Hash32(data + offset, len) << "u," << endl; +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashntTest + +#if TESTING + +static int farmhashntTestResult = farmhashntTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashntTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashntTest::Dump(0, i); + } + farmhashntTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashsaTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +4223616069u, +3696677242u, +4081014168u, +2576519988u, +2212771159u, +1112731063u, +1020067935u, +3955445564u, +1451961420u, +653440099u, +31917516u, +2957164615u, +2590087362u, +3879448744u, +176305566u, +2447367541u, +1359016305u, +3363804638u, +1117290165u, +1062549743u, +2437877004u, +1894455839u, +673206794u, +3486923651u, +3269862919u, +2303349487u, +1380660650u, +595525107u, +1525325287u, +2025609358u, +176408838u, +1592885012u, +864896482u, +2101378090u, +3489229104u, +2118965695u, +581644891u, +2718789079u, +631613207u, +4228658372u, +3867875546u, +3531368319u, +3804516756u, +3317755099u, +1619744564u, +2884717286u, +1088213445u, +2667691076u, +3727873235u, +2330406762u, +858590707u, +123802208u, +4150036245u, +182283099u, +1478882570u, +3282617403u, +819171187u, +1172627392u, +4254302102u, +2957028020u, +437030323u, +2452147680u, +2868246750u, +3530169402u, +3154852132u, +215019192u, +357580983u, +1354454461u, +1108813287u, +2324008118u, +2315997713u, +4181601562u, +1360882441u, +92423273u, +3048866755u, +3369188505u, +3664371439u, +2920710428u, +1027891570u, +2653166430u, +3461888315u, +1475780447u, +292769636u, +1737473313u, +4064110516u, +4170160075u, +762850927u, +3630603695u, +2803307356u, +844987665u, +460980967u, +3005635467u, +2802568977u, +588668033u, +2148940781u, +3239099984u, +1266953698u, +3197808789u, +3519942533u, +2511995334u, +2553810188u, +871667697u, +1358675720u, +1499319171u, +2044931270u, +1210355103u, +807152540u, +3262320756u, +2810214575u, +1813386141u, +4089465863u, +903928165u, +1388899322u, +3209183659u, +834536144u, +2733354550u, +2742289921u, +3689042563u, +2655593281u, +4169686303u, +415985561u, +138892376u, +516115393u, +65683883u, +4162865100u, +889944635u, +313566528u, +3346420907u, +1504303591u, +2256809275u, +742243229u, +779775302u, +3140940172u, +2312556111u, +2304095772u, +1151741606u, +2194712422u, +1714084652u, +3272736835u, +1311540658u, +191179665u, +3996605106u, +1657345233u, +4205442903u, +1553339212u, +2351843044u, +1647502006u, +2525516233u, +292202846u, +1498646290u, +1429323381u, +974274898u, +3759331561u, +2881238887u, +826787221u, +1069622448u, +221991032u, +1462969082u, +2799661508u, +364022781u, +2594244377u, +797773898u, +4097839290u, +1529150125u, +2456805570u, +541503425u, +3936326142u, +3112719954u, +775223581u, +3074018423u, +3198488875u, +1772191849u, +2456535211u, +3154686028u, +1520862019u, +4005829426u, +1306433767u, +1943028506u, +2246000782u, +1057766454u, +3761996982u, +3441075333u, +898641979u, +3450209088u, +3941329307u, +3289922449u, +3085075827u, +1814193220u, +690422997u, +2627846676u, +2653520704u, +3739145533u, +3996776010u, +2287072592u, +1346671698u, +3082629900u, +2298811274u, +3639722036u, +1729419228u, +1836765953u, +3708118742u, +213436u, +950223749u, +3734247682u, +2924575678u, +1382024841u, +2431637732u, +3448846682u, +1341301397u, +4206956590u, +1730650902u, +2581075456u, +1542359141u, +707222542u, +2925350541u, +3846303536u, +3579103295u, +3932175763u, +1339615732u, +848825750u, +1070170828u, +1964973818u, +577060344u, +607721296u, +4031023048u, +406883794u, +3991905552u, +1198544082u, +872468460u, +1044847096u, +3159976313u, +3020028266u, +2108700400u, +3373767922u, +264431841u, +2817097007u, +3700061048u, +1733731531u, +3459415893u, +80378591u, +1479875104u, +19735612u, +1382658977u, +3416562245u, +1959852842u, +2384002344u, +124683828u, +3725782174u, +2300301222u, +393852269u, +1302492002u, +3623776492u, +3787086417u, +1730024749u, +1710531361u, +443700716u, +1461987482u, +671998131u, +3018380746u, +2592292305u, +3390799372u, +3945101155u, +3743494852u, +3716045582u, +996005166u, +320698449u, +3420221765u, +1518157951u, +2555810666u, +3381929684u, +2019638523u, +3088262796u, +2072178906u, +3433649364u, +203906916u, +34663784u, +290301305u, +1188021504u, +3754681145u, +3920313139u, +2840496520u, +1656802962u, +2288475489u, +3399185138u, +1296000826u, +2362384746u, +309633360u, +2719851778u, +776035930u, +3200733043u, +365690832u, +3326378243u, +1500331457u, +1625708592u, +4230903462u, +715344888u, +3363777768u, +2243620288u, +2890765789u, +553154234u, +4044100108u, +4056887320u, +1185656496u, +3671476744u, +1064586897u, +1154949698u, +3493481974u, +1294573722u, +1869224012u, +2530084956u, +995321553u, +833419249u, +563815282u, +250258043u, +2970801822u, +441007535u, +42246961u, +2820426655u, +2878882436u, +2363245780u, +2138489282u, +2972360481u, +2312619393u, +3598664848u, +3071556076u, +776990325u, +3220427357u, +2257939577u, +3817305903u, +1502979698u, +3159755934u, +3955997276u, +2423850008u, +1959927572u, +1219782288u, +4119776679u, +1124253854u, +3678052422u, +2620644947u, +1262408666u, +3480072280u, +2627137665u, +807538749u, +3276646337u, +518510128u, +1137828655u, +1498449110u, +3031692317u, +1125635969u, +1130096111u, +780007336u, +3111856399u, +1014917264u, +780877352u, +2909458336u, +4235949214u, +2423879289u, +275888892u, +3891926795u, +3538163953u, +54815161u, +162228302u, +258154068u, +3554455591u, +1801469029u, +2801563220u, +726560058u, +2450221940u, +3677582978u, +440993800u, +424762443u, +2624525253u, +2587715329u, +2292264424u, +1074856749u, +3294752007u, +3164112672u, +2399146799u, +1920182465u, +3858835361u, +193755240u, +3333610311u, +1757504059u, +2576027039u, +2775253365u, +2939191561u, +1046147275u, +235149906u, +4262218222u, +2900542726u, +2260154702u, +1019551635u, +1194720570u, +3519118691u, +3039483153u, +84918216u, +3053381097u, +2572396843u, +3849763371u, +2782686780u, +3710049554u, +3403430713u, +2346080784u, +2496307442u, +1597281872u, +696018239u, +704625714u, +623026921u, +3182413559u, +3794540330u, +305497722u, +1592680199u, +2377854072u, +3060601746u, +3953057908u, +3941551588u, +1033716182u, +2765716854u, +1309699058u, +3519400181u, +3073370877u, +115583008u, +4032909296u, +2944563574u, +3762753718u, +192842727u, +1711348701u, +3086147235u, +1658229443u, +1479783872u, +3839977157u, +225619117u, +1349684817u, +1964813173u, +565753187u, +2530252046u, +840014353u, +1645183704u, +3668429078u, +3438418557u, +639704059u, +360837811u, +2531807958u, +1572353913u, +2116037299u, +1948437512u, +744553393u, +2380697034u, +3775234105u, +3816065157u, +301868653u, +2960939561u, +3306528247u, +2389296549u, +805918610u, +1759358265u, +1760876328u, +2827601706u, +2944594708u, +3313666458u, +2022601495u, +730938791u, +193539397u, +2026103244u, +802928398u, +2630934308u, +782805818u, +3499326016u, +293509489u, +3646131514u, +3182478647u, +854800333u, +2284531628u, +438528022u, +2339298129u, +1692289216u, +2427728723u, +46501288u, +350652353u, +1355971222u, +889682372u, +944799254u, +2763906061u, +2807550612u, +2683762637u, +100870317u, +2449357318u, +2638348436u, +4206088869u, +1788948473u, +3537588549u, +2782490204u, +134406470u, +2409190528u, +2362439849u, +1861661528u, +2101513194u, +1424834765u, +3581765745u, +3185999525u, +2057487100u, +2303941176u, +3639628788u, +1180265315u, +230437935u, +2108319366u, +1131685143u, +1055685292u, +1509007009u, +1258485140u, +560525005u, +3598799040u, +3835680585u, +1851859628u, +332858996u, +641769248u, +4252450037u, +865386707u, +720719117u, +3133612164u, +3833045874u, +3492515435u, +2465970289u, +4234420011u, +573859916u, +252532886u, +870392318u, +4051320920u, +894929092u, +3748361688u, +699355960u, +1885212350u, +1609756949u, +461896870u, +1337065461u, +1775211059u, +1786193749u, +2815154643u, +2128729882u, +969639529u, +3960427545u, +859416958u, +2739758802u, +2698032197u, +2813292418u, +1985467524u, +396604317u, +4122172759u, +1201259789u, +4282051702u, +3270018895u, +961215209u, +961075860u, +4211926998u, +4088374597u, +577510509u, +3058349487u, +4025377754u, +2815478438u, +471023164u, +3947959608u, +4161486934u, +2299888461u, +1103571511u, +2450153872u, +1839939275u, +108299608u, +858086440u, +1030152945u, +3895328530u, +3009080718u, +3690840454u, +3847025277u, +152331362u, +161365689u, +831319961u, +2166017294u, +3945322722u, +4059970216u, +1420824131u, +2770648308u, +1567250186u, +2181067149u, +1939743488u, +3080158120u, +3435218248u, +2495237495u, +3814085102u, +3180983013u, +3199054292u, +2204745908u, +1140337267u, +2213569784u, +1941879842u, +2105562605u, +3618835614u, +2247103645u, +2492473487u, +856414299u, +166022030u, +4080104712u, +3218935344u, +3284220561u, +4261581452u, +1206944836u, +3496705432u, +2215996876u, +3154627465u, +3384005496u, +742170556u, +1333047620u, +802680366u, +156833431u, +2682100354u, +2493654830u, +584848366u, +1691693131u, +2169934170u, +779968026u, +2099545800u, +1423039695u, +4292110968u, +4266576788u, +149142597u, +748501873u, +3865014822u, +1913588198u, +130285614u, +3500768879u, +915458923u, +3071792750u, +1339986633u, +4143929149u, +4048379479u, +725193827u, +1375113643u, +2425277412u, +4144659274u, +465714768u, +226991589u, +2212127704u, +3936145258u, +2891024846u, +3816000225u, +979331165u, +1749907536u, +53847318u, +1462525833u, +2961425455u, +368859113u, +3572721452u, +453048644u, +1628629918u, +3497673923u, +3619079585u, +139870565u, +1518176798u, +3933074281u, +1878623729u, +2074035641u, +3016759257u, +1313053591u, +2557706970u, +2348296582u, +962370022u, +2337285014u, +1618936717u, +1915877085u, +2743743122u, +3250783882u, +1346652536u, +143311109u, +2443788461u, +1048248964u, +2806619339u, +3263266976u, +1668146349u, +3397428868u, +3276188862u, +1774196343u, +1993847813u, +2771079610u, +476672419u, +2119050359u, +2918326659u, +2245402721u, +2692910474u, +2374383269u, +342400227u, +2961437795u, +3899230368u, +337787132u, +3664444935u, +1269451153u, +2971526729u, +1486511182u, +791070133u, +2570319890u, +3482497490u, +2134230518u, +4273391202u, +1825511330u, +3947753714u, +1389755724u, +3995075516u, +2081052615u, +3626343470u, +4213603435u, +2137917278u, +2898987303u, +3059215715u, +3383237881u, +3003674434u, +409174425u, +1911915604u, +2087728055u, +2942005882u, +3386522440u, +714936074u, +261924004u, +3268784033u, +1141188757u, +2413217552u, +1515163433u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; IsAlive(farmhashsa::Hash32WithSeed(data, len++, SEED)); IsAlive(farmhashsa::Hash32(data, len++)); IsAlive(farmhashsa::Hash32(data, len++)); len -= 3; return alive > 0; } +Check(farmhashsa::Hash32WithSeed(data + offset, len, SEED)); +Check(farmhashsa::Hash32(data + offset, len)); + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashsaTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +cout << farmhashsa::Hash32WithSeed(data + offset, len, SEED) << "u," << endl; +cout << farmhashsa::Hash32(data + offset, len) << "u," << endl; +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashsaTest + +#if TESTING + +static int farmhashsaTestResult = farmhashsaTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashsaTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashsaTest::Dump(0, i); + } + farmhashsaTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashsuTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +4223616069u, +3696677242u, +4081014168u, +2576519988u, +2212771159u, +1112731063u, +1020067935u, +3955445564u, +1451961420u, +653440099u, +31917516u, +2957164615u, +2590087362u, +3879448744u, +176305566u, +2447367541u, +1359016305u, +3363804638u, +1117290165u, +1062549743u, +2437877004u, +1894455839u, +673206794u, +3486923651u, +3269862919u, +2303349487u, +1380660650u, +595525107u, +1525325287u, +2025609358u, +176408838u, +1592885012u, +864896482u, +2101378090u, +3489229104u, +2118965695u, +581644891u, +2718789079u, +631613207u, +4228658372u, +3867875546u, +3531368319u, +3804516756u, +3317755099u, +1619744564u, +2884717286u, +1088213445u, +2667691076u, +3727873235u, +2330406762u, +858590707u, +457744844u, +4150036245u, +2000404290u, +1478882570u, +901678172u, +819171187u, +195942998u, +4254302102u, +3967266927u, +437030323u, +4018009204u, +2868246750u, +3540087514u, +3154852132u, +3319116625u, +357580983u, +3177665294u, +1108813287u, +1253366798u, +2315997713u, +510718750u, +1360882441u, +2770216279u, +3048866755u, +3406961221u, +3664371439u, +1151145514u, +1027891570u, +2699067992u, +3461888315u, +198061905u, +292769636u, +1106771795u, +4064110516u, +3258279756u, +762850927u, +1818699721u, +2803307356u, +3919169404u, +460980967u, +3125535078u, +2802568977u, +3582546426u, +2148940781u, +3963274378u, +1266953698u, +204185123u, +1100034381u, +3009193601u, +4200651967u, +274889605u, +2700589508u, +952511689u, +3765324859u, +3465498478u, +4014967037u, +2070988082u, +2972423530u, +3068638223u, +4156773651u, +489509804u, +1323863238u, +3731914806u, +2846098469u, +2728930632u, +346814072u, +848146907u, +551160669u, +4165126521u, +2039095001u, +4179859388u, +2434936359u, +2764414551u, +238491210u, +732483969u, +3366512764u, +478307468u, +4124179572u, +4142733597u, +1953448206u, +4199329278u, +865077060u, +2627662116u, +2802499360u, +3141206831u, +1959218197u, +911371451u, +125987200u, +2821366175u, +2530992747u, +2409206225u, +117991880u, +2133402461u, +895510531u, +428719601u, +3036014536u, +1223783733u, +733793540u, +970650405u, +547701766u, +570764615u, +3224485368u, +3192714940u, +319942831u, +3940200341u, +362056204u, +2832368105u, +1853281226u, +3296434636u, +3752508307u, +604292768u, +2231940616u, +1204094681u, +866194005u, +2405201650u, +2466384396u, +380829379u, +230033818u, +2783417588u, +4249886729u, +829569301u, +2988322580u, +2299983554u, +74748560u, +737514425u, +3153050211u, +652642663u, +1270205115u, +227197032u, +2773091790u, +325849216u, +49998791u, +4043203010u, +3662748068u, +1709364383u, +1179105165u, +1478504366u, +2980456610u, +1167476429u, +1590390732u, +1306256496u, +292008135u, +374690995u, +1809200819u, +1680595904u, +646040226u, +1742445560u, +2435776844u, +3703683804u, +478742495u, +814967947u, +2698190177u, +1003617993u, +1436118705u, +217056304u, +1412287094u, +2738417466u, +2933279339u, +3461877733u, +1203141205u, +2119492857u, +1134895723u, +1560001021u, +3786320122u, +3748116258u, +3486219595u, +702138030u, +1062984182u, +232789133u, +1566523968u, +3885443778u, +1820171888u, +3655858585u, +2316903005u, +2678779620u, +395625433u, +1609107564u, +3108726411u, +2937837224u, +3911907151u, +557272509u, +3893435978u, +1542613576u, +1079886893u, +2624566322u, +1413700616u, +2796974006u, +1922556114u, +562820464u, +2845409784u, +54180312u, +1898782464u, +3681814953u, +2417064617u, +1815464483u, +911626132u, +2964575550u, +1852696128u, +2319647785u, +1998904590u, +619992689u, +3073207513u, +1238163512u, +3199435982u, +828667254u, +3561155502u, +3943095163u, +1045711849u, +2238679131u, +2114975398u, +713808403u, +3871787494u, +2572031161u, +2360934075u, +2337781107u, +262596504u, +693836699u, +2129369850u, +3543189427u, +962205222u, +3685581020u, +692974477u, +725182211u, +646123906u, +2368836544u, +2505872733u, +1999977610u, +1639885802u, +1475058032u, +207023609u, +2773581234u, +3524857793u, +3433371102u, +3243027613u, +1787668353u, +985757946u, +3896012929u, +702356957u, +3559331129u, +884084870u, +4009998120u, +648888720u, +1403349048u, +1624342778u, +1766674171u, +2518582204u, +3251243146u, +792751003u, +1377201813u, +3629686054u, +1583734324u, +3647107626u, +4258564381u, +1469878609u, +1940598241u, +2755003690u, +1907120418u, +109916701u, +775347954u, +2090960874u, +611281803u, +3470490146u, +3301663253u, +1835412158u, +1803066146u, +591872433u, +550703713u, +1495089683u, +826492808u, +817200035u, +4177474571u, +688070143u, +971427632u, +1442499481u, +3568640348u, +2789993738u, +85808128u, +2058346726u, +394058570u, +3466511434u, +318905230u, +4149248030u, +415308316u, +165997598u, +1219639412u, +1648022659u, +2857432523u, +1422508004u, +468095522u, +296968649u, +430250611u, +1775562314u, +2976361671u, +1040036362u, +1372510167u, +292746272u, +3408238954u, +626061886u, +1317637569u, +1237775792u, +1218490455u, +2224234499u, +590942419u, +713995643u, +3541889330u, +4140218960u, +3529791107u, +354462673u, +842607274u, +365048533u, +2638303414u, +3560458014u, +31621379u, +4210854794u, +1273118792u, +2572743762u, +3513175801u, +402066986u, +602524471u, +565029192u, +180576438u, +1288605959u, +2896244423u, +1420543484u, +1329862227u, +1791567324u, +4248690247u, +12917038u, +3483481310u, +2082050731u, +1611921143u, +2443766548u, +2216338811u, +2528006095u, +2984009021u, +674210884u, +2857608106u, +2155534809u, +1023105067u, +2968955846u, +3303624302u, +2502112850u, +245749006u, +3175229091u, +3342796184u, +3613785362u, +1614168851u, +2582149283u, +895403488u, +416205023u, +3792242000u, +529397534u, +299415203u, +4284673348u, +2096851282u, +1864524731u, +2012577738u, +3426363316u, +1387308508u, +1143610148u, +2027467219u, +3772856163u, +3453862623u, +2661437174u, +2047145955u, +2533381447u, +2059534115u, +439426587u, +1537543414u, +2384289877u, +3174229055u, +2658017753u, +2293148474u, +2359450158u, +3930242475u, +1510302397u, +3354288821u, +920095603u, +2415746928u, +2729472638u, +2261143371u, +848667611u, +919157153u, +3322393117u, +4103299943u, +413569608u, +68911216u, +3334990170u, +1228068652u, +1570056373u, +1905477543u, +2622302276u, +2935063895u, +3224810004u, +4211768578u, +828688131u, +3556122839u, +1930935348u, +2605825202u, +1540993970u, +3209115883u, +122847500u, +665638794u, +506571051u, +2691795295u, +3996966556u, +714660621u, +3662432239u, +470651837u, +1807432621u, +3755290953u, +359878860u, +2793081615u, +4065031431u, +904653062u, +2317800777u, +568501094u, +3492871707u, +2738806116u, +2883859610u, +3242080257u, +364246691u, +3601786516u, +3159362524u, +1578272201u, +1283574375u, +2912186103u, +2256279032u, +1540671086u, +2356088973u, +2892277779u, +3441449267u, +2225005503u, +3846428419u, +2014549218u, +2290734767u, +2126684614u, +4235463487u, +3811556204u, +174739661u, +767525888u, +47684458u, +4211168099u, +889063422u, +469864411u, +767407110u, +413337343u, +1618456644u, +2814499820u, +2401124192u, +632089437u, +1234980238u, +1288585402u, +3153169944u, +2917822069u, +1843320264u, +3794359132u, +3074573530u, +258629454u, +3813357060u, +3806887248u, +1665524736u, +3324533324u, +3005091922u, +793108368u, +1529669805u, +2332660395u, +2217730223u, +2634687611u, +442806463u, +1968135266u, +454523002u, +3177866230u, +2808960136u, +4259114138u, +4103264843u, +3103714075u, +2462967542u, +1466891491u, +477973764u, +834565647u, +741089037u, +218837573u, +1710536528u, +2469088212u, +1229072375u, +2828341u, +176923431u, +985763350u, +4095477420u, +1984145538u, +1870791084u, +674956677u, +1978138947u, +1296493993u, +1818183554u, +3443333721u, +2124949983u, +2549590262u, +2700850794u, +2662736367u, +739638109u, +4061447096u, +2960078422u, +2453781158u, +929570940u, +3200328383u, +2406328791u, +1419180666u, +2152455739u, +2805741044u, +3305999074u, +3183816361u, +2303165050u, +4922104u, +63096005u, +936656347u, +3104453886u, +1088673880u, +1113407526u, +1457890086u, +453478383u, +1107686695u, +3626027824u, +1159687359u, +2248467888u, +2004578380u, +3274954621u, +1787958646u, +2628726704u, +1138419798u, +3735442315u, +692385301u, +313807213u, +2329068673u, +59375364u, +3261084359u, +2088644507u, +2471153194u, +788336435u, +4024527246u, +141504460u, +2307553888u, +1930559950u, +48975711u, +2745693338u, +230161982u, +3429230862u, +1335968626u, +609591304u, +57435073u, +4279281136u, +3152151665u, +3984484924u, +3459883943u, +397478330u, +1738762229u, +3033590066u, +3611539498u, +1363463523u, +3319364965u, +2671169141u, +3819548561u, +1691193757u, +2423834608u, +2820147055u, +1378120632u, +1240565187u, +3180720050u, +680831086u, +3309658414u, +1986166490u, +762099827u, +510883662u, +2047373648u, +3606742294u, +3894965352u, +2342078853u, +1091255717u, +776594727u, +3217317445u, +1574468485u, +3844504016u, +2819598918u, +1037401010u, +2550943503u, +3867184001u, +1687911772u, +165313836u, +1679575281u, +2418947263u, +2038774952u, +3913543652u, +3209155736u, +149905221u, +3859604717u, +713919631u, +4069810796u, +1882959164u, +1019939034u, +2379867302u, +3666323035u, +1157389013u, +2422300650u, +3366777340u, +2526452062u, +1313747885u, +1039617868u, +1620553692u, +2032976978u, +578789528u, +1592846839u, +2270630604u, +897850577u, +1603294178u, +3105664807u, +1442670138u, +1728019360u, +79313861u, +1683031101u, +1913067024u, +4070719870u, +708986470u, +2586453359u, +3993348863u, +3358251279u, +3003552537u, +750174793u, +836888956u, +4190747426u, +4251291318u, +4145164938u, +1366883260u, +1912910955u, +510192669u, +1851315039u, +3574241274u, +3220062924u, +2821142039u, +1317082195u, +2274293302u, +1839219569u, +126586168u, +3989293643u, +2680178207u, +347056948u, +799681430u, +2864517481u, +3180404853u, +213140045u, +1956305184u, +1474675286u, +3085723423u, +2841859626u, +308421914u, +3670309263u, +1765052231u, +245459238u, +113434331u, +4079521092u, +2115235526u, +2943408816u, +1055476938u, +1506442339u, +2291296392u, +3267864332u, +1282145528u, +3700108015u, +1932843667u, +2677701670u, +6041177u, +3889648557u, +1461025478u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; IsAlive(farmhashsu::Hash32WithSeed(data, len++, SEED)); IsAlive(farmhashsu::Hash32(data, len++)); IsAlive(farmhashsu::Hash32(data, len++)); len -= 3; return alive > 0; } +Check(farmhashsu::Hash32WithSeed(data + offset, len, SEED)); +Check(farmhashsu::Hash32(data + offset, len)); + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashsuTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +cout << farmhashsu::Hash32WithSeed(data + offset, len, SEED) << "u," << endl; +cout << farmhashsu::Hash32(data + offset, len) << "u," << endl; +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashsuTest + +#if TESTING + +static int farmhashsuTestResult = farmhashsuTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashsuTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashsuTest::Dump(0, i); + } + farmhashsuTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashteTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +1140953930u, 861465670u, +3277735313u, 2681724312u, +2598464059u, 797982799u, +890626835u, 800175912u, +2603993599u, 921001710u, +1410420968u, 2134990486u, +3283896453u, 1867689945u, +2914424215u, 2244477846u, +255297188u, 2992121793u, +1110588164u, 4186314283u, +161451183u, 3943596029u, +4019337850u, 452431531u, +283198166u, 2741341286u, +3379021470u, 2557197665u, +299850021u, 2532580744u, +452473466u, 1706958772u, +1298374911u, 3099673830u, +2199864459u, 3696623795u, +236935126u, 2976578695u, +4055299123u, 3281581178u, +1053458494u, 1882212500u, +2305012065u, 2169731866u, +3456121707u, 275903667u, +458884671u, 3033004529u, +3058973506u, 2379411653u, +1898235244u, 1402319660u, +2700149065u, 2699376854u, +147814787u, 720739346u, +2433714046u, 4222949502u, +4220361840u, 1712034059u, +3425469811u, 3690733394u, +4148372108u, 1330324210u, +594028478u, 2921867846u, +1635026870u, 192883107u, +780716741u, 1728752234u, +3280331829u, 326029180u, +3969463346u, 1436364519u, +393215742u, 3349570000u, +3824583307u, 1612122221u, +2859809759u, 3808705738u, +1379537552u, 1646032583u, +2233466664u, 1432476832u, +4023053163u, 2650381482u, +2052294713u, 3552092450u, +1628777059u, 1499109081u, +3476440786u, 3829307897u, +2960536756u, 1554038301u, +1145519619u, 3190844552u, +2902102606u, 3600725550u, +237495366u, 540224401u, +65721842u, 489963606u, +1448662590u, 397635823u, +1596489240u, 1562872448u, +1790705123u, 2128624475u, +180854224u, 2604346966u, +1435705557u, 1262831810u, +155445229u, 1672724608u, +1669465176u, 1341975128u, +663607706u, 2077310004u, +3610042449u, 1911523866u, +1043692997u, 1454396064u, +2563776023u, 294527927u, +1099072299u, 1389770549u, +703505868u, 678706990u, +2952353448u, 2026137563u, +3603803785u, 629449419u, +1933894405u, 3043213226u, +226132789u, 2489287368u, +1552847036u, 645684964u, +3828089804u, 3632594520u, +187883449u, 230403464u, +3151491850u, 3272648435u, +3729087873u, 1303930448u, +2002861219u, 165370827u, +916494250u, 1230085527u, +3103338579u, 3064290191u, +3807265751u, 3628174014u, +231181488u, 851743255u, +2295806711u, 1781190011u, +2988893883u, 1554380634u, +1142264800u, 3667013118u, +1968445277u, 315203929u, +2638023604u, 2290487377u, +732137533u, 1909203251u, +440398219u, 1891630171u, +1380301172u, 1498556724u, +4072067757u, 4165088768u, +4204318635u, 441430649u, +3931792696u, 197618179u, +956300927u, 914413116u, +3010839769u, 2837339569u, +2148126371u, 1913303225u, +3074915312u, 3117299654u, +4139181436u, 2993479124u, +3178848746u, 1357272220u, +1438494951u, 507436733u, +667183474u, 2084369203u, +3854939912u, 1413396341u, +126024219u, 146044391u, +1016656857u, 3022024459u, +3254014218u, 429095991u, +990500595u, 3056862311u, +985653208u, 1718653828u, +623071693u, 366414107u, +1771289760u, 2293458109u, +3047342438u, 2991127487u, +3120876698u, 1684583131u, +3638043310u, 1170404994u, +863214540u, 1087193030u, +199124911u, 520792961u, +3169775996u, 1577421232u, +3331828431u, 1013201099u, +1716848157u, 4033596884u, +1770708857u, 4229339322u, +1146169032u, 1434258493u, +3824360466u, 3242407770u, +1926419493u, 2649785113u, +872586426u, 762243036u, +2736953692u, 816692935u, +1571283333u, 3555213933u, +2266795890u, 3781899767u, +4290630595u, 517646945u, +3006163611u, 2180594090u, +959214578u, 558910384u, +1283799121u, 3047062993u, +3830962609u, 2391606125u, +3544509313u, 622325861u, +834785312u, 382936554u, +1421463872u, 788479970u, +1825135056u, 2725923798u, +580988377u, 2826990641u, +247825043u, 3167748333u, +812546227u, 2506885666u, +2584372201u, 1758123094u, +1891789696u, 389974094u, +345313518u, 2022370576u, +3886113119u, 3338548567u, +1083486947u, 2583576230u, +1776047957u, 1771384107u, +3604937815u, 3198590202u, +3027522813u, 4155628142u, +4232136669u, 427759438u, +4244322689u, 542201663u, +1549591985u, 2856634168u, +556609672u, 45845311u, +1175961330u, 3948351189u, +4165739882u, 4194218315u, +1634635545u, 4151937410u, +713127376u, 1467786451u, +1327394015u, 2743592929u, +2638154051u, 810082938u, +3077742128u, 1062268187u, +4084325664u, 3810665822u, +3735739145u, 2794294783u, +2335576331u, 2560479831u, +690240711u, 997658837u, +2442302747u, 3948961926u, +3958366652u, 3067277639u, +2059157774u, 1211737169u, +1516711748u, 2339636583u, +4188504038u, 59581167u, +2767897792u, 1389679610u, +2658147000u, 2643979752u, +3758739543u, 4189944477u, +1454470782u, 100876854u, +2995362413u, 118817200u, +3252925478u, 2062343506u, +2804483644u, 3088828656u, +1231633714u, 4168280671u, +2931588131u, 3284356565u, +1255909792u, 3130054947u, +4173605289u, 1407328702u, +1677744031u, 3532596884u, +3162657845u, 3887208531u, +2256541290u, 3459463480u, +3740979556u, 259034107u, +392987633u, 3233195759u, +3606709555u, 3424793077u, +315836068u, 3200749877u, +4065431359u, 760633989u, +2982018998u, 1811050648u, +234531934u, 1115203611u, +3897494162u, 1516407838u, +1603559457u, 323296368u, +2632963283u, 1778459926u, +2879836826u, 2146672889u, +3486330348u, 492621815u, +1231665285u, 2457048126u, +3438440082u, 2217471853u, +3355404249u, 3275550588u, +1052645068u, 862072556u, +4110617119u, 3745267835u, +2657392572u, 4279236653u, +1688445808u, 701920051u, +956734128u, 581695350u, +3157862788u, 2585726058u, +1192588249u, 1410111809u, +1651193125u, 3326135446u, +1073280453u, 97376972u, +2513844237u, 2187968410u, +3976859649u, 4267859263u, +3429034542u, 564493077u, +3000537321u, 479241367u, +3845637831u, 2868987960u, +51544337u, 1029173765u, +393624922u, 704325635u, +2357610553u, 1418509533u, +2007814586u, 3866658271u, +3082385053u, 735688735u, +916110004u, 3283299459u, +1051684175u, 1083796807u, +4074716319u, 813690332u, +144264390u, 1439630796u, +1508556987u, 675582689u, +3748881891u, 3195309868u, +362884708u, 1616408198u, +43233176u, 837301135u, +881504822u, 3254795114u, +1385506591u, 2799925823u, +1469874582u, 3464841997u, +497175391u, 3929484338u, +3975771289u, 1798536177u, +2926265846u, 1374242438u, +3675707838u, 4205965408u, +3153165629u, 1499475160u, +187287713u, 548490821u, +3255259608u, 4247675634u, +1940181471u, 3779953975u, +687167150u, 2319566715u, +1742785722u, 785893184u, +2296977392u, 2778575413u, +1794720651u, 48131484u, +4084891412u, 1160134629u, +3737623280u, 823113169u, +3423207646u, 3803213486u, +710625654u, 4116162021u, +3693420287u, 4167766971u, +1666602807u, 295320990u, +3513255468u, 2487440590u, +234080704u, 4004655503u, +2971762528u, 1479656873u, +4090178629u, 4044418876u, +391947536u, 1462554406u, +3909295855u, 1239580330u, +1515601363u, 2011102035u, +1442068334u, 4265993528u, +1191921695u, 2291355695u, +4257172787u, 576405853u, +314332944u, 4038839101u, +55559918u, 2378985842u, +711098718u, 2425317635u, +1644327317u, 1401013391u, +4193760037u, 2958260436u, +3167371443u, 3062418115u, +3800755475u, 3167030094u, +3489648204u, 1405430357u, +526177822u, 2602636307u, +915406019u, 4264167741u, +1484090483u, 3070944737u, +254529415u, 4017058800u, +1702710265u, 1029665228u, +2000382906u, 3185573940u, +1381258384u, 4036354071u, +2900841028u, 2670703363u, +2921748807u, 2899069938u, +4130543625u, 688472265u, +4186808827u, 1054670286u, +1132985391u, 2840525968u, +4175776103u, 338058159u, +1735964501u, 1539305024u, +3497121710u, 1568260669u, +2227290760u, 146827036u, +3977176001u, 4060134777u, +857488494u, 250055052u, +4284109679u, 2502815838u, +2592281721u, 1603444633u, +1390562014u, 1556658131u, +616327404u, 2448966429u, +3051191726u, 3891353218u, +1213304082u, 762328245u, +2239052397u, 1082330589u, +2455957292u, 201837927u, +405397452u, 3079886794u, +2583939798u, 2848283092u, +3750724631u, 883849006u, +3204198988u, 3341327098u, +1855234968u, 1982110346u, +1485529487u, 541496720u, +4117290321u, 3607433551u, +2168864636u, 133643215u, +1055817409u, 3847827123u, +2960769387u, 4046101649u, +1176127003u, 4015671361u, +4243643405u, 2849988118u, +517111221u, 1796672358u, +2045051700u, 3452457457u, +2948254999u, 2102063419u, +1556410577u, 1536380876u, +3776661467u, 3281002516u, +1735616066u, 1539151988u, +1087795162u, 3332431596u, +685631442u, 1147951686u, +95237878u, 2005032160u, +4012206915u, 4224354805u, +3204999386u, 2415262714u, +1433635018u, 116647396u, +83167836u, 2881562655u, +2729416454u, 1029284767u, +881378302u, 2159170082u, +555057366u, 1169104445u, +3963877000u, 1919171906u, +336034862u, 2017579106u, +4059340529u, 3020819343u, +865146997u, 2473524405u, +944743644u, 1694443528u, +1804513294u, 2904752429u, +617975720u, 3671562289u, +260177668u, 505662155u, +1885941445u, 2504509403u, +2260041112u, 1019936943u, +3722741628u, 1511077569u, +3100701179u, 1379422864u, +1535670711u, 773792826u, +1103819072u, 2089123665u, +1157547425u, 329152940u, +4142587430u, 484732447u, +2475035432u, 1120017626u, +412145504u, 965125959u, +324924679u, 2809286837u, +2842141483u, 4029205195u, +2974306813u, 515627448u, +3791551981u, 1097806406u, +3873078673u, 136118734u, +1872130856u, 3632422367u, +3574135531u, 4017075736u, +1699452298u, 1403506686u, +344414660u, 1189129691u, +3487080616u, 1516736273u, +1805475756u, 2562064338u, +163335594u, 2732147834u, +4077452507u, 2984955003u, +4271866024u, 3071338162u, +2347111903u, 873829983u, +1948409509u, 1923531348u, +459509140u, 771592405u, +1750124750u, 2334938333u, +213811117u, 2586632018u, +185232757u, 4032960199u, +2447383637u, 284777551u, +1654276320u, 2687561076u, +3512945009u, 308584855u, +1861027147u, 4102279334u, +3203802620u, 1692079268u, +4250142168u, 2565680167u, +1507046104u, 841195925u, +520565830u, 3674576684u, +38924274u, 3770488806u, +2414430882u, 3978473838u, +3703994407u, 69201295u, +3099963860u, 1255084262u, +690971838u, 3539996781u, +3696902571u, 3593730713u, +2363435042u, 54945052u, +1785765213u, 184911581u, +1586241476u, 1939595371u, +2534883189u, 2432427547u, +2374171993u, 2039128933u, +2955715987u, 2295501078u, +2741583197u, 1280920000u, +686818699u, 1238742497u, +3843660102u, 82177963u, +1281043691u, 1121403845u, +1697846708u, 284852964u, +278661677u, 2889101923u, +2127558730u, 713121337u, +872502474u, 511142139u, +1261140657u, 1747052377u, +2108187161u, 927011680u, +955328267u, 3821994995u, +2707230761u, 4142246789u, +4134691985u, 1958963937u, +2498463509u, 1977988705u, +1419293714u, 1636932722u, +2567532373u, 4075249328u, +240575705u, 1956681213u, +2598802768u, 2025886508u, +4104757832u, 3026358429u, +3242615202u, 4026813725u, +255108733u, 1845587644u, +3573008472u, 3615577014u, +1222733548u, 1205557630u, +917608574u, 1363253259u, +1541946015u, 3087190425u, +1138008081u, 1444019663u, +109793386u, 341851980u, +857839960u, 2515339233u, +156283211u, 1906768669u, +3886713057u, 1276595523u, +2809830736u, 460237542u, +3420452099u, 142985419u, +205970448u, 4198897105u, +1950698961u, 2069753399u, +1142216925u, 1113051162u, +1033680610u, 4278599955u, +1106466069u, 356742959u, +531521052u, 3494863964u, +225629455u, 3735275001u, +3662626864u, 1750561299u, +1012864651u, 2101846429u, +1074553219u, 668829411u, +992181339u, 3384018814u, +3330664522u, 860966321u, +1885071395u, 4233785523u, +100741310u, 451656820u, +2148187612u, 1063001151u, +360256231u, 107312677u, +3650357479u, 2390172694u, +22452685u, 237319043u, +3600462351u, 1216645846u, +2088767754u, 164402616u, +2418980170u, 926137824u, +94638678u, 1689811113u, +2751052984u, 1767810825u, +271289013u, 3896132233u, +103797041u, 1397772514u, +3441135892u, 3323383489u, +2491268371u, 1662561885u, +1612872497u, 2986430557u, +2756998822u, 207428029u, +937973965u, 2791656726u, +1949717207u, 2260498180u, +2648427775u, 2360400900u, +2080496169u, 486358863u, +1582022990u, 1263709570u, +1396468647u, 1377764574u, +363008508u, 1293502429u, +224580012u, 4252610345u, +1435134775u, 1099809675u, +533671980u, 1533438766u, +1820532305u, 2776960536u, +3374512975u, 3542220540u, +822810075u, 3716663290u, +1157398049u, 3752806924u, +4081637863u, 337070226u, +3866585976u, 359270190u, +2110942730u, 3267551635u, +644850146u, 1306761320u, +746972907u, 934259457u, +2341378668u, 2220373824u, +1242645122u, 4109252858u, +1625266099u, 1173698481u, +383517064u, 896322512u, +3377483696u, 1788337208u, +455496839u, 3194373887u, +1837689083u, 1336556841u, +1658628529u, 2911512007u, +3838343487u, 2757664765u, +1537187340u, 3712582785u, +367022558u, 3071359622u, +3926147070u, 35432879u, +3093195926u, 2561488770u, +4273132307u, 3898950547u, +2838251049u, 2103926083u, +2549435227u, 536047554u, +1858986613u, 2040551642u, +1147412575u, 1972369852u, +4166184983u, 3528794619u, +4077477194u, 3565689036u, +808048238u, 3826350461u, +1359641525u, 1197100813u, +265993036u, 1864569342u, +725164342u, 2264788336u, +1831223342u, 3329594980u, +923017956u, 490608221u, +3818634478u, 258154469u, +1441714797u, 1174785921u, +3833372385u, 3287246572u, +1677395563u, 3569218731u, +868981704u, 2163330264u, +2649450292u, 500120236u, +465161780u, 746438382u, +1145009669u, 2520062970u, +2810524030u, 1561519055u, +1479878006u, 3864969305u, +2686075657u, 4042710240u, +3224066062u, 2774151984u, +2226179547u, 1643626042u, +2328730865u, 3160666939u, +2107011431u, 96459446u, +3920328742u, 3336407558u, +829404209u, 1878067032u, +1235983679u, 4237425634u, +466519055u, 3870676863u, +934312076u, 2952135524u, +276949224u, 4100839753u, +424001484u, 1955120893u, +4015478120u, 1265237690u, +427484362u, 4246879223u, +3452969617u, 1724363362u, +1553513184u, 834830418u, +1858777639u, 3476334357u, +4144030366u, 2450047160u, +2950762705u, 4277111759u, +358032121u, 2511026735u, +167923105u, 2059208280u, +251949572u, 3065234219u, +1535473864u, 556796152u, +1513237478u, 3150857516u, +1103404394u, 198182691u, +1476438092u, 2913077464u, +207119516u, 3963810232u, +2954651680u, 1535115487u, +3051522276u, 4046477658u, +917804636u, 864395565u, +632704095u, 140762681u, +1802040304u, 990407433u, +3771506212u, 4106024923u, +1287729497u, 2198985327u, +4052924496u, 2926590471u, +3084557148u, 1472898694u, +1009870118u, 559702706u, +4265214507u, 82077489u, +3067891003u, 3295678907u, +2402308151u, 1096697687u, +464407878u, 4190838199u, +4269578403u, 3060919438u, +2899950405u, 3046872820u, +733509243u, 1583801700u, +40453902u, 3879773881u, +1993425202u, 2185339100u, +1877837196u, 3912423882u, +3293122640u, 4104318469u, +1679617763u, 3703603898u, +8759461u, 2540185277u, +1152198475u, 2038345882u, +2503579743u, 1446869792u, +2019419351u, 4051584612u, +3178289407u, 3992503830u, +2879018745u, 2719373510u, +700836153u, 1675560450u, +4121245793u, 2064715719u, +343595772u, 1996164093u, +3130433948u, 405251683u, +2804817126u, 1607133689u, +463852893u, 2864244470u, +2224044848u, 4071581802u, +2537107938u, 2246347953u, +3207234525u, 2028708916u, +2272418128u, 803575837u, +38655481u, 2170452091u, +3272166407u, 557660441u, +4019147902u, 3841480082u, +298459606u, 2600943364u, +2440657523u, 255451671u, +3424361375u, 779434428u, +3088526123u, 490671625u, +1322855877u, 3452203069u, +3057021940u, 2285701422u, +2014993457u, 2390431709u, +2002090272u, 1568745354u, +1783152480u, 823305654u, +4053862835u, 2200236540u, +3009412313u, 3184047862u, +3032187389u, 4159715581u, +2966902888u, 252986948u, +1849329144u, 3160134214u, +3420960112u, 3198900547u, +749160960u, 379139040u, +1208883495u, 1566527339u, +3006227299u, 4194096960u, +556075248u, 497404038u, +1717327230u, 1496132623u, +1775955687u, 1719108984u, +1014328900u, 4189966956u, +2108574735u, 2584236470u, +684087286u, 531310503u, +4264509527u, 773405691u, +3088905079u, 3456882941u, +3105682208u, 3382290593u, +2289363624u, 3296306400u, +4168438718u, 467441309u, +777173623u, 3241407531u, +1183994815u, 1132983260u, +1610606159u, 2540270567u, +2649684057u, 1397502982u, +146657385u, 3318434267u, +2109315753u, 3348545480u, +3193669211u, 811750340u, +1073256162u, 3571673088u, +546596661u, 1017047954u, +3403136990u, 2540585554u, +1477047647u, 4145867423u, +2826408201u, 3531646869u, +784952939u, 943914610u, +2717443875u, 3657384638u, +1806867885u, 1903578924u, +3985088434u, 1911188923u, +1764002686u, 3672748083u, +1832925325u, 241574049u, +519948041u, 3181425568u, +2939747257u, 1634174593u, +3429894862u, 3529565564u, +1089679033u, 240953857u, +2025369941u, 2695166650u, +517086873u, 2964595704u, +3017658263u, 3828377737u, +2144895011u, 994799311u, +1184683823u, 4260564140u, +308018483u, 4262383425u, +1374752558u, 3431057723u, +1572637805u, 383233885u, +3188015819u, 4051263539u, +233319221u, 3794788167u, +2017406667u, 919677938u, +4074952232u, 1683612329u, +4213676186u, 327142514u, +3032591014u, 4204155962u, +206775997u, 2283918569u, +2395147154u, 3427505379u, +2211319468u, 4153726847u, +2217060665u, 350160869u, +2493667051u, 1648200185u, +3441709766u, 1387233546u, +140980u, 1891558063u, +760080239u, 2088061981u, +1580964938u, 740563169u, +422986366u, 330624974u, +4264507722u, 150928357u, +2738323042u, 2948665536u, +918718096u, 376390582u, +3966098971u, 717653678u, +3219466255u, 3799363969u, +3424344721u, 3187805406u, +375347278u, 3490350144u, +1992212097u, 2263421398u, +3855037968u, 1928519266u, +3866327955u, 1129127000u, +1782515131u, 2746577402u, +3059200728u, 2108753646u, +2738070963u, 1336849395u, +1705302106u, 768287270u, +1343511943u, 2247006571u, +1956142255u, 1780259453u, +3475618043u, 212490675u, +622521957u, 917121602u, +1852992332u, 1267987847u, +3170016833u, 2549835613u, +3299763344u, 2864033668u, +3378768767u, 1236609378u, +4169365948u, 3738062408u, +2661022773u, 2006922227u, +2760592161u, 3828932355u, +2636387819u, 2616619070u, +1237256330u, 3449066284u, +2871755260u, 3729280948u, +3862686086u, 431292293u, +3285899651u, 786322314u, +2531158535u, 724901242u, +2377363130u, 1415970351u, +1244759631u, 3263135197u, +965248856u, 174024139u, +2297418515u, 2954777083u, +987586766u, 3206261120u, +4059515114u, 3903854066u, +1931934525u, 2287507921u, +1827135136u, 1781944746u, +574617451u, 2299034788u, +2650140034u, 4081586725u, +2482286699u, 1109175923u, +458483596u, 618705848u, +4059852729u, 1813855658u, +4190721328u, 1129462471u, +4089998050u, 3575732749u, +2375584220u, 1037031473u, +1623777358u, 3389003793u, +546597541u, 352770237u, +1383747654u, 3122687303u, +1646071378u, 1164309901u, +290870767u, 830691298u, +929335420u, 3193251135u, +989577914u, 3626554867u, +591974737u, 3996958215u, +3163711272u, 3071568023u, +1516846461u, 3656006011u, +2698625268u, 2510865430u, +340274176u, 1167681812u, +3698796465u, 3155218919u, +4102288238u, 1673474350u, +3069708839u, 2704165015u, +1237411891u, 1854985978u, +3646837503u, 3625406022u, +921552000u, 1712976649u, +3939149151u, 878608872u, +3406359248u, 1068844551u, +1834682077u, 4155949943u, +2437686324u, 3163786257u, +2645117577u, 1988168803u, +747285578u, 1626463554u, +1235300371u, 1256485167u, +1914142538u, 4141546431u, +3838102563u, 582664250u, +1883344352u, 2083771672u, +2611657933u, 2139079047u, +2250573853u, 804336148u, +3066325351u, 2770847216u, +4275641370u, 1455750577u, +3346357270u, 1674051445u, +601221482u, 3992583643u, +1402445097u, 3622527604u, +2509017299u, 2966108111u, +2557027816u, 900741486u, +1790771021u, 2912643797u, +2631381069u, 4014551783u, +90375300u, 300318232u, +3269968032u, 2679371729u, +2664752123u, 3517585534u, +3253901179u, 542270815u, +1188641600u, 365479232u, +2210121140u, 760762191u, +1273768482u, 1216399252u, +3484324231u, 4287337666u, +16322182u, 643179562u, +325675502u, 3652676161u, +3120716054u, 3330259752u, +1011990087u, 2990167340u, +1097584090u, 3262252593u, +1829409951u, 3665087267u, +1214854475u, 2134299399u, +3704419305u, 411263051u, +1625446136u, 549838529u, +4283196353u, 1342880802u, +3460621305u, 1967599860u, +4282843369u, 1275671016u, +2544665755u, 853593042u, +901109753u, 2682611693u, +110631633u, 797487791u, +1472073141u, 850464484u, +797089608u, 3286110054u, +350397471u, 2775631060u, +366448238u, 3842907484u, +2219863904u, 3623364733u, +1850985302u, 4009616991u, +294963924u, 3693536939u, +3061255808u, 1615375832u, +1920066675u, 4113028420u, +4032223840u, 2318423400u, +2701956286u, 4145497671u, +3991532344u, 2536338351u, +1679099863u, 1728968857u, +449740816u, 2686506989u, +685242457u, 97590863u, +3258354115u, 1502282913u, +1235084019u, 2151665147u, +528459289u, 231097464u, +2477280726u, 3651607391u, +2091754612u, 1178454681u, +980597335u, 1604483865u, +1842333726u, 4146839064u, +3213794286u, 2601416506u, +754220096u, 3571436033u, +488595746u, 1448097974u, +4004834921u, 238887261u, +3320337489u, 1416989070u, +2928916831u, 4093725287u, +186020771u, 2367569534u, +3046087671u, 4090084518u, +3548184546u, 679517009u, +1962659444u, 3539886328u, +4192003933u, 1678423485u, +3827951761u, 3086277222u, +2144472852u, 1390394371u, +2976322029u, 1574517163u, +3553313841u, 119173722u, +1702434637u, 1766260771u, +3629581771u, 1407497759u, +895654784u, 751439914u, +4008409498u, 215917713u, +1482103833u, 695551833u, +1288382231u, 2656990891u, +2581779077u, 1570750352u, +3710689053u, 1741390464u, +2666411616u, 3533987737u, +4289478316u, 3576119563u, +4118694920u, 108199666u, +3869794273u, 963183826u, +2081410737u, 3796810515u, +791123882u, 2525792704u, +1036883117u, 136547246u, +875691100u, 2592925324u, +614302599u, 3013176417u, +2689342539u, 427154472u, +532957601u, 1228758574u, +1898117151u, 1181643858u, +1908591042u, 1464255968u, +446980910u, 2984611177u, +58509511u, 1046943619u, +3508927906u, 2001585786u, +2544767379u, 1525438381u, +552181222u, 1959725830u, +879448844u, 1348536411u, +4242243590u, 2861338018u, +1082052441u, 1034351453u, +601175800u, 764077711u, +530635011u, 3785343245u, +2178026726u, 117256687u, +2378297261u, 457568934u, +76438221u, 4104954272u, +956793873u, 3783168634u, +2485968477u, 2381948487u, +4226929450u, 3148473363u, +2518273601u, 3569490233u, +879369091u, 2180270337u, +3674375989u, 1387729170u, +977997984u, 4270646856u, +568650985u, 951677556u, +4213877384u, 2721005055u, +1073364549u, 2563403831u, +1678669911u, 66786703u, +2273631661u, 1149351924u, +3651298990u, 1581883443u, +246723096u, 1895026827u, +3810605772u, 3711056516u, +4058833288u, 2193790614u, +2080120290u, 3638638708u, +2915672708u, 2263003308u, +2361934197u, 4136767460u, +1976115991u, 3448840877u, +2019238520u, 225333538u, +874340815u, 2976159827u, +1555273378u, 3797521928u, +1942347150u, 3262952567u, +435997738u, 340403353u, +2817830907u, 2078619498u, +749534111u, 1178073973u, +894654712u, 3361226032u, +841092198u, 3288261538u, +1696412169u, 1496966875u, +697501571u, 1059158875u, +3739946319u, 2481012988u, +568983526u, 114945840u, +1559249010u, 2218244008u, +2841706923u, 1632780103u, +4020169654u, 2087949619u, +2438736103u, 24032648u, +833416317u, 3787017905u, +2373238993u, 2575395164u, +3434544481u, 3228481067u, +2542976862u, 2971726178u, +2880371864u, 3642087909u, +2407477975u, 2239080836u, +1043714217u, 3894199764u, +2235879182u, 203853421u, +2933669448u, 2504940536u, +834683330u, 425935223u, +3560796393u, 3565833278u, +1668000829u, 3683399154u, +3414330886u, 1748785729u, +1023171602u, 580966986u, +2531038985u, 3227325488u, +2657385925u, 2124704694u, +233442446u, 1107045577u, +3407293834u, 552770757u, +3899097693u, 1067532701u, +115667924u, 1406028344u, +1707768231u, 3724015962u, +2419657149u, 18613994u, +2532882091u, 3476683808u, +1560838678u, 811220224u, +895961699u, 3762914298u, +1328752423u, 1844996900u, +1420427894u, 1848067707u, +1210281744u, 904215228u, +4055325594u, 1118521573u, +2496554183u, 2579259919u, +3996647489u, 3657647605u, +325254059u, 3136157065u, +3951522674u, 4052925250u, +3341068436u, 2287683323u, +1313073005u, 126005630u, +2505120084u, 1194725057u, +853746559u, 3555092974u, +2689238752u, 49515858u, +1244776042u, 1069300695u, +61073168u, 1010661841u, +1269521335u, 1902040126u, +990632502u, 2378708922u, +3858321250u, 1400735275u, +2974699176u, 2771676666u, +170995186u, 2877798589u, +545726212u, 2225229957u, +1086473152u, 3454177594u, +3859483262u, 1499729584u, +2088002891u, 2883475137u, +3222194252u, 4144472319u, +2212229854u, 4146740722u, +567988835u, 1051332394u, +3932046135u, 542648229u, +3017852446u, 1277887997u, +162888005u, 1669710469u, +1492500905u, 553041029u, +1434876932u, 533989516u, +3817492747u, 584127807u, +4147115982u, 2993670925u, +4020312558u, 710021255u, +3509733475u, 3587959456u, +2088550465u, 1745399498u, +2952242967u, 1259815443u, +869648362u, 1404723176u, +3947542735u, 1334333531u, +3873471582u, 229399758u, +59634866u, 3239516985u, +3844250972u, 1275954779u, +492891666u, 1029533080u, +1552951157u, 367320647u, +699480890u, 3684418197u, +3707014310u, 471105777u, +1824587258u, 4030809053u, +3489914436u, 484559105u, +1235750398u, 1428453396u, +4230459084u, 4255931645u, +1848597055u, 4271715616u, +331780381u, 482425775u, +2435323270u, 3171911678u, +3507210587u, 928543347u, +4197807526u, 3680046204u, +2766042024u, 2159512867u, +179373257u, 313902234u, +4024837592u, 294795361u, +1622282562u, 647086234u, +2825039429u, 577214736u, +4043412446u, 2426981244u, +1277736097u, 1130129573u, +2601395338u, 995791646u, +36668922u, 3344746679u, +1521532225u, 1645086060u, +2622763015u, 4122335794u, +2936887705u, 494465807u, +2580840343u, 1064648931u, +1247887787u, 2752145076u, +1277612417u, 1249660507u, +2288678613u, 3312498873u, +2459273912u, 4238535494u, +3117488020u, 2571979978u, +2680188909u, 1471227427u, +1616494033u, 633688562u, +2268653416u, 3268237290u, +3021962815u, 1959779970u, +3321382074u, 766642813u, +204429780u, 1323319858u, +3676032891u, 1380896111u, +4030639049u, 3647601207u, +1830028502u, 2830263774u, +1375962216u, 1733961041u, +939765180u, 521947915u, +3903267364u, 497472767u, +1619700946u, 189164145u, +3115593885u, 486382294u, +1262445920u, 4062496162u, +2464795849u, 3770038872u, +4032121374u, 3235740744u, +3757765258u, 1777199847u, +2167243108u, 1912506671u, +4180515317u, 2276864677u, +536034089u, 2384915026u, +162938278u, 1588060152u, +4018349945u, 2504457929u, +841450426u, 2790120722u, +2719983588u, 1471020554u, +1390856732u, 3623212998u, +2506944218u, 1035080801u, +348812127u, 3026631806u, +746483541u, 2342164722u, +122104390u, 4074122771u, +3986865419u, 1674890530u, +3693306023u, 3011542850u, +1294951725u, 899303190u, +3577146915u, 3549160092u, +1241677652u, 4290680005u, +3193053279u, 2029187390u, +3298063095u, 3943068002u, +3946220635u, 2273781461u, +889053698u, 1376304022u, +1486839612u, 2127663659u, +344127443u, 1646681121u, +2780117810u, 2142045764u, +2694572773u, 447810651u, +2185527146u, 2366308558u, +290335413u, 584901173u, +2012370276u, 970504950u, +3258236042u, 2008155560u, +3945579565u, 614796295u, +24452072u, 2695940969u, +3983727134u, 3444688454u, +1327044473u, 3545633451u, +1875293322u, 1739318893u, +1707527799u, 2683090634u, +2848082386u, 2814622471u, +4111401777u, 2774816580u, +3849839194u, 437560100u, +2238350150u, 2462124836u, +665017710u, 512012738u, +2945294779u, 3305170944u, +819477765u, 59419271u, +155125658u, 665292744u, +444722813u, 3580039116u, +2355675635u, 663735032u, +3247800169u, 1579404983u, +1985115003u, 3397891494u, +358696453u, 1474896279u, +516388613u, 710590371u, +3490497111u, 2514565805u, +2386143445u, 477509654u, +412854590u, 3624609754u, +3214388668u, 3516075816u, +2731288520u, 1369482895u, +4033204378u, 1314000850u, +829769325u, 1935166880u, +1608191643u, 2607067237u, +423820371u, 3257747610u, +1355298041u, 3776931214u, +4105054901u, 2107080812u, +1911521879u, 3183054185u, +3910177801u, 675129307u, +1209358971u, 4205727791u, +1435726287u, 3333261712u, +1400982708u, 1154611403u, +1663501483u, 2837596667u, +3164734053u, 2759854023u, +4012043629u, 1963228038u, +3981675284u, 2677557877u, +520119591u, 505138315u, +897271356u, 1803966773u, +1016663294u, 616691903u, +2254742522u, 4032705384u, +2468470796u, 798395739u, +3025169002u, 3570037122u, +1461093710u, 3473799845u, +3702624858u, 476400898u, +1043039728u, 2304070437u, +181576948u, 602972493u, +3996616030u, 3289878097u, +2068516226u, 3922247304u, +1299968266u, 2520311409u, +1968824721u, 3214794876u, +1581813122u, 2668800905u, +3297613974u, 748160407u, +1145536484u, 1326769504u, +2973323521u, 3775262814u, +3218653169u, 902775872u, +3498603433u, 1372805534u, +704686363u, 3626542352u, +2271580579u, 1213925114u, +46329775u, 3009384989u, +1330254048u, 1194824134u, +514204310u, 3781981134u, +442526164u, 2835608783u, +3460471867u, 510634034u, +546406434u, 2716786748u, +2840500021u, 1669490957u, +2536189149u, 3251421224u, +1358736072u, 1089334066u, +3260749330u, 250756920u, +2974806681u, 1513718866u, +82635635u, 4041016629u, +3391765744u, 2495807367u, +3962674316u, 2822889695u, +753413337u, 2008251381u, +3123390177u, 106212622u, +490570565u, 1684884205u, +793892547u, 1927268995u, +2344148164u, 2251978818u, +437424236u, 2774023200u, +2674940754u, 3788056262u, +2597882666u, 3678660147u, +3797434193u, 3838215866u, +279687080u, 2656772270u, +2190204787u, 1997584981u, +3384401882u, 3160208845u, +3629379425u, 2668998785u, +1050036757u, 2954162084u, +917091826u, 1744374041u, +1454282570u, 845687881u, +2997173625u, 776018378u, +1137560602u, 1938378389u, +1748082354u, 2066910012u, +2677675207u, 918315064u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; { uint64_t h = farmhashte::Hash64WithSeeds(data, len++, SEED0, SEED1); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashte::Hash64WithSeed(data, len++, SEED); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashte::Hash64(data, len++); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } len -= 3; return alive > 0; } +{ uint64_t h = farmhashte::Hash64WithSeeds(data + offset, len, SEED0, SEED1); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashte::Hash64WithSeed(data + offset, len, SEED); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashte::Hash64(data + offset, len); Check(h >> 32); Check((h << 32) >> 32); } + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashteTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +{ uint64_t h = farmhashte::Hash64WithSeeds(data + offset, len, SEED0, SEED1); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashte::Hash64WithSeed(data + offset, len, SEED); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashte::Hash64(data + offset, len); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashteTest + +#if TESTING + +static int farmhashteTestResult = farmhashteTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashteTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashteTest::Dump(0, i); + } + farmhashteTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashuoTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +3277735313u, 2681724312u, +2598464059u, 797982799u, +2603993599u, 921001710u, +1410420968u, 2134990486u, +2914424215u, 2244477846u, +255297188u, 2992121793u, +161451183u, 3943596029u, +4019337850u, 452431531u, +3379021470u, 2557197665u, +299850021u, 2532580744u, +1298374911u, 3099673830u, +2199864459u, 3696623795u, +4055299123u, 3281581178u, +1053458494u, 1882212500u, +3456121707u, 275903667u, +458884671u, 3033004529u, +1898235244u, 1402319660u, +2700149065u, 2699376854u, +2433714046u, 4222949502u, +4220361840u, 1712034059u, +4148372108u, 1330324210u, +594028478u, 2921867846u, +780716741u, 1728752234u, +3280331829u, 326029180u, +393215742u, 3349570000u, +3824583307u, 1612122221u, +1379537552u, 1646032583u, +2233466664u, 1432476832u, +2052294713u, 3552092450u, +1628777059u, 1499109081u, +2960536756u, 1554038301u, +1145519619u, 3190844552u, +237495366u, 540224401u, +65721842u, 489963606u, +1596489240u, 1562872448u, +1790705123u, 2128624475u, +1435705557u, 1262831810u, +155445229u, 1672724608u, +663607706u, 2077310004u, +3610042449u, 1911523866u, +2563776023u, 294527927u, +1099072299u, 1389770549u, +2952353448u, 2026137563u, +3603803785u, 629449419u, +226132789u, 2489287368u, +1552847036u, 645684964u, +187883449u, 230403464u, +3151491850u, 3272648435u, +2002861219u, 165370827u, +916494250u, 1230085527u, +3807265751u, 3628174014u, +231181488u, 851743255u, +2988893883u, 1554380634u, +1142264800u, 3667013118u, +2638023604u, 2290487377u, +732137533u, 1909203251u, +1380301172u, 1498556724u, +4072067757u, 4165088768u, +3931792696u, 197618179u, +956300927u, 914413116u, +2148126371u, 1913303225u, +3074915312u, 3117299654u, +3178848746u, 1357272220u, +1438494951u, 507436733u, +3854939912u, 1413396341u, +126024219u, 146044391u, +3254014218u, 429095991u, +165589978u, 1578546616u, +623071693u, 366414107u, +249776086u, 1207522198u, +3120876698u, 1684583131u, +46987739u, 1157614300u, +199124911u, 520792961u, +3614377032u, 586863115u, +1716848157u, 4033596884u, +1164298657u, 4140791139u, +3824360466u, 3242407770u, +3725511003u, 232064808u, +2736953692u, 816692935u, +512845449u, 3748861010u, +4290630595u, 517646945u, +22638523u, 648000590u, +1283799121u, 3047062993u, +1024246061u, 4027776454u, +834785312u, 382936554u, +411505255u, 1973395102u, +580988377u, 2826990641u, +3474970689u, 1029055034u, +2584372201u, 1758123094u, +589567754u, 325737734u, +3886113119u, 3338548567u, +257578986u, 3698087965u, +3604937815u, 3198590202u, +2305332220u, 191910725u, +4244322689u, 542201663u, +3315355162u, 2135941665u, +1175961330u, 3948351189u, +23075771u, 3252374102u, +713127376u, 1467786451u, +663013031u, 3444053918u, +3077742128u, 1062268187u, +2115441882u, 4081398201u, +2335576331u, 2560479831u, +1379288194u, 4225182569u, +3958366652u, 3067277639u, +3667516477u, 1709989541u, +4188504038u, 59581167u, +2725013602u, 3639843023u, +3758739543u, 4189944477u, +2470483982u, 877580602u, +3252925478u, 2062343506u, +3981838403u, 3762572073u, +2931588131u, 3284356565u, +1129162571u, 732225574u, +1677744031u, 3532596884u, +3232041815u, 1652884780u, +3740979556u, 259034107u, +2227121257u, 1426140634u, +315836068u, 3200749877u, +1386256573u, 24035717u, +234531934u, 1115203611u, +1598686658u, 3146815575u, +2632963283u, 1778459926u, +739944537u, 579625482u, +1231665285u, 2457048126u, +3903349120u, 389846205u, +1052645068u, 862072556u, +2834153464u, 1481069623u, +1688445808u, 701920051u, +3740748788u, 3388062747u, +1192588249u, 1410111809u, +2633463887u, 4050419847u, +2513844237u, 2187968410u, +2951683019u, 3015806005u, +3000537321u, 479241367u, +252167538u, 1231057113u, +393624922u, 704325635u, +1467197045u, 2066433573u, +3082385053u, 735688735u, +956434529u, 4028590195u, +4074716319u, 813690332u, +2124740535u, 804073145u, +3748881891u, 3195309868u, +841856605u, 2585865274u, +881504822u, 3254795114u, +1241815736u, 970796142u, +497175391u, 3929484338u, +4264993211u, 1835322201u, +3675707838u, 4205965408u, +300298607u, 3858319990u, +3255259608u, 4247675634u, +1095823272u, 1197245408u, +1742785722u, 785893184u, +1702965674u, 850401405u, +4084891412u, 1160134629u, +2555998391u, 1972759056u, +710625654u, 4116162021u, +3352753742u, 85121177u, +3513255468u, 2487440590u, +2480032715u, 2287747045u, +4090178629u, 4044418876u, +1703944517u, 486290428u, +1515601363u, 2011102035u, +573985957u, 3536053779u, +4257172787u, 576405853u, +1523550693u, 1014952061u, +711098718u, 2425317635u, +3460807169u, 3688987163u, +3167371443u, 3062418115u, +3330028292u, 1713171303u, +526177822u, 2602636307u, +1245357025u, 3346699703u, +254529415u, 4017058800u, +1829738451u, 2164236533u, +1381258384u, 4036354071u, +1749181924u, 4118435443u, +4130543625u, 688472265u, +2731071299u, 2547657502u, +4175776103u, 338058159u, +3729582129u, 4181845558u, +2227290760u, 146827036u, +2459178427u, 1025353883u, +4284109679u, 2502815838u, +825124804u, 2533140036u, +616327404u, 2448966429u, +413992636u, 2334782461u, +2239052397u, 1082330589u, +3381164715u, 199381437u, +2583939798u, 2848283092u, +2300168091u, 2156336315u, +1855234968u, 1982110346u, +2482046810u, 3158163887u, +2168864636u, 133643215u, +3904021624u, 3646514568u, +1176127003u, 4015671361u, +100525019u, 3534706803u, +2045051700u, 3452457457u, +1492267772u, 2308393828u, +3776661467u, 3281002516u, +4246334524u, 743955039u, +685631442u, 1147951686u, +2040912376u, 2911148054u, +3204999386u, 2415262714u, +313209105u, 777065474u, +2729416454u, 1029284767u, +1632078298u, 1817552554u, +3963877000u, 1919171906u, +3843219958u, 3073580867u, +865146997u, 2473524405u, +2593817617u, 3643076308u, +617975720u, 3671562289u, +121812599u, 2902367378u, +2260041112u, 1019936943u, +320945955u, 2337845588u, +1535670711u, 773792826u, +3152195900u, 4090794518u, +4142587430u, 484732447u, +419191319u, 3377973345u, +324924679u, 2809286837u, +1562277603u, 1378362199u, +3791551981u, 1097806406u, +1386297408u, 2304900033u, +3574135531u, 4017075736u, +1161238398u, 1358056883u, +3487080616u, 1516736273u, +851615042u, 2927899494u, +4077452507u, 2984955003u, +3907754394u, 3578173844u, +1948409509u, 1923531348u, +3578472493u, 3710074193u, +213811117u, 2586632018u, +1922589216u, 274958014u, +1654276320u, 2687561076u, +2569061755u, 3122046057u, +3203802620u, 1692079268u, +477806878u, 140587742u, +520565830u, 3674576684u, +91246882u, 1010215946u, +3703994407u, 69201295u, +776213083u, 3677771507u, +3696902571u, 3593730713u, +2907901228u, 3239753796u, +1586241476u, 1939595371u, +2268396558u, 3468719670u, +2955715987u, 2295501078u, +2775848696u, 1358532390u, +3843660102u, 82177963u, +4094477877u, 191727221u, +278661677u, 2889101923u, +1352525614u, 2844977667u, +1261140657u, 1747052377u, +2334120653u, 645125282u, +2707230761u, 4142246789u, +1068639717u, 2288162940u, +1419293714u, 1636932722u, +3252686293u, 318543902u, +2598802768u, 2025886508u, +2250788464u, 2711763065u, +255108733u, 1845587644u, +3719270134u, 3940707863u, +917608574u, 1363253259u, +788659330u, 673256220u, +109793386u, 341851980u, +2698465479u, 3011229884u, +3886713057u, 1276595523u, +2439962760u, 2700515456u, +205970448u, 4198897105u, +875511891u, 371715572u, +1033680610u, 4278599955u, +3120038721u, 1256300069u, +225629455u, 3735275001u, +3961944123u, 1769389163u, +1074553219u, 668829411u, +1098679359u, 2573697509u, +1885071395u, 4233785523u, +2513878053u, 2030193788u, +360256231u, 107312677u, +310517502u, 2618936366u, +3600462351u, 1216645846u, +2970730323u, 4278812598u, +94638678u, 1689811113u, +4125738800u, 3103759730u, +103797041u, 1397772514u, +1669653333u, 572567964u, +1612872497u, 2986430557u, +214990655u, 3117607990u, +1949717207u, 2260498180u, +1493936866u, 3554860960u, +1582022990u, 1263709570u, +1244120487u, 3416600761u, +224580012u, 4252610345u, +286306391u, 814956796u, +1820532305u, 2776960536u, +3082703465u, 1659265982u, +1157398049u, 3752806924u, +3508246460u, 2902716664u, +2110942730u, 3267551635u, +902835431u, 405228165u, +2341378668u, 2220373824u, +3303626294u, 1175118221u, +383517064u, 896322512u, +1697257567u, 2202820683u, +1837689083u, 1336556841u, +914535232u, 3634083711u, +1537187340u, 3712582785u, +1088201893u, 3270984620u, +3093195926u, 2561488770u, +1962968100u, 236189500u, +2549435227u, 536047554u, +422609195u, 2958815818u, +4166184983u, 3528794619u, +1042329086u, 3914176886u, +1359641525u, 1197100813u, +1269739674u, 3301844628u, +1831223342u, 3329594980u, +2433669782u, 494908536u, +1441714797u, 1174785921u, +1933050423u, 958901065u, +868981704u, 2163330264u, +3243110680u, 1443133429u, +1145009669u, 2520062970u, +3851564853u, 2664619323u, +2686075657u, 4042710240u, +2125408249u, 4165697916u, +2328730865u, 3160666939u, +588683409u, 2126275847u, +829404209u, 1878067032u, +2567792910u, 897670516u, +934312076u, 2952135524u, +504832490u, 3312698056u, +4015478120u, 1265237690u, +3376133707u, 967674402u, +1553513184u, 834830418u, +2396504772u, 3278582098u, +2950762705u, 4277111759u, +4159211303u, 1290097509u, +251949572u, 3065234219u, +1832020534u, 312136369u, +1103404394u, 198182691u, +1369599600u, 3906710870u, +2954651680u, 1535115487u, +2389327507u, 1813520230u, +632704095u, 140762681u, +3123202913u, 3336005523u, +1287729497u, 2198985327u, +2470730783u, 3821758006u, +1009870118u, 559702706u, +4274686257u, 3187546567u, +2402308151u, 1096697687u, +678932329u, 3716363135u, +2899950405u, 3046872820u, +3754655641u, 2021741414u, +1993425202u, 2185339100u, +2838253700u, 3099212100u, +1679617763u, 3703603898u, +1135665833u, 3559875668u, +2503579743u, 1446869792u, +879818611u, 3788305533u, +2879018745u, 2719373510u, +3606051203u, 2166567748u, +343595772u, 1996164093u, +1577656121u, 475248376u, +463852893u, 2864244470u, +1332049663u, 3326459767u, +3207234525u, 2028708916u, +938916154u, 3115246264u, +3272166407u, 557660441u, +1265684026u, 245033807u, +2440657523u, 255451671u, +3811885130u, 1399880284u, +1322855877u, 3452203069u, +1324994449u, 3796404024u, +2002090272u, 1568745354u, +3700047753u, 31799506u, +3009412313u, 3184047862u, +728680761u, 3848624873u, +1849329144u, 3160134214u, +1272923193u, 1474278816u, +1208883495u, 1566527339u, +4136466541u, 630825649u, +1717327230u, 1496132623u, +2449386742u, 128106940u, +2108574735u, 2584236470u, +2872246579u, 397338552u, +3088905079u, 3456882941u, +1715915153u, 2940716269u, +4168438718u, 467441309u, +872996731u, 3206901319u, +1610606159u, 2540270567u, +1301658081u, 2379410194u, +2109315753u, 3348545480u, +2041927873u, 2644077493u, +546596661u, 1017047954u, +2596792972u, 2783958892u, +2826408201u, 3531646869u, +2219352672u, 4217451852u, +1806867885u, 1903578924u, +2076465705u, 2373061493u, +1832925325u, 241574049u, +1509517110u, 3703614272u, +3429894862u, 3529565564u, +4010000614u, 2256197939u, +517086873u, 2964595704u, +3501035294u, 4079457298u, +1184683823u, 4260564140u, +2339268412u, 3871564102u, +1572637805u, 383233885u, +3351411126u, 3419328182u, +2017406667u, 919677938u, +29804156u, 46276077u, +3032591014u, 4204155962u, +1172319502u, 969309871u, +2211319468u, 4153726847u, +3094193193u, 4240669441u, +3441709766u, 1387233546u, +4048882438u, 1217896566u, +1580964938u, 740563169u, +3691850348u, 3176426539u, +2738323042u, 2948665536u, +1474029445u, 3513354882u, +3219466255u, 3799363969u, +3961796122u, 1055550923u, +1992212097u, 2263421398u, +4289759174u, 2516844140u, +1782515131u, 2746577402u, +721928440u, 3529570984u, +1705302106u, 768287270u, +3474902815u, 4000011125u, +3475618043u, 212490675u, +549130471u, 2970128275u, +3170016833u, 2549835613u, +3691104824u, 2694324482u, +4169365948u, 3738062408u, +602930397u, 2148954730u, +2636387819u, 2616619070u, +301617872u, 374657036u, +3862686086u, 431292293u, +4225245165u, 1358580562u, +2377363130u, 1415970351u, +3885060756u, 1438379807u, +2297418515u, 2954777083u, +3970368221u, 1229801760u, +1931934525u, 2287507921u, +1713471510u, 2145608111u, +2650140034u, 4081586725u, +4196863572u, 1896558394u, +4059852729u, 1813855658u, +2618400836u, 1396056469u, +2375584220u, 1037031473u, +249284003u, 2450077637u, +1383747654u, 3122687303u, +2664431743u, 3855028730u, +929335420u, 3193251135u, +137313762u, 1850894384u, +3163711272u, 3071568023u, +418541677u, 3621223039u, +340274176u, 1167681812u, +4106647531u, 4022465625u, +3069708839u, 2704165015u, +2332023349u, 641449034u, +921552000u, 1712976649u, +1876484273u, 2343049860u, +1834682077u, 4155949943u, +2061821157u, 4240649383u, +747285578u, 1626463554u, +165503115u, 359629739u, +3838102563u, 582664250u, +3878924635u, 4117237498u, +2250573853u, 804336148u, +331393443u, 4242530387u, +3346357270u, 1674051445u, +3348019777u, 1722242971u, +2509017299u, 2966108111u, +4189102509u, 3323592310u, +2631381069u, 4014551783u, +4250787412u, 3448394212u, +2664752123u, 3517585534u, +3605365141u, 1669471183u, +2210121140u, 760762191u, +249697459u, 3416920106u, +16322182u, 643179562u, +1564226597u, 2134630675u, +1011990087u, 2990167340u, +2349550842u, 1642428946u, +1214854475u, 2134299399u, +2704221532u, 2104175211u, +4283196353u, 1342880802u, +198529755u, 2004468390u, +2544665755u, 853593042u, +2090611294u, 2970943872u, +1472073141u, 850464484u, +1407609278u, 3062461105u, +366448238u, 3842907484u, +488797416u, 1432670231u, +294963924u, 3693536939u, +3390549825u, 1583234720u, +4032223840u, 2318423400u, +2965642867u, 930822729u, +1679099863u, 1728968857u, +900822335u, 702309817u, +3258354115u, 1502282913u, +2811888503u, 3924947660u, +2477280726u, 3651607391u, +3788310204u, 1300369123u, +1842333726u, 4146839064u, +2468893861u, 4091095953u, +488595746u, 1448097974u, +1159634090u, 1738834113u, +2928916831u, 4093725287u, +530850094u, 291657799u, +3548184546u, 679517009u, +399175380u, 2658337143u, +3827951761u, 3086277222u, +2067718397u, 3632376023u, +3553313841u, 119173722u, +1702434637u, 1766260771u, +895654784u, 751439914u, +4008409498u, 215917713u, +1288382231u, 2656990891u, +2581779077u, 1570750352u, +2666411616u, 3533987737u, +4289478316u, 3576119563u, +3869794273u, 963183826u, +2081410737u, 3796810515u, +1036883117u, 136547246u, +875691100u, 2592925324u, +2689342539u, 427154472u, +532957601u, 1228758574u, +1908591042u, 1464255968u, +446980910u, 2984611177u, +3508927906u, 2001585786u, +2544767379u, 1525438381u, +879448844u, 1348536411u, +4242243590u, 2861338018u, +601175800u, 764077711u, +530635011u, 3785343245u, +2378297261u, 457568934u, +76438221u, 4104954272u, +2485968477u, 2381948487u, +4226929450u, 3148473363u, +879369091u, 2180270337u, +3674375989u, 1387729170u, +568650985u, 951677556u, +4213877384u, 2721005055u, +1678669911u, 66786703u, +2273631661u, 1149351924u, +246723096u, 1895026827u, +3810605772u, 3711056516u, +2080120290u, 3638638708u, +2915672708u, 2263003308u, +1976115991u, 3448840877u, +2019238520u, 225333538u, +1555273378u, 3797521928u, +1942347150u, 3262952567u, +2817830907u, 2078619498u, +749534111u, 1178073973u, +841092198u, 3288261538u, +1696412169u, 1496966875u, +3739946319u, 2481012988u, +568983526u, 114945840u, +2841706923u, 1632780103u, +4020169654u, 2087949619u, +833416317u, 3787017905u, +2373238993u, 2575395164u, +2542976862u, 2971726178u, +2880371864u, 3642087909u, +1043714217u, 3894199764u, +2235879182u, 203853421u, +834683330u, 425935223u, +3560796393u, 3565833278u, +3414330886u, 1748785729u, +1023171602u, 580966986u, +2657385925u, 2124704694u, +233442446u, 1107045577u, +3899097693u, 1067532701u, +115667924u, 1406028344u, +2419657149u, 18613994u, +2532882091u, 3476683808u, +895961699u, 3762914298u, +1328752423u, 1844996900u, +1210281744u, 904215228u, +4055325594u, 1118521573u, +3996647489u, 3657647605u, +325254059u, 3136157065u, +3341068436u, 2287683323u, +1313073005u, 126005630u, +853746559u, 3555092974u, +2689238752u, 49515858u, +61073168u, 1010661841u, +1269521335u, 1902040126u, +3858321250u, 1400735275u, +2974699176u, 2771676666u, +545726212u, 2225229957u, +1086473152u, 3454177594u, +2088002891u, 2883475137u, +3222194252u, 4144472319u, +567988835u, 1051332394u, +3932046135u, 542648229u, +162888005u, 1669710469u, +1492500905u, 553041029u, +3817492747u, 584127807u, +4147115982u, 2993670925u, +3509733475u, 3587959456u, +2088550465u, 1745399498u, +869648362u, 1404723176u, +3947542735u, 1334333531u, +59634866u, 3239516985u, +3844250972u, 1275954779u, +2512155003u, 1685649437u, +639306006u, 2524620206u, +576786501u, 655707039u, +2864351838u, 3736264674u, +1200907897u, 2384379464u, +15823708u, 206117476u, +1193310960u, 1093099415u, +3696538026u, 4112584792u, +2069527017u, 547588820u, +4178147211u, 2827259351u, +940846775u, 1054995047u, +2976960697u, 1934305529u, +2199137382u, 1005722394u, +1875867180u, 2064356511u, +4019734130u, 3096333006u, +2069509024u, 2906358341u, +2232866485u, 1456016086u, +1422674894u, 867282151u, +1612503136u, 1739843072u, +134947567u, 2978775774u, +1284167756u, 1090844589u, +831688783u, 2079216362u, +1626991196u, 3644714163u, +3678110059u, 898470030u, +3916646913u, 3182422972u, +3630426828u, 969847973u, +3427164640u, 3463937250u, +3044785046u, 897322257u, +3443872170u, 4185408854u, +2557463241u, 4080940424u, +2048168570u, 2429169982u, +3174690447u, 2513494106u, +1213061732u, 3143736628u, +3482268149u, 1250714337u, +31648125u, 3872383625u, +1565760579u, 36665130u, +751041229u, 2257179590u, +2915361862u, 280819225u, +2907818413u, 4254297769u, +3493178615u, 3755944354u, +4043533423u, 1134196225u, +4177134659u, 127246419u, +2442615581u, 923049607u, +1004426206u, 782768297u, +2410586681u, 1430106871u, +4103323427u, 3168399477u, +3716682375u, 3616334719u, +3413209549u, 656672786u, +2876965944u, 182894450u, +456581318u, 2683752067u, +3877875910u, 3190666241u, +3240336907u, 4024807233u, +1681224377u, 1576191191u, +3599250276u, 2381111980u, +3495321877u, 3956024585u, +1611608524u, 3815677453u, +2062334396u, 1656117707u, +5457134u, 3234118251u, +470187419u, 2688566989u, +3259870297u, 660100446u, +442236198u, 2542452448u, +493137955u, 392411099u, +947967568u, 1234595917u, +4230082284u, 2762976773u, +2870085764u, 1455086530u, +2762099647u, 4011882747u, +1215981925u, 3227517889u, +3269061963u, 4037515364u, +3168911474u, 4255057396u, +2026092260u, 1736192508u, +3909727042u, 3114708966u, +1938800693u, 680793595u, +1525265867u, 2808224480u, +2122290603u, 1211197714u, +3520488321u, 3979192396u, +3540779343u, 4192918639u, +2736030448u, 1120335563u, +1698949078u, 3993310631u, +1966048551u, 2228221363u, +597941119u, 3498018399u, +393987327u, 454500547u, +1222959566u, 567151340u, +3774764786u, 1492844524u, +3308300614u, 805568076u, +868414882u, 177406999u, +1608110313u, 642061169u, +1027515771u, 3131251981u, +2851936150u, 4272755262u, +1532845092u, 709643652u, +682573592u, 1244104217u, +796769556u, 2500467040u, +3002618826u, 1112998535u, +1780193104u, 1243644607u, +3691719535u, 2958853053u, +466635014u, 2277292580u, +4082276003u, 1030800045u, +1750863246u, 379050598u, +3576413281u, 731493104u, +132259176u, 4115195437u, +1769890695u, 2715470335u, +1819263183u, 2028531518u, +2154809766u, 3672399742u, +76727603u, 4198182186u, +2304993586u, 1666387627u, +284366017u, 3359785538u, +3469807328u, 2926494787u, +3829072836u, 2493478921u, +3738499303u, 3311304980u, +932916545u, 2235559063u, +2909742396u, 1765719309u, +1456588655u, 508290328u, +1490719640u, 3356513470u, +2908490783u, 251085588u, +830410677u, 3172220325u, +3897208579u, 1940535730u, +151909546u, 2384458112u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; { uint64_t h = farmhashuo::Hash64WithSeed(data, len++, SEED); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashuo::Hash64(data, len++); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashuo::Hash64(data, len++); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } len -= 3; return alive > 0; } +{ uint64_t h = farmhashuo::Hash64WithSeed(data + offset, len, SEED); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashuo::Hash64(data + offset, len); Check(h >> 32); Check((h << 32) >> 32); } + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashuoTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +{ uint64_t h = farmhashuo::Hash64WithSeed(data + offset, len, SEED); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashuo::Hash64(data + offset, len); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashuoTest + +#if TESTING + +static int farmhashuoTestResult = farmhashuoTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashuoTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashuoTest::Dump(0, i); + } + farmhashuoTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif +#ifndef FARMHASH_SELF_TEST_GUARD +#define FARMHASH_SELF_TEST_GUARD +#include +#include +#include + +using std::cout; +using std::cerr; +using std::endl; +using std::hex; + +static const uint64_t kSeed0 = 1234567; +static const uint64_t kSeed1 = k0; +static const int kDataSize = 1 << 20; +static const int kTestSize = 300; +#define kSeed128 Uint128(kSeed0, kSeed1) + +static char data[kDataSize]; + +static int completed_self_tests = 0; +static int errors = 0; + +// Initialize data to pseudorandom values. +void Setup() { + if (completed_self_tests == 0) { + uint64_t a = 9; + uint64_t b = 777; + for (int i = 0; i < kDataSize; i++) { + a += b; + b += a; + a = (a ^ (a >> 41)) * k0; + b = (b ^ (b >> 41)) * k0 + i; + uint8_t u = b >> 37; + memcpy(data + i, &u, 1); // uint8_t -> char + } + } +} + +int NoteErrors() { +#define NUM_SELF_TESTS 9 + if (++completed_self_tests == NUM_SELF_TESTS) + std::exit(errors > 0); + return errors; +} + +template inline bool IsNonZero(T x) { + return x != 0; +} + +template <> inline bool IsNonZero(uint128_t x) { + return x != Uint128(0, 0); +} + +#endif // FARMHASH_SELF_TEST_GUARD + +namespace farmhashxoTest { + +uint32_t CreateSeed(int offset, int salt) { + uint32_t h = static_cast(salt & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h += static_cast(offset & 0xffffffff); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + h = h * c1; + h ^= (h >> 17); + return h; +} + +#undef SEED +#undef SEED1 +#undef SEED0 +#define SEED CreateSeed(offset, -1) +#define SEED0 CreateSeed(offset, 0) +#define SEED1 CreateSeed(offset, 1) + +#undef TESTING +#define TESTING 1 +#if TESTING +uint32_t expected[] = { +1140953930u, 861465670u, +3277735313u, 2681724312u, +2598464059u, 797982799u, +890626835u, 800175912u, +2603993599u, 921001710u, +1410420968u, 2134990486u, +3283896453u, 1867689945u, +2914424215u, 2244477846u, +255297188u, 2992121793u, +1110588164u, 4186314283u, +161451183u, 3943596029u, +4019337850u, 452431531u, +283198166u, 2741341286u, +3379021470u, 2557197665u, +299850021u, 2532580744u, +452473466u, 1706958772u, +1298374911u, 3099673830u, +2199864459u, 3696623795u, +236935126u, 2976578695u, +4055299123u, 3281581178u, +1053458494u, 1882212500u, +2305012065u, 2169731866u, +3456121707u, 275903667u, +458884671u, 3033004529u, +3058973506u, 2379411653u, +1898235244u, 1402319660u, +2700149065u, 2699376854u, +147814787u, 720739346u, +2433714046u, 4222949502u, +4220361840u, 1712034059u, +3425469811u, 3690733394u, +4148372108u, 1330324210u, +594028478u, 2921867846u, +1635026870u, 192883107u, +780716741u, 1728752234u, +3280331829u, 326029180u, +3969463346u, 1436364519u, +393215742u, 3349570000u, +3824583307u, 1612122221u, +2859809759u, 3808705738u, +1379537552u, 1646032583u, +2233466664u, 1432476832u, +4023053163u, 2650381482u, +2052294713u, 3552092450u, +1628777059u, 1499109081u, +3476440786u, 3829307897u, +2960536756u, 1554038301u, +1145519619u, 3190844552u, +2902102606u, 3600725550u, +237495366u, 540224401u, +65721842u, 489963606u, +1448662590u, 397635823u, +1596489240u, 1562872448u, +1790705123u, 2128624475u, +180854224u, 2604346966u, +1435705557u, 1262831810u, +155445229u, 1672724608u, +1669465176u, 1341975128u, +663607706u, 2077310004u, +3610042449u, 1911523866u, +1043692997u, 1454396064u, +2563776023u, 294527927u, +1099072299u, 1389770549u, +703505868u, 678706990u, +2952353448u, 2026137563u, +3603803785u, 629449419u, +1933894405u, 3043213226u, +226132789u, 2489287368u, +1552847036u, 645684964u, +3828089804u, 3632594520u, +187883449u, 230403464u, +3151491850u, 3272648435u, +3729087873u, 1303930448u, +2002861219u, 165370827u, +916494250u, 1230085527u, +3103338579u, 3064290191u, +3807265751u, 3628174014u, +231181488u, 851743255u, +2295806711u, 1781190011u, +2988893883u, 1554380634u, +1142264800u, 3667013118u, +1968445277u, 315203929u, +2638023604u, 2290487377u, +732137533u, 1909203251u, +440398219u, 1891630171u, +1380301172u, 1498556724u, +4072067757u, 4165088768u, +4204318635u, 441430649u, +3931792696u, 197618179u, +956300927u, 914413116u, +3010839769u, 2837339569u, +2148126371u, 1913303225u, +3074915312u, 3117299654u, +4139181436u, 2993479124u, +3178848746u, 1357272220u, +1438494951u, 507436733u, +667183474u, 2084369203u, +3854939912u, 1413396341u, +126024219u, 146044391u, +1016656857u, 3022024459u, +3254014218u, 429095991u, +990500595u, 3056862311u, +985653208u, 1718653828u, +623071693u, 366414107u, +1771289760u, 2293458109u, +3047342438u, 2991127487u, +3120876698u, 1684583131u, +3638043310u, 1170404994u, +863214540u, 1087193030u, +199124911u, 520792961u, +3169775996u, 1577421232u, +3331828431u, 1013201099u, +1716848157u, 4033596884u, +1770708857u, 4229339322u, +1146169032u, 1434258493u, +3824360466u, 3242407770u, +1926419493u, 2649785113u, +872586426u, 762243036u, +2736953692u, 816692935u, +1571283333u, 3555213933u, +2266795890u, 3781899767u, +4290630595u, 517646945u, +3006163611u, 2180594090u, +959214578u, 558910384u, +1283799121u, 3047062993u, +3830962609u, 2391606125u, +3544509313u, 622325861u, +834785312u, 382936554u, +1421463872u, 788479970u, +1825135056u, 2725923798u, +580988377u, 2826990641u, +247825043u, 3167748333u, +812546227u, 2506885666u, +2584372201u, 1758123094u, +1891789696u, 389974094u, +345313518u, 2022370576u, +3886113119u, 3338548567u, +1083486947u, 2583576230u, +1776047957u, 1771384107u, +3604937815u, 3198590202u, +3027522813u, 4155628142u, +4232136669u, 427759438u, +4244322689u, 542201663u, +1549591985u, 2856634168u, +556609672u, 45845311u, +1175961330u, 3948351189u, +4165739882u, 4194218315u, +1634635545u, 4151937410u, +713127376u, 1467786451u, +1327394015u, 2743592929u, +2638154051u, 810082938u, +3077742128u, 1062268187u, +4084325664u, 3810665822u, +3735739145u, 2794294783u, +2335576331u, 2560479831u, +690240711u, 997658837u, +2442302747u, 3948961926u, +3958366652u, 3067277639u, +2059157774u, 1211737169u, +1516711748u, 2339636583u, +4188504038u, 59581167u, +2767897792u, 1389679610u, +2658147000u, 2643979752u, +3758739543u, 4189944477u, +1454470782u, 100876854u, +2995362413u, 118817200u, +3252925478u, 2062343506u, +2804483644u, 3088828656u, +1231633714u, 4168280671u, +2931588131u, 3284356565u, +1255909792u, 3130054947u, +4173605289u, 1407328702u, +1677744031u, 3532596884u, +3162657845u, 3887208531u, +2256541290u, 3459463480u, +3740979556u, 259034107u, +392987633u, 3233195759u, +3606709555u, 3424793077u, +315836068u, 3200749877u, +4065431359u, 760633989u, +2982018998u, 1811050648u, +234531934u, 1115203611u, +3897494162u, 1516407838u, +1603559457u, 323296368u, +2632963283u, 1778459926u, +2879836826u, 2146672889u, +3486330348u, 492621815u, +1231665285u, 2457048126u, +3438440082u, 2217471853u, +3355404249u, 3275550588u, +1052645068u, 862072556u, +4110617119u, 3745267835u, +2657392572u, 4279236653u, +1688445808u, 701920051u, +956734128u, 581695350u, +3157862788u, 2585726058u, +1192588249u, 1410111809u, +1651193125u, 3326135446u, +1073280453u, 97376972u, +2513844237u, 2187968410u, +3976859649u, 4267859263u, +3429034542u, 564493077u, +3000537321u, 479241367u, +3845637831u, 2868987960u, +51544337u, 1029173765u, +393624922u, 704325635u, +2357610553u, 1418509533u, +2007814586u, 3866658271u, +3082385053u, 735688735u, +916110004u, 3283299459u, +1051684175u, 1083796807u, +4074716319u, 813690332u, +144264390u, 1439630796u, +1508556987u, 675582689u, +3748881891u, 3195309868u, +362884708u, 1616408198u, +43233176u, 837301135u, +881504822u, 3254795114u, +1385506591u, 2799925823u, +1469874582u, 3464841997u, +497175391u, 3929484338u, +3975771289u, 1798536177u, +2926265846u, 1374242438u, +3675707838u, 4205965408u, +3153165629u, 1499475160u, +187287713u, 548490821u, +3255259608u, 4247675634u, +1940181471u, 3779953975u, +687167150u, 2319566715u, +1742785722u, 785893184u, +2296977392u, 2778575413u, +1794720651u, 48131484u, +4084891412u, 1160134629u, +3737623280u, 823113169u, +3423207646u, 3803213486u, +710625654u, 4116162021u, +3693420287u, 4167766971u, +1666602807u, 295320990u, +3513255468u, 2487440590u, +234080704u, 4004655503u, +2971762528u, 1479656873u, +4090178629u, 4044418876u, +391947536u, 1462554406u, +3909295855u, 1239580330u, +1515601363u, 2011102035u, +1442068334u, 4265993528u, +1191921695u, 2291355695u, +4257172787u, 576405853u, +314332944u, 4038839101u, +55559918u, 2378985842u, +711098718u, 2425317635u, +1644327317u, 1401013391u, +4193760037u, 2958260436u, +3167371443u, 3062418115u, +3800755475u, 3167030094u, +3489648204u, 1405430357u, +526177822u, 2602636307u, +915406019u, 4264167741u, +1484090483u, 3070944737u, +254529415u, 4017058800u, +1702710265u, 1029665228u, +2000382906u, 3185573940u, +1381258384u, 4036354071u, +2900841028u, 2670703363u, +2921748807u, 2899069938u, +4130543625u, 688472265u, +4186808827u, 1054670286u, +1132985391u, 2840525968u, +4175776103u, 338058159u, +1735964501u, 1539305024u, +3497121710u, 1568260669u, +2227290760u, 146827036u, +3977176001u, 4060134777u, +857488494u, 250055052u, +4284109679u, 2502815838u, +2592281721u, 1603444633u, +1390562014u, 1556658131u, +616327404u, 2448966429u, +3051191726u, 3891353218u, +1213304082u, 762328245u, +2239052397u, 1082330589u, +2455957292u, 201837927u, +405397452u, 3079886794u, +2583939798u, 2848283092u, +3750724631u, 883849006u, +3204198988u, 3341327098u, +1855234968u, 1982110346u, +1485529487u, 541496720u, +4117290321u, 3607433551u, +2168864636u, 133643215u, +1055817409u, 3847827123u, +2960769387u, 4046101649u, +1176127003u, 4015671361u, +4243643405u, 2849988118u, +517111221u, 1796672358u, +2045051700u, 3452457457u, +2948254999u, 2102063419u, +1556410577u, 1536380876u, +3776661467u, 3281002516u, +1735616066u, 1539151988u, +1087795162u, 3332431596u, +685631442u, 1147951686u, +95237878u, 2005032160u, +4012206915u, 4224354805u, +3204999386u, 2415262714u, +1433635018u, 116647396u, +83167836u, 2881562655u, +2729416454u, 1029284767u, +881378302u, 2159170082u, +555057366u, 1169104445u, +3963877000u, 1919171906u, +336034862u, 2017579106u, +4059340529u, 3020819343u, +865146997u, 2473524405u, +944743644u, 1694443528u, +1804513294u, 2904752429u, +617975720u, 3671562289u, +260177668u, 505662155u, +1885941445u, 2504509403u, +2260041112u, 1019936943u, +3722741628u, 1511077569u, +3100701179u, 1379422864u, +1535670711u, 773792826u, +1103819072u, 2089123665u, +1157547425u, 329152940u, +4142587430u, 484732447u, +2475035432u, 1120017626u, +412145504u, 965125959u, +324924679u, 2809286837u, +2842141483u, 4029205195u, +2974306813u, 515627448u, +3791551981u, 1097806406u, +3873078673u, 136118734u, +1872130856u, 3632422367u, +3574135531u, 4017075736u, +1699452298u, 1403506686u, +344414660u, 1189129691u, +3487080616u, 1516736273u, +1805475756u, 2562064338u, +163335594u, 2732147834u, +4077452507u, 2984955003u, +4271866024u, 3071338162u, +2347111903u, 873829983u, +1948409509u, 1923531348u, +459509140u, 771592405u, +1750124750u, 2334938333u, +213811117u, 2586632018u, +185232757u, 4032960199u, +2447383637u, 284777551u, +1654276320u, 2687561076u, +3512945009u, 308584855u, +1861027147u, 4102279334u, +3203802620u, 1692079268u, +4250142168u, 2565680167u, +1507046104u, 841195925u, +520565830u, 3674576684u, +38924274u, 3770488806u, +2414430882u, 3978473838u, +3703994407u, 69201295u, +3099963860u, 1255084262u, +690971838u, 3539996781u, +3696902571u, 3593730713u, +2363435042u, 54945052u, +1785765213u, 184911581u, +1586241476u, 1939595371u, +2534883189u, 2432427547u, +2374171993u, 2039128933u, +2955715987u, 2295501078u, +2741583197u, 1280920000u, +686818699u, 1238742497u, +3843660102u, 82177963u, +1281043691u, 1121403845u, +1697846708u, 284852964u, +278661677u, 2889101923u, +2127558730u, 713121337u, +872502474u, 511142139u, +1261140657u, 1747052377u, +2108187161u, 927011680u, +955328267u, 3821994995u, +2707230761u, 4142246789u, +4134691985u, 1958963937u, +2498463509u, 1977988705u, +1419293714u, 1636932722u, +2567532373u, 4075249328u, +240575705u, 1956681213u, +2598802768u, 2025886508u, +4104757832u, 3026358429u, +3242615202u, 4026813725u, +255108733u, 1845587644u, +3573008472u, 3615577014u, +1222733548u, 1205557630u, +917608574u, 1363253259u, +1541946015u, 3087190425u, +1138008081u, 1444019663u, +109793386u, 341851980u, +857839960u, 2515339233u, +156283211u, 1906768669u, +3886713057u, 1276595523u, +2809830736u, 460237542u, +3420452099u, 142985419u, +205970448u, 4198897105u, +1950698961u, 2069753399u, +1142216925u, 1113051162u, +1033680610u, 4278599955u, +1106466069u, 356742959u, +531521052u, 3494863964u, +225629455u, 3735275001u, +3662626864u, 1750561299u, +1012864651u, 2101846429u, +1074553219u, 668829411u, +992181339u, 3384018814u, +3330664522u, 860966321u, +1885071395u, 4233785523u, +100741310u, 451656820u, +2148187612u, 1063001151u, +360256231u, 107312677u, +3650357479u, 2390172694u, +22452685u, 237319043u, +3600462351u, 1216645846u, +2088767754u, 164402616u, +2418980170u, 926137824u, +94638678u, 1689811113u, +2751052984u, 1767810825u, +271289013u, 3896132233u, +103797041u, 1397772514u, +3441135892u, 3323383489u, +2491268371u, 1662561885u, +1612872497u, 2986430557u, +2756998822u, 207428029u, +937973965u, 2791656726u, +1949717207u, 2260498180u, +2648427775u, 2360400900u, +2080496169u, 486358863u, +1582022990u, 1263709570u, +1396468647u, 1377764574u, +363008508u, 1293502429u, +224580012u, 4252610345u, +1435134775u, 1099809675u, +533671980u, 1533438766u, +1820532305u, 2776960536u, +3374512975u, 3542220540u, +822810075u, 3716663290u, +1157398049u, 3752806924u, +4081637863u, 337070226u, +3866585976u, 359270190u, +2110942730u, 3267551635u, +644850146u, 1306761320u, +746972907u, 934259457u, +2341378668u, 2220373824u, +1242645122u, 4109252858u, +1625266099u, 1173698481u, +383517064u, 896322512u, +3377483696u, 1788337208u, +455496839u, 3194373887u, +1837689083u, 1336556841u, +1658628529u, 2911512007u, +3838343487u, 2757664765u, +1537187340u, 3712582785u, +367022558u, 3071359622u, +3926147070u, 35432879u, +3093195926u, 2561488770u, +4273132307u, 3898950547u, +2838251049u, 2103926083u, +2549435227u, 536047554u, +1858986613u, 2040551642u, +1147412575u, 1972369852u, +4166184983u, 3528794619u, +4077477194u, 3565689036u, +808048238u, 3826350461u, +1359641525u, 1197100813u, +265993036u, 1864569342u, +725164342u, 2264788336u, +1831223342u, 3329594980u, +923017956u, 490608221u, +3818634478u, 258154469u, +1441714797u, 1174785921u, +3833372385u, 3287246572u, +1677395563u, 3569218731u, +868981704u, 2163330264u, +2649450292u, 500120236u, +465161780u, 746438382u, +1145009669u, 2520062970u, +2810524030u, 1561519055u, +1479878006u, 3864969305u, +2686075657u, 4042710240u, +3224066062u, 2774151984u, +2226179547u, 1643626042u, +2328730865u, 3160666939u, +2107011431u, 96459446u, +3920328742u, 3336407558u, +829404209u, 1878067032u, +1235983679u, 4237425634u, +466519055u, 3870676863u, +934312076u, 2952135524u, +276949224u, 4100839753u, +424001484u, 1955120893u, +4015478120u, 1265237690u, +427484362u, 4246879223u, +3452969617u, 1724363362u, +1553513184u, 834830418u, +1858777639u, 3476334357u, +4144030366u, 2450047160u, +2950762705u, 4277111759u, +358032121u, 2511026735u, +167923105u, 2059208280u, +251949572u, 3065234219u, +1535473864u, 556796152u, +1513237478u, 3150857516u, +1103404394u, 198182691u, +1476438092u, 2913077464u, +207119516u, 3963810232u, +2954651680u, 1535115487u, +3051522276u, 4046477658u, +917804636u, 864395565u, +632704095u, 140762681u, +1802040304u, 990407433u, +3771506212u, 4106024923u, +1287729497u, 2198985327u, +4052924496u, 2926590471u, +3084557148u, 1472898694u, +1009870118u, 559702706u, +4265214507u, 82077489u, +3067891003u, 3295678907u, +2402308151u, 1096697687u, +464407878u, 4190838199u, +4269578403u, 3060919438u, +2899950405u, 3046872820u, +733509243u, 1583801700u, +40453902u, 3879773881u, +1993425202u, 2185339100u, +1877837196u, 3912423882u, +3293122640u, 4104318469u, +1679617763u, 3703603898u, +8759461u, 2540185277u, +1152198475u, 2038345882u, +2503579743u, 1446869792u, +2019419351u, 4051584612u, +3178289407u, 3992503830u, +2879018745u, 2719373510u, +700836153u, 1675560450u, +4121245793u, 2064715719u, +343595772u, 1996164093u, +3130433948u, 405251683u, +2804817126u, 1607133689u, +463852893u, 2864244470u, +2224044848u, 4071581802u, +2537107938u, 2246347953u, +3207234525u, 2028708916u, +2272418128u, 803575837u, +38655481u, 2170452091u, +3272166407u, 557660441u, +4019147902u, 3841480082u, +298459606u, 2600943364u, +2440657523u, 255451671u, +3424361375u, 779434428u, +3088526123u, 490671625u, +1322855877u, 3452203069u, +3057021940u, 2285701422u, +2014993457u, 2390431709u, +2002090272u, 1568745354u, +1783152480u, 823305654u, +4053862835u, 2200236540u, +3009412313u, 3184047862u, +3032187389u, 4159715581u, +2966902888u, 252986948u, +1849329144u, 3160134214u, +3420960112u, 3198900547u, +749160960u, 379139040u, +1208883495u, 1566527339u, +3006227299u, 4194096960u, +556075248u, 497404038u, +1717327230u, 1496132623u, +1775955687u, 1719108984u, +1014328900u, 4189966956u, +2108574735u, 2584236470u, +684087286u, 531310503u, +4264509527u, 773405691u, +3088905079u, 3456882941u, +3105682208u, 3382290593u, +2289363624u, 3296306400u, +4168438718u, 467441309u, +777173623u, 3241407531u, +1183994815u, 1132983260u, +1610606159u, 2540270567u, +2649684057u, 1397502982u, +146657385u, 3318434267u, +2109315753u, 3348545480u, +3193669211u, 811750340u, +1073256162u, 3571673088u, +546596661u, 1017047954u, +3403136990u, 2540585554u, +1477047647u, 4145867423u, +2826408201u, 3531646869u, +784952939u, 943914610u, +2717443875u, 3657384638u, +1806867885u, 1903578924u, +3985088434u, 1911188923u, +1764002686u, 3672748083u, +1832925325u, 241574049u, +519948041u, 3181425568u, +2939747257u, 1634174593u, +3429894862u, 3529565564u, +1089679033u, 240953857u, +2025369941u, 2695166650u, +517086873u, 2964595704u, +3017658263u, 3828377737u, +2144895011u, 994799311u, +1184683823u, 4260564140u, +308018483u, 4262383425u, +1374752558u, 3431057723u, +1572637805u, 383233885u, +3188015819u, 4051263539u, +233319221u, 3794788167u, +2017406667u, 919677938u, +4074952232u, 1683612329u, +4213676186u, 327142514u, +3032591014u, 4204155962u, +206775997u, 2283918569u, +2395147154u, 3427505379u, +2211319468u, 4153726847u, +2217060665u, 350160869u, +2493667051u, 1648200185u, +3441709766u, 1387233546u, +140980u, 1891558063u, +760080239u, 2088061981u, +1580964938u, 740563169u, +422986366u, 330624974u, +4264507722u, 150928357u, +2738323042u, 2948665536u, +918718096u, 376390582u, +3966098971u, 717653678u, +3219466255u, 3799363969u, +3424344721u, 3187805406u, +375347278u, 3490350144u, +1992212097u, 2263421398u, +3855037968u, 1928519266u, +3866327955u, 1129127000u, +1782515131u, 2746577402u, +3059200728u, 2108753646u, +2738070963u, 1336849395u, +1705302106u, 768287270u, +1343511943u, 2247006571u, +1956142255u, 1780259453u, +3475618043u, 212490675u, +622521957u, 917121602u, +1852992332u, 1267987847u, +3170016833u, 2549835613u, +3299763344u, 2864033668u, +3378768767u, 1236609378u, +4169365948u, 3738062408u, +2661022773u, 2006922227u, +2760592161u, 3828932355u, +2636387819u, 2616619070u, +1237256330u, 3449066284u, +2871755260u, 3729280948u, +3862686086u, 431292293u, +3285899651u, 786322314u, +2531158535u, 724901242u, +2377363130u, 1415970351u, +1244759631u, 3263135197u, +965248856u, 174024139u, +2297418515u, 2954777083u, +987586766u, 3206261120u, +4059515114u, 3903854066u, +1931934525u, 2287507921u, +1827135136u, 1781944746u, +574617451u, 2299034788u, +2650140034u, 4081586725u, +2482286699u, 1109175923u, +458483596u, 618705848u, +4059852729u, 1813855658u, +4190721328u, 1129462471u, +4089998050u, 3575732749u, +2375584220u, 1037031473u, +1623777358u, 3389003793u, +546597541u, 352770237u, +1383747654u, 3122687303u, +1646071378u, 1164309901u, +290870767u, 830691298u, +929335420u, 3193251135u, +989577914u, 3626554867u, +591974737u, 3996958215u, +3163711272u, 3071568023u, +1516846461u, 3656006011u, +2698625268u, 2510865430u, +340274176u, 1167681812u, +3698796465u, 3155218919u, +4102288238u, 1673474350u, +3069708839u, 2704165015u, +1237411891u, 1854985978u, +3646837503u, 3625406022u, +921552000u, 1712976649u, +3939149151u, 878608872u, +3406359248u, 1068844551u, +1834682077u, 4155949943u, +2437686324u, 3163786257u, +2645117577u, 1988168803u, +747285578u, 1626463554u, +1235300371u, 1256485167u, +1914142538u, 4141546431u, +3838102563u, 582664250u, +1883344352u, 2083771672u, +2611657933u, 2139079047u, +2250573853u, 804336148u, +3066325351u, 2770847216u, +4275641370u, 1455750577u, +3346357270u, 1674051445u, +601221482u, 3992583643u, +1402445097u, 3622527604u, +2509017299u, 2966108111u, +2557027816u, 900741486u, +1790771021u, 2912643797u, +2631381069u, 4014551783u, +90375300u, 300318232u, +3269968032u, 2679371729u, +2664752123u, 3517585534u, +3253901179u, 542270815u, +1188641600u, 365479232u, +2210121140u, 760762191u, +1273768482u, 1216399252u, +3484324231u, 4287337666u, +16322182u, 643179562u, +325675502u, 3652676161u, +3120716054u, 3330259752u, +1011990087u, 2990167340u, +1097584090u, 3262252593u, +1829409951u, 3665087267u, +1214854475u, 2134299399u, +3704419305u, 411263051u, +1625446136u, 549838529u, +4283196353u, 1342880802u, +3460621305u, 1967599860u, +4282843369u, 1275671016u, +2544665755u, 853593042u, +901109753u, 2682611693u, +110631633u, 797487791u, +1472073141u, 850464484u, +797089608u, 3286110054u, +350397471u, 2775631060u, +366448238u, 3842907484u, +2219863904u, 3623364733u, +1850985302u, 4009616991u, +294963924u, 3693536939u, +3061255808u, 1615375832u, +1920066675u, 4113028420u, +4032223840u, 2318423400u, +2701956286u, 4145497671u, +3991532344u, 2536338351u, +1679099863u, 1728968857u, +449740816u, 2686506989u, +685242457u, 97590863u, +3258354115u, 1502282913u, +1235084019u, 2151665147u, +528459289u, 231097464u, +2477280726u, 3651607391u, +2091754612u, 1178454681u, +980597335u, 1604483865u, +1842333726u, 4146839064u, +3213794286u, 2601416506u, +754220096u, 3571436033u, +488595746u, 1448097974u, +4004834921u, 238887261u, +3320337489u, 1416989070u, +2928916831u, 4093725287u, +186020771u, 2367569534u, +3046087671u, 4090084518u, +3548184546u, 679517009u, +1962659444u, 3539886328u, +4192003933u, 1678423485u, +3827951761u, 3086277222u, +2144472852u, 1390394371u, +2976322029u, 1574517163u, +3553313841u, 119173722u, +1702434637u, 1766260771u, +3629581771u, 1407497759u, +895654784u, 751439914u, +4008409498u, 215917713u, +1482103833u, 695551833u, +1288382231u, 2656990891u, +2581779077u, 1570750352u, +3710689053u, 1741390464u, +2666411616u, 3533987737u, +4289478316u, 3576119563u, +4118694920u, 108199666u, +3869794273u, 963183826u, +2081410737u, 3796810515u, +791123882u, 2525792704u, +1036883117u, 136547246u, +875691100u, 2592925324u, +614302599u, 3013176417u, +2689342539u, 427154472u, +532957601u, 1228758574u, +1898117151u, 1181643858u, +1908591042u, 1464255968u, +446980910u, 2984611177u, +58509511u, 1046943619u, +3508927906u, 2001585786u, +2544767379u, 1525438381u, +552181222u, 1959725830u, +879448844u, 1348536411u, +4242243590u, 2861338018u, +1082052441u, 1034351453u, +601175800u, 764077711u, +530635011u, 3785343245u, +2178026726u, 117256687u, +2378297261u, 457568934u, +76438221u, 4104954272u, +956793873u, 3783168634u, +2485968477u, 2381948487u, +4226929450u, 3148473363u, +2518273601u, 3569490233u, +879369091u, 2180270337u, +3674375989u, 1387729170u, +977997984u, 4270646856u, +568650985u, 951677556u, +4213877384u, 2721005055u, +1073364549u, 2563403831u, +1678669911u, 66786703u, +2273631661u, 1149351924u, +3651298990u, 1581883443u, +246723096u, 1895026827u, +3810605772u, 3711056516u, +4058833288u, 2193790614u, +2080120290u, 3638638708u, +2915672708u, 2263003308u, +2361934197u, 4136767460u, +1976115991u, 3448840877u, +2019238520u, 225333538u, +874340815u, 2976159827u, +1555273378u, 3797521928u, +1942347150u, 3262952567u, +435997738u, 340403353u, +2817830907u, 2078619498u, +749534111u, 1178073973u, +894654712u, 3361226032u, +841092198u, 3288261538u, +1696412169u, 1496966875u, +697501571u, 1059158875u, +3739946319u, 2481012988u, +568983526u, 114945840u, +1559249010u, 2218244008u, +2841706923u, 1632780103u, +4020169654u, 2087949619u, +2438736103u, 24032648u, +833416317u, 3787017905u, +2373238993u, 2575395164u, +3434544481u, 3228481067u, +2542976862u, 2971726178u, +2880371864u, 3642087909u, +2407477975u, 2239080836u, +1043714217u, 3894199764u, +2235879182u, 203853421u, +2933669448u, 2504940536u, +834683330u, 425935223u, +3560796393u, 3565833278u, +1668000829u, 3683399154u, +3414330886u, 1748785729u, +1023171602u, 580966986u, +2531038985u, 3227325488u, +2657385925u, 2124704694u, +233442446u, 1107045577u, +3407293834u, 552770757u, +3899097693u, 1067532701u, +115667924u, 1406028344u, +1707768231u, 3724015962u, +2419657149u, 18613994u, +2532882091u, 3476683808u, +1560838678u, 811220224u, +895961699u, 3762914298u, +1328752423u, 1844996900u, +1420427894u, 1848067707u, +1210281744u, 904215228u, +4055325594u, 1118521573u, +2496554183u, 2579259919u, +3996647489u, 3657647605u, +325254059u, 3136157065u, +3951522674u, 4052925250u, +3341068436u, 2287683323u, +1313073005u, 126005630u, +2505120084u, 1194725057u, +853746559u, 3555092974u, +2689238752u, 49515858u, +1244776042u, 1069300695u, +61073168u, 1010661841u, +1269521335u, 1902040126u, +990632502u, 2378708922u, +3858321250u, 1400735275u, +2974699176u, 2771676666u, +170995186u, 2877798589u, +545726212u, 2225229957u, +1086473152u, 3454177594u, +3859483262u, 1499729584u, +2088002891u, 2883475137u, +3222194252u, 4144472319u, +2212229854u, 4146740722u, +567988835u, 1051332394u, +3932046135u, 542648229u, +3017852446u, 1277887997u, +162888005u, 1669710469u, +1492500905u, 553041029u, +1434876932u, 533989516u, +3817492747u, 584127807u, +4147115982u, 2993670925u, +4020312558u, 710021255u, +3509733475u, 3587959456u, +2088550465u, 1745399498u, +2952242967u, 1259815443u, +869648362u, 1404723176u, +3947542735u, 1334333531u, +3873471582u, 229399758u, +59634866u, 3239516985u, +3844250972u, 1275954779u, +1385684948u, 2243700741u, +2512155003u, 1685649437u, +639306006u, 2524620206u, +955360345u, 1646776457u, +576786501u, 655707039u, +2864351838u, 3736264674u, +655621239u, 362070173u, +1200907897u, 2384379464u, +15823708u, 206117476u, +3652870937u, 122927134u, +1193310960u, 1093099415u, +3696538026u, 4112584792u, +1834541277u, 845639252u, +2069527017u, 547588820u, +4178147211u, 2827259351u, +1764455305u, 3312003602u, +940846775u, 1054995047u, +2976960697u, 1934305529u, +3095615046u, 3354962706u, +2199137382u, 1005722394u, +1875867180u, 2064356511u, +3363633633u, 2688499147u, +4019734130u, 3096333006u, +2069509024u, 2906358341u, +3247463123u, 4191788132u, +2232866485u, 1456016086u, +1422674894u, 867282151u, +1851386407u, 1268304058u, +1612503136u, 1739843072u, +134947567u, 2978775774u, +2051592101u, 1017127033u, +1284167756u, 1090844589u, +831688783u, 2079216362u, +2079309682u, 1950585801u, +1626991196u, 3644714163u, +3678110059u, 898470030u, +1117570988u, 2517572125u, +3916646913u, 3182422972u, +3630426828u, 969847973u, +2835126238u, 53541366u, +3427164640u, 3463937250u, +3044785046u, 897322257u, +103038235u, 3804506837u, +3443872170u, 4185408854u, +2557463241u, 4080940424u, +3669923099u, 2789619871u, +2048168570u, 2429169982u, +3174690447u, 2513494106u, +3099587829u, 2627855577u, +1213061732u, 3143736628u, +3482268149u, 1250714337u, +3553412672u, 2689632914u, +31648125u, 3872383625u, +1565760579u, 36665130u, +1282106920u, 359361724u, +751041229u, 2257179590u, +2915361862u, 280819225u, +954406473u, 4101682199u, +2907818413u, 4254297769u, +3493178615u, 3755944354u, +3539557658u, 3330196096u, +4043533423u, 1134196225u, +4177134659u, 127246419u, +4213770762u, 1978302978u, +2442615581u, 923049607u, +1004426206u, 782768297u, +2702745496u, 1880389457u, +2410586681u, 1430106871u, +4103323427u, 3168399477u, +201787012u, 3105353527u, +3716682375u, 3616334719u, +3413209549u, 656672786u, +526032790u, 2895072131u, +2876965944u, 182894450u, +456581318u, 2683752067u, +1287916294u, 1270745752u, +3877875910u, 3190666241u, +3240336907u, 4024807233u, +4227999465u, 2389301430u, +1681224377u, 1576191191u, +3599250276u, 2381111980u, +3995044500u, 995595530u, +3495321877u, 3956024585u, +1611608524u, 3815677453u, +1520987487u, 3669102590u, +2062334396u, 1656117707u, +5457134u, 3234118251u, +4242065111u, 596879987u, +470187419u, 2688566989u, +3259870297u, 660100446u, +1042378442u, 2206034096u, +442236198u, 2542452448u, +493137955u, 392411099u, +3111186954u, 438250493u, +947967568u, 1234595917u, +4230082284u, 2762976773u, +421203727u, 3728409592u, +2870085764u, 1455086530u, +2762099647u, 4011882747u, +1785430706u, 3684427488u, +1215981925u, 3227517889u, +3269061963u, 4037515364u, +1749401388u, 2167451566u, +3168911474u, 4255057396u, +2026092260u, 1736192508u, +4123254745u, 2319366806u, +3909727042u, 3114708966u, +1938800693u, 680793595u, +3933041672u, 616863613u, +1525265867u, 2808224480u, +2122290603u, 1211197714u, +1186177814u, 2395325006u, +3520488321u, 3979192396u, +3540779343u, 4192918639u, +1763872074u, 3402419930u, +2736030448u, 1120335563u, +1698949078u, 3993310631u, +2947659998u, 1461045789u, +1966048551u, 2228221363u, +597941119u, 3498018399u, +1441110751u, 2229999711u, +393987327u, 454500547u, +1222959566u, 567151340u, +2496952483u, 1708770195u, +3774764786u, 1492844524u, +3308300614u, 805568076u, +4068812294u, 3404648243u, +868414882u, 177406999u, +1608110313u, 642061169u, +2093999089u, 222470301u, +1027515771u, 3131251981u, +2851936150u, 4272755262u, +2763002551u, 1881527822u, +1532845092u, 709643652u, +682573592u, 1244104217u, +440905170u, 1111321746u, +796769556u, 2500467040u, +3002618826u, 1112998535u, +1188525643u, 4212674512u, +1780193104u, 1243644607u, +3691719535u, 2958853053u, +2813437721u, 4036584207u, +466635014u, 2277292580u, +4082276003u, 1030800045u, +1899531424u, 609466946u, +1750863246u, 379050598u, +3576413281u, 731493104u, +2707384133u, 2289193651u, +132259176u, 4115195437u, +1769890695u, 2715470335u, +3348954692u, 2166575624u, +1819263183u, 2028531518u, +2154809766u, 3672399742u, +1142139448u, 88299682u, +76727603u, 4198182186u, +2304993586u, 1666387627u, +2488475423u, 3832777692u, +284366017u, 3359785538u, +3469807328u, 2926494787u, +1914195188u, 1134129972u, +3829072836u, 2493478921u, +3738499303u, 3311304980u, +726951526u, 911080963u, +932916545u, 2235559063u, +2909742396u, 1765719309u, +465269850u, 3803621553u, +1456588655u, 508290328u, +1490719640u, 3356513470u, +2262196163u, 1451774941u, +2908490783u, 251085588u, +830410677u, 3172220325u, +4039692645u, 1383603170u, +3897208579u, 1940535730u, +151909546u, 2384458112u, +}; + +// Return false only if offset is -1 and a spot check of 3 hashes all yield 0. +bool Test(int offset, int len = 0) { +#undef Check +#undef IsAlive + +#define Check(x) do { \ + const uint32_t actual = (x), e = expected[index++]; \ + bool ok = actual == e; \ + if (!ok) { \ + cerr << "expected " << hex << e << " but got " << actual << endl; \ + ++errors; \ + } \ + assert(ok); \ +} while (0) + +#define IsAlive(x) do { alive += IsNonZero(x); } while (0) + + // After the following line is where the uses of "Check" and such will go. + static int index = 0; +if (offset == -1) { int alive = 0; { uint64_t h = farmhashxo::Hash64WithSeeds(data, len++, SEED0, SEED1); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashxo::Hash64WithSeed(data, len++, SEED); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } { uint64_t h = farmhashxo::Hash64(data, len++); IsAlive(h >> 32); IsAlive((h << 32) >> 32); } len -= 3; return alive > 0; } +{ uint64_t h = farmhashxo::Hash64WithSeeds(data + offset, len, SEED0, SEED1); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashxo::Hash64WithSeed(data + offset, len, SEED); Check(h >> 32); Check((h << 32) >> 32); } +{ uint64_t h = farmhashxo::Hash64(data + offset, len); Check(h >> 32); Check((h << 32) >> 32); } + + return true; +#undef Check +#undef IsAlive +} + +int RunTest() { + Setup(); + int i = 0; + cout << "Running farmhashxoTest"; + if (!Test(-1)) { + cout << "... Unavailable\n"; + return NoteErrors(); + } + // Good. The function is attempting to hash, so run the full test. + int errors_prior_to_test = errors; + for ( ; i < kTestSize - 1; i++) { + Test(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + Test(0, i); + } + Test(0, kDataSize); + cout << (errors == errors_prior_to_test ? "... OK\n" : "... Failed\n"); + return NoteErrors(); +} + +#else + +// After the following line is where the code to print hash codes will go. +void Dump(int offset, int len) { +{ uint64_t h = farmhashxo::Hash64WithSeeds(data + offset, len, SEED0, SEED1); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashxo::Hash64WithSeed(data + offset, len, SEED); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +{ uint64_t h = farmhashxo::Hash64(data + offset, len); cout << (h >> 32) << "u, " << ((h << 32) >> 32) << "u," << endl; } +} + +#endif + +#undef SEED +#undef SEED1 +#undef SEED0 + +} // namespace farmhashxoTest + +#if TESTING + +static int farmhashxoTestResult = farmhashxoTest::RunTest(); + +#else +int main(int argc, char** argv) { + Setup(); + cout << "uint32_t expected[] = {\n"; + int i = 0; + for ( ; i < kTestSize - 1; i++) { + farmhashxoTest::Dump(i * i, i); + } + for ( ; i < kDataSize; i += i / 7) { + farmhashxoTest::Dump(0, i); + } + farmhashxoTest::Dump(0, kDataSize); + cout << "};\n"; +} +#endif + +#endif // FARMHASHSELFTEST diff --git a/contrib/libfarmhash/farmhash.h b/contrib/libfarmhash/farmhash.h new file mode 100644 index 00000000000..c55fbc2e1fd --- /dev/null +++ b/contrib/libfarmhash/farmhash.h @@ -0,0 +1,290 @@ +// Copyright (c) 2014 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// FarmHash, by Geoff Pike + +// +// http://code.google.com/p/farmhash/ +// +// This file provides a few functions for hashing strings and other +// data. All of them are high-quality functions in the sense that +// they do well on standard tests such as Austin Appleby's SMHasher. +// They're also fast. FarmHash is the successor to CityHash. +// +// Functions in the FarmHash family are not suitable for cryptography. +// +// WARNING: This code has been only lightly tested on big-endian platforms! +// It is known to work well on little-endian platforms that have a small penalty +// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. +// It should work on all 32-bit and 64-bit platforms that allow unaligned reads; +// bug reports are welcome. +// +// By the way, for some hash functions, given strings a and b, the hash +// of a+b is easily derived from the hashes of a and b. This property +// doesn't hold for any hash functions in this file. + +#ifndef FARM_HASH_H_ +#define FARM_HASH_H_ + +#include +#include +#include +#include // for memcpy and memset +#include + +#ifndef NAMESPACE_FOR_HASH_FUNCTIONS +#define NAMESPACE_FOR_HASH_FUNCTIONS farmhash +#endif + +namespace NAMESPACE_FOR_HASH_FUNCTIONS { + +#if defined(FARMHASH_UINT128_T_DEFINED) +inline uint64_t Uint128Low64(const uint128_t x) { + return static_cast(x); +} +inline uint64_t Uint128High64(const uint128_t x) { + return static_cast(x >> 64); +} +inline uint128_t Uint128(uint64_t lo, uint64_t hi) { + return lo + (((uint128_t)hi) << 64); +} +#else +typedef std::pair uint128_t; +inline uint64_t Uint128Low64(const uint128_t x) { return x.first; } +inline uint64_t Uint128High64(const uint128_t x) { return x.second; } +inline uint128_t Uint128(uint64_t lo, uint64_t hi) { return uint128_t(lo, hi); } +#endif + + +// BASIC STRING HASHING + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +size_t Hash(const char* s, size_t len); + +// Hash function for a byte array. Most useful in 32-bit binaries. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint32_t Hash32(const char* s, size_t len); + +// Hash function for a byte array. For convenience, a 32-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed); + +// Hash 128 input bits down to 64 bits of output. +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64(const char* s, size_t len); + +// Hash function for a byte array. For convenience, a 64-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed); + +// Hash function for a byte array. For convenience, two seeds are also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint64_t Hash64WithSeeds(const char* s, size_t len, + uint64_t seed0, uint64_t seed1); + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint128_t Hash128(const char* s, size_t len); + +// Hash function for a byte array. For convenience, a 128-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +uint128_t Hash128WithSeed(const char* s, size_t len, uint128_t seed); + +// BASIC NON-STRING HASHING + +// This is intended to be a reasonably good hash function. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +inline uint64_t Hash128to64(uint128_t x) { + // Murmur-inspired hashing. + const uint64_t kMul = 0x9ddfea08eb382d69ULL; + uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; + a ^= (a >> 47); + uint64_t b = (Uint128High64(x) ^ a) * kMul; + b ^= (b >> 47); + b *= kMul; + return b; +} + +// FINGERPRINTING (i.e., good, portable, forever-fixed hash functions) + +// Fingerprint function for a byte array. Most useful in 32-bit binaries. +uint32_t Fingerprint32(const char* s, size_t len); + +// Fingerprint function for a byte array. +uint64_t Fingerprint64(const char* s, size_t len); + +// Fingerprint function for a byte array. +uint128_t Fingerprint128(const char* s, size_t len); + +// This is intended to be a good fingerprinting primitive. +// See below for more overloads. +inline uint64_t Fingerprint(uint128_t x) { + // Murmur-inspired hashing. + const uint64_t kMul = 0x9ddfea08eb382d69ULL; + uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; + a ^= (a >> 47); + uint64_t b = (Uint128High64(x) ^ a) * kMul; + b ^= (b >> 44); + b *= kMul; + b ^= (b >> 41); + b *= kMul; + return b; +} + +// This is intended to be a good fingerprinting primitive. +inline uint64_t Fingerprint(uint64_t x) { + // Murmur-inspired hashing. + const uint64_t kMul = 0x9ddfea08eb382d69ULL; + uint64_t b = x * kMul; + b ^= (b >> 44); + b *= kMul; + b ^= (b >> 41); + b *= kMul; + return b; +} + +#ifndef FARMHASH_NO_CXX_STRING + +// Convenience functions to hash or fingerprint C++ strings. +// These require that Str::data() return a pointer to the first char +// (as a const char*) and that Str::length() return the string's length; +// they work with std::string, for example. + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline size_t Hash(const Str& s) { + assert(sizeof(s[0]) == 1); + return Hash(s.data(), s.length()); +} + +// Hash function for a byte array. Most useful in 32-bit binaries. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint32_t Hash32(const Str& s) { + assert(sizeof(s[0]) == 1); + return Hash32(s.data(), s.length()); +} + +// Hash function for a byte array. For convenience, a 32-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint32_t Hash32WithSeed(const Str& s, uint32_t seed) { + assert(sizeof(s[0]) == 1); + return Hash32WithSeed(s.data(), s.length(), seed); +} + +// Hash 128 input bits down to 64 bits of output. +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint64_t Hash64(const Str& s) { + assert(sizeof(s[0]) == 1); + return Hash64(s.data(), s.length()); +} + +// Hash function for a byte array. For convenience, a 64-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint64_t Hash64WithSeed(const Str& s, uint64_t seed) { + assert(sizeof(s[0]) == 1); + return Hash64WithSeed(s.data(), s.length(), seed); +} + +// Hash function for a byte array. For convenience, two seeds are also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint64_t Hash64WithSeeds(const Str& s, uint64_t seed0, uint64_t seed1) { + assert(sizeof(s[0]) == 1); + return Hash64WithSeeds(s.data(), s.length(), seed0, seed1); +} + +// Hash function for a byte array. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint128_t Hash128(const Str& s) { + assert(sizeof(s[0]) == 1); + return Hash128(s.data(), s.length()); +} + +// Hash function for a byte array. For convenience, a 128-bit seed is also +// hashed into the result. +// May change from time to time, may differ on different platforms, may differ +// depending on NDEBUG. +template +inline uint128_t Hash128WithSeed(const Str& s, uint128_t seed) { + assert(sizeof(s[0]) == 1); + return Hash128(s.data(), s.length(), seed); +} + +// FINGERPRINTING (i.e., good, portable, forever-fixed hash functions) + +// Fingerprint function for a byte array. Most useful in 32-bit binaries. +template +inline uint32_t Fingerprint32(const Str& s) { + assert(sizeof(s[0]) == 1); + return Fingerprint32(s.data(), s.length()); +} + +// Fingerprint 128 input bits down to 64 bits of output. +// Fingerprint function for a byte array. +template +inline uint64_t Fingerprint64(const Str& s) { + assert(sizeof(s[0]) == 1); + return Fingerprint64(s.data(), s.length()); +} + +// Fingerprint function for a byte array. +template +inline uint128_t Fingerprint128(const Str& s) { + assert(sizeof(s[0]) == 1); + return Fingerprint128(s.data(), s.length()); +} + +#endif + +} // namespace NAMESPACE_FOR_HASH_FUNCTIONS + +#endif // FARM_HASH_H_ diff --git a/contrib/liblz4/CMakeLists.txt b/contrib/liblz4/CMakeLists.txt new file mode 100644 index 00000000000..8022e02afde --- /dev/null +++ b/contrib/liblz4/CMakeLists.txt @@ -0,0 +1,6 @@ +add_library (lz4 + src/lz4.c + src/lz4hc.c + + include/lz4/lz4.h + include/lz4/lz4hc.h) diff --git a/contrib/liblz4/LICENSE b/contrib/liblz4/LICENSE new file mode 100644 index 00000000000..552349d382f --- /dev/null +++ b/contrib/liblz4/LICENSE @@ -0,0 +1,24 @@ +LZ4 Library +Copyright (c) 2011-2014, Yann Collet +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/contrib/liblz4/include/lz4/lz4.h b/contrib/liblz4/include/lz4/lz4.h new file mode 100644 index 00000000000..3e740022561 --- /dev/null +++ b/contrib/liblz4/include/lz4/lz4.h @@ -0,0 +1,360 @@ +/* + LZ4 - Fast LZ compression algorithm + Header File + Copyright (C) 2011-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +#pragma once + +#if defined (__cplusplus) +extern "C" { +#endif + +/* + * lz4.h provides block compression functions, and gives full buffer control to programmer. + * If you need to generate inter-operable compressed data (respecting LZ4 frame specification), + * and can let the library handle its own memory, please use lz4frame.h instead. +*/ + +/************************************** +* Version +**************************************/ +#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ +#define LZ4_VERSION_MINOR 7 /* for new (non-breaking) interface capabilities */ +#define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ +#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE) +int LZ4_versionNumber (void); + +/************************************** +* Tuning parameter +**************************************/ +/* + * LZ4_MEMORY_USAGE : + * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.) + * Increasing memory usage improves compression ratio + * Reduced memory usage can improve speed, due to cache effect + * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache + */ +#define LZ4_MEMORY_USAGE 14 + + +/************************************** +* Simple Functions +**************************************/ + +int LZ4_compress_default(const char* source, char* dest, int sourceSize, int maxDestSize); +int LZ4_decompress_safe (const char* source, char* dest, int compressedSize, int maxDecompressedSize); + +/* +LZ4_compress_default() : + Compresses 'sourceSize' bytes from buffer 'source' + into already allocated 'dest' buffer of size 'maxDestSize'. + Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize). + It also runs faster, so it's a recommended setting. + If the function cannot compress 'source' into a more limited 'dest' budget, + compression stops *immediately*, and the function result is zero. + As a consequence, 'dest' content is not valid. + This function never writes outside 'dest' buffer, nor read outside 'source' buffer. + sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE + maxDestSize : full or partial size of buffer 'dest' (which must be already allocated) + return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize) + or 0 if compression fails + +LZ4_decompress_safe() : + compressedSize : is the precise full size of the compressed block. + maxDecompressedSize : is the size of destination buffer, which must be already allocated. + return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize) + If destination buffer is not large enough, decoding will stop and output an error code (<0). + If the source stream is detected malformed, the function will stop decoding and return a negative result. + This function is protected against buffer overflow exploits, including malicious data packets. + It never writes outside output buffer, nor reads outside input buffer. +*/ + + +/************************************** +* Advanced Functions +**************************************/ +#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ +#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) + +/* +LZ4_compressBound() : + Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible) + This function is primarily useful for memory allocation purposes (destination buffer size). + Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example). + Note that LZ4_compress_default() compress faster when dest buffer size is >= LZ4_compressBound(srcSize) + inputSize : max supported value is LZ4_MAX_INPUT_SIZE + return : maximum output size in a "worst case" scenario + or 0, if input size is too large ( > LZ4_MAX_INPUT_SIZE) +*/ +int LZ4_compressBound(int inputSize); + +/* +LZ4_compress_fast() : + Same as LZ4_compress_default(), but allows to select an "acceleration" factor. + The larger the acceleration value, the faster the algorithm, but also the lesser the compression. + It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed. + An acceleration value of "1" is the same as regular LZ4_compress_default() + Values <= 0 will be replaced by ACCELERATION_DEFAULT (see lz4.c), which is 1. +*/ +int LZ4_compress_fast (const char* source, char* dest, int sourceSize, int maxDestSize, int acceleration); + + +/* +LZ4_compress_fast_extState() : + Same compression function, just using an externally allocated memory space to store compression state. + Use LZ4_sizeofState() to know how much memory must be allocated, + and allocate it on 8-bytes boundaries (using malloc() typically). + Then, provide it as 'void* state' to compression function. +*/ +int LZ4_sizeofState(void); +int LZ4_compress_fast_extState (void* state, const char* source, char* dest, int inputSize, int maxDestSize, int acceleration); + + +/* +LZ4_compress_destSize() : + Reverse the logic, by compressing as much data as possible from 'source' buffer + into already allocated buffer 'dest' of size 'targetDestSize'. + This function either compresses the entire 'source' content into 'dest' if it's large enough, + or fill 'dest' buffer completely with as much data as possible from 'source'. + *sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'. + New value is necessarily <= old value. + return : Nb bytes written into 'dest' (necessarily <= targetDestSize) + or 0 if compression fails +*/ +int LZ4_compress_destSize (const char* source, char* dest, int* sourceSizePtr, int targetDestSize); + + +/* +LZ4_decompress_fast() : + originalSize : is the original and therefore uncompressed size + return : the number of bytes read from the source buffer (in other words, the compressed size) + If the source stream is detected malformed, the function will stop decoding and return a negative result. + Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. + note : This function fully respect memory boundaries for properly formed compressed data. + It is a bit faster than LZ4_decompress_safe(). + However, it does not provide any protection against intentionally modified data stream (malicious input). + Use this function in trusted environment only (data to decode comes from a trusted source). +*/ +int LZ4_decompress_fast (const char* source, char* dest, int originalSize); + +/* +LZ4_decompress_safe_partial() : + This function decompress a compressed block of size 'compressedSize' at position 'source' + into destination buffer 'dest' of size 'maxDecompressedSize'. + The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, + reducing decompression time. + return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) + Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. + Always control how many bytes were decoded. + If the source stream is detected malformed, the function will stop decoding and return a negative result. + This function never writes outside of output buffer, and never reads outside of input buffer. It is therefore protected against malicious data packets +*/ +int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedSize, int targetOutputSize, int maxDecompressedSize); + + +/*********************************************** +* Streaming Compression Functions +***********************************************/ +#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) +#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long)) +/* + * LZ4_stream_t + * information structure to track an LZ4 stream. + * important : init this structure content before first use ! + * note : only allocated directly the structure if you are statically linking LZ4 + * If you are using liblz4 as a DLL, please use below construction methods instead. + */ +typedef struct { long long table[LZ4_STREAMSIZE_U64]; } LZ4_stream_t; + +/* + * LZ4_resetStream + * Use this function to init an allocated LZ4_stream_t structure + */ +void LZ4_resetStream (LZ4_stream_t* streamPtr); + +/* + * LZ4_createStream will allocate and initialize an LZ4_stream_t structure + * LZ4_freeStream releases its memory. + * In the context of a DLL (liblz4), please use these methods rather than the static struct. + * They are more future proof, in case of a change of LZ4_stream_t size. + */ +LZ4_stream_t* LZ4_createStream(void); +int LZ4_freeStream (LZ4_stream_t* streamPtr); + +/* + * LZ4_loadDict + * Use this function to load a static dictionary into LZ4_stream. + * Any previous data will be forgotten, only 'dictionary' will remain in memory. + * Loading a size of 0 is allowed. + * Return : dictionary size, in bytes (necessarily <= 64 KB) + */ +int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize); + +/* + * LZ4_compress_fast_continue + * Compress buffer content 'src', using data from previously compressed blocks as dictionary to improve compression ratio. + * Important : Previous data blocks are assumed to still be present and unmodified ! + * 'dst' buffer must be already allocated. + * If maxDstSize >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. + * If not, and if compressed data cannot fit into 'dst' buffer size, compression stops, and function returns a zero. + */ +int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int maxDstSize, int acceleration); + +/* + * LZ4_saveDict + * If previously compressed data block is not guaranteed to remain available at its memory location + * save it into a safer place (char* safeBuffer) + * Note : you don't need to call LZ4_loadDict() afterwards, + * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue() + * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error + */ +int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int dictSize); + + +/************************************************ +* Streaming Decompression Functions +************************************************/ + +#define LZ4_STREAMDECODESIZE_U64 4 +#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) +typedef struct { unsigned long long table[LZ4_STREAMDECODESIZE_U64]; } LZ4_streamDecode_t; +/* + * LZ4_streamDecode_t + * information structure to track an LZ4 stream. + * init this structure content using LZ4_setStreamDecode or memset() before first use ! + * + * In the context of a DLL (liblz4) please prefer usage of construction methods below. + * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future. + * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure + * LZ4_freeStreamDecode releases its memory. + */ +LZ4_streamDecode_t* LZ4_createStreamDecode(void); +int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream); + +/* + * LZ4_setStreamDecode + * Use this function to instruct where to find the dictionary. + * Setting a size of 0 is allowed (same effect as reset). + * Return : 1 if OK, 0 if error + */ +int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize); + +/* +*_continue() : + These decoding functions allow decompression of multiple blocks in "streaming" mode. + Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB) + In the case of a ring buffers, decoding buffer must be either : + - Exactly same size as encoding buffer, with same update rule (block boundaries at same positions) + In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB). + - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. + maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block. + In which case, encoding and decoding buffers do not need to be synchronized, + and encoding ring buffer can have any size, including small ones ( < 64 KB). + - _At least_ 64 KB + 8 bytes + maxBlockSize. + In which case, encoding and decoding buffers do not need to be synchronized, + and encoding ring buffer can have any size, including larger than decoding buffer. + Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer, + and indicate where it is saved using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxDecompressedSize); +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize); + + +/* +Advanced decoding functions : +*_usingDict() : + These decoding functions work the same as + a combination of LZ4_setStreamDecode() followed by LZ4_decompress_x_continue() + They are stand-alone. They don't need nor update an LZ4_streamDecode_t structure. +*/ +int LZ4_decompress_safe_usingDict (const char* source, char* dest, int compressedSize, int maxDecompressedSize, const char* dictStart, int dictSize); +int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalSize, const char* dictStart, int dictSize); + + + +/************************************** +* Obsolete Functions +**************************************/ +/* Deprecate Warnings */ +/* Should these warnings messages be a problem, + it is generally possible to disable them, + with -Wno-deprecated-declarations for gcc + or _CRT_SECURE_NO_WARNINGS in Visual for example. + You can also define LZ4_DEPRECATE_WARNING_DEFBLOCK. */ +#ifndef LZ4_DEPRECATE_WARNING_DEFBLOCK +# define LZ4_DEPRECATE_WARNING_DEFBLOCK +# define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +# if (LZ4_GCC_VERSION >= 405) || defined(__clang__) +# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) +# elif (LZ4_GCC_VERSION >= 301) +# define LZ4_DEPRECATED(message) __attribute__((deprecated)) +# elif defined(_MSC_VER) +# define LZ4_DEPRECATED(message) __declspec(deprecated(message)) +# else +# pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler") +# define LZ4_DEPRECATED(message) +# endif +#endif /* LZ4_DEPRECATE_WARNING_DEFBLOCK */ + +/* Obsolete compression functions */ +/* These functions are planned to start generate warnings by r131 approximately */ +int LZ4_compress (const char* source, char* dest, int sourceSize); +int LZ4_compress_limitedOutput (const char* source, char* dest, int sourceSize, int maxOutputSize); +int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize); +int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); +int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize); +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize); + +/* Obsolete decompression functions */ +/* These function names are completely deprecated and must no longer be used. + They are only provided here for compatibility with older programs. + - LZ4_uncompress is the same as LZ4_decompress_fast + - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe + These function prototypes are now disabled; uncomment them only if you really need them. + It is highly recommended to stop using these prototypes and migrate to maintained ones */ +/* int LZ4_uncompress (const char* source, char* dest, int outputSize); */ +/* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */ + +/* Obsolete streaming functions; use new streaming interface whenever possible */ +LZ4_DEPRECATED("use LZ4_createStream() instead") void* LZ4_create (char* inputBuffer); +LZ4_DEPRECATED("use LZ4_createStream() instead") int LZ4_sizeofStreamState(void); +LZ4_DEPRECATED("use LZ4_resetStream() instead") int LZ4_resetStreamState(void* state, char* inputBuffer); +LZ4_DEPRECATED("use LZ4_saveDict() instead") char* LZ4_slideInputBuffer (void* state); + +/* Obsolete streaming decoding functions */ +LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize); +LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize); + + +#if defined (__cplusplus) +} +#endif diff --git a/contrib/liblz4/include/lz4/lz4hc.h b/contrib/liblz4/include/lz4/lz4hc.h new file mode 100644 index 00000000000..431f7c87c86 --- /dev/null +++ b/contrib/liblz4/include/lz4/lz4hc.h @@ -0,0 +1,189 @@ +/* + LZ4 HC - High Compression Mode of LZ4 + Header File + Copyright (C) 2011-2015, Yann Collet. + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ +#pragma once + + +#if defined (__cplusplus) +extern "C" { +#endif + +/***************************** +* Includes +*****************************/ +#include /* size_t */ + + +/************************************** +* Block Compression +**************************************/ +int LZ4_compress_HC (const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel); +/* +LZ4_compress_HC : + Destination buffer 'dst' must be already allocated. + Compression completion is guaranteed if 'dst' buffer is sized to handle worst circumstances (data not compressible) + Worst size evaluation is provided by function LZ4_compressBound() (see "lz4.h") + srcSize : Max supported value is LZ4_MAX_INPUT_SIZE (see "lz4.h") + compressionLevel : Recommended values are between 4 and 9, although any value between 0 and 16 will work. + 0 means "use default value" (see lz4hc.c). + Values >16 behave the same as 16. + return : the number of bytes written into buffer 'dst' + or 0 if compression fails. +*/ + + +/* Note : + Decompression functions are provided within LZ4 source code (see "lz4.h") (BSD license) +*/ + + +int LZ4_sizeofStateHC(void); +int LZ4_compress_HC_extStateHC(void* state, const char* src, char* dst, int srcSize, int maxDstSize, int compressionLevel); +/* +LZ4_compress_HC_extStateHC() : + Use this function if you prefer to manually allocate memory for compression tables. + To know how much memory must be allocated for the compression tables, use : + int LZ4_sizeofStateHC(); + + Allocated memory must be aligned on 8-bytes boundaries (which a normal malloc() will do properly). + + The allocated memory can then be provided to the compression functions using 'void* state' parameter. + LZ4_compress_HC_extStateHC() is equivalent to previously described function. + It just uses externally allocated memory for stateHC. +*/ + + +/************************************** +* Streaming Compression +**************************************/ +#define LZ4_STREAMHCSIZE 262192 +#define LZ4_STREAMHCSIZE_SIZET (LZ4_STREAMHCSIZE / sizeof(size_t)) +typedef struct { size_t table[LZ4_STREAMHCSIZE_SIZET]; } LZ4_streamHC_t; +/* + LZ4_streamHC_t + This structure allows static allocation of LZ4 HC streaming state. + State must then be initialized using LZ4_resetStreamHC() before first use. + + Static allocation should only be used in combination with static linking. + If you want to use LZ4 as a DLL, please use construction functions below, which are future-proof. +*/ + + +LZ4_streamHC_t* LZ4_createStreamHC(void); +int LZ4_freeStreamHC (LZ4_streamHC_t* streamHCPtr); +/* + These functions create and release memory for LZ4 HC streaming state. + Newly created states are already initialized. + Existing state space can be re-used anytime using LZ4_resetStreamHC(). + If you use LZ4 as a DLL, use these functions instead of static structure allocation, + to avoid size mismatch between different versions. +*/ + +void LZ4_resetStreamHC (LZ4_streamHC_t* streamHCPtr, int compressionLevel); +int LZ4_loadDictHC (LZ4_streamHC_t* streamHCPtr, const char* dictionary, int dictSize); + +int LZ4_compress_HC_continue (LZ4_streamHC_t* streamHCPtr, const char* src, char* dst, int srcSize, int maxDstSize); + +int LZ4_saveDictHC (LZ4_streamHC_t* streamHCPtr, char* safeBuffer, int maxDictSize); + +/* + These functions compress data in successive blocks of any size, using previous blocks as dictionary. + One key assumption is that previous blocks (up to 64 KB) remain read-accessible while compressing next blocks. + There is an exception for ring buffers, which can be smaller 64 KB. + Such case is automatically detected and correctly handled by LZ4_compress_HC_continue(). + + Before starting compression, state must be properly initialized, using LZ4_resetStreamHC(). + A first "fictional block" can then be designated as initial dictionary, using LZ4_loadDictHC() (Optional). + + Then, use LZ4_compress_HC_continue() to compress each successive block. + It works like LZ4_compress_HC(), but use previous memory blocks as dictionary to improve compression. + Previous memory blocks (including initial dictionary when present) must remain accessible and unmodified during compression. + As a reminder, size 'dst' buffer to handle worst cases, using LZ4_compressBound(), to ensure success of compression operation. + + If, for any reason, previous data blocks can't be preserved unmodified in memory during next compression block, + you must save it to a safer memory space, using LZ4_saveDictHC(). + Return value of LZ4_saveDictHC() is the size of dictionary effectively saved into 'safeBuffer'. +*/ + + + +/************************************** +* Deprecated Functions +**************************************/ +/* Deprecate Warnings */ +/* Should these warnings messages be a problem, + it is generally possible to disable them, + with -Wno-deprecated-declarations for gcc + or _CRT_SECURE_NO_WARNINGS in Visual for example. + You can also define LZ4_DEPRECATE_WARNING_DEFBLOCK. */ +#ifndef LZ4_DEPRECATE_WARNING_DEFBLOCK +# define LZ4_DEPRECATE_WARNING_DEFBLOCK +# define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +# if (LZ4_GCC_VERSION >= 405) || defined(__clang__) +# define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) +# elif (LZ4_GCC_VERSION >= 301) +# define LZ4_DEPRECATED(message) __attribute__((deprecated)) +# elif defined(_MSC_VER) +# define LZ4_DEPRECATED(message) __declspec(deprecated(message)) +# else +# pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler") +# define LZ4_DEPRECATED(message) +# endif +#endif // LZ4_DEPRECATE_WARNING_DEFBLOCK + +/* compression functions */ +/* these functions are planned to trigger warning messages by r131 approximately */ +int LZ4_compressHC (const char* source, char* dest, int inputSize); +int LZ4_compressHC_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize); +int LZ4_compressHC2 (const char* source, char* dest, int inputSize, int compressionLevel); +int LZ4_compressHC2_limitedOutput (const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); +int LZ4_compressHC_withStateHC (void* state, const char* source, char* dest, int inputSize); +int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* source, char* dest, int inputSize, int maxOutputSize); +int LZ4_compressHC2_withStateHC (void* state, const char* source, char* dest, int inputSize, int compressionLevel); +int LZ4_compressHC2_limitedOutput_withStateHC(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); +int LZ4_compressHC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize); +int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize); + +/* Streaming functions following the older model; should no longer be used */ +LZ4_DEPRECATED("use LZ4_createStreamHC() instead") void* LZ4_createHC (char* inputBuffer); +LZ4_DEPRECATED("use LZ4_saveDictHC() instead") char* LZ4_slideInputBufferHC (void* LZ4HC_Data); +LZ4_DEPRECATED("use LZ4_freeStreamHC() instead") int LZ4_freeHC (void* LZ4HC_Data); +LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_compress_HC_continue() instead") int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel); +LZ4_DEPRECATED("use LZ4_createStreamHC() instead") int LZ4_sizeofStreamStateHC(void); +LZ4_DEPRECATED("use LZ4_resetStreamHC() instead") int LZ4_resetStreamStateHC(void* state, char* inputBuffer); + + +#if defined (__cplusplus) +} +#endif diff --git a/contrib/liblz4/src/lz4.c b/contrib/liblz4/src/lz4.c new file mode 100644 index 00000000000..47f3200bed2 --- /dev/null +++ b/contrib/liblz4/src/lz4.c @@ -0,0 +1,1516 @@ +/* + LZ4 - Fast LZ compression algorithm + Copyright (C) 2011-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + + +/************************************** +* Tuning parameters +**************************************/ +/* + * HEAPMODE : + * Select how default compression functions will allocate memory for their hash table, + * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). + */ +#define HEAPMODE 0 + +/* + * ACCELERATION_DEFAULT : + * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 + */ +#define ACCELERATION_DEFAULT 1 + + +/************************************** +* CPU Feature Detection +**************************************/ +/* + * LZ4_FORCE_SW_BITCOUNT + * Define this parameter if your target system or compiler does not support hardware bit count + */ +#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */ +# define LZ4_FORCE_SW_BITCOUNT +#endif + + +/************************************** +* Includes +**************************************/ +#include + + +/************************************** +* Compiler Options +**************************************/ +#ifdef _MSC_VER /* Visual Studio */ +# define FORCE_INLINE static __forceinline +# include +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ +#else +# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# if defined(__GNUC__) || defined(__clang__) +# define FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define FORCE_INLINE static inline +# endif +# else +# define FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +#endif /* _MSC_VER */ + +/* LZ4_GCC_VERSION is defined into lz4.h */ +#if (LZ4_GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__) +# define expect(expr,value) (__builtin_expect ((expr),(value)) ) +#else +# define expect(expr,value) (expr) +#endif + +#define likely(expr) expect((expr) != 0, 1) +#define unlikely(expr) expect((expr) != 0, 0) + + +/************************************** +* Memory routines +**************************************/ +#include /* malloc, calloc, free */ +#define ALLOCATOR(n,s) calloc(n,s) +#define FREEMEM free +#include /* memset, memcpy */ +#define MEM_INIT memset + + +/************************************** +* Basic Types +**************************************/ +#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; +#else + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; +#endif + + +/************************************** +* Reading and writing into memory +**************************************/ +#define STEPSIZE sizeof(size_t) + +static unsigned LZ4_64bits(void) { return sizeof(void*)==8; } + +static unsigned LZ4_isLittleEndian(void) +{ + const union { U32 i; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ + return one.c[0]; +} + + +static U16 LZ4_read16(const void* memPtr) +{ + U16 val16; + memcpy(&val16, memPtr, 2); + return val16; +} + +static U16 LZ4_readLE16(const void* memPtr) +{ + if (LZ4_isLittleEndian()) + { + return LZ4_read16(memPtr); + } + else + { + const BYTE* p = (const BYTE*)memPtr; + return (U16)((U16)p[0] + (p[1]<<8)); + } +} + +static void LZ4_writeLE16(void* memPtr, U16 value) +{ + if (LZ4_isLittleEndian()) + { + memcpy(memPtr, &value, 2); + } + else + { + BYTE* p = (BYTE*)memPtr; + p[0] = (BYTE) value; + p[1] = (BYTE)(value>>8); + } +} + +static U32 LZ4_read32(const void* memPtr) +{ + U32 val32; + memcpy(&val32, memPtr, 4); + return val32; +} + +static U64 LZ4_read64(const void* memPtr) +{ + U64 val64; + memcpy(&val64, memPtr, 8); + return val64; +} + +static size_t LZ4_read_ARCH(const void* p) +{ + if (LZ4_64bits()) + return (size_t)LZ4_read64(p); + else + return (size_t)LZ4_read32(p); +} + + +static void LZ4_copy4(void* dstPtr, const void* srcPtr) { memcpy(dstPtr, srcPtr, 4); } + +static void LZ4_copy8(void* dstPtr, const void* srcPtr) { memcpy(dstPtr, srcPtr, 8); } + +/* customized version of memcpy, which may overwrite up to 7 bytes beyond dstEnd */ +static void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd) +{ + BYTE* d = (BYTE*)dstPtr; + const BYTE* s = (const BYTE*)srcPtr; + BYTE* e = (BYTE*)dstEnd; + do { LZ4_copy8(d,s); d+=8; s+=8; } while (d>3); +# elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctzll((U64)val) >> 3); +# else + static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; + return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; +# endif + } + else /* 32 bits */ + { +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r; + _BitScanForward( &r, (U32)val ); + return (int)(r>>3); +# elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_ctz((U32)val) >> 3); +# else + static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; + return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; +# endif + } + } + else /* Big Endian CPU */ + { + if (LZ4_64bits()) + { +# if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse64( &r, val ); + return (unsigned)(r>>3); +# elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clzll((U64)val) >> 3); +# else + unsigned r; + if (!(val>>32)) { r=4; } else { r=0; val>>=32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } + r += (!val); + return r; +# endif + } + else /* 32 bits */ + { +# if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + unsigned long r = 0; + _BitScanReverse( &r, (unsigned long)val ); + return (unsigned)(r>>3); +# elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (__builtin_clz((U32)val) >> 3); +# else + unsigned r; + if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } + r += (!val); + return r; +# endif + } + } +} + +static unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) +{ + const BYTE* const pStart = pIn; + + while (likely(pIn compression run slower on incompressible data */ + + +/************************************** +* Local Structures and types +**************************************/ +typedef struct { + U32 hashTable[HASH_SIZE_U32]; + U32 currentOffset; + U32 initCheck; + const BYTE* dictionary; + BYTE* bufferStart; /* obsolete, used for slideInputBuffer */ + U32 dictSize; +} LZ4_stream_t_internal; + +typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive; +typedef enum { byPtr, byU32, byU16 } tableType_t; + +typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive; +typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; + +typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive; +typedef enum { full = 0, partial = 1 } earlyEnd_directive; + + +/************************************** +* Local Utils +**************************************/ +int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } +int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } +int LZ4_sizeofState() { return LZ4_STREAMSIZE; } + + + +/******************************** +* Compression functions +********************************/ + +static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType) +{ + if (tableType == byU16) + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); + else + return (((sequence) * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); +} + +static const U64 prime5bytes = 889523592379ULL; +static U32 LZ4_hashSequence64(size_t sequence, tableType_t const tableType) +{ + const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; + const U32 hashMask = (1<> (40 - hashLog)) & hashMask; +} + +static U32 LZ4_hashSequenceT(size_t sequence, tableType_t const tableType) +{ + if (LZ4_64bits()) + return LZ4_hashSequence64(sequence, tableType); + return LZ4_hashSequence((U32)sequence, tableType); +} + +static U32 LZ4_hashPosition(const void* p, tableType_t tableType) { return LZ4_hashSequenceT(LZ4_read_ARCH(p), tableType); } + +static void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t const tableType, const BYTE* srcBase) +{ + switch (tableType) + { + case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p; return; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase); return; } + case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase); return; } + } +} + +static void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); +} + +static const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + if (tableType == byPtr) { const BYTE** hashTable = (const BYTE**) tableBase; return hashTable[h]; } + if (tableType == byU32) { U32* hashTable = (U32*) tableBase; return hashTable[h] + srcBase; } + { U16* hashTable = (U16*) tableBase; return hashTable[h] + srcBase; } /* default, to ensure a return */ +} + +static const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase) +{ + U32 h = LZ4_hashPosition(p, tableType); + return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); +} + +FORCE_INLINE int LZ4_compress_generic( + void* const ctx, + const char* const source, + char* const dest, + const int inputSize, + const int maxOutputSize, + const limitedOutput_directive outputLimited, + const tableType_t tableType, + const dict_directive dict, + const dictIssue_directive dictIssue, + const U32 acceleration) +{ + LZ4_stream_t_internal* const dictPtr = (LZ4_stream_t_internal*)ctx; + + const BYTE* ip = (const BYTE*) source; + const BYTE* base; + const BYTE* lowLimit; + const BYTE* const lowRefLimit = ip - dictPtr->dictSize; + const BYTE* const dictionary = dictPtr->dictionary; + const BYTE* const dictEnd = dictionary + dictPtr->dictSize; + const size_t dictDelta = dictEnd - (const BYTE*)source; + const BYTE* anchor = (const BYTE*) source; + const BYTE* const iend = ip + inputSize; + const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* const matchlimit = iend - LASTLITERALS; + + BYTE* op = (BYTE*) dest; + BYTE* const olimit = op + maxOutputSize; + + U32 forwardH; + size_t refDelta=0; + + /* Init conditions */ + if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */ + switch(dict) + { + case noDict: + default: + base = (const BYTE*)source; + lowLimit = (const BYTE*)source; + break; + case withPrefix64k: + base = (const BYTE*)source - dictPtr->currentOffset; + lowLimit = (const BYTE*)source - dictPtr->dictSize; + break; + case usingExtDict: + base = (const BYTE*)source - dictPtr->currentOffset; + lowLimit = (const BYTE*)source; + break; + } + if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ + if (inputSize> LZ4_skipTrigger); + + if (unlikely(forwardIp > mflimit)) goto _last_literals; + + match = LZ4_getPositionOnHash(h, ctx, tableType, base); + if (dict==usingExtDict) + { + if (match<(const BYTE*)source) + { + refDelta = dictDelta; + lowLimit = dictionary; + } + else + { + refDelta = 0; + lowLimit = (const BYTE*)source; + } + } + forwardH = LZ4_hashPosition(forwardIp, tableType); + LZ4_putPositionOnHash(ip, h, ctx, tableType, base); + + } while ( ((dictIssue==dictSmall) ? (match < lowRefLimit) : 0) + || ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip)) + || (LZ4_read32(match+refDelta) != LZ4_read32(ip)) ); + } + + /* Catch up */ + while ((ip>anchor) && (match+refDelta > lowLimit) && (unlikely(ip[-1]==match[refDelta-1]))) { ip--; match--; } + + { + /* Encode Literal length */ + unsigned litLength = (unsigned)(ip - anchor); + token = op++; + if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) + return 0; /* Check output limit */ + if (litLength>=RUN_MASK) + { + int len = (int)litLength-RUN_MASK; + *token=(RUN_MASK<= 255 ; len-=255) *op++ = 255; + *op++ = (BYTE)len; + } + else *token = (BYTE)(litLength< matchlimit) limit = matchlimit; + matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); + ip += MINMATCH + matchLength; + if (ip==limit) + { + unsigned more = LZ4_count(ip, (const BYTE*)source, matchlimit); + matchLength += more; + ip += more; + } + } + else + { + matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); + ip += MINMATCH + matchLength; + } + + if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength>>8) > olimit))) + return 0; /* Check output limit */ + if (matchLength>=ML_MASK) + { + *token += ML_MASK; + matchLength -= ML_MASK; + for (; matchLength >= 510 ; matchLength-=510) { *op++ = 255; *op++ = 255; } + if (matchLength >= 255) { matchLength-=255; *op++ = 255; } + *op++ = (BYTE)matchLength; + } + else *token += (BYTE)(matchLength); + } + + anchor = ip; + + /* Test end of chunk */ + if (ip > mflimit) break; + + /* Fill table */ + LZ4_putPosition(ip-2, ctx, tableType, base); + + /* Test next position */ + match = LZ4_getPosition(ip, ctx, tableType, base); + if (dict==usingExtDict) + { + if (match<(const BYTE*)source) + { + refDelta = dictDelta; + lowLimit = dictionary; + } + else + { + refDelta = 0; + lowLimit = (const BYTE*)source; + } + } + LZ4_putPosition(ip, ctx, tableType, base); + if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1) + && (match+MAX_DISTANCE>=ip) + && (LZ4_read32(match+refDelta)==LZ4_read32(ip)) ) + { token=op++; *token=0; goto _next_match; } + + /* Prepare next loop */ + forwardH = LZ4_hashPosition(++ip, tableType); + } + +_last_literals: + /* Encode Last Literals */ + { + const size_t lastRun = (size_t)(iend - anchor); + if ((outputLimited) && ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) + return 0; /* Check output limit */ + if (lastRun >= RUN_MASK) + { + size_t accumulator = lastRun - RUN_MASK; + *op++ = RUN_MASK << ML_BITS; + for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; + *op++ = (BYTE) accumulator; + } + else + { + *op++ = (BYTE)(lastRun<= LZ4_compressBound(inputSize)) + { + if (inputSize < LZ4_64Klimit) + return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration); + else + return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); + } + else + { + if (inputSize < LZ4_64Klimit) + return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); + else + return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); + } +} + + +int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +{ +#if (HEAPMODE) + void* ctxPtr = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ +#else + LZ4_stream_t ctx; + void* ctxPtr = &ctx; +#endif + + int result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); + +#if (HEAPMODE) + FREEMEM(ctxPtr); +#endif + return result; +} + + +int LZ4_compress_default(const char* source, char* dest, int inputSize, int maxOutputSize) +{ + return LZ4_compress_fast(source, dest, inputSize, maxOutputSize, 1); +} + + +/* hidden debug function */ +/* strangely enough, gcc generates faster code when this function is uncommented, even if unused */ +int LZ4_compress_fast_force(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +{ + LZ4_stream_t ctx; + + LZ4_resetStream(&ctx); + + if (inputSize < LZ4_64Klimit) + return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); + else + return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); +} + + +/******************************** +* destSize variant +********************************/ + +static int LZ4_compress_destSize_generic( + void* const ctx, + const char* const src, + char* const dst, + int* const srcSizePtr, + const int targetDstSize, + const tableType_t tableType) +{ + const BYTE* ip = (const BYTE*) src; + const BYTE* base = (const BYTE*) src; + const BYTE* lowLimit = (const BYTE*) src; + const BYTE* anchor = ip; + const BYTE* const iend = ip + *srcSizePtr; + const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* const matchlimit = iend - LASTLITERALS; + + BYTE* op = (BYTE*) dst; + BYTE* const oend = op + targetDstSize; + BYTE* const oMaxLit = op + targetDstSize - 2 /* offset */ - 8 /* because 8+MINMATCH==MFLIMIT */ - 1 /* token */; + BYTE* const oMaxMatch = op + targetDstSize - (LASTLITERALS + 1 /* token */); + BYTE* const oMaxSeq = oMaxLit - 1 /* token */; + + U32 forwardH; + + + /* Init conditions */ + if (targetDstSize < 1) return 0; /* Impossible to store anything */ + if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0; /* Unsupported input size, too large (or negative) */ + if ((tableType == byU16) && (*srcSizePtr>=LZ4_64Klimit)) return 0; /* Size too large (not within 64K limit) */ + if (*srcSizePtr> LZ4_skipTrigger); + + if (unlikely(forwardIp > mflimit)) + goto _last_literals; + + match = LZ4_getPositionOnHash(h, ctx, tableType, base); + forwardH = LZ4_hashPosition(forwardIp, tableType); + LZ4_putPositionOnHash(ip, h, ctx, tableType, base); + + } while ( ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip)) + || (LZ4_read32(match) != LZ4_read32(ip)) ); + } + + /* Catch up */ + while ((ip>anchor) && (match > lowLimit) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; } + + { + /* Encode Literal length */ + unsigned litLength = (unsigned)(ip - anchor); + token = op++; + if (op + ((litLength+240)/255) + litLength > oMaxLit) + { + /* Not enough space for a last match */ + op--; + goto _last_literals; + } + if (litLength>=RUN_MASK) + { + unsigned len = litLength - RUN_MASK; + *token=(RUN_MASK<= 255 ; len-=255) *op++ = 255; + *op++ = (BYTE)len; + } + else *token = (BYTE)(litLength< oMaxMatch) + { + /* Match description too long : reduce it */ + matchLength = (15-1) + (oMaxMatch-op) * 255; + } + //printf("offset %5i, matchLength%5i \n", (int)(ip-match), matchLength + MINMATCH); + ip += MINMATCH + matchLength; + + if (matchLength>=ML_MASK) + { + *token += ML_MASK; + matchLength -= ML_MASK; + while (matchLength >= 255) { matchLength-=255; *op++ = 255; } + *op++ = (BYTE)matchLength; + } + else *token += (BYTE)(matchLength); + } + + anchor = ip; + + /* Test end of block */ + if (ip > mflimit) break; + if (op > oMaxSeq) break; + + /* Fill table */ + LZ4_putPosition(ip-2, ctx, tableType, base); + + /* Test next position */ + match = LZ4_getPosition(ip, ctx, tableType, base); + LZ4_putPosition(ip, ctx, tableType, base); + if ( (match+MAX_DISTANCE>=ip) + && (LZ4_read32(match)==LZ4_read32(ip)) ) + { token=op++; *token=0; goto _next_match; } + + /* Prepare next loop */ + forwardH = LZ4_hashPosition(++ip, tableType); + } + +_last_literals: + /* Encode Last Literals */ + { + size_t lastRunSize = (size_t)(iend - anchor); + if (op + 1 /* token */ + ((lastRunSize+240)/255) /* litLength */ + lastRunSize /* literals */ > oend) + { + /* adapt lastRunSize to fill 'dst' */ + lastRunSize = (oend-op) - 1; + lastRunSize -= (lastRunSize+240)/255; + } + ip = anchor + lastRunSize; + + if (lastRunSize >= RUN_MASK) + { + size_t accumulator = lastRunSize - RUN_MASK; + *op++ = RUN_MASK << ML_BITS; + for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; + *op++ = (BYTE) accumulator; + } + else + { + *op++ = (BYTE)(lastRunSize<= LZ4_compressBound(*srcSizePtr)) /* compression success is guaranteed */ + { + return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); + } + else + { + if (*srcSizePtr < LZ4_64Klimit) + return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, byU16); + else + return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, LZ4_64bits() ? byU32 : byPtr); + } +} + + +int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize) +{ +#if (HEAPMODE) + void* ctx = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ +#else + LZ4_stream_t ctxBody; + void* ctx = &ctxBody; +#endif + + int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); + +#if (HEAPMODE) + FREEMEM(ctx); +#endif + return result; +} + + + +/******************************** +* Streaming functions +********************************/ + +LZ4_stream_t* LZ4_createStream(void) +{ + LZ4_stream_t* lz4s = (LZ4_stream_t*)ALLOCATOR(8, LZ4_STREAMSIZE_U64); + LZ4_STATIC_ASSERT(LZ4_STREAMSIZE >= sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ + LZ4_resetStream(lz4s); + return lz4s; +} + +void LZ4_resetStream (LZ4_stream_t* LZ4_stream) +{ + MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); +} + +int LZ4_freeStream (LZ4_stream_t* LZ4_stream) +{ + FREEMEM(LZ4_stream); + return (0); +} + + +#define HASH_UNIT sizeof(size_t) +int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) +{ + LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; + const BYTE* p = (const BYTE*)dictionary; + const BYTE* const dictEnd = p + dictSize; + const BYTE* base; + + if ((dict->initCheck) || (dict->currentOffset > 1 GB)) /* Uninitialized structure, or reuse overflow */ + LZ4_resetStream(LZ4_dict); + + if (dictSize < (int)HASH_UNIT) + { + dict->dictionary = NULL; + dict->dictSize = 0; + return 0; + } + + if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; + dict->currentOffset += 64 KB; + base = p - dict->currentOffset; + dict->dictionary = p; + dict->dictSize = (U32)(dictEnd - p); + dict->currentOffset += dict->dictSize; + + while (p <= dictEnd-HASH_UNIT) + { + LZ4_putPosition(p, dict->hashTable, byU32, base); + p+=3; + } + + return dict->dictSize; +} + + +static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, const BYTE* src) +{ + if ((LZ4_dict->currentOffset > 0x80000000) || + ((size_t)LZ4_dict->currentOffset > (size_t)src)) /* address space overflow */ + { + /* rescale hash table */ + U32 delta = LZ4_dict->currentOffset - 64 KB; + const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; + int i; + for (i=0; ihashTable[i] < delta) LZ4_dict->hashTable[i]=0; + else LZ4_dict->hashTable[i] -= delta; + } + LZ4_dict->currentOffset = 64 KB; + if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB; + LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; + } +} + + +int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) +{ + LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_stream; + const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize; + + const BYTE* smallest = (const BYTE*) source; + if (streamPtr->initCheck) return 0; /* Uninitialized structure detected */ + if ((streamPtr->dictSize>0) && (smallest>dictEnd)) smallest = dictEnd; + LZ4_renormDictT(streamPtr, smallest); + if (acceleration < 1) acceleration = ACCELERATION_DEFAULT; + + /* Check overlapping input/dictionary space */ + { + const BYTE* sourceEnd = (const BYTE*) source + inputSize; + if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) + { + streamPtr->dictSize = (U32)(dictEnd - sourceEnd); + if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB; + if (streamPtr->dictSize < 4) streamPtr->dictSize = 0; + streamPtr->dictionary = dictEnd - streamPtr->dictSize; + } + } + + /* prefix mode : source data follows dictionary */ + if (dictEnd == (const BYTE*)source) + { + int result; + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, withPrefix64k, dictSmall, acceleration); + else + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, withPrefix64k, noDictIssue, acceleration); + streamPtr->dictSize += (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + return result; + } + + /* external dictionary mode */ + { + int result; + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, usingExtDict, dictSmall, acceleration); + else + result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, usingExtDict, noDictIssue, acceleration); + streamPtr->dictionary = (const BYTE*)source; + streamPtr->dictSize = (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + return result; + } +} + + +/* Hidden debug function, to force external dictionary mode */ +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int inputSize) +{ + LZ4_stream_t_internal* streamPtr = (LZ4_stream_t_internal*)LZ4_dict; + int result; + const BYTE* const dictEnd = streamPtr->dictionary + streamPtr->dictSize; + + const BYTE* smallest = dictEnd; + if (smallest > (const BYTE*) source) smallest = (const BYTE*) source; + LZ4_renormDictT((LZ4_stream_t_internal*)LZ4_dict, smallest); + + result = LZ4_compress_generic(LZ4_dict, source, dest, inputSize, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); + + streamPtr->dictionary = (const BYTE*)source; + streamPtr->dictSize = (U32)inputSize; + streamPtr->currentOffset += (U32)inputSize; + + return result; +} + + +int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) +{ + LZ4_stream_t_internal* dict = (LZ4_stream_t_internal*) LZ4_dict; + const BYTE* previousDictEnd = dict->dictionary + dict->dictSize; + + if ((U32)dictSize > 64 KB) dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ + if ((U32)dictSize > dict->dictSize) dictSize = dict->dictSize; + + memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + + dict->dictionary = (const BYTE*)safeBuffer; + dict->dictSize = (U32)dictSize; + + return dictSize; +} + + + +/******************************* +* Decompression functions +*******************************/ +/* + * This generic decompression function cover all use cases. + * It shall be instantiated several times, using different sets of directives + * Note that it is essential this generic function is really inlined, + * in order to remove useless branches during compilation optimization. + */ +FORCE_INLINE int LZ4_decompress_generic( + const char* const source, + char* const dest, + int inputSize, + int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */ + + int endOnInput, /* endOnOutputSize, endOnInputSize */ + int partialDecoding, /* full, partial */ + int targetOutputSize, /* only used if partialDecoding==partial */ + int dict, /* noDict, withPrefix64k, usingExtDict */ + const BYTE* const lowPrefix, /* == dest if dict == noDict */ + const BYTE* const dictStart, /* only if dict==usingExtDict */ + const size_t dictSize /* note : = 0 if noDict */ + ) +{ + /* Local Variables */ + const BYTE* ip = (const BYTE*) source; + const BYTE* const iend = ip + inputSize; + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + outputSize; + BYTE* cpy; + BYTE* oexit = op + targetOutputSize; + const BYTE* const lowLimit = lowPrefix - dictSize; + + const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize; + const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4}; + const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3}; + + const int safeDecode = (endOnInput==endOnInputSize); + const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); + + + /* Special cases */ + if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT; /* targetOutputSize too high => decode everything */ + if ((endOnInput) && (unlikely(outputSize==0))) return ((inputSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */ + if ((!endOnInput) && (unlikely(outputSize==0))) return (*ip==0?1:-1); + + + /* Main Loop */ + while (1) + { + unsigned token; + size_t length; + const BYTE* match; + + /* get literal length */ + token = *ip++; + if ((length=(token>>ML_BITS)) == RUN_MASK) + { + unsigned s; + do + { + s = *ip++; + length += s; + } + while (likely((endOnInput)?ip(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) ) + || ((!endOnInput) && (cpy>oend-COPYLENGTH))) + { + if (partialDecoding) + { + if (cpy > oend) goto _output_error; /* Error : write attempt beyond end of output buffer */ + if ((endOnInput) && (ip+length > iend)) goto _output_error; /* Error : read attempt beyond end of input buffer */ + } + else + { + if ((!endOnInput) && (cpy != oend)) goto _output_error; /* Error : block decoding must stop exactly there */ + if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error; /* Error : input must be consumed */ + } + memcpy(op, ip, length); + ip += length; + op += length; + break; /* Necessarily EOF, due to parsing restrictions */ + } + LZ4_wildCopy(op, ip, cpy); + ip += length; op = cpy; + + /* get offset */ + match = cpy - LZ4_readLE16(ip); ip+=2; + if ((checkOffset) && (unlikely(match < lowLimit))) goto _output_error; /* Error : offset outside destination buffer */ + + /* get matchlength */ + length = token & ML_MASK; + if (length == ML_MASK) + { + unsigned s; + do + { + if ((endOnInput) && (ip > iend-LASTLITERALS)) goto _output_error; + s = *ip++; + length += s; + } while (s==255); + if ((safeDecode) && unlikely((size_t)(op+length)<(size_t)op)) goto _output_error; /* overflow detection */ + } + length += MINMATCH; + + /* check external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) + { + if (unlikely(op+length > oend-LASTLITERALS)) goto _output_error; /* doesn't respect parsing restriction */ + + if (length <= (size_t)(lowPrefix-match)) + { + /* match can be copied as a single segment from external dictionary */ + match = dictEnd - (lowPrefix-match); + memmove(op, match, length); op += length; + } + else + { + /* match encompass external dictionary and current segment */ + size_t copySize = (size_t)(lowPrefix-match); + memcpy(op, dictEnd - copySize, copySize); + op += copySize; + copySize = length - copySize; + if (copySize > (size_t)(op-lowPrefix)) /* overlap within current segment */ + { + BYTE* const endOfMatch = op + copySize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) *op++ = *copyFrom++; + } + else + { + memcpy(op, lowPrefix, copySize); + op += copySize; + } + } + continue; + } + + /* copy repeated sequence */ + cpy = op + length; + if (unlikely((op-match)<8)) + { + const size_t dec64 = dec64table[op-match]; + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += dec32table[op-match]; + LZ4_copy4(op+4, match); + op += 8; match -= dec64; + } else { LZ4_copy8(op, match); op+=8; match+=8; } + + if (unlikely(cpy>oend-12)) + { + if (cpy > oend-LASTLITERALS) goto _output_error; /* Error : last LASTLITERALS bytes must be literals */ + if (op < oend-8) + { + LZ4_wildCopy(op, match, oend-8); + match += (oend-8) - op; + op = oend-8; + } + while (opprefixSize = (size_t) dictSize; + lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize; + lz4sd->externalDict = NULL; + lz4sd->extDictSize = 0; + return 1; +} + +/* +*_continue() : + These decoding functions allow decompression of multiple blocks in "streaming" mode. + Previously decoded blocks must still be available at the memory position where they were decoded. + If it's not possible, save the relevant part of decoded data into a safe buffer, + and indicate where it stands using LZ4_setStreamDecode() +*/ +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) +{ + LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode; + int result; + + if (lz4sd->prefixEnd == (BYTE*)dest) + { + result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + endOnInputSize, full, 0, + usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += result; + lz4sd->prefixEnd += result; + } + else + { + lz4sd->extDictSize = lz4sd->prefixSize; + lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; + result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + endOnInputSize, full, 0, + usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = result; + lz4sd->prefixEnd = (BYTE*)dest + result; + } + + return result; +} + +int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int originalSize) +{ + LZ4_streamDecode_t_internal* lz4sd = (LZ4_streamDecode_t_internal*) LZ4_streamDecode; + int result; + + if (lz4sd->prefixEnd == (BYTE*)dest) + { + result = LZ4_decompress_generic(source, dest, 0, originalSize, + endOnOutputSize, full, 0, + usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += originalSize; + lz4sd->prefixEnd += originalSize; + } + else + { + lz4sd->extDictSize = lz4sd->prefixSize; + lz4sd->externalDict = (BYTE*)dest - lz4sd->extDictSize; + result = LZ4_decompress_generic(source, dest, 0, originalSize, + endOnOutputSize, full, 0, + usingExtDict, (BYTE*)dest, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = originalSize; + lz4sd->prefixEnd = (BYTE*)dest + originalSize; + } + + return result; +} + + +/* +Advanced decoding functions : +*_usingDict() : + These decoding functions work the same as "_continue" ones, + the dictionary must be explicitly provided within parameters +*/ + +FORCE_INLINE int LZ4_decompress_usingDict_generic(const char* source, char* dest, int compressedSize, int maxOutputSize, int safe, const char* dictStart, int dictSize) +{ + if (dictSize==0) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest, NULL, 0); + if (dictStart+dictSize == dest) + { + if (dictSize >= (int)(64 KB - 1)) + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, (BYTE*)dest-64 KB, NULL, 0); + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE*)dest-dictSize, NULL, 0); + } + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize); +} + +int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize); +} + +int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize); +} + +/* debug function */ +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, (BYTE*)dest, (const BYTE*)dictStart, dictSize); +} + + +/*************************************************** +* Obsolete Functions +***************************************************/ +/* obsolete compression functions */ +int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) { return LZ4_compress_default(source, dest, inputSize, maxOutputSize); } +int LZ4_compress(const char* source, char* dest, int inputSize) { return LZ4_compress_default(source, dest, inputSize, LZ4_compressBound(inputSize)); } +int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); } +int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); } +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, maxDstSize, 1); } +int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) { return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); } + +/* +These function names are deprecated and should no longer be used. +They are only provided here for compatibility with older user programs. +- LZ4_uncompress is totally equivalent to LZ4_decompress_fast +- LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe +*/ +int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); } +int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); } + + +/* Obsolete Streaming functions */ + +int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; } + +static void LZ4_init(LZ4_stream_t_internal* lz4ds, BYTE* base) +{ + MEM_INIT(lz4ds, 0, LZ4_STREAMSIZE); + lz4ds->bufferStart = base; +} + +int LZ4_resetStreamState(void* state, char* inputBuffer) +{ + if ((((size_t)state) & 3) != 0) return 1; /* Error : pointer is not aligned on 4-bytes boundary */ + LZ4_init((LZ4_stream_t_internal*)state, (BYTE*)inputBuffer); + return 0; +} + +void* LZ4_create (char* inputBuffer) +{ + void* lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64); + LZ4_init ((LZ4_stream_t_internal*)lz4ds, (BYTE*)inputBuffer); + return lz4ds; +} + +char* LZ4_slideInputBuffer (void* LZ4_Data) +{ + LZ4_stream_t_internal* ctx = (LZ4_stream_t_internal*)LZ4_Data; + int dictSize = LZ4_saveDict((LZ4_stream_t*)LZ4_Data, (char*)ctx->bufferStart, 64 KB); + return (char*)(ctx->bufferStart + dictSize); +} + +/* Obsolete streaming decompression functions */ + +int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB); +} + +int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) +{ + return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, (BYTE*)dest - 64 KB, NULL, 64 KB); +} + +#endif /* LZ4_COMMONDEFS_ONLY */ + diff --git a/contrib/liblz4/src/lz4hc.c b/contrib/liblz4/src/lz4hc.c new file mode 100644 index 00000000000..8cc1a3398da --- /dev/null +++ b/contrib/liblz4/src/lz4hc.c @@ -0,0 +1,731 @@ +/* + LZ4 HC - High Compression Mode of LZ4 + Copyright (C) 2011-2015, Yann Collet. + + BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + You can contact the author at : + - LZ4 source repository : https://github.com/Cyan4973/lz4 + - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c +*/ + + + +/************************************** +* Tuning Parameter +**************************************/ +static const int LZ4HC_compressionLevel_default = 9; + + +/************************************** +* Includes +**************************************/ +#include + + +/************************************** +* Local Compiler Options +**************************************/ +#if defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wunused-function" +#endif + +#if defined (__clang__) +# pragma clang diagnostic ignored "-Wunused-function" +#endif + + +/************************************** +* Common LZ4 definition +**************************************/ +#define LZ4_COMMONDEFS_ONLY +#include "lz4.c" + + +/************************************** +* Local Constants +**************************************/ +#define DICTIONARY_LOGSIZE 16 +#define MAXD (1<> ((MINMATCH*8)-HASH_LOG)) +//#define DELTANEXTU16(p) chainTable[(p) & MAXD_MASK] /* flexible, MAXD dependent */ +#define DELTANEXTU16(p) chainTable[(U16)(p)] /* faster */ + +static U32 LZ4HC_hashPtr(const void* ptr) { return HASH_FUNCTION(LZ4_read32(ptr)); } + + + +/************************************** +* HC Compression +**************************************/ +static void LZ4HC_init (LZ4HC_Data_Structure* hc4, const BYTE* start) +{ + MEM_INIT((void*)hc4->hashTable, 0, sizeof(hc4->hashTable)); + MEM_INIT(hc4->chainTable, 0xFF, sizeof(hc4->chainTable)); + hc4->nextToUpdate = 64 KB; + hc4->base = start - 64 KB; + hc4->end = start; + hc4->dictBase = start - 64 KB; + hc4->dictLimit = 64 KB; + hc4->lowLimit = 64 KB; +} + + +/* Update chains up to ip (excluded) */ +FORCE_INLINE void LZ4HC_Insert (LZ4HC_Data_Structure* hc4, const BYTE* ip) +{ + U16* chainTable = hc4->chainTable; + U32* HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const U32 target = (U32)(ip - base); + U32 idx = hc4->nextToUpdate; + + while(idx < target) + { + U32 h = LZ4HC_hashPtr(base+idx); + size_t delta = idx - HashTable[h]; + if (delta>MAX_DISTANCE) delta = MAX_DISTANCE; + DELTANEXTU16(idx) = (U16)delta; + HashTable[h] = idx; + idx++; + } + + hc4->nextToUpdate = target; +} + + +FORCE_INLINE int LZ4HC_InsertAndFindBestMatch (LZ4HC_Data_Structure* hc4, /* Index table will be updated */ + const BYTE* ip, const BYTE* const iLimit, + const BYTE** matchpos, + const int maxNbAttempts) +{ + U16* const chainTable = hc4->chainTable; + U32* const HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const BYTE* const dictBase = hc4->dictBase; + const U32 dictLimit = hc4->dictLimit; + const U32 lowLimit = (hc4->lowLimit + 64 KB > (U32)(ip-base)) ? hc4->lowLimit : (U32)(ip - base) - (64 KB - 1); + U32 matchIndex; + const BYTE* match; + int nbAttempts=maxNbAttempts; + size_t ml=0; + + /* HC4 match finder */ + LZ4HC_Insert(hc4, ip); + matchIndex = HashTable[LZ4HC_hashPtr(ip)]; + + while ((matchIndex>=lowLimit) && (nbAttempts)) + { + nbAttempts--; + if (matchIndex >= dictLimit) + { + match = base + matchIndex; + if (*(match+ml) == *(ip+ml) + && (LZ4_read32(match) == LZ4_read32(ip))) + { + size_t mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, iLimit) + MINMATCH; + if (mlt > ml) { ml = mlt; *matchpos = match; } + } + } + else + { + match = dictBase + matchIndex; + if (LZ4_read32(match) == LZ4_read32(ip)) + { + size_t mlt; + const BYTE* vLimit = ip + (dictLimit - matchIndex); + if (vLimit > iLimit) vLimit = iLimit; + mlt = LZ4_count(ip+MINMATCH, match+MINMATCH, vLimit) + MINMATCH; + if ((ip+mlt == vLimit) && (vLimit < iLimit)) + mlt += LZ4_count(ip+mlt, base+dictLimit, iLimit); + if (mlt > ml) { ml = mlt; *matchpos = base + matchIndex; } /* virtual matchpos */ + } + } + matchIndex -= DELTANEXTU16(matchIndex); + } + + return (int)ml; +} + + +FORCE_INLINE int LZ4HC_InsertAndGetWiderMatch ( + LZ4HC_Data_Structure* hc4, + const BYTE* const ip, + const BYTE* const iLowLimit, + const BYTE* const iHighLimit, + int longest, + const BYTE** matchpos, + const BYTE** startpos, + const int maxNbAttempts) +{ + U16* const chainTable = hc4->chainTable; + U32* const HashTable = hc4->hashTable; + const BYTE* const base = hc4->base; + const U32 dictLimit = hc4->dictLimit; + const BYTE* const lowPrefixPtr = base + dictLimit; + const U32 lowLimit = (hc4->lowLimit + 64 KB > (U32)(ip-base)) ? hc4->lowLimit : (U32)(ip - base) - (64 KB - 1); + const BYTE* const dictBase = hc4->dictBase; + U32 matchIndex; + int nbAttempts = maxNbAttempts; + int delta = (int)(ip-iLowLimit); + + + /* First Match */ + LZ4HC_Insert(hc4, ip); + matchIndex = HashTable[LZ4HC_hashPtr(ip)]; + + while ((matchIndex>=lowLimit) && (nbAttempts)) + { + nbAttempts--; + if (matchIndex >= dictLimit) + { + const BYTE* matchPtr = base + matchIndex; + if (*(iLowLimit + longest) == *(matchPtr - delta + longest)) + if (LZ4_read32(matchPtr) == LZ4_read32(ip)) + { + int mlt = MINMATCH + LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, iHighLimit); + int back = 0; + + while ((ip+back>iLowLimit) + && (matchPtr+back > lowPrefixPtr) + && (ip[back-1] == matchPtr[back-1])) + back--; + + mlt -= back; + + if (mlt > longest) + { + longest = (int)mlt; + *matchpos = matchPtr+back; + *startpos = ip+back; + } + } + } + else + { + const BYTE* matchPtr = dictBase + matchIndex; + if (LZ4_read32(matchPtr) == LZ4_read32(ip)) + { + size_t mlt; + int back=0; + const BYTE* vLimit = ip + (dictLimit - matchIndex); + if (vLimit > iHighLimit) vLimit = iHighLimit; + mlt = LZ4_count(ip+MINMATCH, matchPtr+MINMATCH, vLimit) + MINMATCH; + if ((ip+mlt == vLimit) && (vLimit < iHighLimit)) + mlt += LZ4_count(ip+mlt, base+dictLimit, iHighLimit); + while ((ip+back > iLowLimit) && (matchIndex+back > lowLimit) && (ip[back-1] == matchPtr[back-1])) back--; + mlt -= back; + if ((int)mlt > longest) { longest = (int)mlt; *matchpos = base + matchIndex + back; *startpos = ip+back; } + } + } + matchIndex -= DELTANEXTU16(matchIndex); + } + + return longest; +} + + +typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive; + +#define LZ4HC_DEBUG 0 +#if LZ4HC_DEBUG +static unsigned debug = 0; +#endif + +FORCE_INLINE int LZ4HC_encodeSequence ( + const BYTE** ip, + BYTE** op, + const BYTE** anchor, + int matchLength, + const BYTE* const match, + limitedOutput_directive limitedOutputBuffer, + BYTE* oend) +{ + int length; + BYTE* token; + +#if LZ4HC_DEBUG + if (debug) printf("literal : %u -- match : %u -- offset : %u\n", (U32)(*ip - *anchor), (U32)matchLength, (U32)(*ip-match)); +#endif + + /* Encode Literal length */ + length = (int)(*ip - *anchor); + token = (*op)++; + if ((limitedOutputBuffer) && ((*op + (length>>8) + length + (2 + 1 + LASTLITERALS)) > oend)) return 1; /* Check output limit */ + if (length>=(int)RUN_MASK) { int len; *token=(RUN_MASK< 254 ; len-=255) *(*op)++ = 255; *(*op)++ = (BYTE)len; } + else *token = (BYTE)(length<>8) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ + if (length>=(int)ML_MASK) { *token+=ML_MASK; length-=ML_MASK; for(; length > 509 ; length-=510) { *(*op)++ = 255; *(*op)++ = 255; } if (length > 254) { length-=255; *(*op)++ = 255; } *(*op)++ = (BYTE)length; } + else *token += (BYTE)(length); + + /* Prepare next loop */ + *ip += matchLength; + *anchor = *ip; + + return 0; +} + + +static int LZ4HC_compress_generic ( + void* ctxvoid, + const char* source, + char* dest, + int inputSize, + int maxOutputSize, + int compressionLevel, + limitedOutput_directive limit + ) +{ + LZ4HC_Data_Structure* ctx = (LZ4HC_Data_Structure*) ctxvoid; + const BYTE* ip = (const BYTE*) source; + const BYTE* anchor = ip; + const BYTE* const iend = ip + inputSize; + const BYTE* const mflimit = iend - MFLIMIT; + const BYTE* const matchlimit = (iend - LASTLITERALS); + + BYTE* op = (BYTE*) dest; + BYTE* const oend = op + maxOutputSize; + + unsigned maxNbAttempts; + int ml, ml2, ml3, ml0; + const BYTE* ref=NULL; + const BYTE* start2=NULL; + const BYTE* ref2=NULL; + const BYTE* start3=NULL; + const BYTE* ref3=NULL; + const BYTE* start0; + const BYTE* ref0; + + + /* init */ + if (compressionLevel > g_maxCompressionLevel) compressionLevel = g_maxCompressionLevel; + if (compressionLevel < 1) compressionLevel = LZ4HC_compressionLevel_default; + maxNbAttempts = 1 << (compressionLevel-1); + ctx->end += inputSize; + + ip++; + + /* Main Loop */ + while (ip < mflimit) + { + ml = LZ4HC_InsertAndFindBestMatch (ctx, ip, matchlimit, (&ref), maxNbAttempts); + if (!ml) { ip++; continue; } + + /* saved, in case we would skip too much */ + start0 = ip; + ref0 = ref; + ml0 = ml; + +_Search2: + if (ip+ml < mflimit) + ml2 = LZ4HC_InsertAndGetWiderMatch(ctx, ip + ml - 2, ip + 1, matchlimit, ml, &ref2, &start2, maxNbAttempts); + else ml2 = ml; + + if (ml2 == ml) /* No better match */ + { + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + continue; + } + + if (start0 < ip) + { + if (start2 < ip + ml0) /* empirical */ + { + ip = start0; + ref = ref0; + ml = ml0; + } + } + + /* Here, start0==ip */ + if ((start2 - ip) < 3) /* First Match too small : removed */ + { + ml = ml2; + ip = start2; + ref =ref2; + goto _Search2; + } + +_Search3: + /* + * Currently we have : + * ml2 > ml1, and + * ip1+3 <= ip2 (usually < ip1+ml1) + */ + if ((start2 - ip) < OPTIMAL_ML) + { + int correction; + int new_ml = ml; + if (new_ml > OPTIMAL_ML) new_ml = OPTIMAL_ML; + if (ip+new_ml > start2 + ml2 - MINMATCH) new_ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = new_ml - (int)(start2 - ip); + if (correction > 0) + { + start2 += correction; + ref2 += correction; + ml2 -= correction; + } + } + /* Now, we have start2 = ip+new_ml, with new_ml = min(ml, OPTIMAL_ML=18) */ + + if (start2 + ml2 < mflimit) + ml3 = LZ4HC_InsertAndGetWiderMatch(ctx, start2 + ml2 - 3, start2, matchlimit, ml2, &ref3, &start3, maxNbAttempts); + else ml3 = ml2; + + if (ml3 == ml2) /* No better match : 2 sequences to encode */ + { + /* ip & ref are known; Now for ml */ + if (start2 < ip+ml) ml = (int)(start2 - ip); + /* Now, encode 2 sequences */ + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + ip = start2; + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml2, ref2, limit, oend)) return 0; + continue; + } + + if (start3 < ip+ml+3) /* Not enough space for match 2 : remove it */ + { + if (start3 >= (ip+ml)) /* can write Seq1 immediately ==> Seq2 is removed, so Seq3 becomes Seq1 */ + { + if (start2 < ip+ml) + { + int correction = (int)(ip+ml - start2); + start2 += correction; + ref2 += correction; + ml2 -= correction; + if (ml2 < MINMATCH) + { + start2 = start3; + ref2 = ref3; + ml2 = ml3; + } + } + + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + ip = start3; + ref = ref3; + ml = ml3; + + start0 = start2; + ref0 = ref2; + ml0 = ml2; + goto _Search2; + } + + start2 = start3; + ref2 = ref3; + ml2 = ml3; + goto _Search3; + } + + /* + * OK, now we have 3 ascending matches; let's write at least the first one + * ip & ref are known; Now for ml + */ + if (start2 < ip+ml) + { + if ((start2 - ip) < (int)ML_MASK) + { + int correction; + if (ml > OPTIMAL_ML) ml = OPTIMAL_ML; + if (ip + ml > start2 + ml2 - MINMATCH) ml = (int)(start2 - ip) + ml2 - MINMATCH; + correction = ml - (int)(start2 - ip); + if (correction > 0) + { + start2 += correction; + ref2 += correction; + ml2 -= correction; + } + } + else + { + ml = (int)(start2 - ip); + } + } + if (LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ref, limit, oend)) return 0; + + ip = start2; + ref = ref2; + ml = ml2; + + start2 = start3; + ref2 = ref3; + ml2 = ml3; + + goto _Search3; + } + + /* Encode Last Literals */ + { + int lastRun = (int)(iend - anchor); + if ((limit) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; /* Check output limit */ + if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK< 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; } + else *op++ = (BYTE)(lastRun<base = NULL; + ((LZ4HC_Data_Structure*)LZ4_streamHCPtr)->compressionLevel = (unsigned)compressionLevel; +} + +int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, const char* dictionary, int dictSize) +{ + LZ4HC_Data_Structure* ctxPtr = (LZ4HC_Data_Structure*) LZ4_streamHCPtr; + if (dictSize > 64 KB) + { + dictionary += dictSize - 64 KB; + dictSize = 64 KB; + } + LZ4HC_init (ctxPtr, (const BYTE*)dictionary); + if (dictSize >= 4) LZ4HC_Insert (ctxPtr, (const BYTE*)dictionary +(dictSize-3)); + ctxPtr->end = (const BYTE*)dictionary + dictSize; + return dictSize; +} + + +/* compression */ + +static void LZ4HC_setExternalDict(LZ4HC_Data_Structure* ctxPtr, const BYTE* newBlock) +{ + if (ctxPtr->end >= ctxPtr->base + 4) + LZ4HC_Insert (ctxPtr, ctxPtr->end-3); /* Referencing remaining dictionary content */ + /* Only one memory segment for extDict, so any previous extDict is lost at this stage */ + ctxPtr->lowLimit = ctxPtr->dictLimit; + ctxPtr->dictLimit = (U32)(ctxPtr->end - ctxPtr->base); + ctxPtr->dictBase = ctxPtr->base; + ctxPtr->base = newBlock - ctxPtr->dictLimit; + ctxPtr->end = newBlock; + ctxPtr->nextToUpdate = ctxPtr->dictLimit; /* match referencing will resume from there */ +} + +static int LZ4_compressHC_continue_generic (LZ4HC_Data_Structure* ctxPtr, + const char* source, char* dest, + int inputSize, int maxOutputSize, limitedOutput_directive limit) +{ + /* auto-init if forgotten */ + if (ctxPtr->base == NULL) + LZ4HC_init (ctxPtr, (const BYTE*) source); + + /* Check overflow */ + if ((size_t)(ctxPtr->end - ctxPtr->base) > 2 GB) + { + size_t dictSize = (size_t)(ctxPtr->end - ctxPtr->base) - ctxPtr->dictLimit; + if (dictSize > 64 KB) dictSize = 64 KB; + + LZ4_loadDictHC((LZ4_streamHC_t*)ctxPtr, (const char*)(ctxPtr->end) - dictSize, (int)dictSize); + } + + /* Check if blocks follow each other */ + if ((const BYTE*)source != ctxPtr->end) + LZ4HC_setExternalDict(ctxPtr, (const BYTE*)source); + + /* Check overlapping input/dictionary space */ + { + const BYTE* sourceEnd = (const BYTE*) source + inputSize; + const BYTE* dictBegin = ctxPtr->dictBase + ctxPtr->lowLimit; + const BYTE* dictEnd = ctxPtr->dictBase + ctxPtr->dictLimit; + if ((sourceEnd > dictBegin) && ((const BYTE*)source < dictEnd)) + { + if (sourceEnd > dictEnd) sourceEnd = dictEnd; + ctxPtr->lowLimit = (U32)(sourceEnd - ctxPtr->dictBase); + if (ctxPtr->dictLimit - ctxPtr->lowLimit < 4) ctxPtr->lowLimit = ctxPtr->dictLimit; + } + } + + return LZ4HC_compress_generic (ctxPtr, source, dest, inputSize, maxOutputSize, ctxPtr->compressionLevel, limit); +} + +int LZ4_compress_HC_continue (LZ4_streamHC_t* LZ4_streamHCPtr, const char* source, char* dest, int inputSize, int maxOutputSize) +{ + if (maxOutputSize < LZ4_compressBound(inputSize)) + return LZ4_compressHC_continue_generic ((LZ4HC_Data_Structure*)LZ4_streamHCPtr, source, dest, inputSize, maxOutputSize, limitedOutput); + else + return LZ4_compressHC_continue_generic ((LZ4HC_Data_Structure*)LZ4_streamHCPtr, source, dest, inputSize, maxOutputSize, noLimit); +} + + +/* dictionary saving */ + +int LZ4_saveDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, char* safeBuffer, int dictSize) +{ + LZ4HC_Data_Structure* streamPtr = (LZ4HC_Data_Structure*)LZ4_streamHCPtr; + int prefixSize = (int)(streamPtr->end - (streamPtr->base + streamPtr->dictLimit)); + if (dictSize > 64 KB) dictSize = 64 KB; + if (dictSize < 4) dictSize = 0; + if (dictSize > prefixSize) dictSize = prefixSize; + memmove(safeBuffer, streamPtr->end - dictSize, dictSize); + { + U32 endIndex = (U32)(streamPtr->end - streamPtr->base); + streamPtr->end = (const BYTE*)safeBuffer + dictSize; + streamPtr->base = streamPtr->end - endIndex; + streamPtr->dictLimit = endIndex - dictSize; + streamPtr->lowLimit = endIndex - dictSize; + if (streamPtr->nextToUpdate < streamPtr->dictLimit) streamPtr->nextToUpdate = streamPtr->dictLimit; + } + return dictSize; +} + + +/*********************************** +* Deprecated Functions +***********************************/ +/* Deprecated compression functions */ +/* These functions are planned to start generate warnings by r131 approximately */ +int LZ4_compressHC(const char* src, char* dst, int srcSize) { return LZ4_compress_HC (src, dst, srcSize, LZ4_compressBound(srcSize), 0); } +int LZ4_compressHC_limitedOutput(const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_HC(src, dst, srcSize, maxDstSize, 0); } +int LZ4_compressHC2(const char* src, char* dst, int srcSize, int cLevel) { return LZ4_compress_HC (src, dst, srcSize, LZ4_compressBound(srcSize), cLevel); } +int LZ4_compressHC2_limitedOutput(const char* src, char* dst, int srcSize, int maxDstSize, int cLevel) { return LZ4_compress_HC(src, dst, srcSize, maxDstSize, cLevel); } +int LZ4_compressHC_withStateHC (void* state, const char* src, char* dst, int srcSize) { return LZ4_compress_HC_extStateHC (state, src, dst, srcSize, LZ4_compressBound(srcSize), 0); } +int LZ4_compressHC_limitedOutput_withStateHC (void* state, const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_HC_extStateHC (state, src, dst, srcSize, maxDstSize, 0); } +int LZ4_compressHC2_withStateHC (void* state, const char* src, char* dst, int srcSize, int cLevel) { return LZ4_compress_HC_extStateHC(state, src, dst, srcSize, LZ4_compressBound(srcSize), cLevel); } +int LZ4_compressHC2_limitedOutput_withStateHC (void* state, const char* src, char* dst, int srcSize, int maxDstSize, int cLevel) { return LZ4_compress_HC_extStateHC(state, src, dst, srcSize, maxDstSize, cLevel); } +int LZ4_compressHC_continue (LZ4_streamHC_t* ctx, const char* src, char* dst, int srcSize) { return LZ4_compress_HC_continue (ctx, src, dst, srcSize, LZ4_compressBound(srcSize)); } +int LZ4_compressHC_limitedOutput_continue (LZ4_streamHC_t* ctx, const char* src, char* dst, int srcSize, int maxDstSize) { return LZ4_compress_HC_continue (ctx, src, dst, srcSize, maxDstSize); } + + +/* Deprecated streaming functions */ +/* These functions currently generate deprecation warnings */ +int LZ4_sizeofStreamStateHC(void) { return LZ4_STREAMHCSIZE; } + +int LZ4_resetStreamStateHC(void* state, char* inputBuffer) +{ + if ((((size_t)state) & (sizeof(void*)-1)) != 0) return 1; /* Error : pointer is not aligned for pointer (32 or 64 bits) */ + LZ4HC_init((LZ4HC_Data_Structure*)state, (const BYTE*)inputBuffer); + ((LZ4HC_Data_Structure*)state)->inputBuffer = (BYTE*)inputBuffer; + return 0; +} + +void* LZ4_createHC (char* inputBuffer) +{ + void* hc4 = ALLOCATOR(1, sizeof(LZ4HC_Data_Structure)); + if (hc4 == NULL) return NULL; /* not enough memory */ + LZ4HC_init ((LZ4HC_Data_Structure*)hc4, (const BYTE*)inputBuffer); + ((LZ4HC_Data_Structure*)hc4)->inputBuffer = (BYTE*)inputBuffer; + return hc4; +} + +int LZ4_freeHC (void* LZ4HC_Data) +{ + FREEMEM(LZ4HC_Data); + return (0); +} + +int LZ4_compressHC2_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int compressionLevel) +{ + return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, 0, compressionLevel, noLimit); +} + +int LZ4_compressHC2_limitedOutput_continue (void* LZ4HC_Data, const char* source, char* dest, int inputSize, int maxOutputSize, int compressionLevel) +{ + return LZ4HC_compress_generic (LZ4HC_Data, source, dest, inputSize, maxOutputSize, compressionLevel, limitedOutput); +} + +char* LZ4_slideInputBufferHC(void* LZ4HC_Data) +{ + LZ4HC_Data_Structure* hc4 = (LZ4HC_Data_Structure*)LZ4HC_Data; + int dictSize = LZ4_saveDictHC((LZ4_streamHC_t*)LZ4HC_Data, (char*)(hc4->inputBuffer), 64 KB); + return (char*)(hc4->inputBuffer + dictSize); +} diff --git a/contrib/libmetrohash/CMakeLists.txt b/contrib/libmetrohash/CMakeLists.txt new file mode 100644 index 00000000000..6ce50e2de42 --- /dev/null +++ b/contrib/libmetrohash/CMakeLists.txt @@ -0,0 +1,13 @@ +include_directories (${CMAKE_CURRENT_BINARY_DIR}) + +IF (NOT AARCH64) # Не используется. Портировать не сложно. + SET(SOURCES_ONLY_ON_X86_64 src/metrohash128crc.cpp) +ENDIF() + +add_library(metrohash + src/metrohash.h + src/testvector.h + + src/metrohash64.cpp + src/metrohash128.cpp + ${SOURCES_ONLY_ON_X86_64}) diff --git a/contrib/libmetrohash/LICENSE b/contrib/libmetrohash/LICENSE new file mode 100644 index 00000000000..0765a504e62 --- /dev/null +++ b/contrib/libmetrohash/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 J. Andrew Rogers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/contrib/libmetrohash/README.md b/contrib/libmetrohash/README.md new file mode 100644 index 00000000000..a8851cdb2d8 --- /dev/null +++ b/contrib/libmetrohash/README.md @@ -0,0 +1,24 @@ +## MetroHash: Faster, Better Hash Functions + +MetroHash is a set of state-of-the-art hash functions for *non-cryptographic* use cases. They are notable for being algorithmically generated in addition to their exceptional performance. The set of published hash functions may be expanded in the future, having been selected from a very large set of hash functions that have been constructed this way. + +* Fastest general-purpose functions for bulk hashing. +* Fastest general-purpose functions for small, variable length keys. +* Robust statistical bias profile, similar to the MD5 cryptographic hash. +* 64-bit, 128-bit, and 128-bit CRC variants currently available. +* Optimized for modern x86-64 microarchitectures. +* Elegant, compact, readable functions. + +You can read more about the design and history [here](http://www.jandrewrogers.com/2015/05/27/metrohash/). + +Six hash functions have been included in the initial release: + +* 64-bit hash functions, "metrohash64_1" and "metrohash64_2" +* 128-bit hash functions, "metrohash128_1" and "metrohash128_2" +* 128-bit hash functions using CRC instructions, "metrohash128crc_1" and "metrohash128crc_2" + +Hash functions in the same family are effectively statistically unique. In other words, if you need two hash functions for a bloom filter, you can use "metrohash64_1" and "metrohash64_2" in the same implementation without issue. An unbounded set of statistically unique functions can be generated in each family. The functions in this repo were generated specifically for public release. + +The hash function generation software made no effort toward portability. While these hash functions should be easily portable to big-endian microarchitectures, they have not been tested on them and the performance optimization algorithms were not targeted at them. ARM64 microarchitectures might be a worthwhile hash function generation targets if I had the hardware. + + diff --git a/contrib/libmetrohash/VERSION b/contrib/libmetrohash/VERSION new file mode 100644 index 00000000000..211ea847416 --- /dev/null +++ b/contrib/libmetrohash/VERSION @@ -0,0 +1,7 @@ +origin: git@github.com:jandrewrogers/MetroHash.git +commit d9dee18a54a8a6766e24c1950b814ac7ca9d1a89 +Merge: 761e8a4 3d06b24 +Author: J. Andrew Rogers +Date: Sat Jun 6 16:12:06 2015 -0700 + + modified README diff --git a/contrib/libmetrohash/src/metrohash.h b/contrib/libmetrohash/src/metrohash.h new file mode 100644 index 00000000000..0d9b76c99cf --- /dev/null +++ b/contrib/libmetrohash/src/metrohash.h @@ -0,0 +1,73 @@ +// metrohash.h +// +// The MIT License (MIT) +// +// Copyright (c) 2015 J. Andrew Rogers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#ifndef METROHASH_METROHASH_H +#define METROHASH_METROHASH_H + +#include +#include + +// MetroHash 64-bit hash functions +void metrohash64_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); +void metrohash64_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); + +// MetroHash 128-bit hash functions +void metrohash128_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); +void metrohash128_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); + +// MetroHash 128-bit hash functions using CRC instruction +void metrohash128crc_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); +void metrohash128crc_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); + + +/* rotate right idiom recognized by compiler*/ +inline static uint64_t rotate_right(uint64_t v, unsigned k) +{ + return (v >> k) | (v << (64 - k)); +} + +// unaligned reads, fast and safe on Nehalem and later microarchitectures +inline static uint64_t read_u64(const void * const ptr) +{ + return static_cast(*reinterpret_cast(ptr)); +} + +inline static uint64_t read_u32(const void * const ptr) +{ + return static_cast(*reinterpret_cast(ptr)); +} + +inline static uint64_t read_u16(const void * const ptr) +{ + return static_cast(*reinterpret_cast(ptr)); +} + +inline static uint64_t read_u8 (const void * const ptr) +{ + return static_cast(*reinterpret_cast(ptr)); +} + + +#endif // #ifndef METROHASH_METROHASH_H diff --git a/contrib/libmetrohash/src/metrohash128.cpp b/contrib/libmetrohash/src/metrohash128.cpp new file mode 100644 index 00000000000..6370412046e --- /dev/null +++ b/contrib/libmetrohash/src/metrohash128.cpp @@ -0,0 +1,178 @@ +// metrohash128.cpp +// +// The MIT License (MIT) +// +// Copyright (c) 2015 J. Andrew Rogers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#include "metrohash.h" + +void metrohash128_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xC83A91E1; + static const uint64_t k1 = 0x8648DBDB; + static const uint64_t k2 = 0x7BDEC03B; + static const uint64_t k3 = 0x2F5870A5; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t v[4]; + + v[0] = ((static_cast(seed) - k0) * k3) + len; + v[1] = ((static_cast(seed) + k1) * k2) + len; + + if (len >= 32) + { + v[2] = ((static_cast(seed) + k0) * k2) + len; + v[3] = ((static_cast(seed) - k1) * k3) + len; + + do + { + v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2]; + v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3]; + v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0]; + v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1]; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 26) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 26) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 26) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 30) * k0; + } + + if ((end - ptr) >= 16) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],33) * k3; + v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],33) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 17) * k1; + v[1] ^= rotate_right((v[1] * k3) + v[0], 17) * k0; + } + + if ((end - ptr) >= 8) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],33) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 20) * k1; + } + + if ((end - ptr) >= 4) + { + v[1] += read_u32(ptr) * k2; ptr += 4; v[1] = rotate_right(v[1],33) * k3; + v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0; + } + + if ((end - ptr) >= 2) + { + v[0] += read_u16(ptr) * k2; ptr += 2; v[0] = rotate_right(v[0],33) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 24) * k1; + } + + if ((end - ptr) >= 1) + { + v[1] += read_u8 (ptr) * k2; v[1] = rotate_right(v[1],33) * k3; + v[1] ^= rotate_right((v[1] * k3) + v[0], 24) * k0; + } + + v[0] += rotate_right((v[0] * k0) + v[1], 13); + v[1] += rotate_right((v[1] * k1) + v[0], 37); + v[0] += rotate_right((v[0] * k2) + v[1], 13); + v[1] += rotate_right((v[1] * k3) + v[0], 37); + + memcpy(out, v, 16); +} + + +void metrohash128_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xD6D018F5; + static const uint64_t k1 = 0xA2AA033B; + static const uint64_t k2 = 0x62992FC1; + static const uint64_t k3 = 0x30BC5B29; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t v[4]; + + v[0] = ((static_cast(seed) - k0) * k3) + len; + v[1] = ((static_cast(seed) + k1) * k2) + len; + + if (len >= 32) + { + v[2] = ((static_cast(seed) + k0) * k2) + len; + v[3] = ((static_cast(seed) - k1) * k3) + len; + + do + { + v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2]; + v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3]; + v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0]; + v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1]; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 33) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 33) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 33) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 33) * k0; + } + + if ((end - ptr) >= 16) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],29) * k3; + v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],29) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 29) * k1; + v[1] ^= rotate_right((v[1] * k3) + v[0], 29) * k0; + } + + if ((end - ptr) >= 8) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],29) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 29) * k1; + } + + if ((end - ptr) >= 4) + { + v[1] += read_u32(ptr) * k2; ptr += 4; v[1] = rotate_right(v[1],29) * k3; + v[1] ^= rotate_right((v[1] * k3) + v[0], 25) * k0; + } + + if ((end - ptr) >= 2) + { + v[0] += read_u16(ptr) * k2; ptr += 2; v[0] = rotate_right(v[0],29) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 30) * k1; + } + + if ((end - ptr) >= 1) + { + v[1] += read_u8 (ptr) * k2; v[1] = rotate_right(v[1],29) * k3; + v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0; + } + + v[0] += rotate_right((v[0] * k0) + v[1], 33); + v[1] += rotate_right((v[1] * k1) + v[0], 33); + v[0] += rotate_right((v[0] * k2) + v[1], 33); + v[1] += rotate_right((v[1] * k3) + v[0], 33); + + memcpy(out, v, 16); +} + diff --git a/contrib/libmetrohash/src/metrohash128crc.cpp b/contrib/libmetrohash/src/metrohash128crc.cpp new file mode 100644 index 00000000000..c04cf5a6b23 --- /dev/null +++ b/contrib/libmetrohash/src/metrohash128crc.cpp @@ -0,0 +1,180 @@ +// metrohash128crc.cpp +// +// The MIT License (MIT) +// +// Copyright (c) 2015 J. Andrew Rogers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + + +#include "metrohash.h" +#include + + +void metrohash128crc_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xC83A91E1; + static const uint64_t k1 = 0x8648DBDB; + static const uint64_t k2 = 0x7BDEC03B; + static const uint64_t k3 = 0x2F5870A5; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t v[4]; + + v[0] = ((static_cast(seed) - k0) * k3) + len; + v[1] = ((static_cast(seed) + k1) * k2) + len; + + if (len >= 32) + { + v[2] = ((static_cast(seed) + k0) * k2) + len; + v[3] = ((static_cast(seed) - k1) * k3) + len; + + do + { + v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8; + v[1] ^= _mm_crc32_u64(v[1], read_u64(ptr)); ptr += 8; + v[2] ^= _mm_crc32_u64(v[2], read_u64(ptr)); ptr += 8; + v[3] ^= _mm_crc32_u64(v[3], read_u64(ptr)); ptr += 8; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 34) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 37) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 34) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 37) * k0; + } + + if ((end - ptr) >= 16) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],34) * k3; + v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],34) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 30) * k1; + v[1] ^= rotate_right((v[1] * k3) + v[0], 30) * k0; + } + + if ((end - ptr) >= 8) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],36) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 23) * k1; + } + + if ((end - ptr) >= 4) + { + v[1] ^= _mm_crc32_u64(v[0], read_u32(ptr)); ptr += 4; + v[1] ^= rotate_right((v[1] * k3) + v[0], 19) * k0; + } + + if ((end - ptr) >= 2) + { + v[0] ^= _mm_crc32_u64(v[1], read_u16(ptr)); ptr += 2; + v[0] ^= rotate_right((v[0] * k2) + v[1], 13) * k1; + } + + if ((end - ptr) >= 1) + { + v[1] ^= _mm_crc32_u64(v[0], read_u8 (ptr)); + v[1] ^= rotate_right((v[1] * k3) + v[0], 17) * k0; + } + + v[0] += rotate_right((v[0] * k0) + v[1], 11); + v[1] += rotate_right((v[1] * k1) + v[0], 26); + v[0] += rotate_right((v[0] * k0) + v[1], 11); + v[1] += rotate_right((v[1] * k1) + v[0], 26); + + memcpy(out, v, 16); +} + + +void metrohash128crc_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xEE783E2F; + static const uint64_t k1 = 0xAD07C493; + static const uint64_t k2 = 0x797A90BB; + static const uint64_t k3 = 0x2E4B2E1B; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t v[4]; + + v[0] = ((static_cast(seed) - k0) * k3) + len; + v[1] = ((static_cast(seed) + k1) * k2) + len; + + if (len >= 32) + { + v[2] = ((static_cast(seed) + k0) * k2) + len; + v[3] = ((static_cast(seed) - k1) * k3) + len; + + do + { + v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8; + v[1] ^= _mm_crc32_u64(v[1], read_u64(ptr)); ptr += 8; + v[2] ^= _mm_crc32_u64(v[2], read_u64(ptr)); ptr += 8; + v[3] ^= _mm_crc32_u64(v[3], read_u64(ptr)); ptr += 8; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 12) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 19) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 12) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 19) * k0; + } + + if ((end - ptr) >= 16) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],41) * k3; + v[1] += read_u64(ptr) * k2; ptr += 8; v[1] = rotate_right(v[1],41) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 10) * k1; + v[1] ^= rotate_right((v[1] * k3) + v[0], 10) * k0; + } + + if ((end - ptr) >= 8) + { + v[0] += read_u64(ptr) * k2; ptr += 8; v[0] = rotate_right(v[0],34) * k3; + v[0] ^= rotate_right((v[0] * k2) + v[1], 22) * k1; + } + + if ((end - ptr) >= 4) + { + v[1] ^= _mm_crc32_u64(v[0], read_u32(ptr)); ptr += 4; + v[1] ^= rotate_right((v[1] * k3) + v[0], 14) * k0; + } + + if ((end - ptr) >= 2) + { + v[0] ^= _mm_crc32_u64(v[1], read_u16(ptr)); ptr += 2; + v[0] ^= rotate_right((v[0] * k2) + v[1], 15) * k1; + } + + if ((end - ptr) >= 1) + { + v[1] ^= _mm_crc32_u64(v[0], read_u8 (ptr)); + v[1] ^= rotate_right((v[1] * k3) + v[0], 18) * k0; + } + + v[0] += rotate_right((v[0] * k0) + v[1], 15); + v[1] += rotate_right((v[1] * k1) + v[0], 27); + v[0] += rotate_right((v[0] * k0) + v[1], 15); + v[1] += rotate_right((v[1] * k1) + v[0], 27); + + memcpy(out, v, 16); +} diff --git a/contrib/libmetrohash/src/metrohash64.cpp b/contrib/libmetrohash/src/metrohash64.cpp new file mode 100644 index 00000000000..bc4b41eb8f2 --- /dev/null +++ b/contrib/libmetrohash/src/metrohash64.cpp @@ -0,0 +1,182 @@ +// metrohash64.cpp +// +// The MIT License (MIT) +// +// Copyright (c) 2015 J. Andrew Rogers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#include "metrohash.h" + +void metrohash64_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xC83A91E1; + static const uint64_t k1 = 0x8648DBDB; + static const uint64_t k2 = 0x7BDEC03B; + static const uint64_t k3 = 0x2F5870A5; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t hash = ((static_cast(seed) + k2) * k0) + len; + + if (len >= 32) + { + uint64_t v[4]; + v[0] = hash; + v[1] = hash; + v[2] = hash; + v[3] = hash; + + do + { + v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2]; + v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3]; + v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0]; + v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1]; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 33) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 33) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 33) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 33) * k0; + hash += v[0] ^ v[1]; + } + + if ((end - ptr) >= 16) + { + uint64_t v0 = hash + (read_u64(ptr) * k0); ptr += 8; v0 = rotate_right(v0,33) * k1; + uint64_t v1 = hash + (read_u64(ptr) * k1); ptr += 8; v1 = rotate_right(v1,33) * k2; + v0 ^= rotate_right(v0 * k0, 35) + v1; + v1 ^= rotate_right(v1 * k3, 35) + v0; + hash += v1; + } + + if ((end - ptr) >= 8) + { + hash += read_u64(ptr) * k3; ptr += 8; + hash ^= rotate_right(hash, 33) * k1; + + } + + if ((end - ptr) >= 4) + { + hash += read_u32(ptr) * k3; ptr += 4; + hash ^= rotate_right(hash, 15) * k1; + } + + if ((end - ptr) >= 2) + { + hash += read_u16(ptr) * k3; ptr += 2; + hash ^= rotate_right(hash, 13) * k1; + } + + if ((end - ptr) >= 1) + { + hash += read_u8 (ptr) * k3; + hash ^= rotate_right(hash, 25) * k1; + } + + hash ^= rotate_right(hash, 33); + hash *= k0; + hash ^= rotate_right(hash, 33); + + memcpy(out, &hash, 8); +} + + +void metrohash64_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out) +{ + static const uint64_t k0 = 0xD6D018F5; + static const uint64_t k1 = 0xA2AA033B; + static const uint64_t k2 = 0x62992FC1; + static const uint64_t k3 = 0x30BC5B29; + + const uint8_t * ptr = reinterpret_cast(key); + const uint8_t * const end = ptr + len; + + uint64_t hash = ((static_cast(seed) + k2) * k0) + len; + + if (len >= 32) + { + uint64_t v[4]; + v[0] = hash; + v[1] = hash; + v[2] = hash; + v[3] = hash; + + do + { + v[0] += read_u64(ptr) * k0; ptr += 8; v[0] = rotate_right(v[0],29) + v[2]; + v[1] += read_u64(ptr) * k1; ptr += 8; v[1] = rotate_right(v[1],29) + v[3]; + v[2] += read_u64(ptr) * k2; ptr += 8; v[2] = rotate_right(v[2],29) + v[0]; + v[3] += read_u64(ptr) * k3; ptr += 8; v[3] = rotate_right(v[3],29) + v[1]; + } + while (ptr <= (end - 32)); + + v[2] ^= rotate_right(((v[0] + v[3]) * k0) + v[1], 30) * k1; + v[3] ^= rotate_right(((v[1] + v[2]) * k1) + v[0], 30) * k0; + v[0] ^= rotate_right(((v[0] + v[2]) * k0) + v[3], 30) * k1; + v[1] ^= rotate_right(((v[1] + v[3]) * k1) + v[2], 30) * k0; + hash += v[0] ^ v[1]; + } + + if ((end - ptr) >= 16) + { + uint64_t v0 = hash + (read_u64(ptr) * k2); ptr += 8; v0 = rotate_right(v0,29) * k3; + uint64_t v1 = hash + (read_u64(ptr) * k2); ptr += 8; v1 = rotate_right(v1,29) * k3; + v0 ^= rotate_right(v0 * k0, 34) + v1; + v1 ^= rotate_right(v1 * k3, 34) + v0; + hash += v1; + } + + if ((end - ptr) >= 8) + { + hash += read_u64(ptr) * k3; ptr += 8; + hash ^= rotate_right(hash, 36) * k1; + } + + if ((end - ptr) >= 4) + { + hash += read_u32(ptr) * k3; ptr += 4; + hash ^= rotate_right(hash, 15) * k1; + } + + if ((end - ptr) >= 2) + { + hash += read_u16(ptr) * k3; ptr += 2; + hash ^= rotate_right(hash, 15) * k1; + } + + if ((end - ptr) >= 1) + { + hash += read_u8 (ptr) * k3; + hash ^= rotate_right(hash, 23) * k1; + } + + hash ^= rotate_right(hash, 28); + hash *= k0; + hash ^= rotate_right(hash, 29); + + memcpy(out, &hash, 8); +} + + diff --git a/contrib/libmetrohash/src/testvector.h b/contrib/libmetrohash/src/testvector.h new file mode 100644 index 00000000000..8c7967453e9 --- /dev/null +++ b/contrib/libmetrohash/src/testvector.h @@ -0,0 +1,70 @@ +// testvector.h +// +// The MIT License (MIT) +// +// Copyright (c) 2015 J. Andrew Rogers +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +#ifndef METROHASH_TESTVECTOR_H +#define METROHASH_TESTVECTOR_H + +#include "metrohash.h" + + +typedef void (*HashFunction) (const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * hash); + +struct TestVectorData +{ + HashFunction function; + uint32_t bits; + const char * key; + uint32_t seed; + uint8_t hash[64]; +}; + +// The test vector string is selected such that it will properly exercise every +// internal branch of the hash function. Currently that requires a string with +// a length of (at least) 63 bytes. + +static const char * test_key_63 = "012345678901234567890123456789012345678901234567890123456789012"; + +const TestVectorData TestVector [] = +{ + // seed = 0 + { metrohash64_1, 64, test_key_63, 0, "658F044F5C730E40" }, + { metrohash64_2, 64, test_key_63, 0, "073CAAB960623211" }, + { metrohash128_1, 128, test_key_63, 0, "ED9997ED9D0A8B0FF3F266399477788F" }, + { metrohash128_2, 128, test_key_63, 0, "7BBA6FE119CF35D45507EDF3505359AB" }, + { metrohash128crc_1, 128, test_key_63, 0, "B329ED67831604D3DFAC4E4876D8262F" }, + { metrohash128crc_2, 128, test_key_63, 0, "0502A67E257BBD77206BBCA6BBEF2653" }, + + // seed = 1 + { metrohash64_1, 64, test_key_63, 1, "AE49EBB0A856537B" }, + { metrohash64_2, 64, test_key_63, 1, "CF518E9CF58402C0" }, + { metrohash128_1, 128, test_key_63, 1, "DDA6BA67F7DE755EFDF6BEABECCFD1F4" }, + { metrohash128_2, 128, test_key_63, 1, "2DA6AF149A5CDBC12B09DB0846D69EF0" }, + { metrohash128crc_1, 128, test_key_63, 1, "E8FAB51AF19F18A7B10D0A57D4276DF2" }, + { metrohash128crc_2, 128, test_key_63, 1, "2D54F87181A0CF64B02C50D95692BC19" }, +}; + + + +#endif // #ifndef METROHASH_TESTVECTOR_H diff --git a/contrib/libpoco/CHANGELOG b/contrib/libpoco/CHANGELOG new file mode 100644 index 00000000000..19bdd0d3d84 --- /dev/null +++ b/contrib/libpoco/CHANGELOG @@ -0,0 +1,2360 @@ +This is the changelog file for the POCO C++ Libraries. + +Release 1.6.1 (2015-08-03) +========================== + +- added project and solution files for Visual Studio 2015 +- upgraded bundled SQLite to 3.8.11.1 +- fixed GH #782: Poco::JSON::PrintHandler not working for nested arrays +- fixed GH #819: JSON Stringifier fails with preserve insert order +- fixed GH #878: UUID tryParse +- fixed GH #869: FIFOBuffer::read(T*, std::size_t) documentation inaccurate +- fixed GH #861: Var BadCastException +- fixed GH #779: BUG in 1.6.0 Zip code +- fixed GH #769: Poco::Var operator== throws exception +- fixed GH #766: Poco::JSON::PrintHandler not working for objects in array +- fixed GH #763: Unable to build static with NetSSL_OpenSSL for OS X +- fixed GH #750: BsonWriter::write missing size ? +- fixed GH #741: Timestamp anomaly in Poco::Logger on WindowsCE +- fixed GH #735: WEC2013 build fails due to missing Poco::Path methods. +- fixed GH #722: poco-1.6.0: Unicode Converter Test confuses string and char types +- fixed GH #719: StreamSocket::receiveBytes and FIFOBuffer issue in 1.6 +- fixed GH #706: POCO1.6 Sample EchoServer BUG +- fixed GH #646: Prevent possible data race in access to Timer::_periodicInerval +- DeflatingStream: do not flush underlying stream on sync() as these can cause + corrupted files in Zip archives + + +Release 1.6.0 (2014-12-22) +========================== + +- fixed GH #625: MongoDB ensureIndex double insert? +- fixed GH #622: Crypto: RSATest::testSign() should verify with public key only +- fixed GH #620: Data documentation sample code outdated +- fixed GH #618: OS X 10.10 defines PAGE_SIZE macro, conflicts with PAGE_SIZE in Thread_POSIX.cpp +- fixed GH #616: Visual Studio warning C4244 +- fixed GH #612: OpenSSLInitializer calls OPENSSL_config but not CONF_modules_free +- fixed GH #608: (Parallel)SocketAcceptor ctor/dtor call virtual functions +- fixed GH #607: Idle Reactor high CPU usage +- fixed GH #606: HTMLForm constructor read application/x-www-form-urlencoded UTF-8 request + body first parameter with BOM in name +- fixed GH #596: For OpenSSL 1.0.1, include openssl/crypto.h not openssl/fips.h +- fixed GH #592: Incorrect format string in Poco::Dynamic::Struct +- fixed GH #590: Poco::Data::SQlite doesn't support URI filenames +- fixed GH #564: URI::encode +- fixed GH #560: DateTime class calculates a wrong day +- fixed GH #549: Memory allocation is not safe between fork() and execve() +- fixed GH #500: SSLManager causes a crash +- fixed GH #490: 2 byte frame with payload length of 0 throws "Incomplete Frame Received" exception +- fixed GH #483: multiple cases for sqlite_busy +- fixed GH #482: Poco::JSON::Stringifier::stringify bad behaviour +- fixed GH #478: HTTPCredentials not according to HTTP spec +- fixed GH #471: vs2010 release builds have optimization disabled ? +- fixed GH #468: HTTPClientSession/HTTPResponse not forwarding exceptions +- fixed GH #438: Poco::File::setLastModified() doesn't work +- fixed GH #402: StreamSocket::receiveBytes(FIFOBuffer&) and sendBytes(FIFOBuffer&) are + not thread safe +- fixed GH #345: Linker warning LNK4221 in Foundation for SignalHandler.obj, String.obj + and ByteOrder.obj +- fixed GH #331: Poco::Zip does not support files with ".." in the name. +- fixed GH #318: Logger local time doesn't automatically account for DST +- fixed GH #294: Poco::Net::TCPServerParams::setMaxThreads(int count) will not accept count == 0. +- fixed GH #215: develop WinCE build broken +- fixed GH #63: Net::NameValueCollection::size() returns int +- Poco::Logger: formatting methods now support up to 10 arguments. +- added Poco::Timestamp::raw() +- Poco::DeflatingOutputStream and Poco::InflatingOutputStreams also flush underlying stream + on flush()/sync(). +- Poco::Util::Timer: prevent re-schedule of cancelled TimerTask +- enabled WinRegistryKey and WinRegistryConfiguration for WinCE +- Poco::BasicEvent improvements and preparations for future support of lambdas/std::function +- upgraded bundled sqlite to 3.8.7.2 +- Poco::Thread: added support for starting functors/lambdas +- Poco::Net::HTTPClientSession: added support for global proxy configuration +- added support for OAuth 1.0/2.0 via Poco::Net::OAuth10Credentials and + Poco::Net::OAuth20Credentials classes. +- Poco::Net::IPAddress: fixed IPv6 prefix handling issue on Windows +- added Poco::Timestamp::TIMEVAL_MIN and Poco::Timestamp::TIMEVAL_MAX +- added Poco::Clock::CLOCKVAL_MIN and Poco::Clock::CLOCKVAL_MAX +- added poco_assert_msg() and poco_assert_msg_dbg() macros +- Poco::Net::Context: fixed a memory leak if the CA file was not found while creating the + Context object (the underlying OpenSSL context would leak) +- Poco::URI: added new constructor to create URI from Path +- Various documentation and style fixes +- Removed support (project/solution files) for Visual Studio.NET 2003 and Visual Studio 2005. +- Improved CMake support + + +Release 1.5.4 (2014-10-14) +========================== + +- fixed GH #326: compile Net lib 1.5.2 without UTF8 support enabled +- fixed GH #518: NetworkInterface.cpp compile error w/ POCO_NO_WSTRING (1.5.3) +- Fixed MSVC 2010 warnings on large alignment +- make HTTPAuthenticationParams::parse() add value on end of string +- fixed GH #482: Poco::JSON::Stringifier::stringify bad behaviour +- fixed GH #508: Can't compile for arm64 architecture +- fixed GH #510: Incorrect RSAKey construction from istream +- fix SharedMemory for WinCE/WEC2013 +- Add NIOS2 double conversion detection, fixes compile errors +- added VS2013 project/solution files for Windows Embedded Compact 2013 +- added Process::isRunning() +- NetSSL: Fix typo in documentation +- NetSSL_OpenSSL: support for TLS 1.1 and 1.2 +- Zip: Added CM_AUTO, which automatically selects CM_STORE or CM_DEFLATE based + on file extension. Used to avoid double-compression of already compressed file + formats such as images. +- added %L modifier to PatternFormatter to switch to local time +- removed unnecessary explicit in some multi-arg constructors +- Allow SecureStreamSocket::attach() to be used in server connections +- added Var::isBoolean() and fixed JSON stringifier +- added poco_unexpected() macro invoking Bugcheck::unexpected() to deal + with unexpected exceptions in destructors +- fixed GH #538 prevent destructors from throwing exceptions +- improved HTTP server handling of errors while reading header +- fixed GH #545: use short for sign +- upgraded SQLite to 3.8.6 +- fixed GH #550 WebSocket fragmented message problem +- improved HTTPClientSession handling of network errors while sending the request +- updated bundled PCRE to 8.35.0 +- fixed GH #552: FIFOBuffer drain() problem +- fixed GH #402: StreamSocket::receiveBytes(FIFOBuffer&) and sendBytes(FIFOBuffer&) are + not thread safe +- HTTPCookie: fix documentation for max age +- added Timestamp::raw() and Clock::raw() +- Poco::Buffer properly handles zero-sized buffers +- GH #512: Poco:Data:ODBC:Binder.h causes a crash +- Added Crypto_Win and NetSSL_Win libraries which are re-implementations of existing + Crypto and NetSSL_OpenSSL libraries based on WinCrypt/Schannel. The new libraries + can be used as an almost drop-in replacement for the OpenSSL based libraries on + Windows and Windows Embedded Compact platforms. Only available from GitHub for now. + + +Release 1.5.3 (2014-06-30) +========================== + +- fixed GH# 316: Poco::DateTimeFormatter::append() gives wrong result for + Poco::LocalDateTime +- Poco::Data::MySQL: added SQLite thread cleanup handler +- Poco::Net::X509Certificate: improved and fixed domain name verification for + wildcard domains +- added Poco::Clock class, which uses a system-provided monotonic clock + (if available) and is thus not affected by system realtime clock changes. + Monotonic Clock is available on Windows, Linux, OS X and on POSIX platforms + supporting clock_gettime() and CLOCK_MONOTONIC. +- Poco::Timer, Poco::Stopwatch, Poco::TimedNotificationQueue and Poco::Util::Timer + have been changed to use Poco::Clock instead of Poco::Timestamp and are now + unaffected by system realtime clock changes. +- fixed GH# 350: Memory leak in Data/ODBC with BLOB +- Correctly set MySQL time_type for Poco::Data::Date. +- fixed GH #352: Removed redundant #includes and fixed spelling mistakes. +- fixed setting of MYSQL_BIND is_unsigned value. +- fixed GH #360: CMakeLists foundation: add Clock.cpp in the list of source files +- Add extern "C" around on HPUX platform. +- added runtests.sh +- fixed CPPUNIT_IGNORE parsing +- fixed Glob from start path, for platforms not alowing transverse from root (Android) +- added NTPClient (Rangel Reale) +- added PowerShell build script +- added SmartOS build support +- fix warnings in headers +- XMLWriter: removed unnecessary apostrophe escaping (&apos) +- MongoDB: use Int32 for messageLength +- fixed GH #380: SecureSocket+DialogSocket crashes with SIGSEGV when timeout occours +- Improve RSADigestEngine, using Poco::Crypto::DigestEngine to calculate hash before signing +- added Poco::PBKDF2Engine +- Fixed GH #380: SecureSocket+DialogSocket crashes with SIGSEGV when timeout occours +- added support for a 'Priority' attribute on cookies. +- GH #386: fixed bug in MailMessage without content-transfer-encoding header +- GH #384: ew hash algorithms support for RSADigestEngine +- fixed Clock overflow bug on Windows +- Poco::ByteOrder now uses intrinsics, if available +- CMake: added /bigobj option for msvc +- Fix typo to restore Net/TestSuite_x64_vs120 build +- correct path for CONFIGURE_FILE in CMakeLists.txt +- Building Poco 1.5.2 for Synology RS812+ (Intel Atom) (honor POCO_NO_INOTIFY) +- added WEC2013 support to buildwin.cmd and buildwin.ps1 +- HTMLForm: in URL encoding, percent-encode more characters +- Fixed #include conflict with other libraries +- Poco::Net::X509Certificate::verify() no longer uses DNS reverse lookups to validate host names +- cert hostname validation is case insensitive and stricter for wildcard certificates +- TCPServer: do not reduce the capacity of the default ThreadPool +- added POCO_LOG_DEBUG flag +- Zip: fixed a crash caused by an I/O error +- added runtest script for windows +- added SQlite Full Text Search support +- added Thread::trySleep() and Thread::wakeUp() +- fixed GH #410: Bug in JSON::Object.stringify() in 1.5.2 +- fixed GH #362: Defect in Var::parseString when there is no space between value and newline +- fixed GH #314: JSON parsing bug +- added GH #313: MetaColumn additions for Data::ODBC and Data::SQLite +- fixed GH #346: Make Poco::Data::Date and Poco::Data::Time compare functions const. +- fixed GH #341: Compiling poco-1.5.2 for Cygwin +- fixed GH #305: There are bugs in Buffer.h +- fixed GH #321: trivial build fixes (BB QNX build) +- fixed GH #440: MongoDB ObjectId string formatting +- added SevenZip library (Guenter Obiltschnig) +- fixed GH #442: Use correct prefix length field of Windows IP_ADAPTER_PREFIX structure +- improved GH #328: NetworkInterface on Windows XP +- fixed GH #154 Add support for MYSQL_TYPE_NEWDECIMAL to Poco::Data::MySQL +- fixed GH #290: Unicode support +- fixed GH #318: Logger local time doesn't automatically account for DST +- fixed GH #363: DateTimeParser tryParse/parse +- added HTMLForm Content-Length calculation (Rangel Reale) +- Make TemporaryFile append a slash to tempDir +- fixed GH #319 android build with cmake +- added hasDelegates() method to AbstractEvent +- fixed GH #230: Poco::Timer problem +- fixed GH #317: Poco::Zip does not support newer Zip file versions. +- fixed GH #176: Poco::JSON::Stringifier UTF encoding +- fixed GH #458: Broadcast address and subnet mask for IEEE802.11 network interface +- fixed GH #456: poco: library install dirs per RUNTIME/LIBRARY/ARCHIVE + + +Release 1.5.2 (2013-09-16) +========================== + +- added MongoDB library +- fixed GH #57: poco-1.5.1: Doesn't compile for Android +- added VoidEvent (Arturo Castro) +- fixed GH #80: NumberFormatter::append broken +- fixed GH #93: ParallelSocketAcceptor virtual functions +- optional small object optimization for IPAddress, SocketAddress, Any and Dynamic::Var +- SQLite events (insert, update, delete, commit, rollback) handlers +- merged GH #91: Improve SQLite multi-threaded use (Rangel Reale) +- merged GH #86: Invalid pointers to vector internals (Adrian Imboden) +- automatic library initialization macros +- fixed GH #110: WebSocket accept() fails when Connection header contains multiple tokens +- fixed GH #71: WebSocket and broken Timeouts (POCO_BROKEN_TIMEOUTS) +- fixed a warning in Poco/Crypto/OpenSSLInitializer.h +- fixed GH #109: Bug in Poco::Net::SMTPClientSession::loginUsingPlain +- added clang libc++ build configurations for Darwin and iPhone (Andrea Bigagli) +- fixed GH #116: Wrong timezone parsing in DateTimeParse (Matej Knopp) +- fixed GH #118: JSON::Object::stringify endless loop +- added Recursive and SortedDirectoryIterator (Marian Krivos) +- added ListMap (map-like container with preserving insertion order) +- MailMessage: attachments saving support and consistent read/write +- fixed GH #124: Possible buffer overrun in Foundation/EventLogChannel +- fixed GH #119: JSON::Object holds values in ordered map +- added JSON::PrintHandler +- renamed JSON::DefaultHandler to ParseHandler (breaking change!) +- fixed GH #127: Eliminate -Wshadow warnings +- fixed GH #79: Poco::Thread leak on Linux +- fixed GH #61: static_md build configs for Crypto and NetSSL +- fixed GH #130: prefer sysconf over sysctlbyname +- fixed GH #131: no timezone global var on OpenBSD +- fixed GH #102: Some subprojects don't have x64 solutions for VS 2010 +- added GH #75: Poco::Uri addQueryParameter method +- Poco::Environment::osDisplayName() now recognizes Windows 8/Server 2012 +- fixed GH #140: Poco::Runnable threading cleanup issue +- simplified default TCP/HTTPServer construction +- fixed GH #141: Application::run() documentation/implementation discrepancy +- changed RowFormatter to SharedPtr in Data::RecordSet interface (breaking change!) +- fixed GH #144: Poco::Dynamic emits invalid JSON +- removed naked pointers from Data interfaces +- fixed GH #82: name conflict in Data::Keywords::bind +- fixed GH #157: MySQL: cannot bind to 'long' data type on Windows/Visual C++ +- fixed GH #158: MySQL: MYSQL_BIND 'is_unsigned' member is not set +- fixed GH #160: MultipartReader ignores first part, if preamble is missing +- fixed GH #156: Possible buffer overrun in Foundation/EventLogChannel +- XML: fixed an issue with parsing a memory buffer > 2 GB +- upgraded to expat 2.1.0 +- Data/ODBC: added support for setting query timeout (via setProperty + of "queryTimeout"). Timeout is int, given in seconds. +- fixed a potential endless loop in SecureStreamSocketImpl::sendBytes() + and also removed unnecessary code. +- fixed GH #159: Crash in openssl CRYPTO_thread_id() after library libPocoCrypto.so + has been unloaded. +- fixed GH #155: MailOutputStream mangles consecutive newline sequences +- fixed GH #139: FileChannel::PROP_FLUSH is invalid (contains a tab character) +- fixed GH #173: HTTPClientSession::proxyConnect forces DNS lookup of host names +- fixed GH #194: MessageNotification constructor is inefficient. +- fixed GH #189: Poco::NumberParser::tryParse() documentation bug +- fixed GH #172: IPv6 Host field is stripped of Brackets in HTTPClientSession +- fixed GH #188: Net: SocketAddress operator < unusable for std::map key +- fixed GH #128: DOMWriter incorrectly adds SYSTEM keyword to DTD if PUBLIC is + already specified +- fixed GH #65: Poco::format() misorders sign and padding specifiers +- upgraded bundled SQLite to 3.7.17 +- replaced JSON parser with Poco::Web::JSON parser (from sandbox) +- added JSON conversion to Dynamic Struct and Array +- added VarIterator +- modified behavior of empty Var (empty == empty) +- added Alignment.h header for C++03 alignment needs +- added Data/WebNotifier (DB, WebSocket) example +- fixed GH #209: Poco::NumberFormatter double length +- fixed GH #204: Upgrade zlib to 1.2.8 +- fixed GH #198: The "application.configDir" property is not always created. +- fixed GH #185: Poco::NumberFormatter::format(double value, int precision) + ignore precision == 0 +- fixed GH #138: FreeBSD JSON tests fail +- fixed GH #99: JSON::Query an JSON::Object +- limited allowed types for JSON::Query to Object, Array, Object::Ptr, + Array::Ptr and empty +- fixed GH #175: HTMLForm does not read URL parameters on POST or PUT +- added GH #187: MySQL: allow access to the underlying connection handle +- added GH #186: MySQL: support for MYSQL_SECURE_AUTH +- fixed GH #174: MySQL: 4GB allocated when reading any largetext or largeblob field +- fixed a potential memory leak in Poco::Net::HTTPClientSession if it is misused + (e.g., sendRequest() is sent two times in a row without an intermediate call to + receiveResponse(), or by calling receiveResponse() two times in a row without + an intermediate call to sendRequest()) - GH #217 +- removed a few unnecessary protected accessor methods from Poco::Net::HTTPClientSession + that would provide inappropriate access to internal state +- merged GH #210: Don't call CloseHandle() twice on Windows; Ability to select the + threadpool that will be used to start an Activity(Patrice Tarabbia) +- fixed GH #212: JSONConfiguration was missing from the vs90 project(Patrice Tarabbia) +- fixed GH #220: add qualifiers for FPEnvironment in C99 (Lucas Clemente) +- fixed GH #222: HTTPCookie doesn't support expiry times in the past (Karl Reid) +- fixed GH #224: building 1.5.1 on Windows for x64 +- fixed GH# 233: ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool ipV6Only) does not work +- fixed GH# 231: Compatibility issue with Poco::Net::NetworkInterface +- fixed GH# 236: Bug in RecursiveDirectoryIterator +- added ColorConsoleChannel and WindowsColorConsoleChannel classes supporting + colorizing log messages +- fixed GH# 259: Poco::EventLogChannel fails to find 64bit Poco Foundation dll +- fixed GH# 254: UTF8::icompare unexpected behavior +- Poco::UUID::tryParse() also accepts UUIDs without hyphens. Also updated documentation + (links to specifications). +- added GH# 268: Method to get JSON object value using Poco::Nullable +- fixed GH# 267: JSON 'find' not returning empty result if object is expected but another value is found +- Added support for ARM64 architecture and iPhone 5s 64-bit builds + (POCO_TARGET_OSARCH=arm64). + + +Release 1.5.1 (2013-01-11) +========================== + +- using double-conversion library for floating-point numeric/string conversions +- added Poco::istring (case-insensitive string) and Poco::isubstr +- added SQLite sys.dual (in-memory system table) +- applied SF Patch #120: The ExpireLRUCache does not compile with a tuple as key on Visual Studio 2010 +- fixed SF Bug #599: JSON::Array and JSON::Object size() member can implicitly lose precision +- fixed SF Bug #602: iterating database table rows not correct if no data in table +- fixed SF Bug #603: count() is missing in HashMap +- fixed GH #23: JSON::Object::stringify throw BadCastException +- fixed GH #16: NetworkInterface::firstAddress() should not throw on unconfigured interfaces +- Android compile/build support (by Rangel Reale) +- TypeHandler::prepare() now takes const-reference +- fixed GH #27: Poco::URI::decode() doesn't properly handle '+' +- fixed GH #31: JSON implementation bug +- fixed SF #597: Configure script ignores cflags +- fixed SF #593: Poco 1.5.0 on FreeBSD: cannot find -ldl +- added SF #542: SocketAddress() needs port-only constructor +- fixed SF #215: Wrong return type in SocketConnector.h +- applied SF Patch #97: fix c++0x / clang++ bugs +- fixed GH32/SF596: Poco::JSON: Parsing long integer (int64) value fails. +- added Net ifconfig sample (contributed by Philip Prindeville) +- merged GH #34: add algorithm header (Roger Meier/Philip Prindeville) +- fixed GH #26: Cannot compile on gcc +- merged SF #111: FTP Client logging (Marian Krivos) +- fixed GH #30: Poco::Path::home() throws when called from Windows Service +- fixed GH #22: MySQL connection string lowercased +- added MySQL support for Date/Time +- upgraded SQLite to version 3.7.15.1 (2012-12-19) +- improved SQLite execute() return (affected rows) value and added tests +- added SQLite::Utility::isThreadSafe() function +- added SQLite::Utility::setThreadMode(int mode) function +- fixed GH #36: 'distclean' requires 3 traversals of project tree +- fixed GH #41: Buffer::resize crash +- fixed GH #42: Linux unbundled builds don't link +- fixed GH #44: Problems with win x64 build +- fixed GH #46: 1.5.1 build fails on OS X when using libc++ +- fixed GH #48: Need getArgs() accessor to Util::Application to retrieve start-up arguments +- fixed GH #49: NetworkInterface::list doesn't return MAC addresses +- fixed GH #51: Android should use isfinite, isinf, isnan and signbit from the std namespace +- fixed GH #53: JSON unicode fixes and running tests on invalid unicode JSON +- added ParallelAcceptor and ParallelReactor classes +- added EOF and error to FIFOBuffer + + +Release 1.5.0 (2012-10-14) +========================== + +- added JSON library +- added Util::JSONConfiguration +- added FIFOBuffer and FIFOBufferStream +- fixed SF# 3522906: Unregistering handlers from SocketReactor +- fixed SF# 3522084: AbstractConfiguration does not support 64-bit integers +- HTTPServer::stopAll(): close the socket instead of just shutting it down, as the latter won't wake up a select() on Windows +- added SMTPLogger +- added cmake support +- fixed SF#3538778: NetworkInterface enumeration uses deprecated API +- fixed SF#3538779: IPAddress lacks useful constructors: from prefix mask, native SOCKADDR +- fixed SF#3538780: SocketAddress needs operator < function +- fixed SF#3538775: Issues building on Fedora/Centos, etc. for AMD64 +- fixed SF#3538786: Use size_t for describing data-blocks in DigestEngine +- added IPAddress bitwise operators (&,|,^,~) +- added IPAddress BinaryReader/Writer << and >> operators +- modified IPAddress to force IPv6 to lowercase (RFC 5952) +- fixed SF#3538785: SMTPClientSession::sendMessage() should take recipient list +- added IPAddress::prefixLength() +- UTF portability improvements +- fixed SF#3556186: Linux shouldn't use in Net/SocketDefs.h +- added IPAddress RFC 4291 compatible site-local prefix support +- fixed SF#3012166: IPv6 patch +- added SF#3558085: Add formatter to MACAddress object +- fixed SF#3552774: Don't hide default target in subordinate makefile +- fixed SF#3534307: Building IPv6 for Linux by default +- fixed SF#3516844: poco missing symbols with external >=lipcre-8.13 +- added SF#3544720: AbstractConfigurator to support 64bit values +- fixed SF#3522081: WinRegistryConfiguration unable to read REG_QWORD values +- fixed SF#3563626: For Win32 set Up/Running flags on NetworkInterface +- fixed SF#3560807: Deprecate setPeerAddress() as this is now done in getifaddrs +- fixed SF#3560776: Fix byte-ordering issues with INADDR_* literals +- fixed SF#3563627: Set IP address on multicast socket from socket family +- fixed SF#3563999: Size BinaryWriter based on buffer's capacity(), not size() +- fixed SF#102 Fix building Poco on Debian GNU/kFreeBSD +- fixed SF#321 Binding DatTime or Timestamp +- fixed SF#307 Detect the SQL driver type at run time +- added VS 2012 Projects/Solutions +- enhanced and accelerated numeric parsing for integers and floats +- fixed SF#590 Segfault on FreeBSD when stack size not rounded +- added warn function and warnmsg macro in CppUnit +- fixed SF# 3558012 Compilation fails when building with -ansi or -std=c++0x +- fixed SF# 3563517 Get rid of loss-of-precision warnings on x64 MacOS +- fixed SF#3562244: Portability fix for AF_LINK +- fixed SF #3562400: DatagramSocketImpl comment is incorrect + + +Release 1.4.7p1 (2014-11-25) +============================ + +- Fixed Visual C++ 2010-2013 project files. Release builds now have optimization enabled. +- Poco::URI: added constructor to create URI from Path. +- fixed GH #618: OS X 10.10 defines PAGE_SIZE macro, conflicts with PAGE_SIZE in Thread_POSIX.cpp +- Poco::Net::HTTPClientSession: added support for global proxy configuration +- fixed GH #331: Poco::Zip does not support files with .. in the name. +- fixed a memory leak in Poco::Net::Context constructor when it fails to load the certificate + or private key files. +- upgraded bundled SQLite to 3.8.7.2 +- fixed GH #229: added missing value() function +- fixed GH #69: MySQL empty text/blob + + +Release 1.4.7 (2014-10-06) +========================== + +- fixed GH #398: PropertyFileConfiguration: input != output +- fixed GH #368: Build failure of Poco 1.4.6p2 on FreeBSD 9.2 +- fixed GH #318: Logger local time doesn't automatically account for DST +- fixed GH #317: Poco::Zip does not support newer Zip file versions. +- fixed GH #454: Fix: handle unhandled exceptions +- fixed GH #463: XML does not compile with XML_UNICODE_WCHAR_T +- fixed GH #282: Using Thread in a global can cause crash on Windows +- fixed GH #424: Poco::Timer deadlock +- fixed GH #465: Fix result enum type XML_Error -> XML_Status +- fixed GH #510: Incorrect RSAKey construction from istream +- fixed GH #332: POCO::ConsoleChannnel::initColors() assigns no color to + PRIO_TRACE and wrong color to PRIO_FATAL +- fixed GH #550: WebSocket fragmented message problem +- Poco::Data::MySQL: added SQLite thread cleanup handler +- Poco::Net::X509Certificate: improved and fixed domain name verification for + wildcard domains +- fixed a crash in Foundation testsuite with Visual C++ 2012 +- improved and fixed domain name verification for wildcard domains in + Poco::Net::X509Certificate +- updated TwitterClient sample to use new 1.1 API and OAuth +- added Poco::Clock class, which uses a system-provided monotonic clock + (if available) and is thus not affected by system realtime clock changes. + Monotonic Clock is available on Windows, Linux, OS X and on POSIX platforms + supporting clock_gettime() and CLOCK_MONOTONIC. +- Poco::Timer, Poco::Stopwatch, Poco::TimedNotificationQueue and Poco::Util::Timer + have been changed to use Poco::Clock instead of Poco::Timestamp and are now + unaffected by system realtime clock changes. +- added Poco::PBKDF2Engine class template +- Poco::Net::HTTPCookie: added support for Priority attribute (backport from develop) +- fixed makedepend.* scripts to work in paths containing '.o*' + (contributed by Per-Erik Bjorkstad, Hakan Bengtsen) +- Upgraded bundled SQLite to 3.8.6 +- Support for Windows Embedded Compact 2013 (Visual Studio 2012) +- Project and solution files for Visual Studio 2013 +- Changes for C++11 compatibility. +- fixed an issue with receiving empty web socket frames (such as ping) +- improved error handling in secure socket classes +- Poco::ByteOrder now uses intrinsics if available +- added new text encoding classes: Latin2Encoding, Windows1250Encoding, Windows1251Encoding +- Zip: Added CM_AUTO, which automatically selects CM_STORE or CM_DEFLATE based on file extension. + Used to avoid double-compression of already compressed file formats such as images. + + +Release 1.4.6p4 (2014-04-18) +============================ + +- no longer use reverse DNS lookups for cert hostname validation +- cert hostname validation is case insensitive and more strict +- HTMLForm: in URL encoding, percent-encode more special characters +- fixed thread priority issues on POSIX platforms with non-standard scheduling policy +- XMLWriter no longer escapes apostrophe character +- fixed GH #316: Poco::DateTimeFormatter::append() gives wrong result for Poco::LocalDateTime +- fixed GH #305 (memcpy in Poco::Buffer uses wrong size if type != char) +- Zip: fixed a crash caused by an I/O error (e.g., full disk) while creating a Zip archive + + +Release 1.4.6p3 (2014-04-02) +============================ + +- Fixed a potential security vulnerability in client-side X509 + certificate verification. + + +Release 1.4.6p2 (2013-09-16) +============================ + +- fixed GH #156: Possible buffer overrun in Foundation/EventLogChannel +- XML: fixed an issue with parsing a memory buffer > 2 GB +- upgraded to expat 2.1.0 +- Data/ODBC: added support for setting query timeout (via setProperty + of "queryTimeout"). Timeout is int, given in seconds. +- fixed a potential endless loop in SecureStreamSocketImpl::sendBytes() + and also removed unnecessary code. +- fixed GH #159: Crash in openssl CRYPTO_thread_id() after library libPocoCrypto.so + has been unloaded. +- fixed GH #155: MailOutputStream mangles consecutive newline sequences +- fixed GH# 139: FileChannel::PROP_FLUSH is invalid (contains a tab character) +- fixed GH# 173: HTTPClientSession::proxyConnect forces DNS lookup of host names +- fixed GH# 194: MessageNotification constructor is inefficient. +- fixed GH# 189: Poco::NumberParser::tryParse() documentation bug +- fixed GH# 172: IPv6 Host field is stripped of Brackets in HTTPClientSession +- fixed GH# 188: Net: SocketAddress operator < unusable for std::map key +- fixed GH# 128: DOMWriter incorrectly adds SYSTEM keyword to DTD if PUBLIC is + already specified +- fixed GH# 65: Poco::format() misorders sign and padding specifiers +- upgraded bundled SQLite to 3.7.17 +- upgraded bundled zlib to 1.2.8 +- fixed a potential memory leak in Poco::Net::HTTPClientSession if it is misused + (e.g., sendRequest() is sent two times in a row without an intermediate call to + receiveResponse(), or by calling receiveResponse() two times in a row without + an intermediate call to sendRequest()) - GH #217 +- removed a few unnecessary protected accessor methods from Poco::Net::HTTPClientSession + that would provide inappropriate access to internal state +- fixed GH# 223 (Poco::Net::HTTPCookie does not support expiry times in the past) +- fixed GH# 233: ServerSocket::bind6(Poco::UInt16 port, bool reuseAddress, bool ipV6Only) + does not work +- added ColorConsoleChannel and WindowsColorConsoleChannel classes supporting + colorizing log messages +- fixed GH# 259: Poco::EventLogChannel fails to find 64bit Poco Foundation dll +- fixed GH# 254: UTF8::icompare unexpected behavior +- Poco::UUID::tryParse() also accepts UUIDs without hyphens. Also updated documentation + (links to specifications). +- Added support for ARM64 architecture and iPhone 5s 64-bit builds + (POCO_TARGET_OSARCH=arm64). + + +Release 1.4.6p1 (2013-03-06) +============================ + +- fixed GH# 71: WebSocket and broken Timeouts (POCO_BROKEN_TIMEOUTS) +- fixed an ambiguity error with VC++ 2010 in Data/MySQL testsuite +- Poco::Net::NetworkInterface now provides the interface index even for IPv4 +- added DNS::reload() as a wrapper for res_init(). +- On Linux, Poco::Environment::nodeId() first always tries to obtain the + MAC address of eth0, before looking for other interfaces. +- Poco::Net::HTTPSession now always resets the buffer in connect() to clear + any leftover data from a (failed) previous session +- fixed copysign namespace issue in FPEnvironment_DUMMY.h +- fixed a warning in Poco/Crypto/OpenSSLInitializer.h +- added a build configuration for BeagleBoard/Angstrom +- fixed GH# 109: Bug in Poco::Net::SMTPClientSession::loginUsingPlain) +- fixed compile errors with clang -std=c++11 +- fixed GH# 116: Wrong timezone parsing in DateTimeParse (fix by Matej Knopp) +- updated bundled SQLite to 3.7.15.2 + + +Release 1.4.6 (2013-01-10) +========================== + +- changed FPEnvironment_DUMMY.h to include instead of +- updated bundled SQLite to 3.7.15.1 +- fixed GH# 30: Poco::Path::home() throws +- fixed SF Patch# 120 The ExpireLRUCache does not compile with a tuple as key on VS2010 +- fixed SF# 603 count() is missing in HashMap +- Crypto and NetSSL_OpenSSL project files now use OpenSSL *MD.lib library files for + static_md builds. Previously, the DLL import libs were used. +- Poco::Environment::osDisplayName() now recognizes Windows 8/Server 2012 + + +Release 1.4.5 (2012-11-19) +========================== + +- added Visual Studio 2012 project files +- buildwin.cmd now support building with msbuild for VS2010 and 2012. +- added Poco::Optional class +- fixed SF# 3558012 Compilation fails when building with -ansi or -std=c++0x +- fixed SF# 3563517 Get rid of loss-of-precision warnings on x64 MacOS +- fixed SF# 3562244: Portability fix for AF_LINK +- fixed SF# 3562400: DatagramSocketImpl comment +- fixed SF# 594: Websocket fails with small masked payloads +- fixed SF# 588: Missing POCO_ARCH and POCO_ARCH_LITTLE_ENDIAN define for WinCE on SH4 +- fixed SF# 581: Out-of-bound array access in Unicode::properties() function. +- fixed SF# 590: Segfault on FreeBSD when stack size not rounded +- fixed SF# 586: Poco::DateTimeParser and ISO8601 issues when seconds fraction has more than 6 digits +- Poco::Net::HTTPSSessionInstantiator::registerInstantiator() now optionally accepts a + Poco::Net::Context object. +- added Poco::XML::XMLWriter::depth() member function. +- added Poco::XML::XMLWriter::uniquePrefix() and Poco::XML::XMLWriter::isNamespaceMapped(). +- Poco::FileChannel now supports a new rotateOnOpen property (true/false) which can be used + to force rotation of the log file when it's opened. +- fixed a bug in Poco::XML::XMLWriter::emptyElement(): need to pop namespace context +- OS X builds now use Clang as default compiler +- Updated SQLite to 3.7.14.1 +- POCO_SERVER_MAIN macro now has a try ... catch block for Poco::Exception and writes + the displayText to stderr. +- Poco/Platform.h now defines POCO_LOCAL_STATIC_INIT_IS_THREADSAFE macro if the compiler + generates thread-safe static local initialization code. + + +Release 1.4.4 (2012-09-03) +========================== + +- ZipStream now builds correctly in unbundled build. +- added proxy digest authentication support to Net library +- integrated MySQL BLOB fixes from Franky Braem. +- use standard OpenSSL import libraries (libeay32.lib, ssleay32.lib) for Crypto and + NetSSL_OpenSSL Visual Studio project files. +- fixed a potential buffer corruption issue in Poco::Net::SecureStreamSocket if lazy + handshake is enabled and the first attempt to complete the handshake fails +- Poco::DateTimeParser::tryParse() without format specifier now correctly parses ISO8601 + date/times with fractional seconds. +- Poco::Process::launch() now has additional overloads allowing to specify an initial + directory and/or environment. +- Poco::Net::FTPClientSession: timeout was not applied to data connection, only to + control connection. +- Fixed potential IPv6 issue with socket constructors if IPv6 SocketAddress is given + (contributed by ??????? ????????? ). +- Added an additional (optional) parameter to Poco::Thread::setOSPriority() allowing to + specify a scheduling policy. Currently this is only used on POSIX platforms and allows + specifying SCHED_OTHER (default), SCHED_FIFO or SCHED_RR, as well as other + platform-specific policy values. +- Added Poco::Crypto::DigestEngine class providing a Poco::DigestEngine interface to + the digest algorithms provided by OpenSSL. +- Fixed some potential compiler warnings in Crypto library +- In some cases, when an SSL exception was unexpectedly closed, a generic Poco::IOException + was thrown. This was fixed to throw a SSLConnectionUnexpectedlyClosedException instead. +- Added Poco::ObjectPool class template. +- Poco::Net::HTTPServer has a new stopAll() method allowing stopping/aborting of all + currently active client connections. +- The HTTP server framework now actively prevents sending a message body in the + response to a HEAD request, or in case of a 204 No Content or 304 Not Modified + response status. +- fixed a DOM parser performance bug (patch by Peter Klotz) +- fixed SF# 3559325: Util Windows broken in non-Unicode +- updated iOS build configuration to use xcode-select for finding toolchain +- Poco::Net::SecureSocketImpl::shutdown() now also shuts down the underlying socket. +- fixed SF# 3552597: Crypto des-ecb error +- fixed SF# 3550553: SecureSocketImpl::connect hangs +- fixed SF# 3543047: Poco::Timer bug for long startInterval/periodic interval +- fixed SF# 3539695: Thread attributes should be destroyed using the pthread_attr_destroy() +- fixed SF# 3532311: Not able to set socket option on ServerSocket before bind + Added Poco::Net::Socket::init(int af) which can be used to explicitely + initialize the underlying socket before calling bind(), connect(), etc. +- fixed SF# 3521347: Typo in UnWindows.h undef +- fixed SF# 3519474: WinRegistryConfiguration bug + Also added tests and fixed another potential issue with an empty root path passed to the constructor. +- fixed SF# 3516827: wrong return value of WinRegistryKey::exists() +- fixed SF# 3515284: RSA publickey format(X.509 SubjectPublicKeyInfo) +- fixed SF# 3503267: VxWorks OS prio is not set in standard constructor +- fixed SF# 3500438: HTTPResponse failure when reason is empty +- fixed SF# 3495656: numberformater, numberparser error in mingw +- fixed SF# 3496493: Reference counting broken in TaskManager postNotification +- fixed SF# 3483174: LogFile flushing behavior on Windows + Flushing is now configurable for FileChannel and SimpleFileChannel + using the "flush" property (true or false). +- fixed SF# 3479561: Subsequent IPs on a NIC is not enumerated +- fixed SF# 3478665: Permission checks in Poco::File not correct for root +- fixed SF# 3475050: Threading bug in initializeNetwork() on Windows +- fixed SF# 3552680: websocket small frames bug and proposed fix +- fixed a WebSocket interop issue with Firefox +- added Poco::Net::MessageHeader::hasToken() +- Poco::AtomicCounter now uses GCC 4.3 builtin atomics on more platforms +- fixed SF# 3555938: NetSSL: socket closed twice +- socket exceptions now include OS error code +- fixed SF# 3556975: Need to fix Shared Memory for memory map +- Poco::Net::SecureSocketImpl::close() now catches exceptions thrown by its call to shutdown(). +- fixed SF# 3535990: POCO_HAVE_IPv6 without POCO_WIN32_UTF8 conflict +- fixed SF# 3559665: Poco::InflatingInputStream may not always inflate completely +- added Poco::DirectoryWatcher class +- fixed SF# 3561464: Poco::File::isDevice() can throw due to sharing violation +- Poco::Zip::Compress::addRecursive() has a second variant that allows to specify the compression method. +- Upgraded internal SQLite to 3.7.14 + + +Release 1.4.3p1 (2012-01-23) +============================ + +- fixed SF# 3476926: RegDeleteKeyEx not available on Windows XP 32-bit + + +Release 1.4.3 (2012-01-16) +========================== + +- fixed a compilation error with Data/MySQL on QNX. +- fixed Util project files for WinCE (removed sources not compileable on CE) +- removed MD2 license text from Ackowledgements document +- fixed iPhone build config for Xcode 4.2 (compiler name changed to llvm-g++) +- Poco::Util::XMLConfiguration: delimiter char (default '.') is now configurable. + This allows for working with XML documents having element names with '.' in them. +- Poco::Util::OptionProcessor: Required option arguments can now be specified as + separate command line arguments, as in "--option value" in addition to the + "--option=value" format. +- Poco::Util::HelpFormatter: improved option help formatting if indentation has + been set explicitely. +- added Mail sample to NetSSL_OpenSSL, showing use of Poco::Net::SecureSMTPClientSession. +- added additional read() overloads to Poco::Net::HTMLForm. +- fixed SF# 3440769: Poco::Net::HTTPResponse doesn't like Amazon EC2 cookies. +- added support for requiring TLSv1 to Poco::Net::Context. +- added an additional constructor to Poco::Net::HTTPBasicCredentials, allowing + the object to be created from a string containing a base64-encoded, colon-separated + username and password. +- Poco::Zip::ZipStreamBuf: fixed a crash if CM_STORE was used. +- Added setContentLength64() and getContentLength64() to Poco::Net::HTTPMessage. +- added Poco::Environment::osDisplayName(). +- fixed SF# 3463096: WinService leaves dangling handles (open() now does not reopen the + service handle if it's already open) +- fixed SF# 3426537: WinRegistryConfiguration can't read virtualized keys +- added Poco::Buffer::resize() +- fixed SF# 3441822: thread safety issue in HTTPClientSession: + always use getaddrinfo() instead of gethostbyname() on all platforms supporting it +- added version resource to POCO DLLs +- fixed SF# 3440599: Dir Path in Quotes in PATH cause PathTest::testFind to fail. +- fixed SF# 3406030: Glob::collect problem +- added Poco::Util::AbstractConfiguration::enableEvents() +- Poco::AtomicCounter now uses GCC builtins with GCC 4.1 or newer + (contributed by Alexey Milovidov) +- made Poco::Logger::formatDump() public as it may be useful for others as well + (SF# 3453446) +- Poco::Net::DialogSocket now has a proper copy constructor (SF# 3414602) +- Poco::Net::MessageHeader and Poco::Net::HTMLForm now limit the maximum number of + fields parsed from a message to prevent certain kinds of denial-of-service + attacks. The field limit can be changed with the new method setFieldLimit(). + The default limit is 100. +- Poco::NumberFormatter, Poco::NumberParser and Poco::format() now always use the + classic ("C") locale to format and parse floating-point numbers. +- added Poco::StreamCopier::copyStream64(), Poco::StreamCopier::copyStreamUnbuffered64() + and Poco::StreamCopier::copyToString64(). These functions use a 64-bit integer + to count the number of bytes copied. +- upgraded internal zlib to 1.2.5 +- upgraded internal sqlite to 3.7.9 +- XML: integrated bugfix for Expat bug# 2958794 (memory leak in poolGrow) +- Added support for HTTP Digest authentication (based on a contribution by + Anton V. Yabchinskiy (arn at bestmx dot ru)). For information on how + to use this, see the Poco::Net::HTTPCredentials, Poco::Net::HTTPDigestCredentials + and Poco::Net::HTTPAuthenticationParams classes. +- Poco::Net::HTTPStreamFactory and Poco::Net::HTTPSStreamFactory now support Basic + and Digest authentication. Username and password must be provided in the URI. +- added Poco::Net::WebSocket, supporting the WebSocket protocol as described in RFC 6455 +- NetSSL_OpenSSL: added client-side support for Server Name Indication. + Poco::Net::SecureSocketImpl::connectSSL() now calls SSL_set_tlsext_host_name() + if its available (OpenSSL 9.8.6f and later). +- added Poco::Net::HTTPClientSession::proxyConnect() (factored out from + Poco::Net::HTTPSClientSession::connect()) +- added Poco::Process::kill(const Poco::ProcessHandle&) which is preferable to + kill(pid) on Windows, as process IDs on Windows may be reused. +- fixed SF# 3471463: Compiler warnings with -Wformat +- Poco::Util::Application::run() now catches and logs exceptions thrown in initialize() +- Fixed a WinCE-specific bug in Poco::Util::ServerApplication where uninitialize() would + be called twice. +- fixed SF# 3471957: WinRegistryKey::deleteKey() unable to delete alt views +- Added additional constructor to Poco::ScopedLock and Poco::ScopedLockWithUnlock + accepting a timeout as second argument. +- Added Poco::Logger::parseLevel() +- Poco::format(): an argument that does not match the format + specifier no longer results in a BadCastException. The string [ERRFMT] is + written to the result string instead. +- PageCompiler: added createSession page attribute. + + +Release 1.4.2p1 (2011-09-24) +============================ + +- On Linux, the RTLD_DEEPBIND option is no longer passed to dlopen(). + This change was introduced in 1.4.2 to solve a specific problem one customer + was having. Unfortunately, it leads to problems with RTTI. +- It's now possible to pass flags (SHLIB_GLOBAL, SHLIB_LOCAL) to + Poco::SharedLibrary::load() (and the constructor implicitly calling load()), + controlling the mode flags (RTLD_GLOBAL, RTLD_LOCAL) passed to dlopen(). + On platforms not using dlopen(), these flags are ignored. +- fixed SF# 3400267: Path_WIN32.cpp bug + + +Release 1.4.2 (2011-08-28) +========================== + +- added Poco::DateTimeFormat::ISO8601_FRAC_FORMAT +- added new Poco::DateTimeFormatter and Poco::DateTimeParser format specifier: + %s for seconds with optional fractions of a second +- fixed a problem with ioctl() on BSD platforms (including OS X) where the + second argument to ioctl() is unsigned long instead of int, causing bad + things on a OS X 64-bit kernel. +- fixed a potential endless loop when enumerating IPv6 network addresses + (reported by Laurent Carcagno) +- new compile-time config option on Windows to set thread names in + debugger. Enable with -DPOCO_WIN32_DEBUGGER_THREAD_NAMES. Available + only in debug builds. +- Cipher can now create Base64 and HexBinary encoded output without linefeeds + (suitable for use in cookies, etc.) +- added Poco::Path::popFrontDirectory() +- improved VxWorks support +- IPv6 fixes: added proper scope id handling in IPAddress, SocketAddress + and related classes. +- Added Poco::Net::ServerSocket::bind6() which allows control over the + IPPROTO_IPV6/IPV6_V6ONLY socket option. +- Removed Poco::MD2Engine class due to licensing issues (the + license for the MD2 code from RSA only allows non-commercial + use). Note that the MD4 and MD5 code from RSA does not have + this issue. +- fixed a Net HTTP client testsuite issue where some tests might + have failed due to prematurely aborted connections by + the HTTPTestServer. +- Poco::Net::SocketAddress: when there is more than one address + returned by a DNS lookup for a name, IPv4 addresses will be + preferred to IPv6 ones. +- NetworkInterface::list() now also returns IPv4 interfaces on Windows when + built with -DPOCO_HAVE_IPv6 +- XMLWriter: fixed a bug with attribute namespaces (no namespace prefix + written if attribute namespace is the same as element namespace) +- fixed SF# 3378588: Mismatched new[]/delete (in RSAEncryptImpl and RSADecryptImpl) +- fixed SF# 3212954 (OpenSSLInitializer::uninitialize() crash) and + SF# 3196862 (Static OpenSSLInitializer instance causes Windows deadlocks) by + removing the static Poco::Crypto::OpenSSLInitializer instance. Automatic OpenSSL + initialization is now done through Poco::Crypto::Cipher, Poco::Crypto::CipherKey, + Poco::Crypto::X509Certificate, Poco::Net::Context classes; however, it is still + recommended to call Poco::Crypto::initializeCrypto() and + Poco::Crypto::uninitializeCrypto() early at application startup, and late at + shutdown respectively (or Poco::Net::initializeSSL()/Poco::Net::uninitializeSSL() + if the NetSSL library is used) to avoid multiple full OpenSSL init/uninit cycles + during application runtime. +- Poco::Logger now also support a symbolic log level "none" + (for use with setLevel()) that disables logging completely + for that Logger (equivalent to setLevel(0)). +- Added experimental Android support, using the existing gmake-based + build system. +- fixed SF# 3288584: DateTimeFormatter link error +- fixed SF# 3187117: Typo in InflatingInputStream doc +- fixed SF# 3309731: _WIN32_WCE comparison should be with 0x600 not 600 +- fixed SF# 3393026: RegularExpression.h identical enum value +- fixed SF# 3274222: AtomicCounter's postfix operators aren't atomic on Windows +- fixed SF# 3317177: Handle leak on windows +- fixed SF# 3181882: Poco::URI::getPathEtc() double-encodes query +- fixed SF# 3379935: ThreadPool Start Bug +- fixed SF# 3354451: Poco::Format::parsePrec never sets the precision to zero +- fixed SF# 3387258: _MAX_PATH used but unknown in Path_WIN32 +- fixed a problem in RSAKeyImpl where direct access to the RSA in a EVP_PKEY + would no longer work in recent OpenSSL versions. Using EVP_PKEY_get1_RSA() + fixes the issue. +- added Poco::Crypto::EncryptingInputStream, Poco::Crypto::EncryptingOutputStream, + Poco::Crypto::DecryptingInputStream and Poco::Crypto::DecryptingOutputStream. +- fixed SF# 3148126: HTTPSClientSession destructor (!) throws an IOException +- fixed SF# 3178098: Add constructor to Poco::TemporaryFile to specify directory +- fixed SF# 3175310: Absolute path when device +- fixed SF# 3301207: Guided tour example contradicts apidoc (API doc was wrong) +- Poco::Net::HTTPMessage::setContentLength() and Poco::Net::HTTPMessage::getContentLength() now + use std::streamsize instead of int. This enables 64-bit Content-Length support at least + on 64-bit platforms. +- fixed SF# 3177530: TemporaryFile::tempName() + glob bug on xp +- fixed SF# 3177372: FileChannel documentation inconsistency +- added %E format specifier to Poco::PattermFormatter (epoch time in seconds + since midnight, January 1 1970) +- On Windows, Poco::Util::ServerApplication now supports a /description command + line argument for specifying a service description (together with /registerService) +- added Poco::Util::WinService::setDescription() and + Poco::Util::WinService::getDescription() +- fixed SF# 3155477: Incorrect URI path handling +- fixed SF# 3309736: Extended Exception macros to set default exception code + new macro is named POCO_DECLARE_EXCEPTION_CODE +- added getter functions for modulus and exponents to Poco::Crypto::RSAKey. +- added Poco::Net::SocketAddress::operator == () and + Poco::Net::SocketAddress::operator != () +- fixed SF# 3182746: IPAddress.cpp IPv6 bug on big-endian +- fixed SF# 3196961: Unix daemon fails to loadConfiguration() if started from cwd +- fixed SF# 3393700: NotificationCenter may call a removed observer and crash. +- Reworked implementation of the events framework (Poco::BasicEvent and friends). + The framework is now completely multithreading save (even in the case that + an event subscriber object unsubscribes and is deleted while an event is + being dispatched). Also, the restriction that any object can only register + one delegate for each event has been removed. For most cases, dispatching + events should be faster, as dispatching an event now needs less dynamic memory + allocations. +- fixed SF# 3178109: getNodeByPath() changes: + getNodeByPath() and getNodeByPathNS() have been moved to Poco::XML::Node. + Furthermore, when invoked on a Poco::XML::Document, the behavior has changed + so that the document element is now included when traversing the path (previously, + traversal would start at the document element, now it starts at the document). + The path expression can now start with a double-slash, which results in a recursive + search for the path's first element in the DOM tree. +- fixed SF# 3382935: String data being truncated using ODBC, and + SF# 2921813: Wrong implementation of the ODBC string binding + + +Release 1.4.1p1 (2011-02-08) +============================ + +- Poco::Mutex is now a recursive mutex again on Linux + (this was caused by an unfortunate feature test for + PTHREAD_MUTEX_RECURSIVE which did not work on Linux + as PTHREAD_MUTEX_RECURSIVE is an enum value and not + a macro) +- Poco::Net::SecureSocketImpl::abort() now only shuts + down the underlying socket connection and does not free + the SSL object, due to multithreading issues. + + +Release 1.4.1 (2011-01-29) +========================== + +- fixed SF# 3150223: Poco::BinaryReader cannot read std::vector correctly +- fixed SF# 3146326: SharedMemory issue +- made Poco::Net::HTTPSession::abort() virtual +- added Poco::Net::SecureStreamSocket::abort() to immediately close + a SSL/TLS connection without performing an orderly SSL/TLS shutdown. +- fixed SF# 3148126: HTTPSClientSession destructor (!) throws an IOException. + Added try/catch block to Poco::Net::SecureSocketImpl destructor. +- added additional constructor to Poco::Net::HTTPSClientSession, taking + both a socket and a session object. +- Poco::Net::HTTPSession::abort() now also can be used with a + Poco::Net::HTTPSClientSession. +- fixed SF# 3148045: make clean and distclean issues +- changed Data library names on Unix/Linux platforms to + match the names on Windows (PocoSQLite -> PocoDataSQLite, + PocoMySQL -> PocoDataMySQL, PocoODBC -> PocoDataODBC) +- added additional options to configure script +- added additional documentation to Poco::Net::HTTPClientSession +- Poco::Net::HTTPClientSession::receiveResponse() closes the connection + if an exception is thrown while reading the response header. + This ensures that a new connection will be set up for the next request + if persistent connections are used. +- improved Poco::Net::MultipartDecoder performance by reading directly from streambuf +- improved performance of Poco::Base64Encoder, Poco::Base64Decoder, + Poco::HexBinaryEncoder and Poco::HexBinaryDecoder by working directly with the + given stream's streambuf. +- improved performance of MessageHeader::read() by reading directly from streambuf + instead of istream. +- it is now possible to specify additional MIME part header fields + for a MIME part through the Poco::Net::PartSource class. +- upgraded SQLite to release 3.7.4 +- added experimental VxWorks support for VxWorks 5.5.1/Tornado 2.2 and + newer. Please see the VxWorks Platform Notes in the reference documentation + for more information. Currently, the VxWorks is untested; full support + will be available in release 1.4.2. +- fixed SF# 3165918: Poco::DynamicAny fails to convert from string to float +- fixed SF# 3165910: Poco::Net::MessageHeader does not accept HTTP conforming header +- made Poco::Task::cancel() virtual so that tasks can implement custom + cancellation behavior. +- added optional argument to Poco::Util::WinRegistryKey constructor + to specify additional flags (in addition to KEY_READ and KEY_WRITE) + for the samDesired argument of RegOpenKeyEx() or RegCreateKeyEx(). +- improved Poco::BasicEvent::notify() performance by avoiding an unnecessary heap + allocation. +- added additional well-known port numbers to Poco::URI: rtsp, sip, sips, xmpp. +- added Poco::Net::MediaType::matchesRange() +- improved invalid socket handling: a Poco::Net::InvalidSocketException is + now thrown instead of an assertion when an operation is attempted on a closed or + otherwise uninitialized socket. + + +Release 1.4.0 (2010-12-14) +========================== + +- SSLManager: documentation fixes, code cleanup +- SSLManager: renamed PrivateKeyPassPhrase event to PrivateKeyPassphraseRequired +- added HTTPServerRequestImpl::socket() to get access to the underlying socket +- added Socket::secure() to find out whether a given socket supports SSL/TLS +- added SecureStreamSocket::havePeerCertificate() +- NetSSL: added support for turning off extended certificate validation (hostname matching) +- fixed SF# 2941228: ICMPClient::ping() issues on Mac OS X +- fixed SF# 2941231: ICMPEventArgs out of bounds array access +- added PageCompiler sample +- added missing newline at end of xmlparse.c +- Poco::Glob can now be used with an empty pattern which will match nothing (patch from Kim Graesman) +- added support for HTTP proxy authentication (Basic authentication only) +- fixed SF# 2958959: XMLWriter must encode CR, LF and TAB in attribute values as character entities. +- HTMLForm now supports PUT requests as well (see ) +- fixed SF# #2970521: FileOutputStream and file permissions. + (also fixed in File class) +- removed an unused (and wrong) default parameter from EventImpl constructor for WIN32. +- added full support for session caching to NetSSL_OpenSSL +- fixed SF# 2984454: Poco::Util::Timer::scheduleAtFixedRate() works incorrectly +- fixed a bug in Poco::Util::Timer that could lead to high CPU load if + the system clock is moved forward. +- added system.nodeId to SystemConfiguration +- added a note to Poco::Util::ServerApplication documentation regarding + creation of threads +- added Poco::Net::IPAddress::broadcast() and Poco::Net::IPAddress::wildcard() to + create broadcast (255.255.255.255) and wildcard (0.0.0.0) addresses. +- fixed SF# 2916154: Poco::Net::IPAddress::isLoopback() only works for 127.0.0.1. +- added build configuration for iPhone Simulator +- GNU Make based build system provides new variables: POCO_HOST_BINDIR, POCO_HOST_BINPATH, + POCO_HOST_LIBDIR, POCO_HOST_LIBPATH and POCO_TARGET_* equivalents. +- Application::initialize() and Application::uninitialize() will now be called from within run(). + This solves various issues with uninitialize() not being called, or being called inappropriately + from the Application destructor. + Please note that this change will break applications that use the Application class, + but only call init() and not run(). +- added /startup option to specify startup mode for Windows services (automatic or manual) +- fixed SF# 2967354: SecureSocketImpl shutdown/close problem +- fixed SF# 3006340: LinearHashTable grows even if key already exists +- fixed a particularly nasty Windows error handling issue that manifested itself on WinCE: + WSAGetLastError() would be called after a std::string was created. The string creation could result + in a heap operation which called a Windows API to allocate memory. This would reset the + GetLastError() error code. Since WSAGetLastError() is just an alias for GetLastError(), the actual + error code from the socket operation would be lost. +- upgraded SQLite to 3.7.3 +- added --header-prefix option to PageCompiler +- fixed SF# 3003875: SQLite data binding is broken +- fixed SF# 2993988: Issue with multiple calls to open()/close() on File*Stream +- fixed SF# 2990256: HTMLForm and file uploads +- fixed SF# 2969227: DateTimeParser bug +- fixed SF# 2966698: Socket connect with timeout issue +- fixed SF# 2981041: Bind NULL to a query (patch supplied) +- fixed SF# 2961419: UTF8Encoding::convert() doesn't work properly in DEBUG mode +- fixed SF# 2957068: Timeout value not picked up by proxy in HTTPSClientSession +- fixed NetSSL_OpenSSL test runner for Poco::Util::Application class changes +- Poco::AbstractEvent, Poco::AbstractCache and related classes now accept a Mutex class as additional template argument. + Poco::NullMutex can be used if no synchronization is desired. +- Added Poco::AbstractEvent::empty() to check whether an event has registered delegates. +- Poco::URI now correctly handles IPv6 addresses. +- Added Poco::Nullable class template. +- Added Poco::NullMutex, a no-op mutex to be used as template argument for template classes + taking a mutex policy argument. +- Poco::XML::XMLWriter: fixed a namespace handling issue that occured with startPrefixMapping() and endPrefixMapping() +- Poco::Net::Context now allows for loading certificates and private keys from Poco::Crypto::X509Certificate objects + and Poco::Crypto::RSAKey objects. +- Poco::Crypto::RSAKey no longer uses temporary files for stream operations. Memory buffers are used instead. +- fixed SF# 2957865: added Poco::UUID::tryParse() +- All Zip classes now use Poco::File[Input|Output]Stream instead of std::[i|o]fstream. + UTF-8 filenames will now be handled correctly on Windows. +- fixed SF# 2902029: zlib flush support (Z_SYNC_FLUSH) +- added Poco::TextBufferIterator class +- fixed SF# 2977249: Use epoll instead select under Linux + Socket::select() and Socket::poll() will use epoll under Linux if the Net library is compiled + with -DPOCO_HAVE_FD_EPOLL. This is the default for the Linux build configuration (but not for + the various build configurations targeting embedded Linux platforms). +- fixed SF# 2941664: Memory leak in DeflatingStream with zero-length streams (also fixed some other potential, + but unlikely, memory leaks) +- fixed SF# 2946457: added RejectCertificateHandler +- fixed SF# 2946621: Poco::Path bug with POCO_WIN32_UTF8 +- fixed SF# 2929805: Environment::nodeId() does not work if no eth0 device exists +- Environment::nodeId() no longer throws if no hardware ethernet address can be determined. + It returns an all-zero address instead. +- Added additional classification functions to Poco::Unicode class; made classification functions inline. +- added Ascii class for ASCII character classification. + Methods of the Ascii class are now used instead of the + standard library functions (std::isspace(), etc.) due to + possible inconsistent results or assertions when the + standard library functions are used with character codes + outside the ASCII range. +- Poco::Net::MailMessage: fixed a bug in StringPartHandler that resulted in incorrect handling of non-ASCII data if + char is signed. +- Improved Poco::Net::SMTPClientSession compatibility with various mail servers when using AUTH_LOGIN authentication. +- Added CRAM-SHA1 support to Poco::Net::SMTPClientSession +- Poco::Net::SMTPClientSession now also supports login with AUTH PLAIN. +- Added Poco::Net::SecureSMTPClientSession class, supporting STARTTLS for secure SMTP connections. +- fixed an issue with SharedMemory on POSIX systems, where a shared memory region would be deleted + despite the server flag set to true (see http://pocoproject.org/forum/viewtopic.php?f=12&t=3494). +- PageCompiler: added a new page context directive, to allow passing custom context objects to the + request handler. +- fixed StreamSocketImpl::sendBytes() for non-blocking sockets +- added Poco::Net::DialogSocket::receiveRawBytes(), which should be used instead of receiveBytes() due to internal + buffering by DialogSocket. +- DOMParser: FEATURE_WHITESPACE has been renamed to FEATURE_FILTER_WHITESPACE (which now matches the underlying URI) + and is now handled correctly (previously we did the exact reverse thing) +- added Poco::Util::AbstractConfiguration::remove() to remove a configuration property; added removeRaw() implementations + to all implementations (contributions by Daniel Hobi and Alexey Shults). +- fixed NetSSL_OpenSSL compilation error on Windows with OpenSSL 1.0 +- Added optional FIPS mode support to NetSSL_OpenSSL (contributed by Lior Okman). + If OpenSSL has been configured and built with FIPS support, then FIPS support can + be enabled by calling Poco::Crypto::OpenSSLInitializer::enableFIPSMode(true); or + by setting the fips property in the OpenSSL configuration to true (see Poco::Net::SSLManager + for details). +- fixed SF# 3031530: Ping and possible no timeout +- added Poco::Net::SocketReactor::onBusy(), called whenever at least one notification will + be dispatched. +- fixed SF# 3034863: Compiler warning in net/IPAddress.h with poco 1.3.2 +- added support for CRAM-SHA1 authentication to SMTPClientSession +- Poco::format(): arguments can now be addressed by their index, e.g. %[2]d +- Poco::Util::Timer::cancel() now accepts an optional boolean argument. + If true is passed, cancel() waits until the task queue has been purged. + Otherwise, it returns immediately and works asynchronously, as before. +- Poco::Net::HTTPServerResponse::redirect() now accepts an optional additional + argument to specify the HTTP status code for the redirection. +- fixed a warning (BinaryReader.cpp) and error (ThreadLocal.cpp) in Foundation when compiling with Visual Studio 2010 +- fixed a wrong exception in Poco::Net::POP3ClientSession +- Poco::Net::FTPClientSession and Poco::Net::SMTPClientSession now set the error code in exceptions they throw +- fixed a potential race condition with terminating a Windows service based on Poco::Util::ServerApplication +- fixed a bug in global build configuration file: explicitly setting POCO_CONFIG did not work on Solaris platforms, + as it was always overridden by the automatically determined configuration. +- Added support for MinGW cross builds on Linux. +- Changed location of statically linked build products in the gmake-based build system. + Statically linked executables are now in bin/$(OSNAME)/$(OSARCH)/static and no longer + have the _s suffix +- The POCO_VERSION macro now is in its own header file, "Poco/Version.h". It is no longer + available through "Poco/Foundation.h". +- added Poco::Net::HTTPCookie::escape() and Poco::Net::HTTPCookie::unescape(). +- fixed SF# 3021173: Thread (POSIX) returns uninitialised value for OS priority +- fixed SF# 3040870: ThreadPool has no function to get assigned name +- fixed SF# 3044303: Can't use own config file on Solaris & OSARCH_64BITS ignored +- fixed SF# 2943896: AsyncChannel::log blocks +- fixed a bug in Poco::Util::WinRegistryKey::getInt(): + The size variable passed to RegQueryValueExW() should be initialized to the size + of the output buffer. +- Added rudimentary support for compiling with Clang 2.0 (Xcode 4) on Mac OS X. +- New build configurations for Mac OS X: Darwin32 and Darwin64 for explicit + 32-bit and 64-bit builds. Note that the default Darwin build configuration + will build 64-bit on Snow Leopard and 32-bit on Leopard, but will always place + build products in Darwin/i386. The new Darwin32 and Darwin64 configurations + will use the correct directories. +- fixed SF# 3051598: Bug in URL encoding +- Poco::ThreadPool::stopAll() (and thus also the destructor) will now wait for each + pooled thread to terminate before returning. This fixes an issue with creating + and orderly shutting down a thread pool in a plugin. Previously, a pooled thread + in a thread pool created by a dynamically loaded library might still be running + when the plugin's shared library was unloaded, resulting in Bad Things happening. + This can now no longer happen. As a downside, a pooled thread that fails to + finish will block stopAll() and the destructor forever. +- NetSSL_OpenSSL: for a SecureStreamSocket, available() now returns the number of bytes that + are pending in the SSL buffer (SSL_pending()), not the actual socket buffer. +- Added Poco::Net::HTTPClientSession::secure() to check for a secure connection. +- Poco::Net::HTTPRequest::setHost() now does not include the port number in the Host header + if it's either 80 or 443. +- log messages can now optionally include source file path and line number +- Poco::PatternFormatter can format source file path and line number (%U, %u) +- logging macros (poco_information(), etc.) now use __LINE__ and __FILE__ +- new logging macros that incorporate Poco::format(): poco_information_f1(logger, format, arg) with up to 4 arguments +- added Poco::Net::HTTPSession::attachSessionData() and Poco::Net::HTTPSession::sessionData() + to attach arbitrary data to a HTTP session. +- added additional constructors to zlib stream classes that allow passing + a windowBits parameter to the underlying zlib library. +- fixed a potential error handling issue in Poco::Net::SecureSocketImpl. +- fixed SF# 3110272: RSACipherImpl bug. +- fixed SF# 3081677: ConfigurationView's getRaw not retrieving xml attributes. +- added basic support for Canonical XML and better pretty-printing support to Poco::XML::XMLWriter. +- Poco::Util::AbstractConfiguration now supports events fired when changing or + removing properties. +- XML: added support for finding DOM nodes by XPath-like + expressions. Only a very minimal subset of XPath is supported. + See Poco::XML::Element::getNodeByPath(), Poco::XML::Element::getNodeByPathNS() + and the same methods in Poco::XML::Document. +- Poco::Timer: If the callback takes longer to execute than the + timer interval, the callback function will not be called until the next + proper interval. The number of skipped invocations since the last + invocation will be recorded and can be obtained by the callback + by calling skipped(). +- Poco::BinaryReader and Poco::BinaryWriter now support reading and + writing std::vectors of the supported basic types. Also, strings + can now be written in a different encoding (a Poco::TextEncoding + can be optionally passed to the constructor). +- Poco::UUID::nil() and Poco::UUID::isNil() have been renamed to + Poco::UUID::null() and Poco::UUID::isNull(), respectively, to avoid + issues with Objective-C++ projects on Mac OS X and iOS where nil is + a system-provided macro. +- Crypto bugfixes: RSACipherImpl now pads every block of data, not just the + last (or last two). +- Improved Crypto testsuite by adding new tests +- Added new Visual Studio project configurations: debug_static_mt and release_static_mt + (linking with static runtime libraries). The existing configurations debug_static + and release_static have been renamed to debug_static_md and release_static_md, respectively. + The suffixes of the static libraries have also changed. The static_md configurations + now build libraries with suffixes md[d], while the libraries built by the static_mt + configurations have mt[d] suffixes. +- Added Visual Studio project files for 64-bit builds. +- Added Visual Studio 2010 project files. +- Removed the use of local static objects in various methods due to + their construction not being threadsafe (and thus leading to + potential race conditions) on Windows/Visual C++. +- Fixed some warning on 64-bit Windows builds. +- The name of the Data connector libraries have changed. They are now + named PocoDataMySQL, PocoDataODBC and PocoDataSQLite. +- fixed SF# 3125498: Linux NetworkInterface::list() doesn't return IPv6 IPs +- fixed SF# 3125457: IPv6 IPAddress tests are wrong +- Added initialization functions for the NetSSL_OpenSSL and Crypto libraries. + These should be called instead of relying on automatic initialization, + implemented with static initializer objects, as this won't work with + statically linked executables (where the linker won't include the + static initializer object). + The functions are Poco::Crypto::initializeCrypto(), Poco::Crypto::uninitializeCrypto(), + Poco::Net::initializeSSL() and Poco::Net::uninitializeSSL(). + Applications using Crypto and/or NetSSL should call these methods appropriately at + program startup and shutdown. + Note: In release 1.3.6, similar functions have been added to the Net library. + + +Release 1.3.6p2 (2010-01-15) +============================ + +- fixed an issue in the Windows implementation Poco::RWLock, where + tryReadLock() sometimes would return false even if no writers + were using the lock (fix contributed by Bjrn Carlsson) +- added Poco::Environment::libraryVersion(). +- fixed SF# 2919461: Context ignores parameter cypherList +- removed an unused enum from RSACipherImpl.cpp (Crypto) +- integrated a new expat patch for CVE-2009-3560. +- fixed SF# 2926458: SSL Context Problem. The Poco::Net::Context + class now makes sure that OpenSSL is properly initialized. +- updated iPhone build configuration (contributed by Martin York) +- fixed SF# 1815124 (reopened): XML Compile failed on VS7.1 with + XML_UNICODE_WCHAR_T +- fixed SF# 2932647: FTPClientSession::getWorkingDirectory() returns a bad result + + +Release 1.3.6p1 (2009-12-21) +============================ + +- added support for using external zlib, pcre, expat and sqlite3 instead of + bundled ones (-DPOCO_UNBUNDLED, configure --unbundled) +- fixed SF# 2911407: Add sh4 support +- fixed SF# 2912746: RSAKey::EXP_LARGE doesn't work +- fixed SF# 2904119: abstractstrategy uses std::set but do not includes it +- fixed SF# 2909946: localtime NULL pointer +- fixed SF# 2914986: potential expat DoS security issues (CVE-2009-3560 and CVE-2009-3720) +- fixed SF# 2916305: SSL Manager crashes +- fixed SF# 2903676: Tuple TypeHander does not handle composites. + + +Release 1.3.6 (2009-11-24) +========================== + +- added Environment::processorCount() +- added POCO_VERSION macro to Poco/Foundation.h +- fixed SF# 2807527: Poco::Timer bug for long startInterval/periodic interval +- fixed a bug similar to SF# 2807527 in Poco::Util::Timer. +- fixed SF# 2795395: Constructor doesn't treat the params "key" and "iv" +- fixed SF# 2804457: DateTime::checkLimit looks wrong +- fixed SF# 2804546: DateTimeParser requires explicit RFC1123 format +- added ReleaseArrayPolicy to Poco::SharedPtr +- upgraded to SQLite 3.6.20 +- fixed SF# 2782709: Missing semicolons in "Logger.h" convenience +- fixed SF# 2526407: DefaultStrategy.h ++it instead of it++ in a loop +- fixed SF# 2502235: Poco STLPort patch +- fixed SF# 2186643: Data::Statement::reset() not implemented in 1.3.3 +- fixed SF# 2164227: Allow File opened read only by FileInputSteam to be writable +- fixed SF# 2791934: use of char_traits::copy in BufferedStreamBuf::underflow +- fixed SF# 2807750: Support additional SQL types in SQLite +- fixed documentation bugs in Timed/PriorityNotificationQueue +- fixed SF# 2828401: Deadlock in SocketReactor/NotificationCenter (also fixes patch# 1956490) + NotificationCenter now uses a std::vector internally instead of a std::list, and the mutex is + no longer held while notifications are sent to observers. +- fixed SF# 2835206: File_WIN32 not checking aganist INVALID_HANDLE_VALUE +- fixed SF# 2841812: Posix ThreadImpl::sleepImpl throws exceptions on EINTR +- fixed SF# 2839579: simple DoS for SSL TCPServer, HTTPS server + No SSL handshake is performed during accept() - the handshake is delayed until + sendBytes(), receiveBytes() or completeHandshake() is called for the first time. + This also allows for better handshake and certificate validation when using + nonblocking connections. +- fixed SF# 2836049: Possible handle leak in FileStream + If sync() fails, close() now simply set's the stream's bad bit. + In any case, close() closes the file handle/descriptor. +- fixed SF# 2814451: NetSSL: receiveBytes crashes if socket is closed +- added a workaround for Vista service network initialization issue + (an Windows service using the Net library running under Vista will + crash in the call to WSAStartup() done in NetworkInitializer). + Workaround is to call WSAStartup() in the application's main(). + Automatic call to WSAStartup() in the Net library can now be disabled + by compiling Net with -DPOCO_NET_NO_AUTOMATIC_WSASTARTUP. Also + the new Poco::Net::initializeNetwork() and Poco::Net::uninitializeNetwork() + functions can be used to call WSAStartup() and WSACleanup(), respectively, + in a platform-independent way (on platforms other than Windows, these + functions will simply do nothing). +- added VCexpress build script support (contributed by Jolyon Wright) +- fixed SF# 2851052: Poco::DirectoryIterator copy constructor is broken +- fixed SF# 2851197: IPAddress ctor throw keyword missing +- added Poco::ProtocolException +- PageCompiler improvements: new tags, support for buffered output, etc. +- better error reporting in Data MySQL connector (patch #2881270 by Jan "HanzZ" Kaluza) +- fixed SF# 1892462: FTPClient:Choose explicitely between EPSV and PASV +- fixed SF# 2806365: Option for PageCompiler to write output to different dir +- fixed a documentation bug (wrong sample code) in Process::launch() documentation +- added --header-output-dir option to PageCompiler +- fixed SF# 2849144: Zip::Decompress notifications error +- SAXParser has a new feature: "http://www.appinf.com/features/enable-partial-reads". + See ParserEngine::setEnablePartialReads() for a description of what this does. +- fixed SF# 2876179: MySQL Signed/Unsigned value bug +- fixed SF# 2877970: possible bug in timer task +- fixed SF# 2874104: wrong parsing empty http headers +- fixed SF# 2860694: Incorrect return code from SecureStreamSocketImpl::sendBytes +- fixed SF# 2849750: Possible bug with XMLWriter? +- added MailMessage::encodeWord() to support RFC 2047 word encoded + mail header fields when sending out mail containing non-ASCII + characters. +- fixed SF# 2890975: SMTPClientSession bug with 7BIT encoding +- fixed an issue with retrieving the value of socket options on Windows 7. + Before obtaining the value of a socket, we now initialize the variable receiving the + socket option value to zero. +- fixed SF# 2836141: Documentation errors +- fixed SF# 2864232: Socket::select() does not detect closed sockets on windows +- fixed SF# 2812143: Socket::select() should check socket descriptors... +- fixed SF# 2801750: NetworkInterface forName returns wrong subnetMask +- fixed SF# 2816315: Problem with POSIX Thread::sleepImpl +- fixed SF# 2795646: IPv6 address parsing bug +- fixed #0000092: ServerApplication::waitForTerminationRequest(), SIGINT and GDB. + Poco::Util::ServerApplication::waitForTerminationRequest() no longer registers a + signal handler for SIGINT if the environment variable POCO_ENABLE_DEBUGGER + is defined. +- fixed SF# 2896070: Poco::Net::Context with non-ASCII paths +- added Unicode Surrogate support to Poco::UTF16Encoding. + See Poco::TextEncoding::queryConvert() and Poco::TextEncoding::sequenceLength() + for how this is implemented. Contributed by Philippe Cuvillier. +- fixed SF# 2897650: [branch 1.3.6] Net.SocketAddress won't compile for CYGWIN +- fixed SF# 2896161: Building on Windows fails when basedir has space in it +- fixed SF# 2864380: Memory leak when using secure sockets +- NetSSL_OpenSSL: the SSL/TLS session cache is now disabled by default and + can be enabled per Context using Poco::Net::Context::enableSessionCache(). +- fixed SF# 2899039: Wrong DST handling in LocalDateTime +- added RWLock::ScopedReadLock and RWLock::ScopedWriteLock (contributed by Marc Chevrier) +- added Thread::TID type, as well as Thread::tid() and Thread::currentTid() to obtain the native + thread handle/ID +- added Zip file comment support +- On Windows, Poco::SharedLibrary::load() now uses LoadLibraryEx instead of LoadLibrary + and uses the LOAD_WITH_ALTERED_SEARCH_PATH if an absolute path is specified. This will + add the directory containing the library to the search path for DLLs that the + loaded library depends upon. +- Mac OS X build settings now match those used by default Xcode projects, making linking the + POCO libs to Xcode projects easier +- Replaced use of std::valarray in Poco::Net::ICMPEventArgs with std::vector due to issues with + std::valarray together with STDCXX debug mode on OS X + + +Release 1.3.5 (2009-05-11) +========================== + +- fixed SF# 2779410: Poco::Data::ODBC::HandleException impovement +- fixed wrong exception text for Poco::UnhandledException +- Fixed a problem with SSL shutdown that causes clients (web browsers) + to hang when the server attempts to perform a clean SSL shutdown. We now call + SSL_shutdown() once, even if the shutdown is not complete after the first call. +- added Poco::Crypto::X509Certificate::save() +- fixed a bug in Poco::Zip::Decompress that results in wrong paths for extracted files +- fixed a bug in Poco::Zip::ZipManipulator where the Zip file was opened in text format + on Windows. +- added Poco::Crypto::X509Certificate::issuedBy() to verify certificate chain. +- fixed 0000089: Thread::sleep() on Linux is extremely inaccurate +- added methods to extract the contents of specific fields from the + subject and issuer distinguished names of a certificate. + + +Release 1.3.4 (2009-04-21) +========================== + +- fixed SF# 2611804: PropertyFileConfiguration continuation lines +- fixed SF# 2529788: ServerApplication::beDaemon() broken +- fixed SF# 2445467: Bug in Thread_WIN32.cpp +- Improved performance of HTTP Server by removing some + string copy operations +- fixed SF# 2310735: HTTPServer: Keep-Alive only works with send() +- fixed appinf.com IP address in Net testsuite +- fixed RFC-00188: NumberFormatter and float/double numbers +- added --pidfile option to ServerApplication on Unix +- fixed SF# 2499504: Bug in Win32_Thread when using from dll (fixed also for POSIX threads) +- fixed SF# 2465794: HTTPServerRequestImpl memory leak +- fixed SF# 2583934: Zip: No Unix permissions set +- the NetSSL_OpenSSL library has been heavily refactored +- added NumberFormatter::append*() and DateTimeFormatter::append() functions +- use NumberFormatter::append() and DateTimeFormatter::append() instead of format() where + it makes sense to gain some performance +- added system.dateTime and system.pid to Poco::Util::SystemConfiguration +- added %F format specifier (fractional seconds/microseconds) to DateTimeFormatter, + DateTimeParser and PatternFormatter. +- fixed SF# 2630476: Thread_POSIX::setStackSize() failure with g++ 4.3 +- fixed SF# 2679279: Handling of -- option broken +- added compile options to reduce memory footprint of statically linked applications + by excluding various classes from automatically being linked. + See the POCO_NO_* macros in Poco/Config.h. +- fixed SF# 2644940: on Windows the COMPUTER-NAME and the HOSTNAME can be different +- added DNS::hostName() function +- added build configuration for iPhone (using Apple's SDK) +- basic support for AIX 5.x/xlC 8 +- fixed a bug resulting in a badly formatted exception message with IOException + thrown due to a socket-related error +- fixed SF# 2644718: NetworkInterface name conflict in MinGW +- added a missing #include to CryptoTransform.h +- fixed SF# 2635377: HTTPServer::HTTPServer should take AutoPtr +- replaced plain pointers with smart pointers in some interfaces +- upgraded to sqlite 3.6.13 +- improved Data::SQLite error reporting +- Poco::Glob now works with UTF-8 encoded strings and supports case-insensitive comparison. + This also fixes SF# 1944831: Glob::glob on windows should be case insensitve +- added Twitter client sample to Net library +- Fixed SF# 2513643: Seg fault in Poco::UTF8::toLower on 64-bit Linux +- Poco::Data::SessionPool: the janitor can be disabled by specifying a zero idle time. +- added Poco::Data::SessionPool::customizeSession() +- added support for different SQLite transaction modes (DEFERRED, IMMEDIATE, EXCLUSIVE) +- fixed a few wrong #if POCO_HAVE_IPv6 in the Net library +- added support for creating an initialized, but unconnected StreamSocket. +- added File::isDevice() +- added family() member function to SocketAddress, +- Data::SQLite: added support for automatic retries if the database is locked +- XMLConfiguration is now writable +- fixed an IPv6 implementation for Windows bug in HostEntry +- Timer class improvement: interval between callback is no longer influenced by the + time needed to execute the callback. +- added PriorityNotificationQueue and TimedNotificationQueue classes to Foundation. + These are variants of the NotificationQueue class that support priority and + timestamp-tagged notifications. +- added Poco::Util::Timer class. This implements a timer that can schedule different + tasks at different times, using only one thread. +- the signatures of Poco::NotificationQueue and Poco::NotificationCenter member functions + have been changed to accept a Poco::Notification::Ptr instead of Poco::Notification* + to improve exception safety. This change should be transparent and fully backwards + compatible. The signature of the methods returning a Poco::Notification* have not been + changed for backwards compatibility. It is recommended, that any Notification* obtained + should be immediately assigned to a Notification::Ptr. +- SQLite::SessionImpl::isTransaction() now uses sqlite3_get_autocommit() to find out + about the transaction state. +- refactored Crypto library to make it independent from NetSSL_OpenSSL. +- added support for RSA-MD5 digital signatures to Crypto library. +- removed SSLInitializer from NetSSL library (now moved to Crypto library) +- added build configs for static libraries to Crypto library +- OpenSSL now depends on Crypto library (which makes more sense than + vice versa, as it was before). Poco::Net::X509Certificate is now + a subclass of Poco::Crypto::X509Certificate (adding the verify() + member function) and the Poco::Net::SSLInitializer class was + moved to Poco::Crypto::OpenSSLInitializer. +- added build configs for static libraries to Zip +- added batch mode to CppUnit::WinTestRunner. + WinTestRunnerApp supports a batch mode, which runs the + test using the standard text-based TestRunner from CppUnit. + To enable batch mode, start the application with the "/b" + or "/B" command line argument. Optionally, a path to a file + where the test output will be written to may be given: + "/b:" or "/B:". + When run in batch mode, the exit code of the application + will denote test success (0) or failure (1). +- testsuites now also work for static builds on Windows +- The IPv6 support for Windows now basically works (Net library compiled with POCO_HAVE_IPv6) +- fixed a potential error when shutting down openssl in a statically linked application +- added static build configs to Data library +- added Poco::AtomicCounter class, which uses OS-specific APIs for atomic (thread-safe) + manipulation of counter values. +- Poco::RefCountedObject and Poco::SharedPtr now use Poco::AtomicCounter for + reference counting +- fixed SF# 2765569: LoadConfiguration failing from current directory + + +Release 1.3.3p1 (2008-10-09) +============================ + +- Fixed SF# 2153031: 1.3.3 Crypto won't compile on 64-bit Linux +- Fixed a warning in MySQL connector +- Updated README +- The global Makefile in the Zip archive is no longer broken + + +Release 1.3.3 (2008-10-07) +========================== + +- Threads now have optional user-settable stack size (if the OS supports that feature) +- Events now support simplified delegate syntax based on delegate function template. + See Poco::AbstractEvent documentation for new syntax. +- Cache supports new access expire strategy. +- Upgraded to SQLite 3.6.2 +- Upgraded to PCRE 7.8 +- added HttpOnly support to Poco::Net::HTTPCookie +- NetworkInterface now has displayName() member (useful only on Windows) +- Poco::Util::WinRegistryKey now has a read-only mode +- Poco::Util::WinRegistryKey::deleteKey() can now recursively delete registry keys +- Poco::File::created() now returns 0 if the creation date/time is not known, as + it's the case on most Unix platforms (including Linux). + On FreeBSD and Mac OS X, it returns the real creation time. +- Time interval based log file rotation (Poco::FileChannel) now works + correctly. Since there's no reliable and portable way to find out the creation + date of a file (Windows has the tunneling "feature", most Unixes don't provide + the creation date), the creation/rotation date of the log file is written into + the log file as the first line. +- added Environment::nodeId() for obtaining the Ethernet address of the system + (this is now also used by UUIDGenerator - the corresponding code from UUIDGenerator + was moved into Environment) +- added a release policy argument to SharedPtr template +- Socket::select() will no longer throw an InvalidArgumentException + on Windows when called with no sockets at all. If all three socket + sets are empty, Socket::select() will return 0 immediately. +- SocketReactor::run() now catches exceptions and reports them via + the ErrorHandler. +- SocketReactor has a new IdleNotification, which will be posted when + the SocketReactor has no sockets to handle. +- added referenceCount() method to Poco::SharedPtr. +- POCO now builds with GCC 4.3 (but there are some stupid warnings: + "suggest parentheses around && within ||". +- Solution and project files for Visual Studio 2008 are included +- The Zip library is now officially part of the standard POCO release. +- The Crypto library (based on OpenSSL) has been added. The original code + was kindly contributed by Ferdinand Beyer. +- A Data Connector to MySQL, contributed by Sergey Kholodilov, is now part + of the POCO release. +- fixed SF# 1859738: AsyncChannel stall +- fixed SF# 1815124: XML Compile failed on VS7.1 with XML_UNICODE_WCHAR_T +- fixed SF# 1867340: Net and NetSSL additional dependency not set - ws2_32.lib +- fixed SF# 1871946: no exception thrown on error +- fixed SF# 1881113: LinearHashTable does not conform to stl iterators +- fixed SF# 1899808: HTMLForm.load() should call clear() first +- fixed SF# 2030074: Cookie problem with .NET server +- fixed SF# 2009707: small bug in Net/ICMPPacketImpl.cpp +- fixed SF# 1988579: Intel Warning: invalid multibyte character sequence +- fixed SF# 2007486: Please clarify license for Data/samples/* +- fixed SF# 1985180: Poco::Net::DNS multithreading issue +- fixed SF# 1968106: DigestOutputStream losing data +- fixed SF# 1980478: FileChannel loses messages with "archive"="timestamp" +- fixed SF# 1906481: mingw build WC_NO_BEST_FIT_CHARS is not defined +- fixed SF# 1916763: Bug in Activity? +- fixed SF# 1956300: HTTPServerConnection hanging +- fixed SF# 1963214: Typo in documentation for NumberParser::parseFloat +- fixed SF# 1981865: Cygwin Makefile lacks ThreadTarget.cpp +- fixed SF# 1981130: pointless comparison of unsigned integer with zero +- fixed SF# 1943728: POCO_APP_MAIN namespace issue +- fixed SF# 1981139: initial value of reference to non-const must be an lvalue +- fixed SF# 1995073: setupRegistry is broken if POCO_WIN32_UTF8 enabled +- fixed SF# 1981125: std::swap_ranges overloading resolution failed +- fixed SF# 2019857: Memory leak in Data::ODBC Extractor +- fixed SF# 1916761: Bug in Stopwatch? +- fixed SF# 1951443: NetworkInterface::list BSD/QNX no netmask and broadcast addr +- fixed SF# 1935310: Unhandled characters in Windows1252Encoding +- fixed SF# 1948361: a little bug for win32 +- fixed SF# 1896482: tryReadLock intermittent error +- workaround for SF# 1959059: Poco::SignalHandler deadlock + the SignalHandler can now be disabled globally by adding a + #define POCO_NO_SIGNAL_HANDLER to Poco/Config.h +- fixed SF# 2012050: Configuration key created on read access +- fixed SF# 1895483: PCRE - possible buffer overflow +- fixed SF# 2062835: Logfile _creationDate is wrong +- fixed SF# 2118943: out_of_bound access in Poco::Data::BLOB:rawContent +- fixed SF# 2121732: Prevent InvalidArgumentException in SocketReactor +- fixed SF# 1891132: Poco::Data::StatementImpl::executeWithLimit is not correct +- fixed SF# 1951604: POCO refuses to compile with g++ 4.3.0 +- fixed SF# 1954327: CYGWIN's pthread does not define PTHREAD_STACK_MIN +- fixed SF# 2124636: Discrepancy between FileWIN32(U)::handleLastError +- fixed SF# 1558300: MinGW/MSYS Builds +- fixed SF# 2123266: Memory leak under QNX6 with dinkum library +- fixed SF# 2140411: ScopedUnlock documentation bug +- fixed SF# 2036460: UUID regression tests are failing on Linux with g++ 4.3.1 +- fixed SF# 2150438: Tuple TypeHandler position increment size is wrong + + +Release 1.3.2 (2008-02-04) +========================== + +Foundation, XML, Net, Util: +- added POCO_NO_SHAREDMEMORY to Config.h +- POCO_NO_WSTRING now really disables all wide string related calls +- added template specialization for string hashfunction (performance) +- XML parser performance improvements (SAX parser is now up to 40 % faster +- added parseMemoryNP() to XMLReader and friends +- URIStreamOpener improvement: redirect logic is now in URIStreamOpener. + this enables support for redirects from http to https. +- added support for temporary redirects and useproxy return code +- added getBlocking() to Socket +- added File::isHidden() +- better WIN64 support (AMD64 and IA64 platforms are recognized) +- added support for timed lock operations to [Fast]Mutex +- SharedLibrary: dlopen() is called with RTLD_GLOBAL instead of RTLD_LOCAL + (see http://gcc.gnu.org/faq.html#dso) +- Poco::Timer threads can now run with a specified priority +- added testcase for SF# 1774351 +- fixed SF# 1784772: Message::swap omits _tid mem +- fixed SF# 1790894: IPAddress(addr,family) doesn't fail on invalid address +- fixed SF# 1804395: Constructor argument name wrong +- fixed SF# 1806807: XMLWriter::characters should ignore empty strings +- fixed SF# 1806994: property application.runAsService set too late +- fixed SF# 1828908: HTMLForm does not encode '+' +- fixed SF# 1831871: Windows configuration file line endings not correct. +- fixed SF# 1845545: TCP server hangs on shutdown +- fixed SF# 1846734: Option::validator() does not behave according to doc +- fixed SF# 1856567: Assertion in DateTimeParser::tryParse() +- fixed SF# 1864832: HTTP server sendFile() uses incorrect date +- HTTPServerResponseImpl now always sets the Date header automatically + in the constructor. +- fixed SF# 1787667: DateTimeFormatter and time related classes + (also SF# 1800031: The wrong behavior of time related classes) +- fixed SF# 1829700: TaskManager::_taskList contains tasks that never started +- fixed SF# 1834127: Anonymous enums in Tuple.h result in invalid C++ +- fixed SF# 1834130: RunnableAdapter::operator= not returning a value +- fixed SF# 1873924: Add exception code to NetException +- fixed SF# 1873929: SMTPClientSession support for name in sender field +- logging performance improvements (PatternFormatter) +- fixed SF# 1883871: TypeList operator < fails for tuples with duplicate values +- CYGWIN build works again (most things work but Foundation testsuite still fails) +- new build configuration for Digi Embedded Linux (ARM9, uclibc) +- new build configuration for PowerPC Linux + +Data: +- fixed SF# 1724388: ODBC Diagnostics +- fixed SF# 1804797: ODBC Statement multiple execution fails +- fixed SF# 1803435: SessionPool onJanitorTimer called too often? +- fixed SF# 1851997: Undefined Behavior in ODBC::Preparation +- updated SQlite to 3.5.5 + + +Release 1.3.1 (2007-08-08) +========================== + +Foundation, XML, Net, Util: +- DynamicAny fixes for char conversions +- fixed SF# 1733362: Strange timeout handling in SocketImpl::poll and Socket::select +- fixed SF patch# 1728912: crash in POCO on Solaris +- fixed SF# 1732138: Bug in WinRegistryConfiguration::getString +- fixed SF# 1730790: Reference counting breaks NetworkInterface::list() +- fixed SF# 1720733: Poco::SignalHandler bug +- fixed SF# 1718724: Poco::StreamCopier::copyStream loops forever +- fixed SF# 1718437: HashMap bug +- changed LinearHashTable iterator implementation. less templates -> good thing. +- fixed SF# 1733964: DynamicAny compile error +- UUIDGenerator: fixed infinite loop with non ethernet interfaces +- updated expat to 2.0.1 +- fixed SF# 1730566: HTTP server throws exception +- Glob supports symbolic links (additional flag to control behavior) +- fixed a problem with non blocking connect in NetSSL_OpenSSL + (see http://www.appinf.com/poco/wiki/tiki-view_forum_thread.php?comments_parentId=441&topics_threshold=0&topics_offset=29&topics_sort_mode=commentDate_desc&topics_find=&forumId=6) +- fixed a problem with SSL renegotiation in NetSSL_OpenSSL (thanks to Sanjay Chouksey for the fix) +- fixed SF# 1714753: NetSSL_OpenSSL: HTTPS connections fail with wildcard certs +- HTTPClientSession: set Host header only if it's not already set (proposed by EHL) +- NetworkInterface (Windows): Loopback interface now has correct netmask; + interfaces that do not have an IP address assigned are no longer reported. +- Fixes for VC++ W4 warnings from EHL +- SharedMemory: first constructor has an additional "server" parameter + Setting to true does not unlink the shared memory region when the SharedMemory object is destroyed. (Alessandro Oliveira Ungaro) +- fixed SF# 1768231: MemoryPool constructor + +Data: +- fixed SF# 1739989: Data::RecordSet::operator = () (in 1.3 branch) +- fixed SF# 1747525: SQLite, Transactions and Session Pooling (in 1.3 branch) +- upgraded to SQLite 3.4.1 + + +Release 1.3.0 (2007-05-07) +========================== + +- added HashMap, HashSet classes +- the HashFunction class template has been changed in an incompatible + way. The member function formerly named hash() is now the function + call operator. If you have defined your own HashFunction classes, + you have to update your code. Sorry for the inconvenience. +- added Poco::Tuple +- added AbstractCache::getAllKeys(), improved performance of the get operation +- fixed AbstractCache::size() to do cache replacement before returning the size +- added additional match() method to RegularExpression and documented the fact that the simple + match() method internally sets RE_ANCHORED and RE_NOTEMPTY. +- added ExpirationDecorator template. Decorates data types so that they can be used with UniqueExpireCaches +- added operator ! to AutoPtr and SharedPtr +- Buffer uses std::size_t instead of int +- Exception::what() now returns exception name instead of message +- added poco_ndc_dbg() macro (same as poco_ndc(), but only enabled in debug builds) +- added Environment::get(name, defaultValue); +- Foundation.h now includes Config.h at the very beginning. +- added replace() and replaceInPlace() to Poco/String.h +- added AutoPtr::assign() and SharedPtr::assign() +- added operator () to AbstractEvent +- gcc Makefiles now strip release builds +- Void now has a == and != operator +- Base64Encoder and HexBinaryEncoder now support an unlimited line length + (no newlines written), by specifying a line length of 0 +- NumberParser now has stricter syntax requirements: garbage following a number leads to a SyntaxException + (Thanks to phireis@gmail.com for the suggestion) +- fixed SF# 1676830: Don't use -rpath in libraries +- fixed SF# 1670279: AbstractConfiguration::unckeckedExpand crash +- fixed a warning in Hashtable +- HTTPClientSession now uses a keepAliveTimeout for better persistent connection handling +- added DateTime::makeUTC() and DateTime::makeLocal() +- added another constructor to LocalDateTime +- POCO_WIN32_UTF8 is ignored on non-Windows platforms +- fixed a timeout bug (with NetSSL) in HTTPSession +- AsyncChannel is automatically opened with first log() +- minor fix to NotificationQueue sample (reported by Laszlo Keresztfalvi) +- added File::canExecute() and File::setExecutable() +- added SharedMemory class to Foundation +- added FileStream, FileInputStream, FileOutputStream to Foundation +- added NodeAppender class to XML for faster DOM tree creation +- HTTPServerRequest and HTTPServerResponse are now abstract base classes, + actual functionality has moved into HTTPServerRequestImpl and + HTTPServerResponseImpl. This allows us to plug other HTTP servers + into POCO. +- added DynamicAny class to Foundation +- replaced std::fstream with Poco::FileStream across POCO. +- added Poco::Checksum class to Foundation. +- fixed SF# 1700811: conflict in threadpool +- bugfix: File::moveTo() does not work if the target is a directory +- File::copyTo() and File::moveTo() now copy/move directories recursively +- refactored NetworkInterface (now using pimpl idiom); + added broadcast address and netmask support +- fixed SF# 1688982: POP3ClientSession fails when retrieving mails with attachment +- fixed SF# 1655104: Enhance Poco::TextEncoding functionality +- added Poco::Condition class, implementing a POSIX-style condition variable +- fixed a bug in File::create() for Windows +- added poco_static_assert (imported from boost) +- added Thread::join(timeout) and Thread::tryJoin() +- ClassLoader support for named manifests (see ClassLibrary.h - POCO_EXPORT_NAMED_MANIFEST) +- POCO_WIN32_UTF8: UNICODE #define is no longer required (and no longer + automatically defined in POCO_WIN32_UTF8 is defined) +- PCRE: upgraded to PCRE version 7.1 +- fixed SF# 1682162: Suggestion on thread priority +- fixed SF# 1613460: MSVC/STLPort warnings +- fixed SF# 1709358: Format double percent std::String bug +- added WindowsConsoleChannel class to Foundation +- added AutoPtr::unsafeCast<>() and SharedPtr::unsafeCast<>() +- fixed SF# 1708552: Failed to build on arm and powerpc +- fixed SF$ 1708529: Failed to build using GCC 4.3: missing #includes +- fixed SF# 1710053: LogStream proposal +- fixed a bug involving empty root directories in Windows DirectoryIterator implementation + (see http://www.appinf.com/poco/wiki/tiki-view_forum_thread.php?comments_parentId=343&forumId=6) +- robustness improvements to ActiveMethod - removed the opportunity for memory leaks in + case something goes while invoking the method +- made C library usage more C++-like - use C++ headers (e.g. ) instead of + C ones (). Also, use C library functions in std namespace. +- added Unicode and UTF8String for improved Unicode support. + The Unicode class can be used to obtain the Unicode properties of a character. + The UTF8 class provides case insensitive comparison and case conversion + for UTF-8 encoded strings. +- added UnWindows.h header file, replaced all #include with #include "Poco/UnWindows.h". + See the Poco/UnWindows.h header file for a rationale and explanations. +- fixed SF# 1713820: StreamSocketImpl::sendBytes sends too many bytes +- File::copyTo(): on Windows, the copy now always has the read-only flag reset, to be consistent + with other platforms. +- With Microsoft Visual C++, the necessary POCO libraries are now implicitly linked when + the corresponding header files are included (#pragma comment(lib, "PocoXYZ.lib") is used). + To disable this, compile POCO with the preprocessor symbol POCO_NO_AUTOMATIC_LIBS #define'd + (see Poco/Foundation.h and Poco/Config.h). +- The Visual Studio project files for the POCO libraries now include configurations + for building static libraries. + + +Release 1.2.9 (2007-02-26) +========================== + +- fixed a formatting problem in Util::HelpFormatter +- HTTPClientSession::sendRequest() now attempts to send the complete request in one network packet. +- improved network performance of ChunkedOutputStream: chunk size and chunk data + are sent in one network packet if possible +- fixed SF# 1655035: Wrong expires field calculation in HTTPCookie + (thanks to Sergey N. Yatskevich for this and other fixes) +- fixed SF# 1655049: Fix discrepancy of a code to the description +- fixed SF# 1655170: Poco::Timezone::standardName() problem on WIN32 +- fixed SF# 1629095: POCO_WIN32_UTF8 problem + There is a new function Path::transcode() that can be used to convert a path (or any other string) + from UTF-8 to the current Windows code page. This string can the be passed as a filename + to an fstream or fopen(). This function only does the conversion on Windows, + and only, if POCO_WIN32_UTF8 is defined. Otherwise, it simply returns the unmodified argument. +- fixed SF# 1659607: Probably a bug in Poco::Net::DialogSocket +- HTTPServer network performance improvement: responses that fit into a single network packet + sent with HTTPServerResponse::sendFile() or the new HTTPServerResponse::sendBuffer() are + sent in only one packet. +- added HTTPServerResponse::sendBuffer() +- HTTPServer now sends a Bad Request response if it fails to parse the HTTP request header. +- HTTPServer now sends an Internal Server Error response if the request handler throws an + exception prior to sending a response.- enabled TCP_NODELAY per default on TCPServer/HTTPServer +- fixed a bug in HTTP persistent connection handling + (server does not send Connection: close when it reaches connection maximum) +- HTMLForm - POST submission of URL encoded form no longer uses chunked transfer encoding + (thus improving interoperability with certain web servers) +- integrated Environment.cpp from Main (missing get(var, default)) +- added missing AutoPtr include to Util/Application + (and using Poco::AutoPtr is no longer necessary for POCO_APP_MAIN macro) +- fixed SF# 1635420: Per Regents of the University of Calfornia letter, + remove advertising from BSD licensed parts +- fixed SF# 1633133: MultipartWriter writes superluous CR-LF at beginning + + +Release 1.2.8 (2007-01-04) +========================== + +- fixed SF# 1613906: Util/Application.h and GCC 3.3 +- fixed a byte order issue (failed test) in IPv6 address formatting +- fixed SF# 1626640: Poco::Net::SocketReactor bug +- fixed client side chunked transfer encoding handling +- fixed client side persistent connection handling +- fixed SF# 1623536: HTTP Server Chunked Transfer Encoding Bug +- improved HTTP server exception text +- fixed SF# 1616294: KeepAlive HTTPServerSession patch +- fixed SF# 1616296: Trivial Poco::TaskCustomNotification patch +- fixed SF# 1619282: PurgeStrategy bug fix +- fixed SF# 1620855: Format problem + there is a new format specifier %z for std::size_t, as well as a new + flag ? for %d, %i, %o, %x meaning any signed or unsigned integer + + +Release 1.2.7 (2006-12-07) +========================== + +- Poco::File: fixed root directory handling +- fixed UUIDGenerator documentation +- clarified Application::setUnixOptions() documentation +- fixes for issue [SOAPLite Transport 0000023]: SOAP Transport Listener should be able to use existing HTTPServer instance +- fixing mantis issues 13, 14, 15, 16, 17, 18, 19, 21 +- fixed SF# 1597022: Signed/unsigned warning in StringTokenizer::operator[] +- fixed SF# 1598601: Message::op= leaks +- fixed SF# 1605960: PatternFormatter crashes on custom property +- fixed SF# 1605950: Memory leak in Logger sample code +- fixed SF# 1591635: Copy Paste Error in sample code +- fixed SF# 1591512: SMTPClientSession response stream +- fixed SF #1592776: LayeredConfiguration: getRaw should enumerate in reverse order +- SF Patch # 1599848 ] VS 2005 Util build fails +- Logger::dump() now uses std::size_t instead of int for buffer size +- LayeredConfiguration now supports a priority value for each configuration. + Also, it's possible to specify for each configuration added whether it + should be writeable. +- ServerApplication: cd to root directory only if running as a daemon +- added Message::swap() +- improvements to build system: + global Makefile has correct dependencies for samples + on Windows, samples build after libraries are ready + configure supports --no-wstring and --no-fpenvironment flags + build system supports POCO_FLAGS environment variable for compiler flags +- RemoteGen: fixed error handling for write protected files (SystemException) + fixing integral constant overflow messages with large cache expiration, m_ support for type serializers, + case-insensitive comparison added + + +Release 1.2.6 (2006-11-19) +========================== + +- added additional match() method to RegularExpression and documented the fact that the simple + match() method internally sets RE_ANCHORED and RE_NOTEMPTY. +- added ExpirationDecorator template. Decorates data types so that they can be used with UniqueExpireCaches +- added operator ! to AutoPtr and SharedPtr +- Buffer uses std::size_t instead of int +- added poco_ndc_dbg() macro (same as poco_ndc(), but only enabled in debug builds) +- Foundation.h now includes Config.h at the very beginning. +- added AutoPtr::assign() and SharedPtr::assign() +- added operator () to AbstractEvent +- gcc Makefiles now strip release builds +- documentation improvements + + +Release 1.2.5 (2006-10-23) +========================== + +- Improved LoggingConfigurator: channel creation and configuration is now a two-step process. + This means that the previous problems with PropertyFileConfiguration and IniFileConfiguration when referencing other channels are solved. +- improved options handling: better handling of (non) ambiguities. + If both an option named "help" and one named "helper" is specified, this no longer causes ambiguity errors. +- added check for duplicate option definition +- ThreadPool bugfix: fixed a crash that occurs on Linux multiprocessor machines + (caused by an thread unsafe string assignment corrupting the heap...) + (SF# 1575315) +- improved ThreadPool performance +- XML now compiles with -DXML_UNICODE_WCHAR_T (SF# 1575174) +- fixed SF# 1572757: HTML forms can have more than one key/value pair with the same name +- got rid of the dynamic casts in Events, Events/Cache: simpler/faster Delegate < operator, + prevents some rare dynamic casts error from occuring when using StrategyCollection with Caches +- improvements to Logger and LoggingConfigurator: + * added Logger::unsafeGet() + * added Logger::setProperty(loggerName, propertyName, value) + * LoggingConfigurator now correctly (re)configures existing Loggers + (prior to this change, if a Logger named "a.b.c" existed before + the LoggingConfigurator started its work, and the LoggingConfigurator + configured a Logger named "a.b", then "a.b.c" would not inherit + the new configuration). +- improvements to SplitterChannel and EventLogChannel configuration +- improved LoggingRegistry exception messages +- MessageHeader::read() is more liberal with malformed message headers. + This fixes problems with certain network cameras sending malformed HTTP headers. + + +Release 1.2.4 (2006-10-02) +========================== + +- some code beautifying and improvements to comments +- DOMParser now automatically sets FEATURE_NAMESPACE_PREFIXES +- fixed SF #1567051: DOMBuilder/DOMParser/NamespaceStrategy bug +- fixed SF #1567364: POCO_APP_MAIN +- added Document::getElementById() (two-argument) and getElementByIdNS() +- added another test for DOMParser +- added AutoPtr::isNull() (to be consistent with SharedPtr) +- this release again compiles on PA-RISC HP-UX systems with aCC +- added CMAKE support files contributed by Andrew J. P. Maclean + + +Release 1.2.3 (2006-09-14) +========================== + +- configure script now checks if (auto)selected configuration is supported +- fixed SF #1552904: NamedEvent bug? +- fixed SF #1552787: POCO not handling EINTR +- fixed SF #1552846: Random::~Random uses scalar delete +- fixed SF #1552987: TLSSlot should explicitly default-construct _value +- IPAddress no longer accepts an empty address string +- split up Observer.h into AbstractObserver.h and Observer.h +- added NObserver class template which supports an AutoPtr + argument for the notification callback +- changed EchoServer sample to use NObserver +- some Windows-specific files were missing in the tarballs + + +Release 1.2.2 (2006-09-01) +========================== + +- fixed SF # 1549973: NotificationCenter::hasObservers() returns wrong result +- fixed a memory leak in EchoServer sample +- fixed SocketReactor TimeoutNotification bug (SF #1549365, SocketNotifier::addObserver() incorrect behavior) +- fixed SF# 1549513: MultipartReader does not work with Unix-style linefeeds +- MailMessage and HTMLForm: processing of multipart messages will no longer fail if a PartHandler does not read all data from the part stream. +- added additional test case (Unix-style line ends) to MultipartReaderTest + + +Release 1.2.1 (2006-08-29) +========================== + +- fixed Config.h header (no more #undefs) + +Release 1.2.0 (2006-08-29) +========================== + +- DateTime fixes: Julian Day is no longer stored internally. + Times (hours, minutes, seconds, ...) are now always taken from an utcValue (if available) and not from the Julian day. + The Julian day is only used for calculating year, month and day (except when the Julian day is the only thing we have) + This helps us get rid of rounding errors that the Julian Day arithmetic introduced.- on Windows, UUIDGenerator no longer uses Netbios, but GetAdaptersInfo instead +- The main Makefile now has correct dependencies +- updated poco-doc.pl with latest version by Caleb Epstein +- fixed SF #1542722: InflatingInputStream: buffer error +- improved Windows UTF-8 support +- added Logger::names() +- added configure script and make install target +- XMLWriter bugfix: pretty-print bug with characters() and rawCharacters() +- improvements to build system: support builds outside of source tree +- added header doc conversion tool contributed by Caleb Epstein +- fixed SF #1542618 (build/config/Linux patch) +- bugfix: BinaryReader/BinaryWriter BOM is now 16 bits, as documented +- fixed SF #1542247 (Compiler warning from OptionCallback) +- fixed SF #1542253 (ServerApplication::handleOption doesn't call Application::handleOption) +- added Application::stopOptionsProcessing() +- updated samples +- Util::Application command line handling now supports: + * argument validation (Option::validator(); see Validator, IntValidator, RegExpValidator) + * binding of argument values to config properties (Option::binding()) + * callbacks for arguments (Option::callback()) + * checking of required parameters +- changed header file locations: + Foundation headers are now in Poco (#include "Poco/Foundation.h") + XML headers are now in Poco/XML, Poco/SAX and Poco/DOM (#include "Poco/XML/XML.h") + Util headers are now in Poco/Util (#include "Poco/Util/Util.h") + etc. + Unfortunately, this change will break existing code. However, fixing the code is + a matter of a few global search/replace operations and can be done quickly. + On the plus side, POCO is now a much better citizen when used with other + libraries. +- changed namespaces: + Foundation is now Poco + XML is now Poco::XML + Util is now Poco::Util + Net is now Poco::Net +- removed namespace macros +- fixed some warnings reported by gcc -Wall -Wextra +- fixed AutoPtr and LayeredConfiguration documentation +- improved StreamSocket::receiveBytes() doc +- added Pipe and PipeStream classes +- added support for I/O redirection (pipes) to Process::launch() +- added LogStream class (ostream interface to Logger) +- improved Makefiles (no more double-building if clean all is specified) +- added CppUnit and DateTime testsuite contributions by Andrew Marlow +- improved Cygwin and minimal MinGW support +- FileChannel: gzip compression if archived files now runs in a background thread (SF #1537481) +- POCO now compiles with large (64-bit) file support on Linux (SF #1536634) +- added format() function, which provides typesafe sprintf-like functionality (SF #1327621) +- added File::isLink() +- bugfix: dangling symbolic links in a directory no longer cause recursive remove to fail with file not found error +- added Void class (useful as argument to ActiveMethod) +- ActiveResult now supports exceptions +- bugfix: Timezone::utcOffset() and Timezone::dst() returned wrong values on Unix platforms (SF #1535428) +- added ActiveDispatcher class +- added ActiveStarter class, which is a policy used by ActiveMethod for starting methods +- ActiveRunnable moved to its own header file +- ThreadPool: added startWithPriority(), which allows for running threads with a different priority +- added error handling to dir sample +- added additional test case to HTTPServer test suite- HTMLForm: should now work with request methods other than POST and GET (all non-POST requests are treated the same as GET) +- clarified HTMLForm documentation +- HTMLForm bugfix: uploaded files no longer end up in value; PartHandler is called instead +- NameValueCollection: added get(name, defaultValue) +- added HTTPFormServer sample +- added Foundation::HashTable and SimpleHashTable +- added Net::HTTPSessionFactory +- improvements to AutoPtr and SharedPtr +- improvements to namespaces handling in XMLWriter +- Foundation Cache: fixed add implementation to match the docu: a 2nd add will now simply overwrite existing entries +- added DateTime::isValid() +- added Exception::rethrow() (virtual, must be overridden by all subclasses) +- Timer can now use a user-supplied ThreadPool +- added rethrow() to exception classes +- Net: made some constructors explicit +- Net: added SocketAddress constructor to HTTPClientSession +- Net: added HTTPSession::networkException() to check for exceptions swallowed by stream classes +- Net: added single string argument constructor to SocketAddress. +- Net: improved HTTPClientSession error handling (no more "Invalid HTTP version string" exceptions when the server prematurely closes the connection due to too much load) +- Net: improved HTTPSession error handling. Exceptions while sending and receiving data are stored for later retrieval and no longer get lost since streambufs swallow them. +- Net: added HTTPLoadTest sample +- fixed a bug when opening logfiles on Unix platforms causing an existing logfile to be truncated +- bugfix: log file purge intervals given in months did not work, due to a stupid typo +- added RawSocket and ICMP classes +- UUID: fixed a doc formatting bug +- NetworkInterface::list() now includes loopback interface on Windows (SF #1460309) +- made Exception::message() and Exception::nested() inline +- added Net::UnsupportedRedirectException +- HTTPStreamFactory throws an UnsupportedRedirectException if it encounters a redirect to https +- HTTP: fixed bad 100 Continue handling in client and server code +- added CONTRIBUTORS file + + +Release 1.1.2 (2006-07-07) +========================== + +- Changed license to Boost license +- DBlite and NetSSL have been removed from the Boost-licensed release. + Please contact Applied Informatics (info@appinf.com) if you're interested in them. + + +Release 1.1.1 (2006-04-03) +========================== + +- NetSSL_OpenSSL now supports separate certificate verification + settings for client and server. +- fixed SF #1460309 (enumerating network interfaces failed on 64bit Linux) +- TCPServer no longer crashes if accept() fails + + +Release 1.1.0 (2006-03-23) +========================== + +- events no longer require awkward += new syntax +- source code and documentation cleanups +- basic support for new compilers and platforms + + +Release 1.1b2 (2006-03-04) +========================== + +- made NetSSL threadsafe (added locking callbacks for OpenSSL) +- improved OpenSSL initialization (random generator seeding) +- various changes to improve compatibility with various platforms + + +Release 1.1b1 (2006-03-03) +========================== + +- New Events package in Foundation. The package supports C#-style event handling +- New Cache package in Foundation: a templates-based caching framework +- added Any class to Foundation +- added DBLite library +- fixed a memory leak with layered configurations in the application +- made POCO_DLL the default (unless POCO_STATIC is #defined) + It is no longer necessary to specify POCO_DLL in projects that use Poco + (SourceForge Patch #1408231 and Feature Request #1407575). +- added Buffer template class to Foundation +- added the UnicodeConverter utility class. This is mainly used for Windows Unicode support and probably of little use for anything else. +- added Path::resolve() +- added Windows Unicode support. This calls the Unicode variant of the Windows API functions. + For this to work, all strings must be UTF-8 encoded and POCO_WIN32_UTF8 must be defined in all compilation units. +- added StreamCopier::copyToString() +- added URIStreamOpener::unregisterStreamFactory() and new variants of URIStreamOpener::open() that also work with filesystem paths. + This fixes SourceForge Bug #1409064 and Feature Request #1409062. +- added NodeIterator::currentNodeNP() to XML library +- added some sanity checks to UTF8Encoding::convert() +- added NetSSL - SSL support for Net library, based on OpenSSL +- console output of processes launched with Process::launch() is now visible + + +Release 1.0.0 (2006-01-19) +========================== + +- removed unnecessary console output from ProcessTest +- documentation fixes + + +Release 1.0b2 (2006-01-16) +========================== + +- added ProcessHandle class +- Process::launch() now returns a ProcessHandle instead of a process ID. + This fixes a potential problem on Windows with Process::wait() when + the process terminates before wait() is called. +- added SplitterChannel::close() +- added Logger::destroy() +- added POP3ClientSession::deleteMessage() +- added test for Process::launch() +- documentation fixes + + +Release 1.0b1 (2006-01-09) +========================== + +- improved recognition of Windows paths in Path::parseGuess() +- added setCurrentLineNumber()/getCurrentLineNumber() to CountingStreamBuf +- improvememts to StreamTokenizer and Token; fixed documentation +- added a workaround for some strange istream behaviour with VS 2005 and FTPClientSessionTest +- improved exception/error reporting in cppunit +- added POP3ClientSession +- added Process::launch() and Process::wait() +- added Mail sample +- added MailStream and SMTPClientSession classes +- renamed some methods in DialogSocket to make them more general +- NullPartHandler has moved out of HTMLForm.cpp into a separate file +- Base64Encoder now always writes \r\n line ends +- MessageHeader::quote has an optional addition arg controlling the treatment of whitespace +- bugfix: MultipartReader had a problem with empty lines (\r\n sequences) in a part +- added MailMessage and MailRecipient classes +- added text encoding support for Windows-1252 codepage + + +Release 1.0a1 (2006-01-03) [internal] +===================================== + +- mediaType is used consistently to refer to a MIME media type (some occurences of contentType and mimeType have been replaced) +- moved MediaType::quote() to MessageHeader and made it public +- added MultipartWriter::stream() +- Renamed AttachmentSource to PartSource and AttachmentHandler to PartHandler +- SIGPIPE is always blocked in main thread on Unix systems +- added EchoServer sample +- fixed a bug in SocketImpl::setBlocking() - did exactly the opposite (value to ioctl was wrong) +- fixed a memory leak in NotificationQueue sample +- added comparison operators to Socket so that Sockets can be used as keys in maps +- added Socket::setBlocking() +- added StreamSocket::connectNB() (non-blocking connect) +- added Observer::accepts() +- added SocketReactor, SocketConnector and SocketAcceptor classes to support event-based socket programming +- NamespacePrefixesStrategy now uses expat's XML_SetReturnNSTriplet(). + The previously used separate namespace handling code has been removed. + This improves performance if NamespacePrefixesStrategy is used (both the n + amespaces and namespace-prefixes SAX2 features are used) +- upgraded expat to 2.0 pre-release (2005-12-27) snapshot +- added TeeInputStream and TeeOutputStream classes +- added download sample for URIStreamOpener +- renamed registerOpener() to registerFactory() in HTTPStreamFactory and FTPStreamFactory +- added LineEndingConverter streams +- added FTPClientSession +- code and documentation clean-up +- added DialogSocket class +- reorganized HTTP test suites +- added FTPClientSession and FTPStreamFactory +- added DialogSocket class + + +Release 0.96.1 (2005-12-28) +=========================== + +- fixed a memory leak caused by a bug in Microsoft's stream implementation (see the comment in Foundation/StreamUtil.h for an explanation) +- added samples for Net library +- added uptime() and startTime() to Util::Application +- added DateTimeFormatter::format() for Timespan +- added ErrorHandler class and better exception handling for threads +- added poco_debugger() and poco_debugger_msg() macros +- added project and solution files for Visual Studio 2005 (due to some bugs/leaks in Microsofts standard library - see + http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=e08bd793-3fef-40ff-adda-ed313e0eafcc + we do not recommend using this for production purposes) +- fixed two problems with out-of-range string iterator in Path (the testsuite triggered an assertion in VC++ 8.0) +- fixed mac line endings in a few files +- added a workaround to the class loader that fixes strange behavior with VC++ 8.0. There seems to be a problem with typeid() not returning a valid typeinfo under certain circumstances. +- added buffer allocator argument to buffered stream buffer templates +- added buffer pools to HTTP to reduce memory fragmentation and to improve performance +- added Net to Windows build.cmd script +- added swap() to various classes that already support assignment +- added a null pointer check in DOMWriter::writeNode() +- fixed documentation in BinaryWriter.h and BinaryReader.h +- added explicit support for network byte order to BinaryReader and BinaryWriter +- added basic support for FreeBSD (needs more testing) +- BinaryReader: renamed readRawData() to readRaw() to be consistent with BinaryWriter::writeRaw() +- added support for uppercase output to HexBinaryEncoder. +- added MediaType class +- added QuotedPrintableEncoder and QuotedPrintableDecoder classes +- renamed ObjectFactory to Instantiator. This should prevent the confusion caused by DynamicFactory and ObjectFactory. Sorry for the inconvenience if you are already using this. +- AttachmentSource::filename() now returns const string& +- added StringAttachmentSource +- replaced old-style C casts with C++ casts in NetworkInterface.cpp +- MutexImpl (WIN32): replaced InitializeCriticalSection with InitializeCriticalSectionAndSpinCount, which should increase performance on multiprocessor or multicore systems when many locks are used. +- fixed a problem with STLport 5.0 when compiling StreamTokenizer +- HTTPStreamOpener now also works with no-path URIs (like http://www.appinf.com) +- fixed wrong delete usage (plain delete instead of delete [] was used in a few cases) +- fixed a handle leak in WinTestRunner + + +Release 0.95.4 (2005-11-07) +=========================== + +- fixed #1348006 and #1348005 + + +Release 0.95.3 (2005-10-28) [internal] +====================================== + +- updated build scripts (patch #1339015) +- added support for AMD64 platforms (patch #1339015) +- MultipartWriter creates its own boundary if an empty string is passed in as boundary +- made MultipartWriter::createBoundary() public +- fixed wrong documentation for DateTimeFormat::HTTP_FORMAT +- added support for HTTP Basic authentication +- added support for HTTP Cookies +- added support for HTML forms + + +Release 0.95.2 (2005-10-22) [internal] +====================================== + +- fixed a potential problems with streams when close in destructor fails (added try..catch block around close in destructors) +- added HTTPServer & friends +- added hasIdleThreads() method to NotificationQueue +- added TCPServer and friend +- added support for HTTP proxies to HTTPClientSession and HTTPStreamOpener +- fixed documentation bugs (Mutex.h, ClassLoader.h) + + +Relesae 0.95.1 (2005-10-15) [internal] +====================================== + +- Tasks can now throw custom notifications (contributed by Alex Fabijanic) +- renamed URIFileStreamFactory to FileStreamFactory +- added a few methods to URI (setPathEtc(), getPathEtc(), getPathAndQuery()) +- added new exception classes +- fixed some documentation +- added basic checks when reading a MessageHeader from a stream +- added HTTP classes (testsuite still incomplete) +- added MessageHeader, NameValueCollection, MultipartReader and MultipartWriter classes +- added Timespan::useconds() +- added ClassLoader::isLibraryLoaded() +- Socket classes use Timespan::useconds() to fill struct timeval +- added DatagramSocket, MulticastSocket and NetworkInterface classes +- added socket classes and related basic stuff +- added additonal constructor/assign to Timespan- added BasicBufferedBidirectionalStreamBuf +- fixed a potential MT issue in Base64Decoder +- code beautifying in [Un]BufferedStreamBuf +- more improvements to ClassLoader +- code cleanup and naming convention fixes (changed all *Imp classes to *Impl for consistency) + + +Release 0.94.1 (2005-09-30) [internal] +====================================== + +- added MetaSingleton (based on a contribution by Alex Fabijanic) +- added ClassLoader::create() +- added ClassLoader::instance() +- code clean-ups in FileChannel and related classes +- added SimpleFileChannel +- RotateAtTimeStrategy: + ::getNextRollover() rewritten (buggy) +- DateTime + microseconds assert corrected + asserts in computeGregorian() (except for year - see comment in computeGregorian()) + milliseconds calculation modified in computeGregorian() + microseconds assigned in computeGregorian() + normalize() and checkLimit() private functions to correct cases of overflow for milli/microseconds +- LocalDateTime: added timestamp() method +- FileChannel: + added "times" property (used to determine whether to use UTC or local time with RotateAtTimeStrategy) + ::setProperty() modified (whenever "times" property is set, methods setRotation and setArchive are + reinvoked to reflect the change) +- FileChannel: added support for archived file compression and archived file purging +- FileChannel tests modified +- FileChannel: put LogFile, RotateStrategy and ArchiveStrategy into their own files +- Message: added thread id field +- PatternFormatter: added %I specifier for thread id +- ThreadPool: PooledThread can be assigned a name +- TaskManager: task name is reflected in thread name +- fixed LocalDateTime::operator - (const Timespan&) [#0000004] +- upon startup all loggers' channels are set to a console channel +- improved search for application configuration files (see loadConfiguration()). +- added Glob class (fixes #1249700) +- upgraded to zlib 1.2.3 (fixes #1261712) +- added Logger::dump() +- fixed a wrong condition in Logger::log(const Message&) +- Path::find() now also works with relative paths in addition to plain file names +- added Path(const Path&, const Path&) constructor +- added SharedPtr template +- added Path::tryParse() +- SAXParser::parse()/EntityResolverImpl now works for both URIs and local filesystem paths (fixes #1254812) + + +Release 0.93.1 (2005-08-01) +=========================== + +This release contains various new features, improvements and bugfixes: +- bugfix: UUIDGenerator throws an exception if no connected ethernet adapter can + be found (and thus no MAC address can be obtained) +- added UUIDGenerator::createOne() method +- added error handling to UUID sample application +- added relational (==, !=, <, <=, >, >=) and arithmetic operators (+, -, +=, -=) to DateTime +- added LocalDateTime class +- added support for LocalDateTime to DateTimeParser and DateTimeFormatter +- added enqueueUrgentNotification() to NotificationQueue +- added support for timezone specifiers (%z, %Z) to PatternFormatter +- added [] operator and count() to StringTokenizer +- added elapsed() and isElapsed() to Timestamp +- added tzd() to Timezone +- added WinRegistryKey and WinService classes (Windows only) +- added index operator and count() to StringTokenizer +- added day/time-based log rotation (thanks to Alex Fabijanic), minor improvements to DateTimeParser +- support for Mac OS X 10.4/gcc 4.0.0 +- added NamedMutex and NamedEvent +- added Process::kill() +- added NoPermissionException +- added Task and TaskManager classes +- added ServerApplication class +- bugfix: EventLogChannel - _logFile was not properly initialized in one constructor +- bugfix: File::createDirectories did not work for hierarchies deeper than three +- added Util::FilesystemConfiguration +- documented logging policy: log() must open channel if it hasn't been opened yet +- FileChannel::log() opens channel if necessary +- the application reference passed to initialize() and reinitialize() is no longer const +- improved application logging initialization +- fixed a problem with configuration view and property placeholders +- fixed Util build configuration for Visual Studio +- improved application samples +- fixed documentation for Semaphore class + + +Release 0.92.1 (2005-05-09) +=========================== + +This release introduces the Util library that provides support for +configuration file parsing (different file formats), command line +argument processing, logging configuration and a framework for +command line/server applications. +There have also been various changes to the Foundation library: +- a new RefCountedObject class that acts as a base class for + various classes that use reference counting +- some missing members have been added to the AutoPtr template +- various improvements and bugfixes to the Logging framework, as well as + new LoggingFactory and LoggingRegistry classses, and a NullChannel class +- the SignalHandler class (Unix platforms only) +- ObjectFactory and DynamicFactory template classes +- the Path::find method for searching a file in a list of directories +- various new Exception classes + + +Release 0.91.4 (2005-04-11) +=========================== + +This is mainly a maintenance release that adds support for QNX Neutrino +and OpenVMS. There are also minor bugfixes and improvements. + +The Unix build system has been modified to work on QNX Neutrino. +The OpenVMS build system has been fixed and works now. +Some missing #include's have been added for QNX Neutrino. +Foundation/String.h: icompare now supports comparison with const char*; +the classic C version of isspace() has been used in a few places instead of the +C++ version, this has been fixed. +Foundation/Exception.h: IllegalStateException added. + + +Release 0.91.3 (2005-03-19) +=========================== + +This is a maintenance release that adds support for Solaris/Sun Forte C++. +No new features have been added. + +An implementation of FPEnvironment for Solaris has been included. +All stream classes have been modified to work around an initialization +problem that surfaced with Sun's C++ compiler when using STLport. +Source-code compatibility with the previous release is not affected. Various +minor changes, mostly adding missing #include's for Solaris. + + +Release 0.91.2 (2005-02-27) +=========================== + +Minor improvements to the Unix build system. No actual changes in the +libraries. + + +Release 0.91.1 (2005-02-21) +=========================== + +This is the first public release of the C++ Portable Components. +The release does not contain all features planned for the later 1.0 release +(the NET library is missing, for example), but is already quite usable. +Please refer to the README file for more information and instructions for +building the libraries. + + +-- +$Id: //poco/1.4/dist/CHANGELOG#59 $ diff --git a/contrib/libpoco/CMakeLists.txt b/contrib/libpoco/CMakeLists.txt new file mode 100644 index 00000000000..17cec0e61d4 --- /dev/null +++ b/contrib/libpoco/CMakeLists.txt @@ -0,0 +1,330 @@ +cmake_minimum_required(VERSION 2.8.0) + +# POCO_BUILD_TYPE +# POCO_STATIC +# POCO_UNBUNDLED +# POCO_NO_LOCALE +# +# ENABLE_{COMPONENT} +# ENABLE_TESTS + +project(Poco) + +file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/libversion" SHARED_LIBRARY_VERSION) + +# Read the version information from the VERSION file +file (STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" PACKAGE_VERSION ) +message(STATUS "Poco package version: ${PACKAGE_VERSION}") +string(REGEX REPLACE "([0-9]+)\\.[0-9]+\\.[0-9]+.*" "\\1" CPACK_PACKAGE_VERSION_MAJOR ${PACKAGE_VERSION}) +string(REGEX REPLACE "[0-9]+\\.([0-9])+\\.[0-9]+.*" "\\1" CPACK_PACKAGE_VERSION_MINOR ${PACKAGE_VERSION}) +string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" CPACK_PACKAGE_VERSION_PATCH ${PACKAGE_VERSION}) + +set(COMPLETE_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) +set(RELEASE_NAME "Unstable-trunk") +set(PROJECT_VERSION ${COMPLETE_VERSION}) + +# Put the libaries and binaries that get built into directories at the +# top of the build tree rather than in hard-to-find leaf +# directories. This simplifies manual testing and the use of the build +# tree rather than installed Boost libraries. +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) +# Windows DLLs are "runtime" for CMake. Output them to "bin" like the Visual Studio projects do. +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) + +# Append our module directory to CMake +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +################################################################################# +# Setup C/C++ compiler options +################################################################################# + +if(NOT MSVC_IDE) + if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING + "Choose the type of build, options are: None Debug Release" FORCE) + endif() + message(STATUS "Setting Poco build type - ${CMAKE_BUILD_TYPE}") +endif() + +if (CMAKE_BUILD_TYPE STREQUAL "") + set( CMAKE_BUILD_TYPE "RelWithDebInfo" ) +endif () + +# http://www.cmake.org/Wiki/CMake_Useful_Variables : +# CMAKE_BUILD_TYPE +# Choose the type of build. CMake has default flags for these: +# +# * None (CMAKE_C_FLAGS or CMAKE_CXX_FLAGS used) +# * Debug (CMAKE_C_FLAGS_DEBUG or CMAKE_CXX_FLAGS_DEBUG) +# * Release (CMAKE_C_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELEASE) +# * RelWithDebInfo (CMAKE_C_FLAGS_RELWITHDEBINFO or CMAKE_CXX_FLAGS_RELWITHDEBINFO +# * MinSizeRel (CMAKE_C_FLAGS_MINSIZEREL or CMAKE_CXX_FLAGS_MINSIZEREL) + +# For Debug build types, append a "d" to the library names. +set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "Set debug library postfix" FORCE) + +# Include some common macros to simpilfy the Poco CMake files +include(PocoMacros) + +# Allow enabling and disabling components +option(ENABLE_XML "Enable the XML" ON) +option(ENABLE_JSON "Enable the JSON" ON) +option(ENABLE_MONGODB "Enable MongoDB" ON) +option(ENABLE_PDF "Enable PDF" OFF) +option(ENABLE_UTIL "Enable Util" ON) +option(ENABLE_NET "Enable Net" ON) +option(ENABLE_NETSSL "Enable NetSSL" ON) +option(ENABLE_NETSSL_WIN "Enable NetSSL Windows" OFF) +option(ENABLE_CRYPTO "Enable Crypto" ON) +option(ENABLE_DATA "Enable Data" ON) +option(ENABLE_DATA_SQLITE "Enable Data SQlite" ON) +option(ENABLE_DATA_MYSQL "Enable Data MySQL" ON) +option(ENABLE_DATA_ODBC "Enable Data ODBC" ON) +option(ENABLE_SEVENZIP "Enable SevenZip" OFF) +option(ENABLE_ZIP "Enable Zip" ON) +option(ENABLE_APACHECONNECTOR "Enable ApacheConnector" OFF) +option(ENABLE_CPPPARSER "Enable C++ parser" OFF) +option(ENABLE_POCODOC "Enable Poco Documentation Generator" OFF) +option(ENABLE_PAGECOMPILER "Enable PageCompiler" ON) +option(ENABLE_PAGECOMPILER_FILE2PAGE "Enable File2Page" ON) + +option(FORCE_OPENSSL "Force usage of OpenSSL even under windows" OFF) + +option(ENABLE_TESTS + "Set to OFF|ON (default is OFF) to control build of POCO tests & samples" OFF) + +option(POCO_STATIC + "Set to OFF|ON (default is ON) to control build of POCO as STATIC library" ON) + +option(POCO_UNBUNDLED + "Set to OFF|ON (default is OFF) to control linking dependencies as external" OFF) + +# Uncomment from next two lines to force statitc or dynamic library, default is autodetection +if(POCO_STATIC) + add_definitions( -DPOCO_STATIC -DPOCO_NO_AUTOMATIC_LIBS) + set( LIB_MODE STATIC ) + message(STATUS "Building static libraries") +else(POCO_STATIC) + set( LIB_MODE SHARED ) + message(STATUS "Building dynamic libraries") +endif(POCO_STATIC) + +if (ENABLE_TESTS) + include(CTest) + enable_testing() + message(STATUS "Building with unittests & samples") +else () + message(STATUS "Building without tests & samples") +endif () + +if (POCO_UNBUNDLED) + add_definitions( -DPOCO_UNBUNDLED) + message(STATUS "Build with using external sqlite, libz, pcre, expat ...") +else () + message(STATUS "Build with using internal copy of sqlite, libz, pcre, expat, ...") +endif () + +include(CheckTypeSize) +find_package(Cygwin) + +# OS Detection +if(WIN32) + add_definitions( -DPOCO_OS_FAMILY_WINDOWS -DUNICODE -D_UNICODE) + #set(SYSLIBS iphlpapi gdi32 odbc32) +endif(WIN32) + +if (UNIX AND NOT ANDROID ) + add_definitions( -DPOCO_OS_FAMILY_UNIX ) + add_definitions( -Wno-unused-private-field -Wno-unused-local-typedef -Wno-for-loop-analysis -Wno-unknown-pragmas ) + # Standard 'must be' defines + if (APPLE) + add_definitions( -DPOCO_HAVE_IPv6 -DPOCO_NO_STAT64) + set(SYSLIBS dl) + else (APPLE) + add_definitions(-D_XOPEN_SOURCE=500 -D_REENTRANT -D_THREAD_SAFE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -DPOCO_HAVE_FD_EPOLL -DPOCO_HAVE_IPv6) + set(SYSLIBS pthread dl rt) + endif (APPLE) +endif(UNIX AND NOT ANDROID ) + +if (CMAKE_SYSTEM MATCHES "SunOS") + add_definitions( -DPOCO_OS_FAMILY_UNIX ) + # Standard 'must be' defines + add_definitions( -D_XOPEN_SOURCE=500 -D_REENTRANT -D_THREAD_SAFE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 ) + set(SYSLIBS pthread socket xnet nsl resolv rt dl) +endif(CMAKE_SYSTEM MATCHES "SunOS") + +if (CMAKE_COMPILER_IS_MINGW) + add_definitions(-DWC_NO_BEST_FIT_CHARS=0x400 -DPOCO_WIN32_UTF8) + add_definitions(-D_WIN32 -DMINGW32 -DWINVER=0x500 -DODBCVER=0x0300 -DPOCO_THREAD_STACK_SIZE) +endif (CMAKE_COMPILER_IS_MINGW) + +if (CYGWIN) +# add_definitions(-DWC_NO_BEST_FIT_CHARS=0x400 -DPOCO_WIN32_UTF8) +endif (CYGWIN) + +# SunPro C++ +if (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro") + add_definitions( -D_BSD_SOURCE -library=stlport4) +endif (${CMAKE_CXX_COMPILER_ID} MATCHES "SunPro") + +# iOS +if (IOS) + add_definitions( -DPOCO_HAVE_IPv6 -DPOCO_NO_FPENVIRONMENT -DPOCO_NO_STAT64 -DPOCO_NO_SHAREDLIBS -DPOCO_NO_NET_IFTYPES ) +endif(IOS) + +#Android +if (ANDROID) + add_definitions( -DPOCO_ANDROID -DPOCO_NO_FPENVIRONMENT -DPOCO_NO_WSTRING -DPOCO_NO_SHAREDMEMORY ) +endif(ANDROID) + + +# Collect the built libraries and include dirs, the will be used to create the PocoConfig.cmake file +set(Poco_COMPONENTS "") + +if (ENABLE_TESTS) + add_subdirectory(CppUnit) +endif () + +add_subdirectory(Foundation) +if(ENABLE_XML) +add_subdirectory(XML) +list(APPEND Poco_COMPONENTS "XML") +endif() +if(ENABLE_JSON) +add_subdirectory(JSON) +list(APPEND Poco_COMPONENTS "JSON") +endif() +if(ENABLE_MONGODB) +add_subdirectory(MongoDB) +list(APPEND Poco_COMPONENTS "MongoDB") +endif() +if(ENABLE_PDF) +add_subdirectory(PDF) +list(APPEND Poco_COMPONENTS "PDF") +endif() +if(ENABLE_UTIL) +add_subdirectory(Util) +list(APPEND Poco_COMPONENTS "Util") +endif() +if(ENABLE_NET) +add_subdirectory(Net) +list(APPEND Poco_COMPONENTS "Net") +endif() + + +#NetSSL + + +if(WIN32 AND ENABLE_NETSSL_WIN) + add_subdirectory(NetSSL_Win) + list(APPEND Poco_COMPONENTS "NetSSL_Win") +endif(WIN32 AND ENABLE_NETSSL_WIN) + +find_package(OpenSSL) +if(OPENSSL_FOUND) + include_directories("${OPENSSL_INCLUDE_DIR}") + if(ENABLE_NETSSL) + add_subdirectory(NetSSL_OpenSSL) + list(APPEND Poco_COMPONENTS "NetSSL_OpenSSL") + endif() + if(ENABLE_CRYPTO) + add_subdirectory(Crypto) + list(APPEND Poco_COMPONENTS "Crypto") + endif() +endif(OPENSSL_FOUND) + +if(ENABLE_DATA) +add_subdirectory(Data) +list(APPEND Poco_COMPONENTS "Data") +endif() +if(ENABLE_SEVENZIP) +add_subdirectory(SevenZip) +list(APPEND Poco_COMPONENTS "SevenZip") +endif() +if(ENABLE_ZIP) +add_subdirectory(Zip) +list(APPEND Poco_COMPONENTS "Zip") +endif() + +find_package(APR) +find_package(Apache2) +if(APRUTIL_FOUND AND APACHE_FOUND) + include_directories( "${APACHE_INCLUDE_DIR}" "${APRUTIL_INCLUDE_DIR}" ) + if(ENABLE_APACHECONNECTOR) + add_subdirectory(ApacheConnector) + list(APPEND Poco_COMPONENTS "ApacheConnector") + endif() +endif(APRUTIL_FOUND AND APACHE_FOUND) + +if(ENABLE_CPPPARSER) +add_subdirectory(CppParser) +list(APPEND Poco_COMPONENTS "CppParser") +endif() + +if(ENABLE_POCODOC) +add_subdirectory(PocoDoc) +list(APPEND Poco_COMPONENTS "PocoDoc") +endif() + +if(ENABLE_PAGECOMPILER) +add_subdirectory(PageCompiler) +list(APPEND Poco_COMPONENTS "PageCompiler") +endif() + +if(ENABLE_PAGECOMPILER_FILE2PAGE) +add_subdirectory(PageCompiler/File2Page) +list(APPEND Poco_COMPONENTS "File2Page") +endif() + +############################################################# +# Uninstall stuff see: http://www.vtk.org/Wiki/CMake_FAQ +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + +add_custom_target(uninstall + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") + +############################################################# +# Enable packaging + +include(InstallRequiredSystemLibraries) + +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Poco Libraries") +set(CPACK_PACKAGE_VENDOR "Applied Informatics Software Engineering GmbH") +set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README") +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") +set(CPACK_PACKAGE_INSTALL_DIRECTORY "/usr/local") + +include(CPack) + +############################################################# +# cmake config files + +configure_file(cmake/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" @ONLY) +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION + "lib/cmake/${PROJECT_NAME}" + COMPONENT + Devel +) + +# in tree build settings +#configure_file(PocoBuildTreeSettings.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/PocoBuildTreeSettings.cmake @ONLY) + + +message(STATUS "CMake ${CMAKE_VERSION} successfully configured ${PROJECT_NAME} using ${CMAKE_GENERATOR} generator") +message(STATUS "Installation target path: ${CMAKE_INSTALL_PREFIX}") + +message(STATUS "C_FLAGS: =${CMAKE_C_FLAGS}") +message(STATUS "CXX_FLAGS:=${CMAKE_CXX_FLAGS}") + +foreach(component ${Poco_COMPONENTS}) +message(STATUS "Building: ${component}") +endforeach() + diff --git a/contrib/libpoco/CONTRIBUTORS b/contrib/libpoco/CONTRIBUTORS new file mode 100644 index 00000000000..4ef48d2a2ad --- /dev/null +++ b/contrib/libpoco/CONTRIBUTORS @@ -0,0 +1,53 @@ +Guenter Obiltschnig +Alex Fabijanic +Peter Schojer +Ferdinand Beyer +Krzysztof Burghardt +Claus Dabringer +Caleb Epstein +Eran Hammer-Lahav +Chris Johnson +Sergey Kholodilov +Ryan Kraay +Larry Lewis +Andrew J. P. Maclean +Andrew Marlow +Paschal Mushubi +Jiang Shan +David Shawley +Sergey Skorokhodov +Tom Tan +Sergey N. Yatskevich +Marc Chevrier +Philippe Cuvillier +Marian Krivos +Franky Braem +Philip Prindeville +Anton Yabchinskiy +Rangel Reale +Fabrizio Duhem +Patrick White +Mike Naquin +Roger Meier +Mathaus Mendel +Arturo Castro +Adrian Imboden +Matej Knopp +Patrice Tarabbia +Lucas Clemente +Karl Reid +Pascal Bach +Cristian Thiago Moecke +Sergei Nikulov +Aaron Kaluszka +Iyed Bennour +Scott Davis +Kristin Cowalcijk +Yuval Kashtan +Christopher Baker +Scott Davis +Jeff Adams +Martin Osborne +Björn Schramke +-- +$Id$ diff --git a/contrib/libpoco/CppUnit/WinTestRunner/include/WinTestRunner/WinTestRunner.h b/contrib/libpoco/CppUnit/WinTestRunner/include/WinTestRunner/WinTestRunner.h new file mode 100644 index 00000000000..2e7ac701443 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/include/WinTestRunner/WinTestRunner.h @@ -0,0 +1,77 @@ +// +// WinTestRunner.h +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/include/WinTestRunner/WinTestRunner.h#1 $ +// +// Application shell for CppUnit's TestRunner dialog. +// + + +#ifndef WinTestRunner_H_INCLUDED +#define WinTestRunner_H_INCLUDED + + +#if !defined(POCO_STATIC) +#if defined(WinTestRunner_EXPORTS) +#define WinTestRunner_API __declspec(dllexport) +#else +#define WinTestRunner_API __declspec(dllimport) +#endif +#else +#define WinTestRunner_API +#endif + + +#include "CppUnit/CppUnit.h" +#include +#include + + +namespace CppUnit { + + +class Test; + + +class WinTestRunner_API WinTestRunner +{ +public: + WinTestRunner(); + ~WinTestRunner(); + + void run(); + void addTest(Test* pTest); + +private: + std::vector _tests; +}; + + +class WinTestRunner_API WinTestRunnerApp: public CWinApp + /// A simple application class that hosts the TestRunner dialog. + /// Create a subclass and override the TestMain() method. + /// + /// WinTestRunnerApp supports a batch mode, which runs the + /// test using the standard text-based TestRunner from CppUnit. + /// To enable batch mode, start the application with the "/b" + /// or "/B" argument. Optionally, a filename may be specified + /// where the test output will be written to: "/b:" or + /// "/B:". + /// + /// When run in batch mode, the exit code of the application + /// will denote test success (0) or failure (1). +{ +public: + virtual BOOL InitInstance(); + + virtual void TestMain() = 0; + + DECLARE_MESSAGE_MAP() +}; + + +} // namespace CppUnit + + +#endif // WinTestRunner_H_INCLUDED + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/res/Resource.h b/contrib/libpoco/CppUnit/WinTestRunner/res/Resource.h new file mode 100644 index 00000000000..cd0bf352877 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/res/Resource.h @@ -0,0 +1,27 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by TestRunner.rc +// +#define IDD_DIALOG_TESTRUNNER 129 +#define IDC_LIST 1000 +#define ID_RUN 1001 +#define ID_STOP 1002 +#define IDC_PROGRESS 1003 +#define IDC_INDICATOR 1004 +#define IDC_COMBO_TEST 1005 +#define IDC_STATIC_RUNS 1007 +#define IDC_STATIC_ERRORS 1008 +#define IDC_STATIC_FAILURES 1009 +#define IDC_EDIT_TIME 1010 +#define IDC_CHK_AUTORUN 1013 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 131 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 1014 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/contrib/libpoco/CppUnit/WinTestRunner/res/WinTestRunner.rc b/contrib/libpoco/CppUnit/WinTestRunner/res/WinTestRunner.rc new file mode 100644 index 00000000000..c4056f26972 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/res/WinTestRunner.rc @@ -0,0 +1,175 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "#define _AFX_NO_SPLITTER_RESOURCES\r\n" + "#define _AFX_NO_OLE_RESOURCES\r\n" + "#define _AFX_NO_TRACKER_RESOURCES\r\n" + "#define _AFX_NO_PROPERTY_RESOURCES\r\n" + "\r\n" + "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n" + "#ifdef _WIN32\r\n" + "LANGUAGE 9, 1\r\n" + "#pragma code_page(1252)\r\n" + "#endif\r\n" + "#include ""..\\res\\WinTestRunner.rc2"" // non-Microsoft Visual C++ edited resources\r\n" + "#include ""afxres.rc"" // Standard components\r\n" + "#endif\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "FileDescription", "CppUnit WinTestRunner DLL" + VALUE "FileVersion", "1, 0, 0, 1" + VALUE "InternalName", "WinTestRunner" + VALUE "LegalCopyright", "Copyright (c) 2005" + VALUE "OriginalFilename", "TestRunner.dll" + VALUE "ProductName", "CppUnit WinTestRunner Dynamic Link Library" + VALUE "ProductVersion", "1, 0, 0, 1" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +IDD_DIALOG_TESTRUNNER DIALOGEX 0, 0, 512, 300 +STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "CppUnit WinTestRunner" +FONT 8, "MS Sans Serif", 0, 0, 0x0 +BEGIN + COMBOBOX IDC_COMBO_TEST,7,20,424,273,CBS_DROPDOWNLIST | + WS_VSCROLL | WS_TABSTOP + DEFPUSHBUTTON "Run",ID_RUN,455,7,50,14 + DEFPUSHBUTTON "Stop",ID_STOP,455,24,50,14 + CONTROL "List1",IDC_LIST,"SysListView32",LVS_REPORT | WS_BORDER | + WS_TABSTOP,7,110,498,160 + PUSHBUTTON "Close",IDOK,455,279,50,14 + LTEXT "Test Name:",IDC_STATIC,7,9,179,9 + LTEXT "Progress:",IDC_STATIC,7,55,49,9 + LTEXT "Errors and Failures:",IDC_STATIC,7,99,67,9 + LTEXT "Runs:",IDC_STATIC,457,54,26,10 + LTEXT "Failures:",IDC_STATIC,457,80,26,10 + LTEXT "Errors:",IDC_STATIC,457,67,26,10 + RTEXT "0",IDC_STATIC_RUNS,487,54,16,10 + RTEXT "0",IDC_STATIC_ERRORS,487,67,16,10 + RTEXT "0",IDC_STATIC_FAILURES,487,80,16,10 + EDITTEXT IDC_EDIT_TIME,7,281,440,12,ES_AUTOHSCROLL | ES_READONLY | + NOT WS_BORDER + LTEXT "",IDC_PROGRESS,7,67,424,20,SS_SUNKEN | NOT WS_VISIBLE + CONTROL "Auto Run",IDC_CHK_AUTORUN,"Button",BS_AUTOCHECKBOX | + BS_LEFTTEXT | WS_TABSTOP,383,38,46,10 +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO +BEGIN + IDD_DIALOG_TESTRUNNER, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 505 + TOPMARGIN, 7 + BOTTOMMARGIN, 293 + END +END +#endif // APSTUDIO_INVOKED + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// +#define _AFX_NO_SPLITTER_RESOURCES +#define _AFX_NO_OLE_RESOURCES +#define _AFX_NO_TRACKER_RESOURCES +#define _AFX_NO_PROPERTY_RESOURCES + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE 9, 1 +#pragma code_page(1252) +#endif +#include "afxres.rc" // Standard components +#endif +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.cpp new file mode 100644 index 00000000000..2fc5110fad3 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.cpp @@ -0,0 +1,44 @@ +// +// ActiveTest.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/ActiveTest.cpp#1 $ +// + + +#include +#include "ActiveTest.h" + + +namespace CppUnit { + + +// Spawn a thread to a test +void ActiveTest::run(TestResult* result) +{ + CWinThread* thread; + + setTestResult(result); + _runCompleted.ResetEvent(); + + thread = AfxBeginThread(threadFunction, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); + DuplicateHandle(GetCurrentProcess(), thread->m_hThread, GetCurrentProcess(), &_threadHandle, 0, FALSE, DUPLICATE_SAME_ACCESS); + + thread->ResumeThread(); +} + + +// Simple execution thread. Assuming that an ActiveTest instance +// only creates one of these at a time. +UINT ActiveTest::threadFunction(LPVOID thisInstance) +{ + ActiveTest* test = (ActiveTest*) thisInstance; + + test->run(); + test->_runCompleted.SetEvent(); + + return 0; +} + + +} // namespace CppUnit + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.h b/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.h new file mode 100644 index 00000000000..bf9df5e7da1 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/ActiveTest.h @@ -0,0 +1,89 @@ +// +// ActiveTest.h +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/ActiveTest.h#1 $ +// + + +#ifndef ActiveTest_INCLUDED +#define ActiveTest_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/TestDecorator.h" +#include + + +namespace CppUnit { + + +/* A Microsoft-specific active test + * + * An active test manages its own + * thread of execution. This one + * is very simple and only sufficient + * for the limited use we put it through + * in the TestRunner. It spawns a thread + * on run (TestResult *) and signals + * completion of the test. + * + * We assume that only one thread + * will be active at once for each + * instance. + * + */ +class ActiveTest: public TestDecorator +{ +public: + ActiveTest(Test* test); + ~ActiveTest(); + + void run(TestResult* result); + +protected: + HANDLE _threadHandle; + CEvent _runCompleted; + TestResult* _currentTestResult; + + void run (); + void setTestResult(TestResult* result); + static UINT threadFunction(LPVOID thisInstance); +}; + + +// Construct the active test +inline ActiveTest::ActiveTest(Test *test): TestDecorator(test) +{ + _currentTestResult = NULL; + _threadHandle = INVALID_HANDLE_VALUE; +} + + +// Pend until the test has completed +inline ActiveTest::~ActiveTest() +{ + CSingleLock(&_runCompleted, TRUE); + CloseHandle(_threadHandle); +} + + +// Set the test result that we are to run +inline void ActiveTest::setTestResult(TestResult* result) +{ + _currentTestResult = result; +} + + +// Run our test result +inline void ActiveTest::run() +{ + TestDecorator::run(_currentTestResult); +} + + +} // namespace CppUnit + + +#endif // ActiveTest_INCLUDED + + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/DLLMain.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/DLLMain.cpp new file mode 100644 index 00000000000..8fd293115fe --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/DLLMain.cpp @@ -0,0 +1,50 @@ +// +// DLLMain.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/DLLMain.cpp#1 $ +// + + +#include +#include + + +static AFX_EXTENSION_MODULE TestRunnerDLL = { NULL, NULL }; + + +extern "C" int APIENTRY +DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) +{ + // Remove this if you use lpReserved + UNREFERENCED_PARAMETER(lpReserved); + + if (dwReason == DLL_PROCESS_ATTACH) + { + TRACE0("WinTestRunner.DLL Initializing\n"); + + // Extension DLL one-time initialization + if (!AfxInitExtensionModule(TestRunnerDLL, hInstance)) + return 0; + + // Insert this DLL into the resource chain + // NOTE: If this Extension DLL is being implicitly linked to by + // an MFC Regular DLL (such as an ActiveX Control) + // instead of an MFC application, then you will want to + // remove this line from DllMain and put it in a separate + // function exported from this Extension DLL. The Regular DLL + // that uses this Extension DLL should then explicitly call that + // function to initialize this Extension DLL. Otherwise, + // the CDynLinkLibrary object will not be attached to the + // Regular DLL's resource chain, and serious problems will + // result. + + new CDynLinkLibrary(TestRunnerDLL); + } + else if (dwReason == DLL_PROCESS_DETACH) + { + TRACE0("WinTestRunner.DLL Terminating\n"); + // Terminate the library before destructors are called + AfxTermExtensionModule(TestRunnerDLL); + } + return 1; // ok +} diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.cpp new file mode 100644 index 00000000000..9486d02b572 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.cpp @@ -0,0 +1,52 @@ +// +// GUITestResult.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/GUITestResult.cpp#1 $ +// + + +#include "TestRunnerDlg.h" +#include "GUITestResult.h" + + +namespace CppUnit { + + +void GUITestResult::addError(Test *test, CppUnitException *e) +{ + ExclusiveZone zone(_syncObject); + + TestResult::addError(test, e); + _runner->addError(this, test, e); +} + + +void GUITestResult::addFailure(Test *test, CppUnitException *e) +{ + ExclusiveZone zone(_syncObject); + + TestResult::addFailure(test, e); + _runner->addFailure(this, test, e); +} + + +void GUITestResult::startTest(Test *test) +{ + ExclusiveZone zone(_syncObject); + + TestResult::startTest(test); + _runner->startTest(test); +} + + +void GUITestResult::endTest(Test *test) +{ + ExclusiveZone zone(_syncObject); + + TestResult::endTest(test); + _runner->endTest(this, test); +} + + +} // namespace CppUnit + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.h b/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.h new file mode 100644 index 00000000000..3fae4e00a0c --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/GUITestResult.h @@ -0,0 +1,83 @@ +// +// GUITestResult.h +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/GUITestResult.h#1 $ +// + + +#ifndef GuiTestResult_INCLUDED +#define GuiTestResult_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/TestResult.h" +#include + + +namespace CppUnit { + + +class TestRunnerDlg; + + +class GUITestResult: public TestResult +{ +public: + GUITestResult(TestRunnerDlg* runner); + ~GUITestResult(); + + void addError(Test* test, CppUnitException* e); + void addFailure(Test* test, CppUnitException* e); + + void startTest(Test* test); + void endTest(Test* test); + void stop(); + +protected: + class LightweightSynchronizationObject: public TestResult::SynchronizationObject + { + public: + void lock() + { + _syncObject.Lock(); + } + + void unlock() + { + _syncObject.Unlock(); + } + + private: + CCriticalSection _syncObject; + }; + +private: + TestRunnerDlg *_runner; +}; + + + +// Construct with lightweight synchronization +inline GUITestResult::GUITestResult(TestRunnerDlg* runner): _runner(runner) +{ + setSynchronizationObject(new LightweightSynchronizationObject()); +} + + +// Destructor +inline GUITestResult::~GUITestResult() +{ +} + + +// Override without protection to prevent deadlock +inline void GUITestResult::stop() +{ + _stop = true; +} + + +} // namespace CppUnit + + +#endif // GuiTestResult_INCLUDED diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.cpp new file mode 100644 index 00000000000..08935e548e6 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.cpp @@ -0,0 +1,140 @@ +// +// ProgressBar.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/ProgressBar.cpp#1 $ +// + + +#include "ProgressBar.h" + + +namespace CppUnit { + + +// Paint the progress bar in response to a paint message +void ProgressBar::paint(CDC& dc) +{ + paintBackground (dc); + paintStatus (dc); +} + + +// Paint the background of the progress bar region +void ProgressBar::paintBackground (CDC& dc) +{ + CBrush brshBackground; + CPen penGray (PS_SOLID, 1, RGB (128, 128, 128)); + CPen penWhite (PS_SOLID, 1, RGB (255, 255, 255)); + + VERIFY (brshBackground.CreateSolidBrush (::GetSysColor (COLOR_BTNFACE))); + + dc.FillRect (_bounds, &brshBackground); + + CPen *pOldPen; + + pOldPen = dc.SelectObject (&penGray); + { + dc.MoveTo (_bounds.left, _bounds.top); + dc.LineTo (_bounds.left + _bounds.Width () -1, _bounds.top); + + dc.MoveTo (_bounds.left, _bounds.top); + dc.LineTo (_bounds.left, _bounds.top + _bounds.Height () -1); + + } + dc.SelectObject (&penWhite); + { + dc.MoveTo (_bounds.left + _bounds.Width () -1, _bounds.top); + dc.LineTo (_bounds.left + _bounds.Width () -1, _bounds.top + _bounds.Height () -1); + + dc.MoveTo (_bounds.left, _bounds.top + _bounds.Height () -1); + dc.LineTo (_bounds.left + _bounds.Width () -1, _bounds.top + _bounds.Height () -1); + + } + dc.SelectObject (pOldPen); + +} + + +// Paint the actual status of the progress bar +void ProgressBar::paintStatus (CDC& dc) +{ + if (_progress <= 0) + return; + + CBrush brshStatus; + CRect rect (_bounds.left, _bounds.top, + _bounds.left + _progressX, _bounds.bottom); + + COLORREF statusColor = getStatusColor (); + + VERIFY (brshStatus.CreateSolidBrush (statusColor)); + + rect.DeflateRect (1, 1); + dc.FillRect (rect, &brshStatus); + +} + + +// Paint the current step +void ProgressBar::paintStep (int startX, int endX) +{ + // kludge: painting the whole region on each step + _baseWindow->RedrawWindow (_bounds); + _baseWindow->UpdateWindow (); + +} + + +// Setup the progress bar for execution over a total number of steps +void ProgressBar::start (int total) +{ + _total = total; + reset (); +} + + +// Take one step, indicating whether it was a successful step +void ProgressBar::step (bool successful) +{ + _progress++; + + int x = _progressX; + + _progressX = scale (_progress); + + if (!_error && !successful) + { + _error = true; + x = 1; + } + + paintStep (x, _progressX); + +} + + +// Map from steps to display units +int ProgressBar::scale (int value) +{ + if (_total > 0) + return max (1, value * (_bounds.Width () - 1) / _total); + + return value; + +} + + +// Reset the progress bar +void ProgressBar::reset () +{ + _progressX = 1; + _progress = 0; + _error = false; + + _baseWindow->RedrawWindow (_bounds); + _baseWindow->UpdateWindow (); + +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.h b/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.h new file mode 100644 index 00000000000..09eaa479607 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/ProgressBar.h @@ -0,0 +1,74 @@ +// +// ProgressBar.h +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/ProgressBar.h#1 $ +// + + +#ifndef ProgressBar_INCLUDED +#define ProgressBar_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include + + +namespace CppUnit { + + +/* A Simple ProgressBar for test execution display + */ +class ProgressBar +{ +public: + ProgressBar(CWnd* baseWindow, CRect& bounds); + + void step(bool successful); + void paint(CDC& dc); + int scale(int value); + void reset(); + void start(int total); + +protected: + void paintBackground(CDC& dc); + void paintStatus(CDC& dc); + COLORREF getStatusColor(); + void paintStep(int startX, int endX); + + CWnd* _baseWindow; + CRect _bounds; + + bool _error; + int _total; + int _progress; + int _progressX; +}; + + +// Construct a ProgressBar +inline ProgressBar::ProgressBar(CWnd* baseWindow, CRect& bounds): + _baseWindow(baseWindow), + _bounds(bounds), + _error(false), + _total(0), + _progress(0), + _progressX(0) +{ + WINDOWINFO wi; + wi.cbSize = sizeof(WINDOWINFO); + baseWindow->GetWindowInfo(&wi); + _bounds.OffsetRect(-wi.rcClient.left, -wi.rcClient.top); +} + + +// Get the current color +inline COLORREF ProgressBar::getStatusColor() +{ + return _error ? RGB(255, 0, 0) : RGB(0, 255, 0); +} + + +} // namespace CppUnit + + +#endif // ProgressBar_INCLUDED diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/SynchronizedTestResult.h b/contrib/libpoco/CppUnit/WinTestRunner/src/SynchronizedTestResult.h new file mode 100644 index 00000000000..55dc01c55bf --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/SynchronizedTestResult.h @@ -0,0 +1,106 @@ +#ifndef SYNCHRONIZEDTESTRESULTDECORATOR_H +#define SYNCHRONIZEDTESTRESULTDECORATOR_H + +#include +#include "TestResultDecorator.h" + +class SynchronizedTestResult : public TestResultDecorator +{ +public: + SynchronizedTestResult (TestResult *result); + ~SynchronizedTestResult (); + + + bool shouldStop (); + void addError (Test *test, CppUnitException *e); + void addFailure (Test *test, CppUnitException *e); + void startTest (Test *test); + void endTest (Test *test); + int runTests (); + int testErrors (); + int testFailures (); + bool wasSuccessful (); + void stop (); + + vector& errors (); + vector& failures (); + +private: + CCriticalSection m_criticalSection; + +}; + + +// Constructor +inline SynchronizedTestResult::SynchronizedTestResult (TestResult *result) +: TestResultDecorator (result) {} + +// Destructor +inline SynchronizedTestResult::~SynchronizedTestResult () +{} + +// Returns whether the test should stop +inline bool SynchronizedTestResult::shouldStop () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->shouldStop (); } + + +// Adds an error to the list of errors. The passed in exception +// caused the error +inline void SynchronizedTestResult::addError (Test *test, CppUnitException *e) +{ CSingleLock sync (&m_criticalSection, TRUE); m_result->addError (test, e); } + + +// Adds a failure to the list of failures. The passed in exception +// caused the failure. +inline void SynchronizedTestResult::addFailure (Test *test, CppUnitException *e) +{ CSingleLock sync (&m_criticalSection, TRUE); m_result->addFailure (test, e); } + + +// Informs the result that a test will be started. +inline void SynchronizedTestResult::startTest (Test *test) +{ CSingleLock sync (&m_criticalSection, TRUE); m_result->startTest (test); } + + +// Informs the result that a test was completed. +inline void SynchronizedTestResult::endTest (Test *test) +{ CSingleLock sync (&m_criticalSection, TRUE); m_result->endTest (test); } + + +// Gets the number of run tests. +inline int SynchronizedTestResult::runTests () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->runTests (); } + + +// Gets the number of detected errors. +inline int SynchronizedTestResult::testErrors () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->testErrors (); } + + +// Gets the number of detected failures. +inline int SynchronizedTestResult::testFailures () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->testFailures (); } + + +// Returns whether the entire test was successful or not. +inline bool SynchronizedTestResult::wasSuccessful () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->wasSuccessful (); } + + +// Marks that the test run should stop. +inline void SynchronizedTestResult::stop () +{ CSingleLock sync (&m_criticalSection, TRUE); m_result->stop (); } + + +// Returns a vector of the errors. +inline vector& SynchronizedTestResult::errors () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->errors (); } + + +// Returns a vector of the failures. +inline vector& SynchronizedTestResult::failures () +{ CSingleLock sync (&m_criticalSection, TRUE); return m_result->failures (); } + + +#endif + + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/TestResultDecorator.h b/contrib/libpoco/CppUnit/WinTestRunner/src/TestResultDecorator.h new file mode 100644 index 00000000000..33ed48db009 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/TestResultDecorator.h @@ -0,0 +1,102 @@ +#ifndef CPP_UNIT_TESTRESULTDECORATOR_H +#define CPP_UNIT_TESTRESULTDECORATOR_H + +#include "TestResult.h" + +class TestResultDecorator +{ +public: + TestResultDecorator (TestResult *result); + virtual ~TestResultDecorator (); + + + virtual bool shouldStop (); + virtual void addError (Test *test, CppUnitException *e); + virtual void addFailure (Test *test, CppUnitException *e); + virtual void startTest (Test *test); + virtual void endTest (Test *test); + virtual int runTests (); + virtual int testErrors (); + virtual int testFailures (); + virtual bool wasSuccessful (); + virtual void stop (); + + vector& errors (); + vector& failures (); + +protected: + TestResult *m_result; +}; + + +inline TestResultDecorator::TestResultDecorator (TestResult *result) +: m_result (result) {} + +inline TestResultDecorator::~TestResultDecorator () +{} + +// Returns whether the test should stop +inline bool TestResultDecorator::shouldStop () +{ return m_result->shouldStop (); } + + +// Adds an error to the list of errors. The passed in exception +// caused the error +inline void TestResultDecorator::addError (Test *test, CppUnitException *e) +{ m_result->addError (test, e); } + + +// Adds a failure to the list of failures. The passed in exception +// caused the failure. +inline void TestResultDecorator::addFailure (Test *test, CppUnitException *e) +{ m_result->addFailure (test, e); } + + +// Informs the result that a test will be started. +inline void TestResultDecorator::startTest (Test *test) +{ m_result->startTest (test); } + + +// Informs the result that a test was completed. +inline void TestResultDecorator::endTest (Test *test) +{ m_result->endTest (test); } + + +// Gets the number of run tests. +inline int TestResultDecorator::runTests () +{ return m_result->runTests (); } + + +// Gets the number of detected errors. +inline int TestResultDecorator::testErrors () +{ return m_result->testErrors (); } + + +// Gets the number of detected failures. +inline int TestResultDecorator::testFailures () +{ return m_result->testFailures (); } + + +// Returns whether the entire test was successful or not. +inline bool TestResultDecorator::wasSuccessful () +{ return m_result->wasSuccessful (); } + + +// Marks that the test run should stop. +inline void TestResultDecorator::stop () +{ m_result->stop (); } + + +// Returns a vector of the errors. +inline vector& TestResultDecorator::errors () +{ return m_result->errors (); } + + +// Returns a vector of the failures. +inline vector& TestResultDecorator::failures () +{ return m_result->failures (); } + + +#endif + + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.cpp new file mode 100644 index 00000000000..b8aca676a84 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.cpp @@ -0,0 +1,439 @@ +// +// TestRunnerDlg.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/TestRunnerDlg.cpp#1 $ +// + + +#include +#include +#include +#include +#include "TestRunnerDlg.h" +#include "ActiveTest.h" +#include "GUITestResult.h" +#include "ProgressBar.h" +#include "CppUnit/TestSuite.h" +#include "TestRunnerDlg.h" + + +namespace CppUnit { + + +TestRunnerDlg::TestRunnerDlg(CWnd* pParent): CDialog(TestRunnerDlg::IDD, pParent) +{ + //{{AFX_DATA_INIT(TestRunnerDlg) + // NOTE: the ClassWizard will add member initialization here + //}}AFX_DATA_INIT + + _testsProgress = 0; + _selectedTest = 0; + _currentTest = 0; +} + + +void TestRunnerDlg::DoDataExchange(CDataExchange* pDX) +{ + CDialog::DoDataExchange(pDX); + //{{AFX_DATA_MAP(TestRunnerDlg) + // NOTE: the ClassWizard will add DDX and DDV calls here + //}}AFX_DATA_MAP +} + + +BEGIN_MESSAGE_MAP(TestRunnerDlg, CDialog) + //{{AFX_MSG_MAP(TestRunnerDlg) + ON_BN_CLICKED(ID_RUN, OnRun) + ON_BN_CLICKED(ID_STOP, OnStop) + ON_CBN_SELCHANGE(IDC_COMBO_TEST, OnSelchangeComboTest) + ON_BN_CLICKED(IDC_CHK_AUTORUN, OnBnClickedAutorun) + ON_WM_PAINT() + //}}AFX_MSG_MAP +END_MESSAGE_MAP() + + +BOOL TestRunnerDlg::OnInitDialog() +{ + CDialog::OnInitDialog(); + + CListCtrl *listCtrl = (CListCtrl *)GetDlgItem (IDC_LIST); + CComboBox *comboBox = (CComboBox *)GetDlgItem (IDC_COMBO_TEST); + + ASSERT (listCtrl); + ASSERT (comboBox); + + CString title; + GetWindowText(title); +#if defined(_DEBUG) + title.Append(" [debug]"); +#else + title.Append(" [release]"); +#endif + SetWindowText(title); + + listCtrl->InsertColumn (0,"Type", LVCFMT_LEFT, 16 + listCtrl->GetStringWidth ("Type"), 1); + listCtrl->InsertColumn (1,"Name", LVCFMT_LEFT, 16 * listCtrl->GetStringWidth ("X"), 2); + listCtrl->InsertColumn (2,"Failed Condition", LVCFMT_LEFT, 24 * listCtrl->GetStringWidth ("M"), 3); + listCtrl->InsertColumn (3,"Line", LVCFMT_LEFT, 16 + listCtrl->GetStringWidth ("0000"), 4); + listCtrl->InsertColumn (4,"File Name", LVCFMT_LEFT, 36 * listCtrl->GetStringWidth ("M"), 5); + + int numberOfCases = 0; + + CWinApp* pApp = AfxGetApp(); + CString lastTestCS = pApp->GetProfileString("Tests", "lastTest"); + std::string lastTest((LPCSTR) lastTestCS); + int sel = -1; + for (std::vector::iterator it = _tests.begin (); it != _tests.end (); ++it) + { + std::string cbName(it->level*4, ' '); + cbName.append(it->pTest->toString()); + comboBox->AddString (cbName.c_str ()); + if (sel < 0) + { + if (lastTest.empty() || lastTest == it->pTest->toString()) + { + _selectedTest = it->pTest; + sel = numberOfCases; + } + } + numberOfCases++; + } + + if (numberOfCases > 0) + { + if (sel < 0) + { + _selectedTest = _tests[0].pTest; + sel = 0; + } + comboBox->SetCurSel (sel); + } + else + { + beRunDisabled (); + } + CWnd *pProgress = GetDlgItem(IDC_PROGRESS); + CRect rect; + pProgress->GetWindowRect(&rect); + _testsProgress = new ProgressBar (this, rect); + + CButton* autoRunBtn = (CButton*) GetDlgItem(IDC_CHK_AUTORUN); + autoRunBtn->SetCheck(pApp->GetProfileInt("Tests", "autoRun", BST_UNCHECKED)); + + reset (); + + if (autoRunBtn->GetCheck() == BST_CHECKED) + { + OnRun(); + } + + return TRUE; // return TRUE unless you set the focus to a control + // EXCEPTION: OCX Property Pages should return FALSE +} + + +TestRunnerDlg::~TestRunnerDlg () +{ + freeState (); + delete _testsProgress; +} + + +void TestRunnerDlg::OnRun() +{ + if (_selectedTest == 0) + return; + + freeState (); + reset (); + + beRunning (); + + int numberOfTests = _selectedTest->countTestCases (); + + _testsProgress->start (numberOfTests); + + _result = new GUITestResult ((TestRunnerDlg *)this); + _activeTest = new ActiveTest (_selectedTest); + + _testStartTime = timeGetTime (); + + _activeTest->run (_result); + + _testEndTime = timeGetTime (); + +} + + +void TestRunnerDlg::addListEntry(const std::string& type, TestResult *result, Test *test, CppUnitException *e) +{ + char stage [80]; + LV_ITEM lvi; + CListCtrl *listCtrl = (CListCtrl *)GetDlgItem (IDC_LIST); + int currentEntry = result->testErrors () + result->testFailures () -1; + + sprintf (stage, "%s", type.c_str ()); + + lvi.mask = LVIF_TEXT; + lvi.iItem = currentEntry; + lvi.iSubItem = 0; + lvi.pszText = stage; + lvi.iImage = 0; + lvi.stateMask = 0; + lvi.state = 0; + + listCtrl->InsertItem (&lvi); + + // Set class string + listCtrl->SetItemText (currentEntry, 1, test->toString ().c_str ()); + + // Set the asserted text + listCtrl->SetItemText(currentEntry, 2, e->what ()); + + // Set the line number + if (e->lineNumber () == CppUnitException::CPPUNIT_UNKNOWNLINENUMBER) + sprintf (stage, ""); + else + sprintf (stage, "%ld", e->lineNumber ()); + + listCtrl->SetItemText(currentEntry, 3, stage); + + // Set the file name + listCtrl->SetItemText(currentEntry, 4, e->fileName ().c_str ()); + + listCtrl->RedrawItems (currentEntry, currentEntry); + listCtrl->UpdateWindow (); + +} + + +void TestRunnerDlg::addError (TestResult *result, Test *test, CppUnitException *e) +{ + addListEntry ("Error", result, test, e); + _errors++; + + _currentTest = 0; + updateCountsDisplay (); + +} + + +void TestRunnerDlg::addFailure (TestResult *result, Test *test, CppUnitException *e) +{ + addListEntry ("Failure", result, test, e); + _failures++; + + _currentTest = 0; + updateCountsDisplay (); + +} + + +void TestRunnerDlg::startTest(Test* test) +{ + _currentTest = test; + updateCountsDisplay(); +} + + +void TestRunnerDlg::endTest (TestResult *result, Test *test) +{ + if (_selectedTest == 0) + return; + _currentTest = 0; + + _testsRun++; + updateCountsDisplay (); + _testsProgress->step (_failures == 0 && _errors == 0); + + _testEndTime = timeGetTime (); + + updateCountsDisplay (); + + if (_testsRun >= _selectedTest->countTestCases ()) + beIdle (); +} + + +void TestRunnerDlg::beRunning () +{ + CButton *runButton = (CButton *)GetDlgItem (ID_RUN); + CButton *closeButton = (CButton *)GetDlgItem (IDOK); + + runButton->EnableWindow (FALSE); + closeButton->EnableWindow (FALSE); + +} + + +void TestRunnerDlg::beIdle () +{ + CButton *runButton = (CButton *)GetDlgItem (ID_RUN); + CButton *closeButton = (CButton *)GetDlgItem (IDOK); + + runButton->EnableWindow (TRUE); + closeButton->EnableWindow (TRUE); + +} + + +void TestRunnerDlg::beRunDisabled () +{ + CButton *runButton = (CButton *)GetDlgItem (ID_RUN); + CButton *closeButton = (CButton *)GetDlgItem (IDOK); + CButton *stopButton = (CButton *)GetDlgItem (ID_STOP); + + runButton->EnableWindow (FALSE); + stopButton->EnableWindow (FALSE); + closeButton->EnableWindow (TRUE); + +} + + +void TestRunnerDlg::freeState () +{ + delete _activeTest; + delete _result; + +} + + +void TestRunnerDlg::reset () +{ + _testsRun = 0; + _errors = 0; + _failures = 0; + _testEndTime = _testStartTime; + + updateCountsDisplay (); + + _activeTest = 0; + _result = 0; + + CListCtrl *listCtrl = (CListCtrl *)GetDlgItem (IDC_LIST); + + listCtrl->DeleteAllItems (); + _testsProgress->reset (); + +} + + +void TestRunnerDlg::updateCountsDisplay () +{ + CStatic *statTestsRun = (CStatic *)GetDlgItem (IDC_STATIC_RUNS); + CStatic *statErrors = (CStatic *)GetDlgItem (IDC_STATIC_ERRORS); + CStatic *statFailures = (CStatic *)GetDlgItem (IDC_STATIC_FAILURES); + CEdit *editTime = (CEdit *)GetDlgItem (IDC_EDIT_TIME); + + CString argumentString; + + argumentString.Format ("%d", _testsRun); + statTestsRun ->SetWindowText (argumentString); + + argumentString.Format ("%d", _errors); + statErrors ->SetWindowText (argumentString); + + argumentString.Format ("%d", _failures); + statFailures ->SetWindowText (argumentString); + + if (_currentTest) + argumentString.Format ("Execution Time: %3.3lf seconds, Current Test: %s", (_testEndTime - _testStartTime) / 1000.0, _currentTest->toString().c_str()); + else + argumentString.Format ("Execution Time: %3.3lf seconds", (_testEndTime - _testStartTime) / 1000.0); + + editTime ->SetWindowText (argumentString); + + +} + + +void TestRunnerDlg::OnStop() +{ + if (_result) + _result->stop (); + + beIdle (); + +} + + +void TestRunnerDlg::OnOK() +{ + if (_result) + _result->stop (); + + CDialog::OnOK (); +} + + +void TestRunnerDlg::OnSelchangeComboTest() +{ + CComboBox *testsSelection = (CComboBox *)GetDlgItem (IDC_COMBO_TEST); + + int currentSelection = testsSelection->GetCurSel (); + + if (currentSelection >= 0 && currentSelection < _tests.size ()) + { + _selectedTest = (_tests.begin () + currentSelection)->pTest; + beIdle (); + CWinApp* pApp = AfxGetApp(); + pApp->WriteProfileString("Tests", "lastTest", _selectedTest->toString().c_str()); + } + else + { + _selectedTest = 0; + beRunDisabled (); + + } + + freeState (); + reset (); + +} + + +void TestRunnerDlg::OnBnClickedAutorun() +{ + CButton *autoRunBtn = (CButton *)GetDlgItem (IDC_CHK_AUTORUN); + CWinApp* pApp = AfxGetApp(); + pApp->WriteProfileInt("Tests", "autoRun", autoRunBtn->GetCheck()); +} + + +void TestRunnerDlg::OnPaint() +{ + CPaintDC dc (this); + + _testsProgress->paint (dc); +} + + +void TestRunnerDlg::setTests(const std::vector& tests) +{ + _tests.clear(); + for (std::vector::const_iterator it = tests.begin(); it != tests.end(); ++it) + { + addTest(*it, 0); + } +} + + +void TestRunnerDlg::addTest(Test* pTest, int level) +{ + TestInfo ti; + ti.pTest = pTest; + ti.level = level; + _tests.push_back(ti); + TestSuite* pSuite = dynamic_cast(pTest); + if (pSuite) + { + const std::vector& tests = pSuite->tests(); + for (std::vector::const_iterator it = tests.begin(); it != tests.end(); ++it) + { + addTest(*it, level + 1); + } + } +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.h b/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.h new file mode 100644 index 00000000000..8357bda2ead --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/TestRunnerDlg.h @@ -0,0 +1,94 @@ +// +// TestRunnerDlg.h +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/TestRunnerDlg.h#1 $ +// + + +#ifndef TestRunnerDlg_INCLUDED +#define TestRunnerDlg_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/CppUnitException.h" +#include "ActiveTest.h" +#include +#include "../res/Resource.h" +#include +#include "afxwin.h" + + +namespace CppUnit { + + +class ProgressBar; + + +class TestRunnerDlg: public CDialog +{ +public: + TestRunnerDlg(CWnd* pParent = NULL); + ~TestRunnerDlg(); + + void setTests(const std::vector& tests); + + void addError(TestResult* result, Test* test, CppUnitException* e); + void addFailure(TestResult* result, Test* test, CppUnitException* e); + void startTest(Test* test); + void endTest(TestResult* result, Test* test); + + //{{AFX_DATA(TestRunnerDlg) + enum { IDD = IDD_DIALOG_TESTRUNNER }; + // NOTE: the ClassWizard will add data members here + //}}AFX_DATA + + //{{AFX_VIRTUAL(TestRunnerDlg) + protected: + virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support + //}}AFX_VIRTUAL + +protected: + //{{AFX_MSG(TestRunnerDlg) + virtual BOOL OnInitDialog(); + afx_msg void OnRun(); + afx_msg void OnStop(); + virtual void OnOK(); + afx_msg void OnSelchangeComboTest(); + afx_msg void OnBnClickedAutorun(); + afx_msg void OnPaint(); + //}}AFX_MSG + DECLARE_MESSAGE_MAP() + + void addListEntry(const std::string& type, TestResult* result, Test* test, CppUnitException* e); + void beIdle(); + void beRunning(); + void beRunDisabled(); + void reset(); + void freeState(); + void updateCountsDisplay(); + void addTest(Test* pTest, int level); + + struct TestInfo + { + Test* pTest; + int level; + }; + std::vector _tests; + ProgressBar* _testsProgress; + Test* _selectedTest; + ActiveTest* _activeTest; + TestResult* _result; + int _testsRun; + int _errors; + int _failures; + DWORD _testStartTime; + DWORD _testEndTime; + Test* _currentTest; +}; + + +} // namespace CppUnit + + +#endif // TestRunnerDlg_INCLUDED + diff --git a/contrib/libpoco/CppUnit/WinTestRunner/src/WinTestRunner.cpp b/contrib/libpoco/CppUnit/WinTestRunner/src/WinTestRunner.cpp new file mode 100644 index 00000000000..cc5ff5721d4 --- /dev/null +++ b/contrib/libpoco/CppUnit/WinTestRunner/src/WinTestRunner.cpp @@ -0,0 +1,108 @@ +// +// WinTestRunner.cpp +// +// $Id: //poco/1.4/CppUnit/WinTestRunner/src/WinTestRunner.cpp#1 $ +// + + +#include "WinTestRunner/WinTestRunner.h" +#include "TestRunnerDlg.h" +#include "CppUnit/TestRunner.h" +#include + + +namespace CppUnit { + + +WinTestRunner::WinTestRunner() +{ +} + + +WinTestRunner::~WinTestRunner() +{ + for (std::vector::iterator it = _tests.begin(); it != _tests.end(); ++it) + delete *it; +} + + +void WinTestRunner::run() +{ + // Note: The following code is some evil hack to + // add batch capability to the MFC based WinTestRunner. + + std::string cmdLine(AfxGetApp()->m_lpCmdLine); + if (cmdLine.size() >= 2 && cmdLine[0] == '/' && (cmdLine[1] == 'b' || cmdLine[1] == 'B')) + { + TestRunner runner; + for (std::vector::iterator it = _tests.begin(); it != _tests.end(); ++it) + runner.addTest((*it)->toString(), *it); + _tests.clear(); + std::vector args; + args.push_back("WinTestRunner"); + args.push_back("-all"); + bool success = runner.run(args); + ExitProcess(success ? 0 : 1); + } + else + { + // We're running in interactive mode. + TestRunnerDlg dlg; + dlg.setTests(_tests); + dlg.DoModal(); + } +} + + +void WinTestRunner::addTest(Test* pTest) +{ + _tests.push_back(pTest); +} + + +BEGIN_MESSAGE_MAP(WinTestRunnerApp, CWinApp) +END_MESSAGE_MAP() + + +BOOL WinTestRunnerApp::InitInstance() +{ + std::string cmdLine(AfxGetApp()->m_lpCmdLine); + if (cmdLine.size() >= 2 && cmdLine[0] == '/' && (cmdLine[1] == 'b' || cmdLine[1] == 'B')) + { + // We're running in batch mode. + std::string outPath; + if (cmdLine.size() > 4 && cmdLine[2] == ':') + { + outPath = cmdLine.substr(3); + } + else + { + char buffer[1024]; + GetModuleFileName(NULL, buffer, sizeof(buffer)); + outPath = buffer; + outPath += ".out"; + } + freopen(outPath.c_str(), "w", stdout); + freopen(outPath.c_str(), "w", stderr); + TestMain(); + } + else + { + AllocConsole(); + SetConsoleTitle("CppUnit WinTestRunner Console"); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); + freopen("CONIN$", "r", stdin); + TestMain(); + FreeConsole(); + } + return FALSE; +} + + +void WinTestRunnerApp::TestMain() +{ +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/doc/README.html b/contrib/libpoco/CppUnit/doc/README.html new file mode 100644 index 00000000000..5f8a1418a1b --- /dev/null +++ b/contrib/libpoco/CppUnit/doc/README.html @@ -0,0 +1,75 @@ + + + + +CppUnit 1.5 + + + + +

+

CppUnit 1.5

+

Last Revision: 12/15/99 - Michael Feathers (mfeathers@acm.org) - written in standard C++, tested under Microsoft Visual C++ 6.0

+


+

Background

+

CppUnit is a simple unit test framework for C++. It is a port from JUnit, a testing framework for Java, developed by Kent Beck and Erich Gamma.

+

Contents

+
README.html                     this file
+    
+    test                        the source code
+        framework               the testing framework
+		extensions	some framework extension classes 
+        textui                  a command line interface to run tests 
+    ms                          code for a Microsoft specific TestRunner
+    samples                     some example test cases and extensions to the framework
+        multicaster             a sample illustrating a publish/subscribe 
+				multicaster under test
+    doc                         documentation
+

Installation

+

To use the test framework, create a makefile or load all files in test\framework into your IDE. In this incarnation of CppUnit, all includes assume the current directory first. A makefile or project can be used to resolve the dependencies.

+

The directory test\textui contains a simple command line example that uses the framework.

+

Documentation

+

CppUnit comes with the following documentation:

+ +
    +
  • a cookbook: doc\cookbook.htm
  • +
  • this file
+ +

Samples

+

You can find several sample test cases in the samples directory:

+ +
    +
  • ExampleTestCase - some simple tests
  • +
  • Multicaster - test cases for a sample publish/subscribe multicaster class
+ +

Also, the wiki page http://c2.com/cgi/wiki?ClassHierarchyTestingInCppUnit shows how to automatically apply tests of classes to the classes' subclasses.

+ +

Extensions

+

You can find several classes that illustrate framework extensions in the extensions directory:

+ +
    +
  • TestDecorator - A Decorator for Test. You can use it as the base class for decorators that extend test cases.
  • +
  • TestSetup - A Decorator that can be used to set up and tear down additional fixture state. Subclass TestSetup and insert it into your tests when you want to set up additional state once before the test is run.
  • +
  • Orthodox - a template class which can be used to verify operations on an arbitrary class.
+ + +

Notes

+

Porting this framework has been fun. I've tried to maintain the spirit and utility of JUnit in a C++ environment. Naturally, the move from Java to standard C++ forces out several nice JUnit features:

+
    + +
  1. Platform independent GUI.
  2. +
  3. Stack traces of test failures
  4. +
  5. Active (threaded) tests
  6. +
  7. Direct invocation of test cases via reflection
  8. +
  9. Run-time loading of new tests
+ +

In addition, the lack of garbage collection in C++ requires some careful use of the framework classes. In particular, TestSuites are composites that manage the lifetime of any tests added to them. Holding onto a TestResult past the lifetime of the tests which filled it is a bad idea. This is because TestResults hold TestFailures and TestFailures hold pointers to the Tests that generated them.

+

On the plus side, we can use the C++ macro preprocessor to get the exact line at which a failure occurs, along with the actual text inside the assert () call that detected the failure. The features of C++ that enable this are the __LINE__ and __FILE__ preprocessor definitions, along with the stringizing operator. If you find that generating this much literal text bulks up your test executables, you can use the CPP_UNIT_SOURCEANNOT define to disable that portion of the reporting.

+

Note: If you use the C++ macro "assert ()" in your code, or include assert.h, you may have a name clash with CppUnit's assert macro. This can be remedied by changing the name of the macro to "cu_assert ()" in TestCase.h.

+

I'd like to thank Kent Beck and Erich Gamma for the inspiration, design, and a wonderful cookbook that was easily/shamelessly mutated to describe CppUnit. Double thanks to Erich for thinking up a way to implement TestCaller. Additional thanks to Kent, Ward Cunningham, Ron Jeffries, Martin Fowler, and several other netizens of the WikiWikiWeb. I don't think any other bunch of people could have convinced me that rapid development with unit tests can be both effective and easy.

+

Thanks also to Fred Huls for mentioning the idea of template-based testing. The orthodox template class demonstrates only a small part of what can be done with templated test cases.

+

History Of Changes

+

1.2 -- Added the TestCaller template class. There is now no need to use the CPP_UNIT_TESTCASEDISPATCH macro unless you are using a C++ compiler which does not support templates well. CPP_UNIT_TESTCASEDISPATCH remains in TestCase.h for backward compatibility. I've also kept the use of the macro in the Multicaster sample to leave in an example.

+

1.3 -- Retired the CPP_UNIT_TESTCASEDISPATCH macro and cleaned up the include structure. Fixed bug in the textui version.

+

1.4 -- Removed using directives for std in CppUnit headers. Merged the old AssertionFailedError into CppUnitException. Fixed a memory leak in the TestRunner class of the MS GUI TestRunner. Removed CppUnit.h file. Now headers for each class must be included directly.

+

1.5 -- Upgraded projects from VC++ 5.0 to 6.0.

diff --git a/contrib/libpoco/CppUnit/doc/cookbook.htm b/contrib/libpoco/CppUnit/doc/cookbook.htm new file mode 100644 index 00000000000..08224883be9 --- /dev/null +++ b/contrib/libpoco/CppUnit/doc/cookbook.htm @@ -0,0 +1,164 @@ + + + + +CppUnit Cookbook + + + + +

+

CppUnit Cookbook

+

Here is a short cookbook to help you get started.

+

Simple Test Case

+

You want to know whether your code is working. How do you do it? There are many ways. Stepping through a debugger or littering your code with stream output calls are two of the simpler ways, but they both have drawbacks. Stepping through your code is a good idea, but it is not automatic. You have to do it every time you make changes. Streaming out text is also fine, but it makes code ugly and it generates far more information than you need most of the time.

+

Tests in CppUnit can be run automatically. They are easy to set up and once you have written them, they are always there to help you keep confidence in the quality of your code.

+

To make a simple test, here is what you do:

+

Subclass the TestCase class. Override the method "runTest ()". When you want to check a value, call "assert (bool)" and pass in an expression that is true if the test succeeds.

+

For example, to test the equality comparison for a Complex number class, write:

+
	class ComplexNumberTest : public TestCase { 
+	public: 
+                    ComplexNumberTest (string name) : TestCase (name) {}
+        void        runTest () {
+                        assert (Complex (10, 1) == Complex (10, 1));
+                        assert (!(Complex (1, 1) == Complex (2, 2)));
+                    }
+        };
+

That was a very simple test. Ordinarily, you'll have many little test cases that you'll want to run on the same set of objects. To do this, use a fixture.

+

 

+

Fixture

+

A fixture is a known set of objects that serves as a base for a set of test cases. Fixtures come in very handy when you are testing as you develop. Let's try out this style of development and learn about fixtures along the away. Suppose that we are really developing a complex number class. Let's start by defining a empty class named Complex.

+
	class Complex {}; 
+

Now create an instance of ComplexNumberTest above, compile the code and see what happens. The first thing we notice is a few compiler errors. The test uses operator==, but it is not defined. Let's fix that.

+
	bool operator== (const Complex& a, const Complex& b) { return true; }
+

Now compile the test, and run it. This time it compiles but the test fails. We need a bit more to get an operator== working correctly, so we revisit the code.

+
	class Complex { 
+        friend bool operator== (const Complex& a, const Complex& b);
+        double      real, imaginary;
+        public:
+                    Complex ()  {
+                    real = imaginary = 0.0;
+                    }
+        };
+
+        bool operator== (const Complex& a, const Complex& b)
+        { return eq(a.real,b.real) && eq(a.imaginary,b.imaginary); }
+

If we compile now and run our test it will pass.

+

Now we are ready to add new operations and new tests. At this point a fixture would be handy. We would probably be better off when doing our tests if we decided to instantiate three or four complex numbers and reuse them across our tests.

+

Here is how we do it:

+
    + +
  1. Add member variables for each part of the fixture
  2. +
  3. Override "setUp ()" to initialize the variables
  4. +
  5. Override "tearDown ()" to release any permanent resources you allocated in "setUp ()"
+ +
	class ComplexNumberTest : public TestCase  {
+	private:
+        Complex 	*m_10_1, *m_1_1; *m_11_2;
+	protected:
+	void		setUp ()  {
+			    m_10_1 = new Complex (10, 1);
+			    m_1_1  = new Complex (1, 1);
+			    m_11_2  = new Complex (11, 2);  
+                        }
+	void		tearDown ()  {
+			    delete m_10_1, delete m_1_1, delete m_11_2;
+			}
+	};
+

Once we have this fixture, we can add the complex addition test case any any others that we need over the course of our development.

+

 

+

Test Case

+

How do you write and invoke individual tests using a fixture?

+

There are two steps to this process:

+
    + +
  1. Write the test case as a method in the fixture class
  2. +
  3. Create a TestCaller which runs that particular method
+ +

Here is our test case class with a few extra case methods:

+
	class ComplexNumberTest : public TestCase  {
+	private:
+        Complex 	*m_10_1, *m_1_1; *m_11_2;
+	protected:
+	void		setUp ()  {
+			    m_10_1 = new Complex (10, 1);
+			    m_1_1  = new Complex (1, 1);
+			    m_11_2 = new Complex (11, 2);  
+                        }
+	void		tearDown ()  {
+			    delete m_10_1, delete m_1_1, delete m_11_2;
+			}
+	void		testEquality ()  {
+			    assert (*m_10_1 == *m_10_1);
+			    assert (!(*m_10_1 == *m_11_2));
+			}
+	void		testAddition ()  {
+			    assert (*m_10_1 + *m_1_1 == *m_11_2);
+                 	}
+	};
+

Create and run instances for each test case like this:

+
	test = new TestCaller<ComplexNumberTest>("testEquality", ComplexNumberTest::testEquality);
+        test->run (); 
+

The second argument to the test caller constructor is the address of a method on ComplexNumberTest. When the test caller is run, that specific method will be run.

+

Once you have several tests, organize them into a suite.

+

 

+

Suite

+

How do you set up your tests so that you can run them all at once?
+
+CppUnit provides a TestSuite class that runs any number of TestCases together. For example, to run a single test case, you execute:

+
	TestResult result;
+	TestCaller<ComplexNumberTest> test ("testAddition", ComplexNumberTest::testAddition);
+	Test.run (&result);
+

 

+

To create a suite of two or more tests, you do the following:

+
	TestSuite suite;
+	TestResult result;
+	suite.addTest (new TestCaller<ComplexNumberTest>("testEquality", ComplexNumberTest::testEquality));
+	suite.addTest (new TestCaller<ComplexNumberTest>("testAddition", ComplexNumberTest::testAddition));
+	suite.run (&result);
+           
+

TestSuites don't only have to contain callers for TestCases. They can contain any object that implements the Test interface. For example, you can create a TestSuite in your code and I can create one in mine, and we can run them together by creating a TestSuite that contains both:

+
	TestSuite suite;
+	suite.addTest (ComplexNumberTest.suite ());
+	suite.addTest (SurrealNumberTest.suite ());
+	suite.run (&result);
+

 

+

TestRunner

+

How do you run your tests and collect their results?

+

Once you have a test suite, you'll want to run it. CppUnit provides tools to define the suite to be run and to display its results. You make your suite accessible to a TestRunner program with a static method suite that returns a test suite.
+For example, to make a ComplexNumberTest suite available to a TestRunner, add the following code to ComplexNumberTest:

+
	public: static Test *suite ()  {
+	    TestSuite *suiteOfTests = new TestSuite;
+	    suiteOfTests->addTest (new TestCaller<ComplexNumberTest>("testEquality", testEquality));
+	    suiteOfTests->addTest (new TestCaller<ComplexNumberTest>("testAddition", testAddition));
+            return suiteOfTests;
+	}
+

CppUnit provides both a textual version of a TestRunner tool, and a Micosoft Visual C++ 5.0 graphical version. If you are running on another platform, take a look at the graphical version. It is easy to port.

+

To use the text version, include the header file for the test in TestRunner.cpp:

+
	#include "ExampleTestCase.h"
+	#include "ComplexNumberTest.h"
+

And add a call to "addTest (string, Test *) in the "main ()" function:

+
	int main (int ac, char **av)  {
+	    TestRunner runner;
+	    runner.addTest (ExampleTestCase::suite ());
+	    runner.addTest (ComplexNumberTest::suite ());
+	    runner.run ();
+	    return 0;
+	}
+

The TestRunner will run the tests. If all the tests pass, you'll get an informative message. If any fail, you'll get the following information:

+
    + +
  1. The name of the test case that failed
  2. +
  3. The name of the source file that contains the test
  4. +
  5. The line number where the failure occurred
  6. +
  7. All of the text inside the call to assert which detected the failure
+ +

CppUnit distinguishes between failures and errors. A failure is anticipated and checked for with assertions. Errors are unanticipated problems like division by zero and other exceptions thrown by the C++ runtime or your code.

+

If you are running MS Developer's Studio, you can build the GUI version rather easily. There are three projects: culib, TestRunner, and HostApp. They make a static library for the framework, a dialog based TestRunner in a DLL and an example Hosting application, respectively. To incorporate a TestRunner in an application you are developing, link with the static library and the TestRunner DLL. Note that the TestRunner DLL must be in the home directory of your application, the system directory or the path. In your application, create an instance of TestRunnerDlg whenever you want to run tests. Pass tests you want to run to the dialog object and then execute.

+

Here is a screen shot of the TestRunner in use:

+

+

 

+

More notes about the implementation of CppUnit can be found in README.HTML.

+

 

+

 

+ diff --git a/contrib/libpoco/CppUnit/doc/license.htm b/contrib/libpoco/CppUnit/doc/license.htm new file mode 100644 index 00000000000..951a688b58f --- /dev/null +++ b/contrib/libpoco/CppUnit/doc/license.htm @@ -0,0 +1,15 @@ + + + + +License Agreement + + + + +

Permission to reproduce and create derivative works from the Software ("Software Derivative Works") is hereby granted to you under the copyright of Michael Feathers.  Michael Feathers also grants you the right to distribute the Software and Software Derivative Works.

+

Michael Feathers licenses the Software to you on an "AS IS" basis, without warranty of any kind. Michael Feathers HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, NON INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.  You are solely responsible for determining the appropriateness of using the Software and assume all risks associated with the use and distribution of this Software, including but not limited to the risks of program errors, damage to or loss of data, programs or equipment, and unavailability or interruption of operations.  MICHAEL FEATHERS WILL NOT BE LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF MICHAEL FEATHERS HAD BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  Michael Feathers will not be liable for the loss of, or damage to, your records or data, or any damages claimed by you based on a third party claim.

+

You agree to distribute the Software and any Software Derivatives under a license agreement that: 1) is sufficient to notify all licensees of the Software and Software Derivatives that Michael Feathers assumes no liability for any claim that may arise regarding the Software or Software Derivatives, and 2) that disclaims all warranties, both express and implied, from Michael Feathers regarding the Software and Software Derivatives.  (If you include this Agreement with any distribution of the Software and Software Derivatives you will have meet this requirement).  You agree that you will not delete any copyright notices in the Software.

+

This Agreement is the exclusive statement of your rights in the Software as provided by Michael Feathers.  Except for the licenses granted to you in the second paragraph above, no other licenses are granted hereunder, by estoppel, implication or otherwise.

+ diff --git a/contrib/libpoco/CppUnit/doc/test.gif b/contrib/libpoco/CppUnit/doc/test.gif new file mode 100644 index 00000000000..861cf14b31c Binary files /dev/null and b/contrib/libpoco/CppUnit/doc/test.gif differ diff --git a/contrib/libpoco/CppUnit/include/CppUnit/CppUnit.h b/contrib/libpoco/CppUnit/include/CppUnit/CppUnit.h new file mode 100644 index 00000000000..57583bc8bcc --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/CppUnit.h @@ -0,0 +1,60 @@ +// +// CppUnit.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/CppUnit.h#1 $ +// + + +#ifndef CppUnit_CppUnit_INCLUDED +#define CppUnit_CppUnit_INCLUDED + + +// +// Ensure that POCO_DLL is default unless POCO_STATIC is defined +// +#if defined(_WIN32) && defined(_DLL) + #if !defined(POCO_DLL) && !defined(POCO_STATIC) + #define POCO_DLL + #endif +#endif + + +// +// The following block is the standard way of creating macros which make exporting +// from a DLL simpler. All files within this DLL are compiled with the CppUnit_EXPORTS +// symbol defined on the command line. this symbol should not be defined on any project +// that uses this DLL. This way any other project whose source files include this file see +// CppUnit_API functions as being imported from a DLL, wheras this DLL sees symbols +// defined with this macro as being exported. +// +#if defined(_WIN32) && defined(POCO_DLL) + #if defined(CppUnit_EXPORTS) + #define CppUnit_API __declspec(dllexport) + #else + #define CppUnit_API __declspec(dllimport) + #endif +#endif + + +#if !defined(CppUnit_API) + #if defined (__GNUC__) && (__GNUC__ >= 4) + #define CppUnit_API __attribute__ ((visibility ("default"))) + #else + #define CppUnit_API + #endif +#endif + + +// Turn off some annoying warnings +#ifdef _MSC_VER + #pragma warning(disable:4786) // identifier truncation warning + #pragma warning(disable:4503) // decorated name length exceeded - mainly a problem with STLPort + #pragma warning(disable:4018) // signed/unsigned comparison + #pragma warning(disable:4284) // return type for operator -> is not UDT + #pragma warning(disable:4251) // ... needs to have dll-interface warning + #pragma warning(disable:4273) + #pragma warning(disable:4275) // ... non dll-interface class used as base for dll-interface class +#endif + + +#endif // CppUnit_CppUnit_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/CppUnitException.h b/contrib/libpoco/CppUnit/include/CppUnit/CppUnitException.h new file mode 100644 index 00000000000..27c50a3cb57 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/CppUnitException.h @@ -0,0 +1,141 @@ +// +// CppUnitException.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/CppUnitException.h#1 $ +// + + +#ifndef CppUnit_CppUnitException_INCLUDED +#define CppUnit_CppUnitException_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include +#include + + +namespace CppUnit { + + +class CppUnit_API CppUnitException: public std::exception + /// CppUnitException is an exception that serves + /// descriptive strings through its what() method +{ +public: + CppUnitException(const std::string& message = "", + long lineNumber = CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CPPUNIT_UNKNOWNFILENAME); + CppUnitException(const std::string& message, + long lineNumber, + long data1lineNumber, + const std::string& fileName); + CppUnitException(const std::string& message, + long lineNumber, + long data1lineNumber, + long data2lineNumber, + const std::string& fileName); + CppUnitException(const CppUnitException& other); + virtual ~CppUnitException() throw(); + + CppUnitException& operator = (const CppUnitException& other); + + const char* what() const throw (); + + long lineNumber() const; + long data1LineNumber() const; + long data2LineNumber() const; + const std::string& fileName() const; + + static const std::string CPPUNIT_UNKNOWNFILENAME; + static const int CPPUNIT_UNKNOWNLINENUMBER; + +private: + std::string _message; + long _lineNumber; + long _data1lineNumber; + long _data2lineNumber; + std::string _fileName; +}; + + +inline CppUnitException::CppUnitException(const CppUnitException& other): exception (other) +{ + _message = other._message; + _lineNumber = other._lineNumber; + _data1lineNumber = other._data1lineNumber; + _data2lineNumber = other._data2lineNumber; + _fileName = other._fileName; +} + + +inline CppUnitException::CppUnitException (const std::string& message, long lineNumber, const std::string& fileName): _message(message), _lineNumber(lineNumber), _data1lineNumber(CPPUNIT_UNKNOWNLINENUMBER), _data2lineNumber(CPPUNIT_UNKNOWNLINENUMBER), _fileName(fileName) +{ +} + + +inline CppUnitException::CppUnitException (const std::string& message, long lineNumber, long data1lineNumber, const std::string& fileName): _message(message), _lineNumber(lineNumber), _data1lineNumber(data1lineNumber), _data2lineNumber(CPPUNIT_UNKNOWNLINENUMBER), _fileName(fileName) +{ +} + + +inline CppUnitException::CppUnitException (const std::string& message, long lineNumber, long data1lineNumber, long data2lineNumber, const std::string& fileName): _message(message), _lineNumber(lineNumber), _data1lineNumber(data1lineNumber), _data2lineNumber(data2lineNumber), _fileName(fileName) +{ +} + + +inline CppUnitException::~CppUnitException () throw() +{ +} + + +inline CppUnitException& CppUnitException::operator = (const CppUnitException& other) +{ + exception::operator= (other); + + if (&other != this) + { + _message = other._message; + _lineNumber = other._lineNumber; + _data1lineNumber = other._data1lineNumber; + _data2lineNumber = other._data2lineNumber; + _fileName = other._fileName; + } + return *this; +} + + +inline const char* CppUnitException::what() const throw () +{ + return _message.c_str(); +} + + +inline long CppUnitException::lineNumber() const +{ + return _lineNumber; +} + + +inline long CppUnitException::data1LineNumber() const +{ + return _data1lineNumber; +} + + +inline long CppUnitException::data2LineNumber() const +{ + return _data2lineNumber; +} + + +// The file in which the error occurred +inline const std::string& CppUnitException::fileName() const +{ + return _fileName; +} + + +} // namespace CppUnit + + +#endif // CppUnit_CppUnitException_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/Guards.h b/contrib/libpoco/CppUnit/include/CppUnit/Guards.h new file mode 100644 index 00000000000..ef8a7d6abe9 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/Guards.h @@ -0,0 +1,19 @@ +// +// Guards.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/Guards.h#1 $ +// + + +#ifndef CppUnit_Guards_INCLUDED +#define CppUnit_Guards_INCLUDED + + +// Prevent copy construction and assignment for a class +#define REFERENCEOBJECT(className) \ +private: \ + className(const className& other); \ + className& operator = (const className& other); + + +#endif // CppUnit_Guards_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/Orthodox.h b/contrib/libpoco/CppUnit/include/CppUnit/Orthodox.h new file mode 100644 index 00000000000..3a038975cbe --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/Orthodox.h @@ -0,0 +1,105 @@ +// +// Orthodox.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/Orthodox.h#1 $ +// + + +#ifndef CppUnit_Orthodox_INCLUDED +#define CppUnit_Orthodox_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/TestCase.h" + + +namespace CppUnit { + + +/* + * Orthodox performs a simple set of tests on an arbitary + * class to make sure that it supports at least the + * following operations: + * + * default construction - constructor + * equality/inequality - operator== && operator!= + * assignment - operator= + * negation - operator! + * safe passage - copy construction + * + * If operations for each of these are not declared + * the template will not instantiate. If it does + * instantiate, tests are performed to make sure + * that the operations have correct semantics. + * + * Adding an orthodox test to a suite is very + * easy: + * + * public: Test *suite () { + * TestSuite *suiteOfTests = new TestSuite; + * suiteOfTests->addTest (new ComplexNumberTest ("testAdd"); + * suiteOfTests->addTest (new TestCaller > ()); + * return suiteOfTests; + * } + * + * Templated test cases be very useful when you are want to + * make sure that a group of classes have the same form. + * + * see TestSuite + */ +template +class Orthodox: public TestCase +{ +public: + Orthodox(): TestCase("Orthodox") + { + } + +protected: + ClassUnderTest call(ClassUnderTest object); + void runTest (); +}; + + +// Run an orthodoxy test +template +void Orthodox::runTest() +{ + // make sure we have a default constructor + ClassUnderTest a, b, c; + + // make sure we have an equality operator + assert (a == b); + + // check the inverse + b.operator= (a.operator! ()); + assert (a != b); + + // double inversion + b = !!a; + assert (a == b); + + // invert again + b = !a; + + // check calls + c = a; + assert (c == call (a)); + + c = b; + assert (c == call (b)); +} + + +// Exercise a call +template +ClassUnderTest Orthodox::call(ClassUnderTest object) +{ + return object; +} + + +} // namespace CppUnit + + +#endif // CppUnit_Orthodox_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/RepeatedTest.h b/contrib/libpoco/CppUnit/include/CppUnit/RepeatedTest.h new file mode 100644 index 00000000000..1842762efef --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/RepeatedTest.h @@ -0,0 +1,77 @@ +// +// RepeatedTest.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/RepeatedTest.h#1 $ +// + + +#ifndef CppUnit_RepeatedTest_INCLUDED +#define CppUnit_RepeatedTest_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/TestDecorator.h" + + +namespace CppUnit { + + +class Test; +class TestResult; + + +/* + * A decorator that runs a test repeatedly. + * Does not assume ownership of the test it decorates + * + */ +class CppUnit_API RepeatedTest: public TestDecorator +{ + REFERENCEOBJECT (RepeatedTest) + +public: + RepeatedTest(Test* test, int timesRepeat): TestDecorator (test), _timesRepeat (timesRepeat) + { + } + + int countTestCases(); + std::string toString(); + void run(TestResult *result); + +private: + const int _timesRepeat; +}; + + +// Counts the number of test cases that will be run by this test. +inline RepeatedTest::countTestCases () +{ + return TestDecorator::countTestCases() * _timesRepeat; +} + + +// Returns the name of the test instance. +inline std::string RepeatedTest::toString() +{ + return TestDecorator::toString() + " (repeated)"; +} + + +// Runs a repeated test +inline void RepeatedTest::run(TestResult *result) +{ + for (int n = 0; n < _timesRepeat; n++) + { + if (result->shouldStop()) + break; + + TestDecorator::run(result); + } +} + + +} // namespace CppUnit + + +#endif // CppUnit_RepeatedTest_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/Test.h b/contrib/libpoco/CppUnit/include/CppUnit/Test.h new file mode 100644 index 00000000000..84be1679387 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/Test.h @@ -0,0 +1,65 @@ +// +// Test.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/Test.h#1 $ +// + + +#ifndef CppUnit_Test_INCLUDED +#define CppUnit_Test_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include + + +namespace CppUnit { + + +class TestResult; + + +/* + * A Test can be run and collect its results. + * See TestResult. + * + */ +class CppUnit_API Test +{ +public: + virtual ~Test() = 0; + virtual void run(TestResult* result) = 0; + virtual int countTestCases() = 0; + virtual std::string toString() = 0; +}; + + +inline Test::~Test() +{ +} + + +// Runs a test and collects its result in a TestResult instance. +inline void Test::run(TestResult *result) +{ +} + + +// Counts the number of test cases that will be run by this test. +inline int Test::countTestCases() +{ + return 0; +} + + +// Returns the name of the test instance. +inline std::string Test::toString() +{ + return ""; +} + + +} // namespace CppUnit + + +#endif // CppUnit_Test_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestCaller.h b/contrib/libpoco/CppUnit/include/CppUnit/TestCaller.h new file mode 100644 index 00000000000..b6f7c64b693 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestCaller.h @@ -0,0 +1,95 @@ +// +// TestCaller.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestCaller.h#1 $ +// + + +#ifndef CppUnit_TestCaller_INCLUDED +#define CppUnit_TestCaller_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "Guards.h" +#include "TestCase.h" +#include + + +namespace CppUnit { + + +/* + * A test caller provides access to a test case method + * on a test case class. Test callers are useful when + * you want to run an individual test or add it to a + * suite. + * + * Here is an example: + * + * class MathTest : public TestCase { + * ... + * public: + * void setUp (); + * void tearDown (); + * + * void testAdd (); + * void testSubtract (); + * }; + * + * Test *MathTest::suite () { + * TestSuite *suite = new TestSuite; + * + * suite->addTest (new TestCaller ("testAdd", testAdd)); + * return suite; + * } + * + * You can use a TestCaller to bind any test method on a TestCase + * class, as long as it returns accepts void and returns void. + * + * See TestCase + */ +template +class TestCaller: public TestCase +{ + REFERENCEOBJECT (TestCaller) + + typedef void (Fixture::*TestMethod)(); + +public: + TestCaller(const std::string& name, TestMethod test): + TestCase(name), + _test(test), + _fixture(new Fixture(name)) + { + } + +protected: + void runTest() + { + (_fixture.get()->*_test)(); + } + + void setUp() + { + _fixture.get()->setUp(); + } + + void tearDown() + { + _fixture.get()->tearDown(); + } + +private: + TestMethod _test; + std::unique_ptr _fixture; +}; + + +} // namespace CppUnit + + +#define CppUnit_addTest(suite, cls, mth) \ + suite->addTest(new CppUnit::TestCaller(#mth, &cls::mth)) + + +#endif // CppUnit_TestCaller_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestCase.h b/contrib/libpoco/CppUnit/include/CppUnit/TestCase.h new file mode 100644 index 00000000000..1811d428dc7 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestCase.h @@ -0,0 +1,257 @@ +// +// TestCase.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestCase.h#1 $ +// + + +#ifndef CppUnit_TestCase_INCLUDED +#define CppUnit_TestCase_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/Test.h" +#include "CppUnit/CppUnitException.h" +#include +#include + + +namespace CppUnit { + + +class TestResult; + + +/* + * A test case defines the fixture to run multiple tests. To define a test case + * 1) implement a subclass of TestCase + * 2) define instance variables that store the state of the fixture + * 3) initialize the fixture state by overriding setUp + * 4) clean-up after a test by overriding tearDown. + * + * Each test runs in its own fixture so there + * can be no side effects among test runs. + * Here is an example: + * + * class MathTest : public TestCase { + * protected: int m_value1; + * protected: int m_value2; + * + * public: MathTest (std::string name) + * : TestCase (name) { + * } + * + * protected: void setUp () { + * m_value1 = 2; + * m_value2 = 3; + * } + * } + * + * + * For each test implement a method which interacts + * with the fixture. Verify the expected results with assertions specified + * by calling assert on the expression you want to test: + * + * protected: void testAdd () { + * int result = value1 + value2; + * assert (result == 5); + * } + * + * Once the methods are defined you can run them. To do this, use + * a TestCaller. + * + * Test *test = new TestCaller("testAdd", MathTest::testAdd); + * test->run (); + * + * + * The tests to be run can be collected into a TestSuite. CppUnit provides + * different test runners which can run a test suite and collect the results. + * The test runners expect a static method suite as the entry + * point to get a test to run. + * + * public: static MathTest::suite () { + * TestSuite *suiteOfTests = new TestSuite; + * suiteOfTests->addTest(new TestCaller("testAdd", testAdd)); + * suiteOfTests->addTest(new TestCaller("testDivideByZero", testDivideByZero)); + * return suiteOfTests; + * } + * + * Note that the caller of suite assumes lifetime control + * for the returned suite. + * + * see TestResult, TestSuite and TestCaller + * + */ +class CppUnit_API TestCase: public Test +{ + REFERENCEOBJECT (TestCase) + +public: + TestCase(const std::string& Name); + ~TestCase(); + + virtual void run(TestResult* result); + virtual TestResult* run(); + virtual int countTestCases(); + const std::string& name() const; + std::string toString(); + + virtual void setUp(); + virtual void tearDown(); + +protected: + virtual void runTest(); + TestResult* defaultResult(); + + void assertImplementation(bool condition, + const std::string& conditionExpression = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void loop1assertImplementation(bool condition, + const std::string& conditionExpression = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + long dataLineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void loop2assertImplementation(bool condition, + const std::string& conditionExpression = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + long data1LineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + long data2LineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void assertEquals(long expected, + long actual, + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void assertEquals(double expected, + double actual, + double delta, + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void assertEquals(const std::string& expected, + const std::string& actual, + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void assertEquals(const void* expected, + const void* actual, + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + std::string notEqualsMessage(long expected, long actual); + std::string notEqualsMessage(double expected, double actual); + std::string notEqualsMessage(const void* expected, const void* actual); + std::string notEqualsMessage(const std::string& expected, const std::string& actual); + + void assertNotNull(const void* pointer, + const std::string& pointerExpression = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void assertNull(const void* pointer, + const std::string& pointerExpression = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void fail(const std::string& message = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + void warn(const std::string& message = "", + long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER, + const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME); + + +private: + const std::string _name; +}; + + +// Constructs a test case +inline TestCase::TestCase(const std::string& name): _name (name) +{ +} + + +// Destructs a test case +inline TestCase::~TestCase() +{ +} + + +// Returns a count of all the tests executed +inline int TestCase::countTestCases() +{ + return 1; +} + + +// Returns the name of the test case +inline const std::string& TestCase::name() const +{ + return _name; +} + + +// A hook for fixture set up +inline void TestCase::setUp() +{ +} + + +// A hook for fixture tear down +inline void TestCase::tearDown() +{ +} + + +// Returns the name of the test case instance +inline std::string TestCase::toString() +{ + const std::type_info& thisClass = typeid(*this); + return std::string(thisClass.name()) + "." + name(); +} + + +// A set of macros which allow us to get the line number +// and file name at the point of an error. +// Just goes to show that preprocessors do have some +// redeeming qualities. +#undef assert +#define assert(condition) \ + (this->assertImplementation((condition), (#condition), __LINE__, __FILE__)) + +#define loop_1_assert(data1line, condition) \ + (this->loop1assertImplementation((condition), (#condition), __LINE__, data1line, __FILE__)) + +#define loop_2_assert(data1line, data2line, condition) \ + (this->loop2assertImplementation((condition), (#condition), __LINE__, data1line, data2line, __FILE__)) + +#define assertEqualDelta(expected, actual, delta) \ + (this->assertEquals((expected), (actual), (delta), __LINE__, __FILE__)) + +#define assertEqual(expected, actual) \ + (this->assertEquals((expected), (actual), __LINE__, __FILE__)) + +#define assertNullPtr(ptr) \ + (this->assertNull((ptr), #ptr, __LINE__, __FILE__)) + +#define assertNotNullPtr(ptr) \ + (this->assertNotNull((ptr), #ptr, __LINE__, __FILE__)) + +#define failmsg(msg) \ + (this->fail(msg, __LINE__, __FILE__)) + +#define warnmsg(msg) \ + (this->fail(msg, __LINE__, __FILE__)) + + +} // namespace CppUnit + + +#endif // CppUnit_TestCase_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestDecorator.h b/contrib/libpoco/CppUnit/include/CppUnit/TestDecorator.h new file mode 100644 index 00000000000..ac57c591e4b --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestDecorator.h @@ -0,0 +1,52 @@ +// +// TestDecorator.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestDecorator.h#1 $ +// + + +#ifndef CppUnit_TestDecorator_INCLUDED +#define CppUnit_TestDecorator_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/Test.h" + + +namespace CppUnit { + + +class TestResult; + + +/* + * A Decorator for Tests + * + * Does not assume ownership of the test it decorates + * + */ +class CppUnit_API TestDecorator: public Test +{ + REFERENCEOBJECT(TestDecorator) + +public: + TestDecorator(Test* test); + + virtual ~TestDecorator(); + + int countTestCases(); + + void run(TestResult* result); + + std::string toString(); + +protected: + Test* _test; +}; + + +} // namespace CppUnit + + +#endif // CppUnit_TestDecorator_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestFailure.h b/contrib/libpoco/CppUnit/include/CppUnit/TestFailure.h new file mode 100644 index 00000000000..c1f845d7211 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestFailure.h @@ -0,0 +1,86 @@ +// +// TestFailure.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestFailure.h#1 $ +// + + +#ifndef CppUnit_TestFailure_INCLUDED +#define CppUnit_TestFailure_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/CppUnitException.h" +#include "CppUnit/Guards.h" + + +namespace CppUnit { + + +class Test; + + +/* + * A TestFailure collects a failed test together with + * the caught exception. + * + * TestFailure assumes lifetime control for any exception + * passed to it. The lifetime of tests is handled by + * their TestSuite (if they have been added to one) or + * whomever creates them. + * + * see TestResult + * see TestSuite + * + */ +class CppUnit_API TestFailure +{ + REFERENCEOBJECT (TestFailure) + +public: + TestFailure(Test* failedTest, CppUnitException* thrownException); + ~TestFailure(); + + Test* failedTest(); + CppUnitException* thrownException(); + std::string toString(); + +protected: + Test* _failedTest; + CppUnitException *_thrownException; +}; + + +// Constructs a TestFailure with the given test and exception. +inline TestFailure::TestFailure(Test* failedTest, CppUnitException* thrownException): _failedTest(failedTest), _thrownException(thrownException) +{ +} + + +// Deletes the owned exception. +inline TestFailure::~TestFailure() +{ + delete _thrownException; +} + + +// Gets the failed test. +inline Test* TestFailure::failedTest() +{ + return _failedTest; +} + + +// Gets the thrown exception. +inline CppUnitException* TestFailure::thrownException() +{ + return _thrownException; +} + + +} // namespace CppUnit + + +#endif // CppUnit_TestFailure_INCLUDED + + diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestResult.h b/contrib/libpoco/CppUnit/include/CppUnit/TestResult.h new file mode 100644 index 00000000000..22fbd44c44b --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestResult.h @@ -0,0 +1,231 @@ +// +// TestResult.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestResult.h#1 $ +// + + +#ifndef CppUnit_TestResult_INCLUDED +#define CppUnit_TestResult_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/TestFailure.h" +#include + + +namespace CppUnit { + + +class CppUnitException; +class Test; + + +/* + * A TestResult collects the results of executing a test case. It is an + * instance of the Collecting Parameter pattern. + * + * The test framework distinguishes between failures and errors. + * A failure is anticipated and checked for with assertions. Errors are + * unanticipated problems signified by exceptions that are not generated + * by the framework. + * + * TestResult supplies a template method 'setSynchronizationObject ()' + * so that subclasses can provide mutual exclusion in the face of multiple + * threads. This can be useful when tests execute in one thread and + * they fill a subclass of TestResult which effects change in another + * thread. To have mutual exclusion, override setSynchronizationObject () + * and make sure that you create an instance of ExclusiveZone at the + * beginning of each method. + * + * see Test + */ +class CppUnit_API TestResult +{ + REFERENCEOBJECT (TestResult) + +public: + TestResult(); + virtual ~TestResult(); + + virtual void addError(Test* test, CppUnitException* e); + virtual void addFailure(Test* test, CppUnitException* e); + virtual void startTest(Test* test); + virtual void endTest(Test* test); + virtual int runTests(); + virtual int testErrors(); + virtual int testFailures(); + virtual bool wasSuccessful(); + virtual bool shouldStop(); + virtual void stop(); + + virtual std::vector& errors(); + virtual std::vector& failures(); + + class SynchronizationObject + { + public: + SynchronizationObject() + { + } + + virtual ~SynchronizationObject() + { + } + + virtual void lock() + { + } + + virtual void unlock() + { + } + }; + + class ExclusiveZone + { + SynchronizationObject* m_syncObject; + + public: + ExclusiveZone(SynchronizationObject* syncObject): m_syncObject(syncObject) + { + m_syncObject->lock(); + } + + ~ExclusiveZone() + { + m_syncObject->unlock(); + } + }; + +protected: + virtual void setSynchronizationObject(SynchronizationObject* syncObject); + + std::vector _errors; + std::vector _failures; + int _runTests; + bool _stop; + SynchronizationObject* _syncObject; + +}; + + +// Construct a TestResult +inline TestResult::TestResult(): _syncObject(new SynchronizationObject()) +{ + _runTests = 0; + _stop = false; +} + + +// Adds an error to the list of errors. The passed in exception +// caused the error +inline void TestResult::addError(Test* test, CppUnitException* e) +{ + ExclusiveZone zone(_syncObject); + _errors.push_back(new TestFailure(test, e)); +} + + +// Adds a failure to the list of failures. The passed in exception +// caused the failure. +inline void TestResult::addFailure(Test* test, CppUnitException* e) +{ + ExclusiveZone zone(_syncObject); + _failures.push_back(new TestFailure(test, e)); +} + + +// Informs the result that a test will be started. +inline void TestResult::startTest(Test* test) +{ + ExclusiveZone zone(_syncObject); + _runTests++; +} + + +// Informs the result that a test was completed. +inline void TestResult::endTest(Test* test) +{ + ExclusiveZone zone(_syncObject); +} + + +// Gets the number of run tests. +inline int TestResult::runTests() +{ + ExclusiveZone zone(_syncObject); + return _runTests; +} + + +// Gets the number of detected errors. +inline int TestResult::testErrors() +{ + ExclusiveZone zone(_syncObject); + return (int) _errors.size(); +} + + +// Gets the number of detected failures. +inline int TestResult::testFailures() +{ + ExclusiveZone zone(_syncObject); + return (int) _failures.size(); +} + + +// Returns whether the entire test was successful or not. +inline bool TestResult::wasSuccessful() +{ + ExclusiveZone zone(_syncObject); + return _failures.size() == 0 && _errors.size () == 0; +} + + +// Returns a std::vector of the errors. +inline std::vector& TestResult::errors() +{ + ExclusiveZone zone(_syncObject); + return _errors; +} + + +// Returns a std::vector of the failures. +inline std::vector& TestResult::failures() +{ + ExclusiveZone zone(_syncObject); + return _failures; +} + + +// Returns whether testing should be stopped +inline bool TestResult::shouldStop() +{ + ExclusiveZone zone(_syncObject); + return _stop; +} + + +// Stop testing +inline void TestResult::stop() +{ + ExclusiveZone zone(_syncObject); + _stop = true; +} + + +// Accept a new synchronization object for protection of this instance +// TestResult assumes ownership of the object +inline void TestResult::setSynchronizationObject(SynchronizationObject* syncObject) +{ + delete _syncObject; + _syncObject = syncObject; +} + + +} // namespace CppUnit + + +#endif // CppUnit_TestResult_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestRunner.h b/contrib/libpoco/CppUnit/include/CppUnit/TestRunner.h new file mode 100644 index 00000000000..e4393465901 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestRunner.h @@ -0,0 +1,103 @@ +// +// TestRunner.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestRunner.h#2 $ +// + + +#ifndef CppUnit_TestRunner_INCLUDED +#define CppUnit_TestRunner_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include +#include +#include +#if defined(POCO_VXWORKS) +#include +#endif + + +namespace CppUnit { + + +class Test; + + +/* + * A command line based tool to run tests. + * TestRunner expects as its only argument the name of a TestCase class. + * TestRunner prints out a trace as the tests are executed followed by a + * summary at the end. + * + * You can add to the tests that the TestRunner knows about by + * making additional calls to "addTest (...)" in main. + * + * Here is the synopsis: + * + * TestRunner [-all] [-print] [-wait] ExampleTestCase + * + */ +class CppUnit_API TestRunner +{ + typedef std::pair Mapping; + typedef std::vector Mappings; + +public: + TestRunner(); + TestRunner(std::ostream& ostr); + ~TestRunner(); + + bool run(const std::vector& args); + void addTest(const std::string& name, Test* test); + +protected: + bool run(Test* test); + void printBanner(); + void print(const std::string& name, Test* pTest, int indent); + Test* find(const std::string& name, Test* pTest, const std::string& testName); + +private: + std::ostream& _ostr; + Mappings _mappings; +}; + + +} // namespace CppUnit + + +#if defined(POCO_VXWORKS) +#define CppUnitMain(testCase) \ + int testCase##Runner(const char* arg0, ...) \ + { \ + std::vector args; \ + args.push_back(#testCase "Runner"); \ + args.push_back(std::string(arg0)); \ + va_list vargs; \ + va_start(vargs, arg0); \ + const char* arg = va_arg(vargs, const char*); \ + while (arg) \ + { \ + args.push_back(std::string(arg)); \ + arg = va_arg(vargs, const char*); \ + } \ + va_end(vargs); \ + CppUnit::TestRunner runner; \ + runner.addTest(#testCase, testCase::suite()); \ + return runner.run(args) ? 0 : 1; \ + } +#else +#define CppUnitMain(testCase) \ + int main(int ac, char **av) \ + { \ + std::vector args; \ + for (int i = 0; i < ac; ++i) \ + args.push_back(std::string(av[i])); \ + CppUnit::TestRunner runner; \ + runner.addTest(#testCase, testCase::suite()); \ + return runner.run(args) ? 0 : 1; \ + } +#endif + + +#endif // CppUnit_TestRunner_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestSetup.h b/contrib/libpoco/CppUnit/include/CppUnit/TestSetup.h new file mode 100644 index 00000000000..789a076e526 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestSetup.h @@ -0,0 +1,57 @@ +// +// TestSetup.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestSetup.h#1 $ +// + + +#ifndef CppUnit_TestSetup_INCLUDED +#define CppUnit_TestSetup_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/TestDecorator.h" + + +namespace CppUnit { + + +class Test; +class TestResult; + + +class CppUnit_API TestSetup: public TestDecorator +{ + REFERENCEOBJECT (TestSetup) + +public: + TestSetup(Test* test): TestDecorator(test) + { + } + + void run(TestResult* result); + +protected: + void setUp() + { + } + + void tearDown() + { + } +}; + + +inline void TestSetup::run(TestResult* result) +{ + setUp(); + TestDecorator::run(result); + tearDown(); +} + + +} // namespace CppUnit + + +#endif // CppUnit_TestSetup_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TestSuite.h b/contrib/libpoco/CppUnit/include/CppUnit/TestSuite.h new file mode 100644 index 00000000000..197cf2747fa --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TestSuite.h @@ -0,0 +1,98 @@ +// +// TestSuite.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TestSuite.h#1 $ +// + + +#ifndef CppUnit_TestSuite_INCLUDED +#define CppUnit_TestSuite_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/Guards.h" +#include "CppUnit/Test.h" +#include +#include + + +namespace CppUnit { + + +class TestResult; + + +/* + * A TestSuite is a Composite of Tests. + * It runs a collection of test cases. Here is an example. + * + * TestSuite *suite= new TestSuite(); + * suite->addTest(new TestCaller ("testAdd", testAdd)); + * suite->addTest(new TestCaller ("testDivideByZero", testDivideByZero)); + * + * Note that TestSuites assume lifetime + * control for any tests added to them. + * + * see Test and TestCaller + */ +class CppUnit_API TestSuite: public Test +{ + REFERENCEOBJECT (TestSuite) + +public: + TestSuite(const std::string& name = ""); + ~TestSuite(); + + void run(TestResult* result); + int countTestCases(); + void addTest(Test* test); + std::string toString(); + + virtual void deleteContents(); + + const std::vector tests() const; + +private: + std::vector _tests; + const std::string _name; +}; + + +// Default constructor +inline TestSuite::TestSuite(const std::string& name): _name(name) +{ +} + + +// Destructor +inline TestSuite::~TestSuite() +{ + deleteContents(); +} + + +// Adds a test to the suite. +inline void TestSuite::addTest(Test* test) +{ + _tests.push_back(test); +} + + +// Returns a std::string representation of the test suite. +inline std::string TestSuite::toString() +{ + return "suite " + _name; +} + + +// Returns all tests +inline const std::vector TestSuite::tests() const +{ + return _tests; +} + + +} // namespace CppUnit + + +#endif // CppUnit_TestSuite_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/TextTestResult.h b/contrib/libpoco/CppUnit/include/CppUnit/TextTestResult.h new file mode 100644 index 00000000000..d3b191fd913 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/TextTestResult.h @@ -0,0 +1,56 @@ +// +// TextTestResult.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/TextTestResult.h#1 $ +// + + +#ifndef CppUnit_TextTestResult_INCLUDED +#define CppUnit_TextTestResult_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include "CppUnit/TestResult.h" +#include +#include + + +namespace CppUnit { + + +class CppUnit_API TextTestResult: public TestResult +{ +public: + TextTestResult(); + TextTestResult(std::ostream& ostr); + + virtual void addError(Test* test, CppUnitException* e); + virtual void addFailure(Test* test, CppUnitException* e); + virtual void startTest(Test* test); + virtual void print(std::ostream& stream); + virtual void printErrors(std::ostream& stream); + virtual void printFailures(std::ostream& stream); + virtual void printHeader(std::ostream& stream); + +protected: + std::string shortName(const std::string& testName); + void setup(); + +private: + std::ostream& _ostr; + std::set _ignored; +}; + + +/* insertion operator for easy output */ +inline std::ostream& operator<< (std::ostream& stream, TextTestResult& result) +{ + result.print(stream); + return stream; +} + + +} // namespace CppUnit + + +#endif // CppUnit_TextTestResult_INCLUDED diff --git a/contrib/libpoco/CppUnit/include/CppUnit/estring.h b/contrib/libpoco/CppUnit/include/CppUnit/estring.h new file mode 100644 index 00000000000..cc3fe99d941 --- /dev/null +++ b/contrib/libpoco/CppUnit/include/CppUnit/estring.h @@ -0,0 +1,73 @@ +// +// estring.h +// +// $Id: //poco/1.4/CppUnit/include/CppUnit/estring.h#1 $ +// + + +#ifndef CppUnit_estring_INCLUDED +#define CppUnit_estring_INCLUDED + + +#include "CppUnit/CppUnit.h" +#include +#include + + +namespace CppUnit { + + +// Create a std::string from a const char pointer +inline std::string estring(const char *cstring) +{ + return std::string(cstring); +} + + +// Create a std::string from a std::string (for uniformities' sake) +inline std::string estring(std::string& expandedString) +{ + return expandedString; +} + + +// Create a std::string from an int +inline std::string estring(int number) +{ + char buffer[50]; + sprintf(buffer, "%d", number); + return std::string (buffer); +} + + +// Create a string from a long +inline std::string estring(long number) +{ + char buffer[50]; + sprintf(buffer, "%ld", number); + return std::string (buffer); +} + + +// Create a std::string from a double +inline std::string estring(double number) +{ + char buffer[50]; + sprintf(buffer, "%lf", number); + return std::string(buffer); +} + + +// Create a std::string from a double +inline std::string estring(const void* ptr) +{ + char buffer[50]; + sprintf(buffer, "%p", ptr); + return std::string(buffer); +} + + +} // namespace CppUnit + + +#endif // CppUnit_estring_INCLUDED diff --git a/contrib/libpoco/CppUnit/src/CppUnitException.cpp b/contrib/libpoco/CppUnit/src/CppUnitException.cpp new file mode 100644 index 00000000000..fa4edfcaf9c --- /dev/null +++ b/contrib/libpoco/CppUnit/src/CppUnitException.cpp @@ -0,0 +1,18 @@ +// +// CppUnitException.cpp +// +// $Id: //poco/1.4/CppUnit/src/CppUnitException.cpp#1 $ +// + + +#include "CppUnit/CppUnitException.h" + + +namespace CppUnit { + + +const std::string CppUnitException::CPPUNIT_UNKNOWNFILENAME = ""; +const int CppUnitException::CPPUNIT_UNKNOWNLINENUMBER = -1; + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestCase.cpp b/contrib/libpoco/CppUnit/src/TestCase.cpp new file mode 100644 index 00000000000..0e8669d9725 --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestCase.cpp @@ -0,0 +1,189 @@ +// +// TestCase.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestCase.cpp#1 $ +// + + +#include +#include +#include "CppUnit/TestCase.h" +#include "CppUnit/TestResult.h" +#include "CppUnit/estring.h" +#include +#include + + +using namespace std; + + +namespace CppUnit { + + +// Create a default TestResult +TestResult* TestCase::defaultResult() +{ + return new TestResult; +} + + +// Check for a failed general assertion +void TestCase::assertImplementation(bool condition, const std::string& conditionExpression, long lineNumber, const std::string& fileName) +{ + if (!condition) + throw CppUnitException(conditionExpression, lineNumber, fileName); +} + + +void TestCase::loop1assertImplementation(bool condition, const std::string& conditionExpression, long lineNumber, long data1lineNumber, const std::string& fileName) +{ + if (!condition) + throw CppUnitException(conditionExpression, lineNumber, data1lineNumber, fileName); +} + + +void TestCase::loop2assertImplementation(bool condition, const std::string& conditionExpression, long lineNumber, long data1lineNumber, long data2lineNumber, const std::string& fileName) +{ + if (!condition) + throw CppUnitException(conditionExpression, lineNumber, data1lineNumber, data2lineNumber, fileName); +} + + +// Check for a failed equality assertion +void TestCase::assertEquals(long expected, long actual, long lineNumber, const std::string& fileName) +{ + if (expected != actual) + assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName); +} + + +// Check for a failed equality assertion +void TestCase::assertEquals(double expected, double actual, double delta, long lineNumber, const std::string& fileName) +{ + if (fabs(expected - actual) > delta) + assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName); +} + + +// Check for a failed equality assertion +void TestCase::assertEquals(const void* expected, const void* actual, long lineNumber, const std::string& fileName) +{ + if (expected != actual) + assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName); +} + + +// Check for a failed equality assertion +void TestCase::assertEquals(const std::string& expected, const std::string& actual, long lineNumber, const std::string& fileName) +{ + if (expected != actual) + assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName); +} + + +void TestCase::assertNotNull(const void* pointer, const std::string& pointerExpression, long lineNumber, const std::string& fileName) +{ + if (pointer == NULL) + throw CppUnitException(pointerExpression + " must not be NULL", lineNumber, fileName); +} + + +void TestCase::assertNull(const void* pointer, const std::string& pointerExpression, long lineNumber, const std::string& fileName) +{ + if (pointer != NULL) + throw CppUnitException(pointerExpression + " must be NULL", lineNumber, fileName); +} + + +void TestCase::fail(const std::string& message, long lineNumber, const std::string& fileName) +{ + throw CppUnitException(std::string("fail: ") + message, lineNumber, fileName); +} + + +void TestCase::warn(const std::string& message, long lineNumber, const std::string& fileName) +{ + std::cout << "Warning [" << fileName << ':' << lineNumber << "]: " << message << std::endl; +} + + +// Run the test and catch any exceptions that are triggered by it +void TestCase::run(TestResult *result) +{ + result->startTest(this); + + setUp(); + try + { + runTest(); + } + catch (CppUnitException& e) + { + CppUnitException* copy = new CppUnitException(e); + result->addFailure(this, copy); + } + catch (std::exception& e) + { + std::string msg(typeid(e).name()); + msg.append(": "); + msg.append(e.what()); + result->addError(this, new CppUnitException(msg)); + + } +#if !defined(_WIN32) + catch (...) + { + CppUnitException *e = new CppUnitException ("unknown exception"); + result->addError (this, e); + } +#endif + tearDown (); + result->endTest(this); +} + + +// A default run method +TestResult* TestCase::run() +{ + TestResult* result = defaultResult(); + + run(result); + return result; +} + + +// All the work for runTest is deferred to subclasses +void TestCase::runTest() +{ +} + + +// Build a message about a failed equality check +std::string TestCase::notEqualsMessage(long expected, long actual) +{ + return "expected: " + estring(expected) + " but was: " + estring(actual); +} + + +// Build a message about a failed equality check +std::string TestCase::notEqualsMessage(double expected, double actual) +{ + return "expected: " + estring(expected) + " but was: " + estring(actual); +} + + +// Build a message about a failed equality check +std::string TestCase::notEqualsMessage(const void* expected, const void* actual) +{ + return "expected: " + estring(expected) + " but was: " + estring(actual); +} + + +// Build a message about a failed equality check +std::string TestCase::notEqualsMessage(const std::string& expected, const std::string& actual) +{ + return "expected: \"" + expected + "\" but was: \"" + actual + "\""; +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestDecorator.cpp b/contrib/libpoco/CppUnit/src/TestDecorator.cpp new file mode 100644 index 00000000000..40f4b6662bb --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestDecorator.cpp @@ -0,0 +1,43 @@ +// +// TestDecorator.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestDecorator.cpp#1 $ +// + + +#include "CppUnit/TestDecorator.h" + + +namespace CppUnit { + + +TestDecorator::TestDecorator(Test* test) +{ + _test = test; +} + + +TestDecorator::~TestDecorator() +{ +} + + +int TestDecorator::countTestCases() +{ + return _test->countTestCases(); +} + + +void TestDecorator::run(TestResult* result) +{ + _test->run(result); +} + + +std::string TestDecorator::toString() +{ + return _test->toString(); +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestFailure.cpp b/contrib/libpoco/CppUnit/src/TestFailure.cpp new file mode 100644 index 00000000000..bfae516069f --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestFailure.cpp @@ -0,0 +1,22 @@ +// +// TestFailure.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestFailure.cpp#1 $ +// + + +#include "CppUnit/TestFailure.h" +#include "CppUnit/Test.h" + + +namespace CppUnit { + + +// Returns a short description of the failure. +std::string TestFailure::toString() +{ + return _failedTest->toString () + ": " + _thrownException->what(); +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestResult.cpp b/contrib/libpoco/CppUnit/src/TestResult.cpp new file mode 100644 index 00000000000..d6e3fcbbf28 --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestResult.cpp @@ -0,0 +1,29 @@ +// +// TestResult.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestResult.cpp#1 $ +// + + +#include "CppUnit/TestResult.h" + + +namespace CppUnit { + + +// Destroys a test result +TestResult::~TestResult() +{ + std::vector::iterator it; + + for (it = _errors.begin(); it != _errors.end(); ++it) + delete *it; + + for (it = _failures.begin(); it != _failures.end(); ++it) + delete *it; + + delete _syncObject; +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestRunner.cpp b/contrib/libpoco/CppUnit/src/TestRunner.cpp new file mode 100644 index 00000000000..196fbdeed0d --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestRunner.cpp @@ -0,0 +1,188 @@ +// +// TestRunner.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestRunner.cpp#1 $ +// + + +#include "CppUnit/TestRunner.h" +#include "CppUnit/Test.h" +#include "CppUnit/TestSuite.h" +#include "CppUnit/TextTestResult.h" +#include + + +namespace CppUnit { + + +TestRunner::TestRunner(): + _ostr(std::cout) +{ +} + + +TestRunner::TestRunner(std::ostream& ostr): + _ostr(ostr) +{ +} + + +TestRunner::~TestRunner() +{ + for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) + delete it->second; +} + + +void TestRunner::printBanner() +{ + _ostr + << "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl + << " where name is the name of a test case class" << std::endl; +} + + +bool TestRunner::run(const std::vector& args) +{ + std::string testCase; + int numberOfTests = 0; + bool success = true; + bool all = false; + bool wait = false; + bool printed = false; + + for (int i = 1; i < args.size(); i++) + { + const std::string& arg = args[i]; + if (arg == "-wait") + { + wait = true; + continue; + } + else if (arg == "-all") + { + all = true; + continue; + } + else if (arg == "-print") + { + for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) + { + print(it->first, it->second, 0); + } + printed = true; + continue; + } + + if (!all) + { + testCase = arg; + + if (testCase == "") + { + printBanner(); + return false; + } + + Test* testToRun = 0; + for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it) + { + testToRun = find(testCase, it->second, it->first); + } + if (testToRun) + { + if (!run(testToRun)) success = false; + } + numberOfTests++; + + if (!testToRun) + { + _ostr << "Test " << testCase << " not found." << std::endl; + return false; + } + } + } + + if (all) + { + for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) + { + if (!run(it->second)) success = false; + numberOfTests++; + } + } + + if (numberOfTests == 0 && !printed) + { + printBanner(); + return false; + } + + if (wait) + { + _ostr << " to continue" << std::endl; + std::cin.get(); + } + + return success; +} + + +bool TestRunner::run(Test* test) +{ + TextTestResult result(_ostr); + + test->run(&result); + _ostr << result << std::endl; + + return result.wasSuccessful(); +} + + +void TestRunner::addTest(const std::string& name, Test* test) +{ + _mappings.push_back(Mapping(name, test)); +} + + +void TestRunner::print(const std::string& name, Test* pTest, int indent) +{ + for (int i = 0; i < indent; ++i) + _ostr << " "; + _ostr << name << std::endl; + TestSuite* pSuite = dynamic_cast(pTest); + if (pSuite) + { + const std::vector& tests = pSuite->tests(); + for (std::vector::const_iterator it = tests.begin(); it != tests.end(); ++it) + { + print((*it)->toString(), *it, indent + 1); + } + } +} + + +Test* TestRunner::find(const std::string& name, Test* pTest, const std::string& testName) +{ + if (testName.find(name) != std::string::npos) + { + return pTest; + } + else + { + TestSuite* pSuite = dynamic_cast(pTest); + if (pSuite) + { + const std::vector& tests = pSuite->tests(); + for (std::vector::const_iterator it = tests.begin(); it != tests.end(); ++it) + { + Test* result = find(name, *it, (*it)->toString()); + if (result) return result; + } + } + return 0; + } +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TestSuite.cpp b/contrib/libpoco/CppUnit/src/TestSuite.cpp new file mode 100644 index 00000000000..066949243e7 --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TestSuite.cpp @@ -0,0 +1,49 @@ +// +// TestSuite.cpp +// +// $Id: //poco/1.4/CppUnit/src/TestSuite.cpp#1 $ +// + + +#include "CppUnit/TestSuite.h" +#include "CppUnit/TestResult.h" + + +namespace CppUnit { + + +// Deletes all tests in the suite. +void TestSuite::deleteContents() +{ + for (std::vector::iterator it = _tests.begin(); it != _tests.end(); ++it) + delete *it; +} + + +// Runs the tests and collects their result in a TestResult. +void TestSuite::run(TestResult *result) +{ + for (std::vector::iterator it = _tests.begin(); it != _tests.end(); ++it) + { + if (result->shouldStop ()) + break; + + Test *test = *it; + test->run(result); + } +} + + +// Counts the number of test cases that will be run by this test. +int TestSuite::countTestCases() +{ + int count = 0; + + for (std::vector::iterator it = _tests.begin (); it != _tests.end (); ++it) + count += (*it)->countTestCases(); + + return count; +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/CppUnit/src/TextTestResult.cpp b/contrib/libpoco/CppUnit/src/TextTestResult.cpp new file mode 100644 index 00000000000..b77318941c5 --- /dev/null +++ b/contrib/libpoco/CppUnit/src/TextTestResult.cpp @@ -0,0 +1,225 @@ +// +// TextTestResult.cpp +// +// $Id: //poco/1.4/CppUnit/src/TextTestResult.cpp#1 $ +// + + +#include "CppUnit/TextTestResult.h" +#include "CppUnit/CppUnitException.h" +#include "CppUnit/Test.h" +#include "CppUnit/estring.h" +#include +#include +#include +#include + + +namespace CppUnit { + + +TextTestResult::TextTestResult(): + _ostr(std::cout) +{ + setup(); +} + + +TextTestResult::TextTestResult(std::ostream& ostr): + _ostr(ostr) +{ + setup(); +} + + +void TextTestResult::setup() +{ +#if !defined(_WIN32_WCE) + const char* env = std::getenv("CPPUNIT_IGNORE"); + if (env) + { + std::string ignored = env; + std::string::const_iterator it = ignored.begin(); + std::string::const_iterator end = ignored.end(); + while (it != end) + { + while (it != end && std::isspace(*it)) ++it; + std::string test; + while (it != end && !std::isspace(*it)) test += *it++; + if (!test.empty()) _ignored.insert(test); + } + } +#endif +} + + +void TextTestResult::addError(Test* test, CppUnitException* e) +{ + if (_ignored.find(test->toString()) == _ignored.end()) + { + TestResult::addError(test, e); + _ostr << "ERROR" << std::flush; + } + else + { + _ostr << "ERROR (ignored)" << std::flush; + } +} + + +void TextTestResult::addFailure(Test* test, CppUnitException* e) +{ + if (_ignored.find(test->toString()) == _ignored.end()) + { + TestResult::addFailure(test, e); + _ostr << "FAILURE" << std::flush; + } + else + { + _ostr << "FAILURE (ignored)" << std::flush; + } +} + + +void TextTestResult::startTest(Test* test) +{ + TestResult::startTest(test); + _ostr << "\n" << shortName(test->toString()) << ": "; +} + + +void TextTestResult::printErrors(std::ostream& stream) +{ + if (testErrors() != 0) + { + stream << "\n"; + + if (testErrors() == 1) + stream << "There was " << testErrors() << " error: " << std::endl; + else + stream << "There were " << testErrors() << " errors: " << std::endl; + + int i = 1; + for (std::vector::iterator it = errors().begin(); it != errors().end(); ++it) + { + TestFailure* failure = *it; + CppUnitException* e = failure->thrownException(); + + stream << std::setw(2) << i + << ": " + << failure->failedTest()->toString() << "\n" + << " \"" << (e ? e->what() : "") << "\"\n" + << " in \"" + << (e ? e->fileName() : std::string()) + << "\", line "; + if (e == 0) + { + stream << "0"; + } + else + { + stream << e->lineNumber(); + if (e->data2LineNumber() != CppUnitException::CPPUNIT_UNKNOWNLINENUMBER) + { + stream << " data lines " << e->data1LineNumber() + << ", " << e->data2LineNumber(); + } + else if (e->data1LineNumber() != CppUnitException::CPPUNIT_UNKNOWNLINENUMBER) + { + stream << " data line " << e->data1LineNumber(); + } + } + stream << "\n"; + i++; + } + } +} + + +void TextTestResult::printFailures(std::ostream& stream) +{ + if (testFailures() != 0) + { + stream << "\n"; + if (testFailures() == 1) + stream << "There was " << testFailures() << " failure: " << std::endl; + else + stream << "There were " << testFailures() << " failures: " << std::endl; + + int i = 1; + + for (std::vector::iterator it = failures().begin(); it != failures().end(); ++it) + { + TestFailure* failure = *it; + CppUnitException* e = failure->thrownException(); + + stream << std::setw(2) << i + << ": " + << failure->failedTest()->toString() << "\n" + << " \"" << (e ? e->what() : "") << "\"\n" + << " in \"" + << (e ? e->fileName() : std::string()) + << "\", line "; + if (e == 0) + { + stream << "0"; + } + else + { + stream << e->lineNumber(); + if (e->data2LineNumber() != CppUnitException::CPPUNIT_UNKNOWNLINENUMBER) + { + stream << " data lines " + << e->data1LineNumber() + << ", " << e->data2LineNumber(); + } + else if (e->data1LineNumber() != CppUnitException::CPPUNIT_UNKNOWNLINENUMBER) + { + stream << " data line " << e->data1LineNumber(); + } + } + stream << "\n"; + i++; + } + } +} + + +void TextTestResult::print(std::ostream& stream) +{ + printHeader(stream); + printErrors(stream); + printFailures(stream); +} + + +void TextTestResult::printHeader(std::ostream& stream) +{ + stream << "\n\n"; + if (wasSuccessful()) + stream << "OK (" + << runTests() << " tests)" + << std::endl; + else + stream << "!!!FAILURES!!!" << std::endl + << "Runs: " + << runTests () + << " Failures: " + << testFailures () + << " Errors: " + << testErrors () + << std::endl; +} + + +std::string TextTestResult::shortName(const std::string& testName) +{ + std::string::size_type pos = testName.rfind('.'); + if (pos != std::string::npos) + return std::string(testName, pos + 1); + else + return testName; +} + + +} // namespace CppUnit diff --git a/contrib/libpoco/Crypto/CMakeLists.txt b/contrib/libpoco/Crypto/CMakeLists.txt new file mode 100644 index 00000000000..f69f9e9ce30 --- /dev/null +++ b/contrib/libpoco/Crypto/CMakeLists.txt @@ -0,0 +1,27 @@ +set(LIBNAME "PocoCrypto") +set(POCO_LIBNAME "${LIBNAME}") + +# Sources +file(GLOB SRCS_G "src/*.cpp") +POCO_SOURCES_AUTO( SRCS ${SRCS_G}) + +# Headers +file(GLOB_RECURSE HDRS_G "include/*.h" ) +POCO_HEADERS_AUTO( SRCS ${HDRS_G}) + +#add_definitions(-D_USRDLL) + +add_library( "${LIBNAME}" ${LIB_MODE} ${SRCS} ) +set_target_properties( "${LIBNAME}" + PROPERTIES + VERSION ${SHARED_LIBRARY_VERSION} SOVERSION ${SHARED_LIBRARY_VERSION} + OUTPUT_NAME ${POCO_LIBNAME} + DEFINE_SYMBOL Crypto_EXPORTS + ) + +target_link_libraries( "${LIBNAME}" PocoFoundation ${OPENSSL_LIBRARIES} ) + +if (ENABLE_TESTS) + add_subdirectory(samples) + add_subdirectory(testsuite) +endif () diff --git a/contrib/libpoco/Crypto/cmake/PocoCryptoConfig.cmake b/contrib/libpoco/Crypto/cmake/PocoCryptoConfig.cmake new file mode 100644 index 00000000000..fe147f3afdc --- /dev/null +++ b/contrib/libpoco/Crypto/cmake/PocoCryptoConfig.cmake @@ -0,0 +1,3 @@ +include(CMakeFindDependencyMacro) +find_dependency(PocoFoundation) +include("${CMAKE_CURRENT_LIST_DIR}/PocoCryptoTargets.cmake") diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/Cipher.h b/contrib/libpoco/Crypto/include/Poco/Crypto/Cipher.h new file mode 100644 index 00000000000..30d17f3cd0d --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/Cipher.h @@ -0,0 +1,140 @@ +// +// Cipher.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/Cipher.h#3 $ +// +// Library: Crypto +// Package: Cipher +// Module: Cipher +// +// Definition of the Cipher class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_Cipher_INCLUDED +#define Crypto_Cipher_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/RefCountedObject.h" +#include "Poco/AutoPtr.h" +#include +#include +#include + + +namespace Poco { +namespace Crypto { + + +class CryptoTransform; + + +class Crypto_API Cipher: public Poco::RefCountedObject + /// Represents the abstract base class from which all implementations of + /// symmetric/assymetric encryption algorithms must inherit. Use the CipherFactory + /// class to obtain an instance of this class: + /// + /// CipherFactory& factory = CipherFactory::defaultFactory(); + /// // Creates a 256-bit AES cipher + /// Cipher* pCipher = factory.createCipher(CipherKey("aes-256")); + /// Cipher* pRSACipher = factory.createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL)); + /// + /// Check the different Key constructors on how to initialize/create + /// a key. The above example auto-generates random keys. + /// + /// Note that you won't be able to decrypt data encrypted with a random key + /// once the Cipher is destroyed unless you persist the generated key and IV. + /// An example usage for random keys is to encrypt data saved in a temporary + /// file. + /// + /// Once your key is set up, you can use the Cipher object to encrypt or + /// decrypt strings or, in conjunction with a CryptoInputStream or a + /// CryptoOutputStream, to encrypt streams of data. + /// + /// Since encrypted strings will contain arbitary binary data that will cause + /// problems in applications that are not binary-safe (eg., when sending + /// encrypted data in e-mails), the encryptString() and decryptString() can + /// encode (or decode, respectively) encrypted data using a "transport encoding". + /// Supported encodings are Base64 and BinHex. + /// + /// The following example encrypts and decrypts a string utilizing Base64 + /// encoding: + /// + /// std::string plainText = "This is my secret information"; + /// std::string encrypted = pCipher->encryptString(plainText, Cipher::ENC_BASE64); + /// std::string decrypted = pCipher->decryptString(encrypted, Cipher::ENC_BASE64); + /// + /// In order to encrypt a stream of data (eg. to encrypt files), you can use + /// a CryptoStream: + /// + /// // Create an output stream that will encrypt all data going through it + /// // and write pass it to the underlying file stream. + /// Poco::FileOutputStream sink("encrypted.dat"); + /// CryptoOutputStream encryptor(sink, pCipher->createEncryptor()); + /// + /// Poco::FileInputStream source("source.txt"); + /// Poco::StreamCopier::copyStream(source, encryptor); + /// + /// // Always close output streams to flush all internal buffers + /// encryptor.close(); + /// sink.close(); +{ +public: + typedef Poco::AutoPtr Ptr; + typedef std::vector ByteVec; + + enum Encoding + /// Transport encoding to use for encryptString() and decryptString(). + { + ENC_NONE = 0x00, /// Plain binary output + ENC_BASE64 = 0x01, /// Base64-encoded output + ENC_BINHEX = 0x02, /// BinHex-encoded output + ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds + ENC_BINHEX_NO_LF = 0x82, /// BinHex-encoded output, no linefeeds + + }; + + virtual ~Cipher(); + /// Destroys the Cipher. + + virtual const std::string& name() const = 0; + /// Returns the name of the Cipher. + + virtual CryptoTransform* createEncryptor() = 0; + /// Creates an encrytor object to be used with a CryptoStream. + + virtual CryptoTransform* createDecryptor() = 0; + /// Creates a decryptor object to be used with a CryptoStream. + + virtual std::string encryptString(const std::string& str, Encoding encoding = ENC_NONE); + /// Directly encrypt a string and encode it using the given encoding. + + virtual std::string decryptString(const std::string& str, Encoding encoding = ENC_NONE); + /// Directly decrypt a string that is encoded with the given encoding. + + virtual void encrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE); + /// Directly encrypts an input stream and encodes it using the given encoding. + + virtual void decrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE); + /// Directly decrypt an input stream that is encoded with the given encoding. + +protected: + Cipher(); + /// Creates a new Cipher object. + +private: + Cipher(const Cipher&); + Cipher& operator = (const Cipher&); +}; + + +} } // namespace Poco::Crypto + + +#endif // Crypto_Cipher_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CipherFactory.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherFactory.h new file mode 100644 index 00000000000..7cfd73e6fda --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherFactory.h @@ -0,0 +1,77 @@ +// +// CipherFactory.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CipherFactory.h#1 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherFactory +// +// Definition of the CipherFactory class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CipherFactory_INCLUDED +#define Crypto_CipherFactory_INCLUDED + + +#include "Poco/Crypto/Crypto.h" + + +namespace Poco { +namespace Crypto { + + +class Cipher; +class CipherKey; +class RSAKey; + + +class Crypto_API CipherFactory + /// A factory for Cipher objects. See the Cipher class for examples on how to + /// use the CipherFactory. +{ +public: + CipherFactory(); + /// Creates a new CipherFactory object. + + virtual ~CipherFactory(); + /// Destroys the CipherFactory. + + Cipher* createCipher(const CipherKey& key); + /// Creates a Cipher object for the given Cipher name. Valid cipher + /// names depend on the OpenSSL version the library is linked with; + /// see the output of + /// + /// openssl enc --help + /// + /// for a list of supported block and stream ciphers. + /// + /// Common examples are: + /// + /// * AES: "aes-128", "aes-256" + /// * DES: "des", "des3" + /// * Blowfish: "bf" + + Cipher* createCipher(const RSAKey& key, RSAPaddingMode paddingMode = RSA_PADDING_PKCS1); + /// Creates a RSACipher using the given RSA key and padding mode + /// for public key encryption/private key decryption. + + static CipherFactory& defaultFactory(); + /// Returns the default CipherFactory. + +private: + CipherFactory(const CipherFactory&); + CipherFactory& operator = (const CipherFactory&); +}; + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CipherFactory_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CipherImpl.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherImpl.h new file mode 100644 index 00000000000..1a1f10b6e50 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherImpl.h @@ -0,0 +1,71 @@ +// +// CipherImpl.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CipherImpl.h#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherImpl +// +// Definition of the CipherImpl class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CipherImpl_INCLUDED +#define Crypto_CipherImpl_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/CipherKey.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include + + +namespace Poco { +namespace Crypto { + + +class CipherImpl: public Cipher + /// An implementation of the Cipher class for OpenSSL's crypto library. +{ +public: + CipherImpl(const CipherKey& key); + /// Creates a new CipherImpl object for the given CipherKey. + + virtual ~CipherImpl(); + /// Destroys the CipherImpl. + + const std::string& name() const; + /// Returns the name of the cipher. + + CryptoTransform* createEncryptor(); + /// Creates an encrytor object. + + CryptoTransform* createDecryptor(); + /// Creates a decrytor object. + +private: + CipherKey _key; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// Inlines +// +inline const std::string& CipherImpl::name() const +{ + return _key.name(); +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CipherImpl_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKey.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKey.h new file mode 100644 index 00000000000..792de73b2e1 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKey.h @@ -0,0 +1,184 @@ +// +// CipherKey.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CipherKey.h#1 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherKey +// +// Definition of the CipherKey class. +// +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CipherKey_INCLUDED +#define Crypto_CipherKey_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/CipherKeyImpl.h" + + +namespace Poco { +namespace Crypto { + + +class Crypto_API CipherKey + /// CipherKey stores the key information for decryption/encryption of data. + /// To create a random key, using the following code: + /// + /// CipherKey key("aes-256"); + /// + /// Note that you won't be able to decrypt data encrypted with a random key + /// once the Cipher is destroyed unless you persist the generated key and IV. + /// An example usage for random keys is to encrypt data saved in a temporary + /// file. + /// + /// To create a key using a human-readable password + /// string, use the following code. We create a AES Cipher and + /// use a salt value to make the key more robust: + /// + /// std::string password = "secret"; + /// std::string salt("asdff8723lasdf(**923412"); + /// CipherKey key("aes-256", password, salt); + /// +{ +public: + typedef CipherKeyImpl::Mode Mode; + typedef CipherKeyImpl::ByteVec ByteVec; + + enum + { + DEFAULT_ITERATION_COUNT = 2000 + /// Default iteration count to use with + /// generateKey(). RSA security recommends + /// an iteration count of at least 1000. + }; + + CipherKey(const std::string& name, + const std::string& passphrase, + const std::string& salt = "", + int iterationCount = DEFAULT_ITERATION_COUNT); + /// Creates a new CipherKeyImpl object using the given + /// cipher name, passphrase, salt value and iteration count. + + CipherKey(const std::string& name, + const ByteVec& key, + const ByteVec& iv); + /// Creates a new CipherKeyImpl object using the given cipher + /// name, key and initialization vector. + + CipherKey(const std::string& name); + /// Creates a new CipherKeyImpl object. Autoinitializes key and + /// initialization vector. + + ~CipherKey(); + /// Destroys the CipherKeyImpl. + + const std::string& name() const; + /// Returns the name of the Cipher. + + int keySize() const; + /// Returns the key size of the Cipher. + + int blockSize() const; + /// Returns the block size of the Cipher. + + int ivSize() const; + /// Returns the IV size of the Cipher. + + Mode mode() const; + /// Returns the Cipher's mode of operation. + + const ByteVec& getKey() const; + /// Returns the key for the Cipher. + + void setKey(const ByteVec& key); + /// Sets the key for the Cipher. + + const ByteVec& getIV() const; + /// Returns the initialization vector (IV) for the Cipher. + + void setIV(const ByteVec& iv); + /// Sets the initialization vector (IV) for the Cipher. + + CipherKeyImpl::Ptr impl(); + /// Returns the impl object + +private: + CipherKeyImpl::Ptr _pImpl; +}; + + +// +// inlines +// +inline const std::string& CipherKey::name() const +{ + return _pImpl->name(); +} + + +inline int CipherKey::keySize() const +{ + return _pImpl->keySize(); +} + + +inline int CipherKey::blockSize() const +{ + return _pImpl->blockSize(); +} + + +inline int CipherKey::ivSize() const +{ + return _pImpl->ivSize(); +} + + +inline CipherKey::Mode CipherKey::mode() const +{ + return _pImpl->mode(); +} + + +inline const CipherKey::ByteVec& CipherKey::getKey() const +{ + return _pImpl->getKey(); +} + + +inline void CipherKey::setKey(const CipherKey::ByteVec& key) +{ + _pImpl->setKey(key); +} + + +inline const CipherKey::ByteVec& CipherKey::getIV() const +{ + return _pImpl->getIV(); +} + + +inline void CipherKey::setIV(const CipherKey::ByteVec& iv) +{ + _pImpl->setIV(iv); +} + + +inline CipherKeyImpl::Ptr CipherKey::impl() +{ + return _pImpl; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CipherKey_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKeyImpl.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKeyImpl.h new file mode 100644 index 00000000000..3b9e7bfce0a --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CipherKeyImpl.h @@ -0,0 +1,172 @@ +// +// CipherKeyImpl.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CipherKeyImpl.h#3 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherKeyImpl +// +// Definition of the CipherKeyImpl class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CipherKeyImpl_INCLUDED +#define Crypto_CipherKeyImpl_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include "Poco/RefCountedObject.h" +#include "Poco/AutoPtr.h" +#include + + +struct evp_cipher_st; +typedef struct evp_cipher_st EVP_CIPHER; + + +namespace Poco { +namespace Crypto { + + +class CipherKeyImpl: public RefCountedObject + /// An implementation of the CipherKey class for OpenSSL's crypto library. +{ +public: + typedef std::vector ByteVec; + typedef Poco::AutoPtr Ptr; + + enum Mode + /// Cipher mode of operation. This mode determines how multiple blocks + /// are connected; this is essential to improve security. + { + MODE_STREAM_CIPHER, /// Stream cipher + MODE_ECB, /// Electronic codebook (plain concatenation) + MODE_CBC, /// Cipher block chaining (default) + MODE_CFB, /// Cipher feedback + MODE_OFB /// Output feedback + }; + + CipherKeyImpl(const std::string& name, + const std::string& passphrase, + const std::string& salt, + int iterationCount); + /// Creates a new CipherKeyImpl object, using + /// the given cipher name, passphrase, salt value + /// and iteration count. + + CipherKeyImpl(const std::string& name, + const ByteVec& key, + const ByteVec& iv); + /// Creates a new CipherKeyImpl object, using the + /// given cipher name, key and initialization vector. + + CipherKeyImpl(const std::string& name); + /// Creates a new CipherKeyImpl object. Autoinitializes key + /// and initialization vector. + + virtual ~CipherKeyImpl(); + /// Destroys the CipherKeyImpl. + + const std::string& name() const; + /// Returns the name of the Cipher. + + int keySize() const; + /// Returns the key size of the Cipher. + + int blockSize() const; + /// Returns the block size of the Cipher. + + int ivSize() const; + /// Returns the IV size of the Cipher. + + Mode mode() const; + /// Returns the Cipher's mode of operation. + + const ByteVec& getKey() const; + /// Returns the key for the Cipher. + + void setKey(const ByteVec& key); + /// Sets the key for the Cipher. + + const ByteVec& getIV() const; + /// Returns the initialization vector (IV) for the Cipher. + + void setIV(const ByteVec& iv); + /// Sets the initialization vector (IV) for the Cipher. + + const EVP_CIPHER* cipher(); + /// Returns the cipher object + +private: + void generateKey(const std::string& passphrase, + const std::string& salt, + int iterationCount); + /// Generates key and IV from a password and optional salt string. + + void generateKey(); + /// Generates key and IV from random data. + + void getRandomBytes(ByteVec& vec, std::size_t count); + /// Stores random bytes in vec. + +private: + const EVP_CIPHER* _pCipher; + std::string _name; + ByteVec _key; + ByteVec _iv; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// Inlines +// +inline const std::string& CipherKeyImpl::name() const +{ + return _name; +} + + +inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getKey() const +{ + return _key; +} + + +inline void CipherKeyImpl::setKey(const ByteVec& key) +{ + poco_assert(key.size() == static_cast(keySize())); + _key = key; +} + + +inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getIV() const +{ + return _iv; +} + + +inline void CipherKeyImpl::setIV(const ByteVec& iv) +{ + poco_assert(iv.size() == static_cast(ivSize())); + _iv = iv; +} + + +inline const EVP_CIPHER* CipherKeyImpl::cipher() +{ + return _pCipher; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CipherKeyImpl_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/Crypto.h b/contrib/libpoco/Crypto/include/Poco/Crypto/Crypto.h new file mode 100644 index 00000000000..fcfb20ec26f --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/Crypto.h @@ -0,0 +1,119 @@ +// +// Crypto.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/Crypto.h#3 $ +// +// Library: Crypto +// Package: CryptoCore +// Module: Crypto +// +// Basic definitions for the Poco Crypto library. +// This file must be the first file included by every other Crypto +// header file. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_Crypto_INCLUDED +#define Crypto_Crypto_INCLUDED + + +#if defined(__APPLE__) +// OS X 10.7 deprecates some OpenSSL functions +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + + +#include "Poco/Foundation.h" + + +enum RSAPaddingMode + /// The padding mode used for RSA public key encryption. +{ + RSA_PADDING_PKCS1, + /// PKCS #1 v1.5 padding. This currently is the most widely used mode. + + RSA_PADDING_PKCS1_OAEP, + /// EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty + /// encoding parameter. This mode is recommended for all new applications. + + RSA_PADDING_SSLV23, + /// PKCS #1 v1.5 padding with an SSL-specific modification that denotes + /// that the server is SSL3 capable. + + RSA_PADDING_NONE + /// Raw RSA encryption. This mode should only be used to implement cryptographically + /// sound padding modes in the application code. Encrypting user data directly with RSA + /// is insecure. +}; + + +// +// The following block is the standard way of creating macros which make exporting +// from a DLL simpler. All files within this DLL are compiled with the Crypto_EXPORTS +// symbol defined on the command line. this symbol should not be defined on any project +// that uses this DLL. This way any other project whose source files include this file see +// Crypto_API functions as being imported from a DLL, wheras this DLL sees symbols +// defined with this macro as being exported. +// +#if defined(_WIN32) && defined(POCO_DLL) + #if defined(Crypto_EXPORTS) + #define Crypto_API __declspec(dllexport) + #else + #define Crypto_API __declspec(dllimport) + #endif +#endif + + +#if !defined(Crypto_API) + #if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4) + #define Crypto_API __attribute__ ((visibility ("default"))) + #else + #define Crypto_API + #endif +#endif + + +// +// Automatically link Crypto library. +// +#if defined(_MSC_VER) + #if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Crypto_EXPORTS) + #pragma comment(lib, "PocoCrypto" POCO_LIB_SUFFIX) + #endif +#endif + + +namespace Poco { +namespace Crypto { + + +void Crypto_API initializeCrypto(); + /// Initialize the Crypto library, as well as the underlying OpenSSL + /// libraries, by calling OpenSSLInitializer::initialize(). + /// + /// Should be called before using any class from the Crypto library. + /// The Crypto library will be initialized automatically, through + /// OpenSSLInitializer instances held by various Crypto classes + /// (Cipher, CipherKey, RSAKey, X509Certificate). + /// However, it is recommended to call initializeCrypto() + /// in any case at application startup. + /// + /// Can be called multiple times; however, for every call to + /// initializeCrypto(), a matching call to uninitializeCrypto() + /// must be performed. + + +void Crypto_API uninitializeCrypto(); + /// Uninitializes the Crypto library by calling + /// OpenSSLInitializer::uninitialize(). + + +} } // namespace Poco::Crypto + + +#endif // Crypto_Crypto_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoStream.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoStream.h new file mode 100644 index 00000000000..469b05cf6d1 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoStream.h @@ -0,0 +1,194 @@ +// +// CryptoStream.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CryptoStream.h#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CryptoStream +// +// Definition of the CryptoStreamBuf, CryptoInputStream and CryptoOutputStream +// classes. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CryptoStream_INCLUDED +#define Crypto_CryptoStream_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/BufferedStreamBuf.h" +#include "Poco/Buffer.h" +#include + + +namespace Poco { +namespace Crypto { + + +class CryptoTransform; +class Cipher; + + +class Crypto_API CryptoStreamBuf: public Poco::BufferedStreamBuf + /// This stream buffer performs cryptographic transformation on the data + /// going through it. +{ +public: + CryptoStreamBuf(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + CryptoStreamBuf(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + + virtual ~CryptoStreamBuf(); + + void close(); + /// Flushes all buffers and finishes the encryption. + +protected: + int readFromDevice(char* buffer, std::streamsize length); + int writeToDevice(const char* buffer, std::streamsize length); + +private: + CryptoTransform* _pTransform; + std::istream* _pIstr; + std::ostream* _pOstr; + bool _eof; + + Poco::Buffer _buffer; + + CryptoStreamBuf(const CryptoStreamBuf&); + CryptoStreamBuf& operator = (const CryptoStreamBuf&); +}; + + +class Crypto_API CryptoIOS: public virtual std::ios + /// The base class for CryptoInputStream and CryptoOutputStream. + /// + /// This class is needed to ensure correct initialization order of the + /// stream buffer and base classes. +{ +public: + CryptoIOS(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + CryptoIOS(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + ~CryptoIOS(); + CryptoStreamBuf* rdbuf(); + +protected: + CryptoStreamBuf _buf; +}; + + +class Crypto_API CryptoInputStream: public CryptoIOS, public std::istream + /// This stream transforms all data passing through it using the given + /// CryptoTransform. + /// + /// Use a CryptoTransform object provided by Cipher::createEncrytor() or + /// Cipher::createDecryptor() to create an encrypting or decrypting stream, + /// respectively. +{ +public: + CryptoInputStream(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + /// Create a new CryptoInputStream object. The CryptoInputStream takes the + /// ownership of the given CryptoTransform object. + + CryptoInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new encrypting CryptoInputStream object using the given cipher. + + ~CryptoInputStream(); + /// Destroys the CryptoInputStream. +}; + + +class Crypto_API CryptoOutputStream: public CryptoIOS, public std::ostream + /// This stream transforms all data passing through it using the given + /// CryptoTransform. + /// + /// Use a CryptoTransform object provided by Cipher::createEncrytor() or + /// Cipher::createDecryptor() to create an encrypting or decrypting stream, + /// respectively. + /// + /// After all data has been passed through the stream, close() must be called + /// to ensure completion of cryptographic transformation. +{ +public: + CryptoOutputStream(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize = 8192); + /// Create a new CryptoOutputStream object. The CryptoOutputStream takes the + /// ownership of the given CryptoTransform object. + + CryptoOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new decrypting CryptoOutputStream object using the given cipher. + + ~CryptoOutputStream(); + /// Destroys the CryptoOutputStream. + + void close(); + /// Flushes all buffers and finishes the encryption. +}; + + +class Crypto_API DecryptingInputStream: public CryptoIOS, public std::istream + /// This stream decrypts all data passing through it using the given + /// Cipher. +{ +public: + DecryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new DecryptingInputStream object using the given cipher. + + ~DecryptingInputStream(); + /// Destroys the DecryptingInputStream. +}; + + +class Crypto_API DecryptingOutputStream: public CryptoIOS, public std::ostream + /// This stream decrypts all data passing through it using the given + /// Cipher. +{ +public: + DecryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new DecryptingOutputStream object using the given cipher. + + ~DecryptingOutputStream(); + /// Destroys the DecryptingOutputStream. + + void close(); + /// Flushes all buffers and finishes the decryption. +}; + + +class Crypto_API EncryptingInputStream: public CryptoIOS, public std::istream + /// This stream encrypts all data passing through it using the given + /// Cipher. +{ +public: + EncryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new EncryptingInputStream object using the given cipher. + + ~EncryptingInputStream(); + /// Destroys the EncryptingInputStream. +}; + + +class Crypto_API EncryptingOutputStream: public CryptoIOS, public std::ostream + /// This stream encrypts all data passing through it using the given + /// Cipher. +{ +public: + EncryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize = 8192); + /// Create a new EncryptingOutputStream object using the given cipher. + + ~EncryptingOutputStream(); + /// Destroys the EncryptingOutputStream. + + void close(); + /// Flushes all buffers and finishes the encryption. +}; + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CryptoStream_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoTransform.h b/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoTransform.h new file mode 100644 index 00000000000..1bae01ff547 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/CryptoTransform.h @@ -0,0 +1,78 @@ +// +// CryptoTransform.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/CryptoTransform.h#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CryptoTransform +// +// Definition of the CryptoTransform class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_CryptoTransform_INCLUDED +#define Crypto_CryptoTransform_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include + + +namespace Poco { +namespace Crypto { + + +class Crypto_API CryptoTransform + /// This interface represents the basic operations for cryptographic + /// transformations to be used with a CryptoInputStream or a + /// CryptoOutputStream. + /// + /// Implementations of this class are returned by the Cipher class to + /// perform encryption or decryption of data. +{ +public: + CryptoTransform(); + /// Creates a new CryptoTransform object. + + virtual ~CryptoTransform(); + /// Destroys the CryptoTransform. + + virtual std::size_t blockSize() const = 0; + /// Returns the block size for this CryptoTransform. + + virtual int setPadding(int padding); + /// Enables or disables padding. By default encryption operations are padded using standard block + /// padding and the padding is checked and removed when decrypting. If the padding parameter is zero then + /// no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of + /// the block size or an error will occur. + + virtual std::streamsize transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength) = 0; + /// Transforms a chunk of data. The inputLength is arbitrary and does not + /// need to be a multiple of the block size. The output buffer has a maximum + /// capacity of the given outputLength that must be at least + /// inputLength + blockSize() - 1 + /// Returns the number of bytes written to the output buffer. + + virtual std::streamsize finalize(unsigned char* output, std::streamsize length) = 0; + /// Finalizes the transformation. The output buffer must contain enough + /// space for at least two blocks, ie. + /// length >= 2*blockSize() + /// must be true. Returns the number of bytes written to the output + /// buffer. +}; + + +} } // namespace Poco::Crypto + + +#endif // Crypto_CryptoTransform_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/DigestEngine.h b/contrib/libpoco/Crypto/include/Poco/Crypto/DigestEngine.h new file mode 100644 index 00000000000..5de75392a83 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/DigestEngine.h @@ -0,0 +1,82 @@ +// +// DigestEngine.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/DigestEngine.h#1 $ +// +// Library: Crypto +// Package: Digest +// Module: DigestEngine +// +// Definition of the DigestEngine class. +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_DigestEngine_INCLUDED +#define Crypto_DigestEngine_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include "Poco/DigestEngine.h" +#include + + +namespace Poco { +namespace Crypto { + + +class Crypto_API DigestEngine: public Poco::DigestEngine + /// This class implements a Poco::DigestEngine for all + /// digest algorithms supported by OpenSSL. +{ +public: + DigestEngine(const std::string& name); + /// Creates a DigestEngine using the digest with the given name + /// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.). + /// See the OpenSSL documentation for a list of supported digest algorithms. + /// + /// Throws a Poco::NotFoundException if no algorithm with the given name exists. + + ~DigestEngine(); + /// Destroys the DigestEngine. + + const std::string& algorithm() const; + /// Returns the name of the digest algorithm. + + int nid() const; + /// Returns the NID (OpenSSL object identifier) of the digest algorithm. + + // DigestEngine + std::size_t digestLength() const; + void reset(); + const Poco::DigestEngine::Digest& digest(); + +protected: + void updateImpl(const void* data, std::size_t length); + +private: + std::string _name; + EVP_MD_CTX* _ctx; + Poco::DigestEngine::Digest _digest; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// inlines +// +inline const std::string& DigestEngine::algorithm() const +{ + return _name; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_DigestEngine_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/OpenSSLInitializer.h b/contrib/libpoco/Crypto/include/Poco/Crypto/OpenSSLInitializer.h new file mode 100644 index 00000000000..868530062cb --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/OpenSSLInitializer.h @@ -0,0 +1,117 @@ +// +// OpenSSLInitializer.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/OpenSSLInitializer.h#1 $ +// +// Library: Crypto +// Package: CryptoCore +// Module: OpenSSLInitializer +// +// Definition of the OpenSSLInitializer class. +// +// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_OpenSSLInitializer_INCLUDED +#define Crypto_OpenSSLInitializer_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Mutex.h" +#include "Poco/AtomicCounter.h" +#include +#include +#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_NUMBER < 0x010001000L +#include +#endif + + +extern "C" +{ + struct CRYPTO_dynlock_value + { + Poco::FastMutex _mutex; + }; +} + + +namespace Poco { +namespace Crypto { + + +class Crypto_API OpenSSLInitializer + /// Initalizes the OpenSSL library. + /// + /// The class ensures the earliest initialization and the + /// latest shutdown of the OpenSSL library. +{ +public: + OpenSSLInitializer(); + /// Automatically initialize OpenSSL on startup. + + ~OpenSSLInitializer(); + /// Automatically shut down OpenSSL on exit. + + static void initialize(); + /// Initializes the OpenSSL machinery. + + static void uninitialize(); + /// Shuts down the OpenSSL machinery. + + static bool isFIPSEnabled(); + // Returns true if FIPS mode is enabled, false otherwise. + + static void enableFIPSMode(bool enabled); + // Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything. + +protected: + enum + { + SEEDSIZE = 256 + }; + + // OpenSSL multithreading support + static void lock(int mode, int n, const char* file, int line); + static unsigned long id(); + static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line); + static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line); + static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line); + +private: + static Poco::FastMutex* _mutexes; + static Poco::AtomicCounter _rc; +}; + + +// +// inlines +// +inline bool OpenSSLInitializer::isFIPSEnabled() +{ +#ifdef OPENSSL_FIPS + return FIPS_mode() ? true : false; +#else + return false; +#endif +} + +#ifdef OPENSSL_FIPS +inline void OpenSSLInitializer::enableFIPSMode(bool enabled) +{ + FIPS_mode_set(enabled); +} +#else +inline void OpenSSLInitializer::enableFIPSMode(bool /*enabled*/) +{ +} +#endif + + +} } // namespace Poco::Crypto + + +#endif // Crypto_OpenSSLInitializer_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/RSACipherImpl.h b/contrib/libpoco/Crypto/include/Poco/Crypto/RSACipherImpl.h new file mode 100644 index 00000000000..6d433ed1f4b --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/RSACipherImpl.h @@ -0,0 +1,79 @@ +// +// RSACipherImpl.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/RSACipherImpl.h#2 $ +// +// Library: Crypto +// Package: RSA +// Module: RSACipherImpl +// +// Definition of the RSACipherImpl class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_RSACipherImpl_INCLUDED +#define Crypto_RSACipherImpl_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/RSAKey.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include + + +namespace Poco { +namespace Crypto { + + +class RSACipherImpl: public Cipher + /// An implementation of the Cipher class for + /// assymetric (public-private key) encryption + /// based on the the RSA algorithm in OpenSSL's + /// crypto library. + /// + /// Encryption is using the public key, decryption + /// requires the private key. +{ +public: + RSACipherImpl(const RSAKey& key, RSAPaddingMode paddingMode); + /// Creates a new RSACipherImpl object for the given RSAKey + /// and using the given padding mode. + + virtual ~RSACipherImpl(); + /// Destroys the RSACipherImpl. + + const std::string& name() const; + /// Returns the name of the Cipher. + + CryptoTransform* createEncryptor(); + /// Creates an encrytor object. + + CryptoTransform* createDecryptor(); + /// Creates a decrytor object. + +private: + RSAKey _key; + RSAPaddingMode _paddingMode; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// Inlines +// +inline const std::string& RSACipherImpl::name() const +{ + return _key.name(); +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_RSACipherImpl_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/RSADigestEngine.h b/contrib/libpoco/Crypto/include/Poco/Crypto/RSADigestEngine.h new file mode 100644 index 00000000000..e4e8479151a --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/RSADigestEngine.h @@ -0,0 +1,113 @@ +// +// RSADigestEngine.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/RSADigestEngine.h#1 $ +// +// Library: Crypto +// Package: RSA +// Module: RSADigestEngine +// +// Definition of the RSADigestEngine class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_RSADigestEngine_INCLUDED +#define Crypto_RSADigestEngine_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/RSAKey.h" +#include "Poco/DigestEngine.h" +#include "Poco/Crypto/DigestEngine.h" +#include +#include + + +namespace Poco { +namespace Crypto { + + +class Crypto_API RSADigestEngine: public Poco::DigestEngine + /// This class implements a Poco::DigestEngine that can be + /// used to compute a secure digital signature. + /// + /// First another Poco::Crypto::DigestEngine is created and + /// used to compute a cryptographic hash of the data to be + /// signed. Then, the hash value is encrypted, using + /// the RSA private key. + /// + /// To verify a signature, pass it to the verify() + /// member function. It will decrypt the signature + /// using the RSA public key and compare the resulting + /// hash with the actual hash of the data. +{ +public: + enum DigestType + { + DIGEST_MD5, + DIGEST_SHA1 + }; + + //@ deprecated + RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1); + /// Creates the RSADigestEngine with the given RSA key, + /// using the MD5 or SHA-1 hash algorithm. + /// Kept for backward compatibility + + RSADigestEngine(const RSAKey& key, const std::string &name); + /// Creates the RSADigestEngine with the given RSA key, + /// using the hash algorithm with the given name + /// (e.g., "MD5", "SHA1", "SHA256", "SHA512", etc.). + /// See the OpenSSL documentation for a list of supported digest algorithms. + /// + /// Throws a Poco::NotFoundException if no algorithm with the given name exists. + + ~RSADigestEngine(); + /// Destroys the RSADigestEngine. + + std::size_t digestLength() const; + /// Returns the length of the digest in bytes. + + void reset(); + /// Resets the engine so that a new + /// digest can be computed. + + const DigestEngine::Digest& digest(); + /// Finishes the computation of the digest + /// (the first time it's called) and + /// returns the message digest. + /// + /// Can be called multiple times. + + const DigestEngine::Digest& signature(); + /// Signs the digest using the RSA algorithm + /// and the private key (teh first time it's + /// called) and returns the result. + /// + /// Can be called multiple times. + + bool verify(const DigestEngine::Digest& signature); + /// Verifies the data against the signature. + /// + /// Returns true if the signature can be verified, false otherwise. + +protected: + void updateImpl(const void* data, std::size_t length); + +private: + RSAKey _key; + Poco::Crypto::DigestEngine _engine; + Poco::DigestEngine::Digest _digest; + Poco::DigestEngine::Digest _signature; +}; + + +} } // namespace Poco::Crypto + + +#endif // Crypto_RSADigestEngine_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKey.h b/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKey.h new file mode 100644 index 00000000000..a6a6bc206db --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKey.h @@ -0,0 +1,133 @@ +// +// RSAKey.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/RSAKey.h#2 $ +// +// Library: Crypto +// Package: RSA +// Module: RSAKey +// +// Definition of the RSAKey class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_RSAKey_INCLUDED +#define Crypto_RSAKey_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/RSAKeyImpl.h" + + +namespace Poco { +namespace Crypto { + + +class X509Certificate; + + +class Crypto_API RSAKey + /// This class stores an RSA key pair, consisting + /// of private and public key. Storage of the private + /// key is optional. + /// + /// If a private key is available, the RSAKey can be + /// used for decrypting data (encrypted with the public key) + /// or computing secure digital signatures. +{ +public: + enum KeyLength + { + KL_512 = 512, + KL_1024 = 1024, + KL_2048 = 2048, + KL_4096 = 4096 + }; + + enum Exponent + { + EXP_SMALL = 0, + EXP_LARGE + }; + + explicit RSAKey(const X509Certificate& cert); + /// Extracts the RSA public key from the given certificate. + + RSAKey(KeyLength keyLength, Exponent exp); + /// Creates the RSAKey. Creates a new public/private keypair using the given parameters. + /// Can be used to sign data and verify signatures. + + RSAKey(const std::string& publicKeyFile, const std::string& privateKeyFile = "", const std::string& privateKeyPassphrase = ""); + /// Creates the RSAKey, by reading public and private key from the given files and + /// using the given passphrase for the private key. + /// + /// Cannot be used for signing or decryption unless a private key is available. + /// + /// If a private key is specified, you don't need to specify a public key file. + /// OpenSSL will auto-create the public key from the private key. + + RSAKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = ""); + /// Creates the RSAKey, by reading public and private key from the given streams and + /// using the given passphrase for the private key. + /// + /// Cannot be used for signing or decryption unless a private key is available. + /// + /// If a private key is specified, you don't need to specify a public key file. + /// OpenSSL will auto-create the public key from the private key. + + ~RSAKey(); + /// Destroys the RSAKey. + + int size() const; + /// Returns the RSA modulus size. + + RSAKeyImpl::ByteVec modulus() const; + /// Returns the RSA modulus. + + RSAKeyImpl::ByteVec encryptionExponent() const; + /// Returns the RSA encryption exponent. + + RSAKeyImpl::ByteVec decryptionExponent() const; + /// Returns the RSA decryption exponent. + + void save(const std::string& publicKeyFile, const std::string& privateKeyFile = "", const std::string& privateKeyPassphrase = ""); + /// Exports the public and private keys to the given files. + /// + /// If an empty filename is specified, the corresponding key + /// is not exported. + + void save(std::ostream* pPublicKeyStream, std::ostream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = ""); + /// Exports the public and private key to the given streams. + /// + /// If a null pointer is passed for a stream, the corresponding + /// key is not exported. + + RSAKeyImpl::Ptr impl() const; + /// Returns the impl object. + + const std::string& name() const; + /// Returns "rsa" + +private: + RSAKeyImpl::Ptr _pImpl; +}; + + +// +// inlines +// +inline RSAKeyImpl::Ptr RSAKey::impl() const +{ + return _pImpl; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_RSAKey_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKeyImpl.h b/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKeyImpl.h new file mode 100644 index 00000000000..f439a3d1874 --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/RSAKeyImpl.h @@ -0,0 +1,131 @@ +// +// RSAKeyImpl.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/RSAKeyImpl.h#3 $ +// +// Library: Crypto +// Package: RSA +// Module: RSAKeyImpl +// +// Definition of the RSAKeyImpl class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_RSAKeyImplImpl_INCLUDED +#define Crypto_RSAKeyImplImpl_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include "Poco/RefCountedObject.h" +#include "Poco/AutoPtr.h" +#include +#include +#include + + +struct bignum_st; +struct rsa_st; +typedef struct bignum_st BIGNUM; +typedef struct rsa_st RSA; + + +namespace Poco { +namespace Crypto { + + +class X509Certificate; + + +class RSAKeyImpl: public Poco::RefCountedObject + /// class RSAKeyImpl +{ +public: + typedef Poco::AutoPtr Ptr; + typedef std::vector ByteVec; + + explicit RSAKeyImpl(const X509Certificate& cert); + /// Extracts the RSA public key from the given certificate. + + RSAKeyImpl(int keyLength, unsigned long exponent); + /// Creates the RSAKey. Creates a new public/private keypair using the given parameters. + /// Can be used to sign data and verify signatures. + + RSAKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase); + /// Creates the RSAKey, by reading public and private key from the given files and + /// using the given passphrase for the private key. Can only by used for signing if + /// a private key is available. + + RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase); + /// Creates the RSAKey. Can only by used for signing if pPrivKey + /// is not null. If a private key file is specified, you don't need to + /// specify a public key file. OpenSSL will auto-create it from the private key. + + ~RSAKeyImpl(); + /// Destroys the RSAKeyImpl. + + RSA* getRSA(); + /// Returns the OpenSSL RSA object. + + const RSA* getRSA() const; + /// Returns the OpenSSL RSA object. + + int size() const; + /// Returns the RSA modulus size. + + ByteVec modulus() const; + /// Returns the RSA modulus. + + ByteVec encryptionExponent() const; + /// Returns the RSA encryption exponent. + + ByteVec decryptionExponent() const; + /// Returns the RSA decryption exponent. + + void save(const std::string& publicKeyFile, const std::string& privateKeyFile = "", const std::string& privateKeyPassphrase = ""); + /// Exports the public and private keys to the given files. + /// + /// If an empty filename is specified, the corresponding key + /// is not exported. + + void save(std::ostream* pPublicKeyStream, std::ostream* pPrivateKeyStream = 0, const std::string& privateKeyPassphrase = ""); + /// Exports the public and private key to the given streams. + /// + /// If a null pointer is passed for a stream, the corresponding + /// key is not exported. + +private: + void freeRSA(); + + static ByteVec convertToByteVec(const BIGNUM* bn); + +private: + RSA* _pRSA; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// inlines +// +inline RSA* RSAKeyImpl::getRSA() +{ + return _pRSA; +} + + +inline const RSA* RSAKeyImpl::getRSA() const +{ + return _pRSA; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_RSAKeyImplImpl_INCLUDED diff --git a/contrib/libpoco/Crypto/include/Poco/Crypto/X509Certificate.h b/contrib/libpoco/Crypto/include/Poco/Crypto/X509Certificate.h new file mode 100644 index 00000000000..472c537637e --- /dev/null +++ b/contrib/libpoco/Crypto/include/Poco/Crypto/X509Certificate.h @@ -0,0 +1,185 @@ +// +// X509Certificate.h +// +// $Id: //poco/1.4/Crypto/include/Poco/Crypto/X509Certificate.h#2 $ +// +// Library: Crypto +// Package: Certificate +// Module: X509Certificate +// +// Definition of the X509Certificate class. +// +// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Crypto_X509Certificate_INCLUDED +#define Crypto_X509Certificate_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "Poco/Crypto/OpenSSLInitializer.h" +#include "Poco/DateTime.h" +#include "Poco/SharedPtr.h" +#include +#include +#include + + +namespace Poco { +namespace Crypto { + + +class Crypto_API X509Certificate + /// This class represents a X509 Certificate. +{ +public: + enum NID + /// Name identifier for extracting information from + /// a certificate subject's or issuer's distinguished name. + { + NID_COMMON_NAME = 13, + NID_COUNTRY = 14, + NID_LOCALITY_NAME = 15, + NID_STATE_OR_PROVINCE = 16, + NID_ORGANIZATION_NAME = 17, + NID_ORGANIZATION_UNIT_NAME = 18 + }; + + explicit X509Certificate(std::istream& istr); + /// Creates the X509Certificate object by reading + /// a certificate in PEM format from a stream. + + explicit X509Certificate(const std::string& path); + /// Creates the X509Certificate object by reading + /// a certificate in PEM format from a file. + + explicit X509Certificate(X509* pCert); + /// Creates the X509Certificate from an existing + /// OpenSSL certificate. Ownership is taken of + /// the certificate. + + X509Certificate(X509* pCert, bool shared); + /// Creates the X509Certificate from an existing + /// OpenSSL certificate. Ownership is taken of + /// the certificate. If shared is true, the + /// certificate's reference count is incremented. + + X509Certificate(const X509Certificate& cert); + /// Creates the certificate by copying another one. + + X509Certificate& operator = (const X509Certificate& cert); + /// Assigns a certificate. + + void swap(X509Certificate& cert); + /// Exchanges the certificate with another one. + + ~X509Certificate(); + /// Destroys the X509Certificate. + + const std::string& issuerName() const; + /// Returns the certificate issuer's distinguished name. + + std::string issuerName(NID nid) const; + /// Extracts the information specified by the given + /// NID (name identifier) from the certificate issuer's + /// distinguished name. + + const std::string& subjectName() const; + /// Returns the certificate subject's distinguished name. + + std::string subjectName(NID nid) const; + /// Extracts the information specified by the given + /// NID (name identifier) from the certificate subject's + /// distinguished name. + + std::string commonName() const; + /// Returns the common name stored in the certificate + /// subject's distinguished name. + + void extractNames(std::string& commonName, std::set& domainNames) const; + /// Extracts the common name and the alias domain names from the + /// certificate. + + Poco::DateTime validFrom() const; + /// Returns the date and time the certificate is valid from. + + Poco::DateTime expiresOn() const; + /// Returns the date and time the certificate expires. + + void save(std::ostream& stream) const; + /// Writes the certificate to the given stream. + /// The certificate is written in PEM format. + + void save(const std::string& path) const; + /// Writes the certificate to the file given by path. + /// The certificate is written in PEM format. + + bool issuedBy(const X509Certificate& issuerCertificate) const; + /// Checks whether the certificate has been issued by + /// the issuer given by issuerCertificate. This can be + /// used to validate a certificate chain. + /// + /// Verifies if the certificate has been signed with the + /// issuer's private key, using the public key from the issuer + /// certificate. + /// + /// Returns true if verification against the issuer certificate + /// was successfull, false otherwise. + + const X509* certificate() const; + /// Returns the underlying OpenSSL certificate. + +protected: + void load(std::istream& stream); + /// Loads the certificate from the given stream. The + /// certificate must be in PEM format. + + void load(const std::string& path); + /// Loads the certificate from the given file. The + /// certificate must be in PEM format. + + void init(); + /// Extracts issuer and subject name from the certificate. + +private: + enum + { + NAME_BUFFER_SIZE = 256 + }; + + std::string _issuerName; + std::string _subjectName; + X509* _pCert; + OpenSSLInitializer _openSSLInitializer; +}; + + +// +// inlines +// +inline const std::string& X509Certificate::issuerName() const +{ + return _issuerName; +} + + +inline const std::string& X509Certificate::subjectName() const +{ + return _subjectName; +} + + +inline const X509* X509Certificate::certificate() const +{ + return _pCert; +} + + +} } // namespace Poco::Crypto + + +#endif // Crypto_X509Certificate_INCLUDED diff --git a/contrib/libpoco/Crypto/samples/CMakeLists.txt b/contrib/libpoco/Crypto/samples/CMakeLists.txt new file mode 100644 index 00000000000..2dbff2363ef --- /dev/null +++ b/contrib/libpoco/Crypto/samples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory( genrsakey ) diff --git a/contrib/libpoco/Crypto/samples/genrsakey/CMakeLists.txt b/contrib/libpoco/Crypto/samples/genrsakey/CMakeLists.txt new file mode 100644 index 00000000000..2feabdc9415 --- /dev/null +++ b/contrib/libpoco/Crypto/samples/genrsakey/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SAMPLE_NAME "genrsakey") + +set(LOCAL_SRCS "") +aux_source_directory(src LOCAL_SRCS) + +add_executable( ${SAMPLE_NAME} ${LOCAL_SRCS} ) +target_link_libraries( ${SAMPLE_NAME} PocoCrypto PocoUtil PocoXML PocoFoundation ) diff --git a/contrib/libpoco/Crypto/samples/genrsakey/src/genrsakey.cpp b/contrib/libpoco/Crypto/samples/genrsakey/src/genrsakey.cpp new file mode 100644 index 00000000000..40ffc0a163f --- /dev/null +++ b/contrib/libpoco/Crypto/samples/genrsakey/src/genrsakey.cpp @@ -0,0 +1,198 @@ +// +// genrsakey.cpp +// +// $Id: //poco/1.4/Crypto/samples/genrsakey/src/genrsakey.cpp#1 $ +// +// This sample demonstrates the XYZ class. +// +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Util/Application.h" +#include "Poco/Util/Option.h" +#include "Poco/Util/OptionException.h" +#include "Poco/Util/OptionSet.h" +#include "Poco/Util/HelpFormatter.h" +#include "Poco/Util/AbstractConfiguration.h" +#include "Poco/AutoPtr.h" +#include "Poco/NumberFormatter.h" +#include "Poco/NumberParser.h" +#include "Poco/String.h" +#include "Poco/Crypto/RSAKey.h" +#include + + +using Poco::Util::Application; +using Poco::Util::Option; +using Poco::Util::OptionSet; +using Poco::Util::HelpFormatter; +using Poco::Util::AbstractConfiguration; +using Poco::Util::OptionCallback; +using Poco::AutoPtr; +using Poco::NumberParser; +using Poco::Crypto::RSAKey; + + +class RSAApp: public Application + /// This sample demonstrates some of the features of the Util::Application class, + /// such as configuration file handling and command line arguments processing. + /// + /// Try genrsakey --help (on Unix platforms) or genrsakey /help (elsewhere) for + /// more information. +{ +public: + RSAApp(): + _helpRequested(false), + _length(RSAKey::KL_1024), + _exp(RSAKey::EXP_LARGE), + _name(), + _pwd() + { + Poco::Crypto::initializeCrypto(); + } + + ~RSAApp() + { + Poco::Crypto::uninitializeCrypto(); + } + +protected: + void initialize(Application& self) + { + loadConfiguration(); // load default configuration files, if present + Application::initialize(self); + } + + void uninitialize() + { + Application::uninitialize(); + } + + void reinitialize(Application& self) + { + Application::reinitialize(self); + } + + void defineOptions(OptionSet& options) + { + Application::defineOptions(options); + + options.addOption( + Option("help", "h", "display help information on command line arguments") + .required(false) + .repeatable(false) + .callback(OptionCallback(this, &RSAApp::handleHelp))); + + options.addOption( + Option("?", "?", "display help information on command line arguments") + .required(false) + .repeatable(false) + .callback(OptionCallback(this, &RSAApp::handleHelp))); + + options.addOption( + Option("key", "k", "define the key length") + .required(false) + .repeatable(false) + .argument("512|1024|2048|4096") + .callback(OptionCallback(this, &RSAApp::handleKeyLength))); + + options.addOption( + Option("exponent", "e", "defines the exponent of the key") + .required(false) + .repeatable(false) + .argument("small|large") + .callback(OptionCallback(this, &RSAApp::handleExponent))); + + options.addOption( + Option("file", "f", "defines the file base name. creates a file.pub and a file.priv") + .required(true) + .repeatable(false) + .argument("filebasename") + .callback(OptionCallback(this, &RSAApp::handleFilePrefix))); + + options.addOption( + Option("password", "p", "defines the password used to encrypt the private key file. If not defined user will be asked via stdin to provide in") + .required(false) + .repeatable(false) + .argument("pwd") + .callback(OptionCallback(this, &RSAApp::handlePassword))); + } + + void handleHelp(const std::string& name, const std::string& value) + { + _helpRequested = true; + displayHelp(); + stopOptionsProcessing(); + } + + void handleKeyLength(const std::string& name, const std::string& value) + { + int keyLen = Poco::NumberParser::parse(value); + if (keyLen == 512 || keyLen == 1024 || keyLen == 2048 || keyLen == 4096) + _length = (RSAKey::KeyLength)keyLen; + else + throw Poco::Util::IncompatibleOptionsException("Illegal key length value"); + } + + void handleExponent(const std::string& name, const std::string& value) + { + if (Poco::icompare(value, "small") == 0) + _exp = RSAKey::EXP_SMALL; + else + _exp = RSAKey::EXP_LARGE; + } + + void handleFilePrefix(const std::string& name, const std::string& value) + { + if (value.empty()) + throw Poco::Util::IncompatibleOptionsException("Empty file prefix forbidden"); + _name = value; + } + + void handlePassword(const std::string& name, const std::string& value) + { + _pwd = value; + } + + void displayHelp() + { + HelpFormatter helpFormatter(options()); + helpFormatter.setCommand(commandName()); + helpFormatter.setUsage("OPTIONS"); + helpFormatter.setHeader("Application for generating RSA public/private key pairs."); + helpFormatter.format(std::cout); + } + + int main(const std::vector& args) + { + if (!_helpRequested) + { + logger().information("Generating key with length " + Poco::NumberFormatter::format((int)_length)); + logger().information(std::string("Exponent is ") + ((_exp == RSAKey::EXP_SMALL)?"small":"large")); + logger().information("Generating key"); + RSAKey key(_length, _exp); + logger().information("Generating key: DONE"); + std::string pubFile(_name + ".pub"); + std::string privFile(_name + ".priv"); + + logger().information("Saving key to " + pubFile + " and " + privFile); + key.save(pubFile, privFile, _pwd); + logger().information("Key saved"); + } + return Application::EXIT_OK; + } + +private: + bool _helpRequested; + RSAKey::KeyLength _length; + RSAKey::Exponent _exp; + std::string _name; + std::string _pwd; +}; + + +POCO_APP_MAIN(RSAApp) diff --git a/contrib/libpoco/Crypto/src/Cipher.cpp b/contrib/libpoco/Crypto/src/Cipher.cpp new file mode 100644 index 00000000000..55ebff7568e --- /dev/null +++ b/contrib/libpoco/Crypto/src/Cipher.cpp @@ -0,0 +1,142 @@ +// +// Cipher.cpp +// +// $Id: //poco/1.4/Crypto/src/Cipher.cpp#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: Cipher +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/CryptoStream.h" +#include "Poco/Crypto/CryptoTransform.h" +#include "Poco/Base64Encoder.h" +#include "Poco/Base64Decoder.h" +#include "Poco/HexBinaryEncoder.h" +#include "Poco/HexBinaryDecoder.h" +#include "Poco/StreamCopier.h" +#include "Poco/Exception.h" +#include +#include + + +namespace Poco { +namespace Crypto { + + +Cipher::Cipher() +{ +} + + +Cipher::~Cipher() +{ +} + + +std::string Cipher::encryptString(const std::string& str, Encoding encoding) +{ + std::istringstream source(str); + std::ostringstream sink; + + encrypt(source, sink, encoding); + + return sink.str(); +} + + +std::string Cipher::decryptString(const std::string& str, Encoding encoding) +{ + std::istringstream source(str); + std::ostringstream sink; + + decrypt(source, sink, encoding); + return sink.str(); +} + + +void Cipher::encrypt(std::istream& source, std::ostream& sink, Encoding encoding) +{ + CryptoInputStream encryptor(source, createEncryptor()); + + switch (encoding) + { + case ENC_NONE: + StreamCopier::copyStream(encryptor, sink); + break; + + case ENC_BASE64: + case ENC_BASE64_NO_LF: + { + Poco::Base64Encoder encoder(sink); + if (encoding == ENC_BASE64_NO_LF) + { + encoder.rdbuf()->setLineLength(0); + } + StreamCopier::copyStream(encryptor, encoder); + encoder.close(); + } + break; + + case ENC_BINHEX: + case ENC_BINHEX_NO_LF: + { + Poco::HexBinaryEncoder encoder(sink); + if (encoding == ENC_BINHEX_NO_LF) + { + encoder.rdbuf()->setLineLength(0); + } + StreamCopier::copyStream(encryptor, encoder); + encoder.close(); + } + break; + + default: + throw Poco::InvalidArgumentException("Invalid argument", "encoding"); + } +} + + +void Cipher::decrypt(std::istream& source, std::ostream& sink, Encoding encoding) +{ + CryptoOutputStream decryptor(sink, createDecryptor()); + + switch (encoding) + { + case ENC_NONE: + StreamCopier::copyStream(source, decryptor); + decryptor.close(); + break; + + case ENC_BASE64: + case ENC_BASE64_NO_LF: + { + Poco::Base64Decoder decoder(source); + StreamCopier::copyStream(decoder, decryptor); + decryptor.close(); + } + break; + + case ENC_BINHEX: + case ENC_BINHEX_NO_LF: + { + Poco::HexBinaryDecoder decoder(source); + StreamCopier::copyStream(decoder, decryptor); + decryptor.close(); + } + break; + + default: + throw Poco::InvalidArgumentException("Invalid argument", "encoding"); + } +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CipherFactory.cpp b/contrib/libpoco/Crypto/src/CipherFactory.cpp new file mode 100644 index 00000000000..e8f5f6fb0cc --- /dev/null +++ b/contrib/libpoco/Crypto/src/CipherFactory.cpp @@ -0,0 +1,67 @@ +// +// CipherFactory.cpp +// +// $Id: //poco/1.4/Crypto/src/CipherFactory.cpp#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherFactory +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CipherFactory.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/CipherKey.h" +#include "Poco/Crypto/RSAKey.h" +#include "Poco/Crypto/CipherImpl.h" +#include "Poco/Crypto/RSACipherImpl.h" +#include "Poco/Exception.h" +#include "Poco/SingletonHolder.h" +#include +#include + + +namespace Poco { +namespace Crypto { + + +CipherFactory::CipherFactory() +{ +} + + +CipherFactory::~CipherFactory() +{ +} + + +namespace +{ + static Poco::SingletonHolder holder; +} + + +CipherFactory& CipherFactory::defaultFactory() +{ + return *holder.get(); +} + + +Cipher* CipherFactory::createCipher(const CipherKey& key) +{ + return new CipherImpl(key); +} + + +Cipher* CipherFactory::createCipher(const RSAKey& key, RSAPaddingMode paddingMode) +{ + return new RSACipherImpl(key, paddingMode); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CipherImpl.cpp b/contrib/libpoco/Crypto/src/CipherImpl.cpp new file mode 100644 index 00000000000..c953aae52e2 --- /dev/null +++ b/contrib/libpoco/Crypto/src/CipherImpl.cpp @@ -0,0 +1,191 @@ +// +// CipherImpl.cpp +// +// $Id: //poco/1.4/Crypto/src/CipherImpl.cpp#3 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherImpl +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CipherImpl.h" +#include "Poco/Crypto/CryptoTransform.h" +#include "Poco/Exception.h" +#include + + +namespace Poco { +namespace Crypto { + + +namespace +{ + void throwError() + { + unsigned long err; + std::string msg; + + while ((err = ERR_get_error())) + { + if (!msg.empty()) + msg.append("; "); + msg.append(ERR_error_string(err, 0)); + } + + throw Poco::IOException(msg); + } + + + class CryptoTransformImpl: public CryptoTransform + { + public: + typedef Cipher::ByteVec ByteVec; + + enum Direction + { + DIR_ENCRYPT, + DIR_DECRYPT + }; + + CryptoTransformImpl( + const EVP_CIPHER* pCipher, + const ByteVec& key, + const ByteVec& iv, + Direction dir); + + ~CryptoTransformImpl(); + + std::size_t blockSize() const; + + int setPadding(int padding); + + std::streamsize transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength); + + std::streamsize finalize( + unsigned char* output, + std::streamsize length); + + private: + const EVP_CIPHER* _pCipher; + EVP_CIPHER_CTX _ctx; + ByteVec _key; + ByteVec _iv; + }; + + + CryptoTransformImpl::CryptoTransformImpl( + const EVP_CIPHER* pCipher, + const ByteVec& key, + const ByteVec& iv, + Direction dir): + _pCipher(pCipher), + _key(key), + _iv(iv) + { + EVP_CipherInit( + &_ctx, + _pCipher, + &_key[0], + _iv.empty() ? 0 : &_iv[0], + (dir == DIR_ENCRYPT) ? 1 : 0); + } + + + CryptoTransformImpl::~CryptoTransformImpl() + { + EVP_CIPHER_CTX_cleanup(&_ctx); + } + + + std::size_t CryptoTransformImpl::blockSize() const + { + return EVP_CIPHER_CTX_block_size(&_ctx); + } + + + int CryptoTransformImpl::setPadding(int padding) + { + return EVP_CIPHER_CTX_set_padding(&_ctx, padding); + } + + + std::streamsize CryptoTransformImpl::transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength) + { + poco_assert (outputLength >= std::streamsize(inputLength + blockSize() - 1)); + + int outLen = static_cast(outputLength); + int rc = EVP_CipherUpdate( + &_ctx, + output, + &outLen, + input, + static_cast(inputLength)); + + if (rc == 0) + throwError(); + + return static_cast(outLen); + } + + + std::streamsize CryptoTransformImpl::finalize( + unsigned char* output, + std::streamsize length) + { + poco_assert (length >= (std::streamsize)blockSize()); + + int len = static_cast(length); + + // Use the '_ex' version that does not perform implicit cleanup since we + // will call EVP_CIPHER_CTX_cleanup() from the dtor as there is no + // guarantee that finalize() will be called if an error occurred. + int rc = EVP_CipherFinal_ex(&_ctx, output, &len); + + if (rc == 0) + throwError(); + + return static_cast(len); + } +} + + +CipherImpl::CipherImpl(const CipherKey& key): + _key(key) +{ +} + + +CipherImpl::~CipherImpl() +{ +} + + +CryptoTransform* CipherImpl::createEncryptor() +{ + CipherKeyImpl::Ptr p = _key.impl(); + return new CryptoTransformImpl(p->cipher(), p->getKey(), p->getIV(), CryptoTransformImpl::DIR_ENCRYPT); +} + + +CryptoTransform* CipherImpl::createDecryptor() +{ + CipherKeyImpl::Ptr p = _key.impl(); + return new CryptoTransformImpl(p->cipher(), p->getKey(), p->getIV(), CryptoTransformImpl::DIR_DECRYPT); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CipherKey.cpp b/contrib/libpoco/Crypto/src/CipherKey.cpp new file mode 100644 index 00000000000..eb535e9d89a --- /dev/null +++ b/contrib/libpoco/Crypto/src/CipherKey.cpp @@ -0,0 +1,47 @@ +// +// CipherKey.cpp +// +// $Id: //poco/1.4/Crypto/src/CipherKey.cpp#1 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherKey +// +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CipherKey.h" + + +namespace Poco { +namespace Crypto { + + +CipherKey::CipherKey(const std::string& name, const std::string& passphrase, const std::string& salt, int iterationCount): + _pImpl(new CipherKeyImpl(name, passphrase, salt, iterationCount)) +{ +} + + +CipherKey::CipherKey(const std::string& name, const ByteVec& key, const ByteVec& iv): + _pImpl(new CipherKeyImpl(name, key, iv)) +{ +} + + +CipherKey::CipherKey(const std::string& name): + _pImpl(new CipherKeyImpl(name)) +{ +} + + +CipherKey::~CipherKey() +{ +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CipherKeyImpl.cpp b/contrib/libpoco/Crypto/src/CipherKeyImpl.cpp new file mode 100644 index 00000000000..58b51c9424d --- /dev/null +++ b/contrib/libpoco/Crypto/src/CipherKeyImpl.cpp @@ -0,0 +1,198 @@ +// +// CipherKeyImpl.cpp +// +// $Id: //poco/1.4/Crypto/src/CipherKeyImpl.cpp#1 $ +// +// Library: Crypto +// Package: Cipher +// Module: CipherKeyImpl +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CipherKeyImpl.h" +#include "Poco/Crypto/CryptoTransform.h" +#include "Poco/Crypto/CipherFactory.h" +#include "Poco/Exception.h" +#include "Poco/RandomStream.h" +#include +#include + + +namespace Poco { +namespace Crypto { + + +CipherKeyImpl::CipherKeyImpl(const std::string& name, + const std::string& passphrase, + const std::string& salt, + int iterationCount): + _pCipher(0), + _name(name), + _key(), + _iv() +{ + // dummy access to Cipherfactory so that the EVP lib is initilaized + CipherFactory::defaultFactory(); + _pCipher = EVP_get_cipherbyname(name.c_str()); + + if (!_pCipher) + throw Poco::NotFoundException("Cipher " + name + " was not found"); + _key = ByteVec(keySize()); + _iv = ByteVec(ivSize()); + generateKey(passphrase, salt, iterationCount); +} + + +CipherKeyImpl::CipherKeyImpl(const std::string& name, + const ByteVec& key, + const ByteVec& iv): + _pCipher(0), + _name(name), + _key(key), + _iv(iv) +{ + // dummy access to Cipherfactory so that the EVP lib is initilaized + CipherFactory::defaultFactory(); + _pCipher = EVP_get_cipherbyname(name.c_str()); + + if (!_pCipher) + throw Poco::NotFoundException("Cipher " + name + " was not found"); +} + + +CipherKeyImpl::CipherKeyImpl(const std::string& name): + _pCipher(0), + _name(name), + _key(), + _iv() +{ + // dummy access to Cipherfactory so that the EVP lib is initilaized + CipherFactory::defaultFactory(); + _pCipher = EVP_get_cipherbyname(name.c_str()); + + if (!_pCipher) + throw Poco::NotFoundException("Cipher " + name + " was not found"); + _key = ByteVec(keySize()); + _iv = ByteVec(ivSize()); + generateKey(); +} + + +CipherKeyImpl::~CipherKeyImpl() +{ +} + + +CipherKeyImpl::Mode CipherKeyImpl::mode() const +{ + switch (EVP_CIPHER_mode(_pCipher)) + { + case EVP_CIPH_STREAM_CIPHER: + return MODE_STREAM_CIPHER; + + case EVP_CIPH_ECB_MODE: + return MODE_ECB; + + case EVP_CIPH_CBC_MODE: + return MODE_CBC; + + case EVP_CIPH_CFB_MODE: + return MODE_CFB; + + case EVP_CIPH_OFB_MODE: + return MODE_OFB; + } + throw Poco::IllegalStateException("Unexpected value of EVP_CIPHER_mode()"); +} + + +void CipherKeyImpl::generateKey() +{ + ByteVec vec; + + getRandomBytes(vec, keySize()); + setKey(vec); + + getRandomBytes(vec, ivSize()); + setIV(vec); +} + + +void CipherKeyImpl::getRandomBytes(ByteVec& vec, std::size_t count) +{ + Poco::RandomInputStream random; + + vec.clear(); + vec.reserve(count); + + for (std::size_t i = 0; i < count; ++i) + vec.push_back(static_cast(random.get())); +} + + +void CipherKeyImpl::generateKey( + const std::string& password, + const std::string& salt, + int iterationCount) +{ + unsigned char keyBytes[EVP_MAX_KEY_LENGTH]; + unsigned char ivBytes[EVP_MAX_IV_LENGTH]; + + // OpenSSL documentation specifies that the salt must be an 8-byte array. + unsigned char saltBytes[8]; + + if (!salt.empty()) + { + int len = static_cast(salt.size()); + // Create the salt array from the salt string + for (int i = 0; i < 8; ++i) + saltBytes[i] = salt.at(i % len); + for (int i = 8; i < len; ++i) + saltBytes[i % 8] ^= salt.at(i); + } + + // Now create the key and IV, using the MD5 digest algorithm. + int keySize = EVP_BytesToKey( + _pCipher, + EVP_md5(), + (salt.empty() ? 0 : saltBytes), + reinterpret_cast(password.data()), + static_cast(password.size()), + iterationCount, + keyBytes, + ivBytes); + + // Copy the buffers to our member byte vectors. + _key.assign(keyBytes, keyBytes + keySize); + + if (ivSize() == 0) + _iv.clear(); + else + _iv.assign(ivBytes, ivBytes + ivSize()); +} + + +int CipherKeyImpl::keySize() const +{ + return EVP_CIPHER_key_length(_pCipher); +} + + +int CipherKeyImpl::blockSize() const +{ + return EVP_CIPHER_block_size(_pCipher); +} + + +int CipherKeyImpl::ivSize() const +{ + return EVP_CIPHER_iv_length(_pCipher); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CryptoStream.cpp b/contrib/libpoco/Crypto/src/CryptoStream.cpp new file mode 100644 index 00000000000..34ce13b4c39 --- /dev/null +++ b/contrib/libpoco/Crypto/src/CryptoStream.cpp @@ -0,0 +1,357 @@ +// +// CryptoStream.cpp +// +// $Id: //poco/1.4/Crypto/src/CryptoStream.cpp#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CryptoStream +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CryptoStream.h" +#include "Poco/Crypto/CryptoTransform.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Exception.h" +#include + + +#undef min +#undef max + + +namespace Poco { +namespace Crypto { + + +// +// CryptoStreamBuf +// + + +CryptoStreamBuf::CryptoStreamBuf(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize): + Poco::BufferedStreamBuf(bufferSize, std::ios::in), + _pTransform(pTransform), + _pIstr(&istr), + _pOstr(0), + _eof(false), + _buffer(static_cast(bufferSize)) +{ + poco_check_ptr (pTransform); + poco_assert ((size_t)bufferSize > 2 * pTransform->blockSize()); +} + + +CryptoStreamBuf::CryptoStreamBuf(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize): + Poco::BufferedStreamBuf(bufferSize, std::ios::out), + _pTransform(pTransform), + _pIstr(0), + _pOstr(&ostr), + _eof(false), + _buffer(static_cast(bufferSize)) +{ + poco_check_ptr (pTransform); + poco_assert ((size_t)bufferSize > 2 * pTransform->blockSize()); +} + + +CryptoStreamBuf::~CryptoStreamBuf() +{ + try + { + close(); + } + catch (...) + { + } + delete _pTransform; +} + + +void CryptoStreamBuf::close() +{ + sync(); + + if (_pIstr) + { + _pIstr = 0; + } + else if (_pOstr) + { + // Close can be called multiple times. By zeroing the pointer we make + // sure that we call finalize() only once, even if an exception is + // thrown. + std::ostream* pOstr = _pOstr; + _pOstr = 0; + + // Finalize transformation. + std::streamsize n = _pTransform->finalize(_buffer.begin(), static_cast(_buffer.size())); + + if (n > 0) + { + pOstr->write(reinterpret_cast(_buffer.begin()), n); + if (!pOstr->good()) + throw Poco::IOException("Output stream failure"); + } + } +} + + +int CryptoStreamBuf::readFromDevice(char* buffer, std::streamsize length) +{ + if (!_pIstr) + return 0; + + int count = 0; + + while (!_eof) + { + int m = (static_cast(length) - count)/2 - static_cast(_pTransform->blockSize()); + + // Make sure we can read at least one more block. Explicitely check + // for m < 0 since blockSize() returns an unsigned int and the + // comparison might give false results for m < 0. + if (m <= 0) + break; + + int n = 0; + + if (_pIstr->good()) + { + _pIstr->read(reinterpret_cast(_buffer.begin()), m); + n = static_cast(_pIstr->gcount()); + } + + if (n == 0) + { + _eof = true; + + // No more data, finalize transformation + count += static_cast(_pTransform->finalize( + reinterpret_cast(buffer + count), + static_cast(length) - count)); + } + else + { + // Transform next chunk of data + count += static_cast(_pTransform->transform( + _buffer.begin(), + n, + reinterpret_cast(buffer + count), + static_cast(length) - count)); + } + } + + return count; +} + + +int CryptoStreamBuf::writeToDevice(const char* buffer, std::streamsize length) +{ + if (!_pOstr) + return 0; + + std::size_t maxChunkSize = _buffer.size()/2; + std::size_t count = 0; + + while (count < (size_t)length) + { + // Truncate chunk size so that the maximum output fits into _buffer. + std::size_t n = static_cast(length) - count; + if (n > maxChunkSize) + n = maxChunkSize; + + // Transform next chunk of data + std::streamsize k = _pTransform->transform( + reinterpret_cast(buffer + count), + static_cast(n), + _buffer.begin(), + static_cast(_buffer.size())); + + // Attention: (n != k) might be true. In count, we have to track how + // many bytes from buffer have been consumed, not how many bytes have + // been written to _pOstr! + count += n; + + if (k > 0) + { + _pOstr->write(reinterpret_cast(_buffer.begin()), k); + if (!_pOstr->good()) + throw Poco::IOException("Output stream failure"); + } + } + + return static_cast(count); +} + + +// +// CryptoIOS +// + + +CryptoIOS::CryptoIOS(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize): + _buf(istr, pTransform, bufferSize) +{ + poco_ios_init(&_buf); +} + + +CryptoIOS::CryptoIOS(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize): + _buf(ostr, pTransform, bufferSize) +{ + poco_ios_init(&_buf); +} + + +CryptoIOS::~CryptoIOS() +{ +} + + +CryptoStreamBuf* CryptoIOS::rdbuf() +{ + return &_buf; +} + + +// +// CryptoInputStream +// + + +CryptoInputStream::CryptoInputStream(std::istream& istr, CryptoTransform* pTransform, std::streamsize bufferSize): + CryptoIOS(istr, pTransform, bufferSize), + std::istream(&_buf) +{ +} + + +CryptoInputStream::CryptoInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(istr, cipher.createEncryptor(), bufferSize), + std::istream(&_buf) +{ +} + + +CryptoInputStream::~CryptoInputStream() +{ +} + + +// +// CryptoOutputStream +// + + +CryptoOutputStream::CryptoOutputStream(std::ostream& ostr, CryptoTransform* pTransform, std::streamsize bufferSize): + CryptoIOS(ostr, pTransform, bufferSize), + std::ostream(&_buf) +{ +} + + +CryptoOutputStream::CryptoOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(ostr, cipher.createDecryptor(), bufferSize), + std::ostream(&_buf) +{ +} + + +CryptoOutputStream::~CryptoOutputStream() +{ +} + + +void CryptoOutputStream::close() +{ + _buf.close(); +} + + +// +// EncryptingInputStream +// + + +EncryptingInputStream::EncryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(istr, cipher.createEncryptor(), bufferSize), + std::istream(&_buf) +{ +} + + +EncryptingInputStream::~EncryptingInputStream() +{ +} + + +// +// EncryptingOuputStream +// + + +EncryptingOutputStream::EncryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(ostr, cipher.createEncryptor(), bufferSize), + std::ostream(&_buf) +{ +} + + +EncryptingOutputStream::~EncryptingOutputStream() +{ +} + + +void EncryptingOutputStream::close() +{ + _buf.close(); +} + + +// +// DecryptingInputStream +// + + +DecryptingInputStream::DecryptingInputStream(std::istream& istr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(istr, cipher.createDecryptor(), bufferSize), + std::istream(&_buf) +{ +} + + +DecryptingInputStream::~DecryptingInputStream() +{ +} + + +// +// DecryptingOuputStream +// + + +DecryptingOutputStream::DecryptingOutputStream(std::ostream& ostr, Cipher& cipher, std::streamsize bufferSize): + CryptoIOS(ostr, cipher.createDecryptor(), bufferSize), + std::ostream(&_buf) +{ +} + + +DecryptingOutputStream::~DecryptingOutputStream() +{ +} + + +void DecryptingOutputStream::close() +{ + _buf.close(); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/CryptoTransform.cpp b/contrib/libpoco/Crypto/src/CryptoTransform.cpp new file mode 100644 index 00000000000..20157ab7d0c --- /dev/null +++ b/contrib/libpoco/Crypto/src/CryptoTransform.cpp @@ -0,0 +1,40 @@ +// +// CryptoTransform.cpp +// +// $Id: //poco/1.4/Crypto/src/CryptoTransform.cpp#2 $ +// +// Library: Crypto +// Package: Cipher +// Module: CryptoTransform +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/CryptoTransform.h" + + +namespace Poco { +namespace Crypto { + + +CryptoTransform::CryptoTransform() +{ +} + + +CryptoTransform::~CryptoTransform() +{ +} + + +int CryptoTransform::setPadding(int padding) +{ + return 1; +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/DigestEngine.cpp b/contrib/libpoco/Crypto/src/DigestEngine.cpp new file mode 100644 index 00000000000..6e574ab42e1 --- /dev/null +++ b/contrib/libpoco/Crypto/src/DigestEngine.cpp @@ -0,0 +1,77 @@ +// +// DigestEngine.cpp +// +// $Id: //poco/1.4/Crypto/src/DigestEngine.cpp#1 $ +// +// Library: Crypto +// Package: Digest +// Module: DigestEngine +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/DigestEngine.h" +#include "Poco/Exception.h" + + +namespace Poco { +namespace Crypto { + + +DigestEngine::DigestEngine(const std::string& name): + _name(name) +{ + const EVP_MD* md = EVP_get_digestbyname(_name.c_str()); + if (!md) throw Poco::NotFoundException(_name); + _ctx = EVP_MD_CTX_create(); + EVP_DigestInit_ex(_ctx, md, NULL); +} + + +DigestEngine::~DigestEngine() +{ + EVP_MD_CTX_destroy(_ctx); +} + +int DigestEngine::nid() const +{ + return EVP_MD_nid(_ctx->digest); +} + +std::size_t DigestEngine::digestLength() const +{ + return EVP_MD_CTX_size(_ctx); +} + + +void DigestEngine::reset() +{ + EVP_MD_CTX_cleanup(_ctx); + const EVP_MD* md = EVP_get_digestbyname(_name.c_str()); + if (!md) throw Poco::NotFoundException(_name); + EVP_DigestInit_ex(_ctx, md, NULL); +} + + +const Poco::DigestEngine::Digest& DigestEngine::digest() +{ + _digest.clear(); + unsigned len = EVP_MD_CTX_size(_ctx); + _digest.resize(len); + EVP_DigestFinal_ex(_ctx, &_digest[0], &len); + reset(); + return _digest; +} + + +void DigestEngine::updateImpl(const void* data, std::size_t length) +{ + EVP_DigestUpdate(_ctx, data, length); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/OpenSSLInitializer.cpp b/contrib/libpoco/Crypto/src/OpenSSLInitializer.cpp new file mode 100644 index 00000000000..28ecd115de0 --- /dev/null +++ b/contrib/libpoco/Crypto/src/OpenSSLInitializer.cpp @@ -0,0 +1,166 @@ +// +// OpenSSLInitializer.cpp +// +// $Id: //poco/1.4/Crypto/src/OpenSSLInitializer.cpp#3 $ +// +// Library: Crypto +// Package: CryptoCore +// Module: OpenSSLInitializer +// +// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/OpenSSLInitializer.h" +#include "Poco/RandomStream.h" +#include "Poco/Thread.h" +#include +#include +#include +#include +#if OPENSSL_VERSION_NUMBER >= 0x0907000L +#include +#endif + + +using Poco::RandomInputStream; +using Poco::Thread; + + +namespace Poco { +namespace Crypto { + + +Poco::FastMutex* OpenSSLInitializer::_mutexes(0); +Poco::AtomicCounter OpenSSLInitializer::_rc; + + +OpenSSLInitializer::OpenSSLInitializer() +{ + initialize(); +} + + +OpenSSLInitializer::~OpenSSLInitializer() +{ + try + { + uninitialize(); + } + catch (...) + { + poco_unexpected(); + } +} + + +void OpenSSLInitializer::initialize() +{ + if (++_rc == 1) + { +#if OPENSSL_VERSION_NUMBER >= 0x0907000L + OPENSSL_config(NULL); +#endif + SSL_library_init(); + SSL_load_error_strings(); + OpenSSL_add_all_algorithms(); + + char seed[SEEDSIZE]; + RandomInputStream rnd; + rnd.read(seed, sizeof(seed)); + RAND_seed(seed, SEEDSIZE); + + int nMutexes = CRYPTO_num_locks(); + _mutexes = new Poco::FastMutex[nMutexes]; + CRYPTO_set_locking_callback(&OpenSSLInitializer::lock); +#ifndef POCO_OS_FAMILY_WINDOWS +// Not needed on Windows (see SF #110: random unhandled exceptions when linking with ssl). +// https://sourceforge.net/p/poco/bugs/110/ +// +// From http://www.openssl.org/docs/crypto/threads.html : +// "If the application does not register such a callback using CRYPTO_THREADID_set_callback(), +// then a default implementation is used - on Windows and BeOS this uses the system's +// default thread identifying APIs" + CRYPTO_set_id_callback(&OpenSSLInitializer::id); +#endif + CRYPTO_set_dynlock_create_callback(&OpenSSLInitializer::dynlockCreate); + CRYPTO_set_dynlock_lock_callback(&OpenSSLInitializer::dynlock); + CRYPTO_set_dynlock_destroy_callback(&OpenSSLInitializer::dynlockDestroy); + } +} + + +void OpenSSLInitializer::uninitialize() +{ + if (--_rc == 0) + { + EVP_cleanup(); + ERR_free_strings(); + CRYPTO_set_locking_callback(0); +#ifndef POCO_OS_FAMILY_WINDOWS + CRYPTO_set_id_callback(0); +#endif + delete [] _mutexes; + + CONF_modules_free(); + } +} + + +void OpenSSLInitializer::lock(int mode, int n, const char* file, int line) +{ + if (mode & CRYPTO_LOCK) + _mutexes[n].lock(); + else + _mutexes[n].unlock(); +} + + +unsigned long OpenSSLInitializer::id() +{ + // Note: we use an old-style C cast here because + // neither static_cast<> nor reinterpret_cast<> + // work uniformly across all platforms. + return (unsigned long) Poco::Thread::currentTid(); +} + + +struct CRYPTO_dynlock_value* OpenSSLInitializer::dynlockCreate(const char* file, int line) +{ + return new CRYPTO_dynlock_value; +} + + +void OpenSSLInitializer::dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line) +{ + poco_check_ptr (lock); + + if (mode & CRYPTO_LOCK) + lock->_mutex.lock(); + else + lock->_mutex.unlock(); +} + + +void OpenSSLInitializer::dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line) +{ + delete lock; +} + + +void initializeCrypto() +{ + OpenSSLInitializer::initialize(); +} + + +void uninitializeCrypto() +{ + OpenSSLInitializer::uninitialize(); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/RSACipherImpl.cpp b/contrib/libpoco/Crypto/src/RSACipherImpl.cpp new file mode 100644 index 00000000000..01b23851282 --- /dev/null +++ b/contrib/libpoco/Crypto/src/RSACipherImpl.cpp @@ -0,0 +1,320 @@ +// +// RSACipherImpl.cpp +// +// $Id: //poco/1.4/Crypto/src/RSACipherImpl.cpp#3 $ +// +// Library: Crypto +// Package: RSA +// Module: RSACipherImpl +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/RSACipherImpl.h" +#include "Poco/Crypto/CryptoTransform.h" +#include "Poco/Exception.h" +#include +#include +#include + + +namespace Poco { +namespace Crypto { + + +namespace +{ + void throwError() + { + unsigned long err; + std::string msg; + + while ((err = ERR_get_error())) + { + if (!msg.empty()) + msg.append("; "); + msg.append(ERR_error_string(err, 0)); + } + + throw Poco::IOException(msg); + } + + + int mapPaddingMode(RSAPaddingMode paddingMode) + { + switch (paddingMode) + { + case RSA_PADDING_PKCS1: + return RSA_PKCS1_PADDING; + case RSA_PADDING_PKCS1_OAEP: + return RSA_PKCS1_OAEP_PADDING; + case RSA_PADDING_SSLV23: + return RSA_SSLV23_PADDING; + case RSA_PADDING_NONE: + return RSA_NO_PADDING; + default: + poco_bugcheck(); + return RSA_NO_PADDING; + } + } + + + class RSAEncryptImpl: public CryptoTransform + { + public: + RSAEncryptImpl(const RSA* pRSA, RSAPaddingMode paddingMode); + ~RSAEncryptImpl(); + + std::size_t blockSize() const; + std::size_t maxDataSize() const; + + std::streamsize transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength); + + std::streamsize finalize(unsigned char* output, std::streamsize length); + + private: + const RSA* _pRSA; + RSAPaddingMode _paddingMode; + std::streamsize _pos; + unsigned char* _pBuf; + }; + + + RSAEncryptImpl::RSAEncryptImpl(const RSA* pRSA, RSAPaddingMode paddingMode): + _pRSA(pRSA), + _paddingMode(paddingMode), + _pos(0), + _pBuf(0) + { + _pBuf = new unsigned char[blockSize()]; + } + + + RSAEncryptImpl::~RSAEncryptImpl() + { + delete [] _pBuf; + } + + + std::size_t RSAEncryptImpl::blockSize() const + { + return RSA_size(_pRSA); + } + + + std::size_t RSAEncryptImpl::maxDataSize() const + { + std::size_t size = blockSize(); + switch (_paddingMode) + { + case RSA_PADDING_PKCS1: + case RSA_PADDING_SSLV23: + size -= 11; + break; + case RSA_PADDING_PKCS1_OAEP: + size -= 41; + break; + default: + break; + } + return size; + } + + + std::streamsize RSAEncryptImpl::transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength) + { + // always fill up the buffer before writing! + std::streamsize maxSize = static_cast(maxDataSize()); + std::streamsize rsaSize = static_cast(blockSize()); + poco_assert_dbg(_pos <= maxSize); + poco_assert (outputLength >= rsaSize); + int rc = 0; + while (inputLength > 0) + { + // check how many data bytes we are missing to get the buffer full + poco_assert_dbg (maxSize >= _pos); + std::streamsize missing = maxSize - _pos; + if (missing == 0) + { + poco_assert (outputLength >= rsaSize); + int n = RSA_public_encrypt(static_cast(maxSize), _pBuf, output, const_cast(_pRSA), mapPaddingMode(_paddingMode)); + if (n == -1) + throwError(); + rc += n; + output += n; + outputLength -= n; + _pos = 0; + + } + else + { + if (missing > inputLength) + missing = inputLength; + + std::memcpy(_pBuf + _pos, input, static_cast(missing)); + input += missing; + _pos += missing; + inputLength -= missing; + } + } + return rc; + } + + + std::streamsize RSAEncryptImpl::finalize(unsigned char* output, std::streamsize length) + { + poco_assert ((size_t)length >= blockSize()); + poco_assert ((size_t)_pos <= maxDataSize()); + int rc = 0; + if (_pos > 0) + { + rc = RSA_public_encrypt(static_cast(_pos), _pBuf, output, const_cast(_pRSA), mapPaddingMode(_paddingMode)); + if (rc == -1) throwError(); + } + return rc; + } + + + class RSADecryptImpl: public CryptoTransform + { + public: + RSADecryptImpl(const RSA* pRSA, RSAPaddingMode paddingMode); + ~RSADecryptImpl(); + + std::size_t blockSize() const; + + std::streamsize transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength); + + std::streamsize finalize( + unsigned char* output, + std::streamsize length); + + private: + const RSA* _pRSA; + RSAPaddingMode _paddingMode; + std::streamsize _pos; + unsigned char* _pBuf; + }; + + + RSADecryptImpl::RSADecryptImpl(const RSA* pRSA, RSAPaddingMode paddingMode): + _pRSA(pRSA), + _paddingMode(paddingMode), + _pos(0), + _pBuf(0) + { + _pBuf = new unsigned char[blockSize()]; + } + + + RSADecryptImpl::~RSADecryptImpl() + { + delete [] _pBuf; + } + + + std::size_t RSADecryptImpl::blockSize() const + { + return RSA_size(_pRSA); + } + + + std::streamsize RSADecryptImpl::transform( + const unsigned char* input, + std::streamsize inputLength, + unsigned char* output, + std::streamsize outputLength) + { + + // always fill up the buffer before decrypting! + std::streamsize rsaSize = static_cast(blockSize()); + poco_assert_dbg(_pos <= rsaSize); + poco_assert (outputLength >= rsaSize); + int rc = 0; + while (inputLength > 0) + { + // check how many data bytes we are missing to get the buffer full + poco_assert_dbg (rsaSize >= _pos); + std::streamsize missing = rsaSize - _pos; + if (missing == 0) + { + int tmp = RSA_private_decrypt(static_cast(rsaSize), _pBuf, output, const_cast(_pRSA), mapPaddingMode(_paddingMode)); + if (tmp == -1) + throwError(); + rc += tmp; + output += tmp; + outputLength -= tmp; + _pos = 0; + + } + else + { + if (missing > inputLength) + missing = inputLength; + + std::memcpy(_pBuf + _pos, input, static_cast(missing)); + input += missing; + _pos += missing; + inputLength -= missing; + } + } + return rc; + } + + + std::streamsize RSADecryptImpl::finalize(unsigned char* output, std::streamsize length) + { + poco_assert ((size_t)length >= blockSize()); + int rc = 0; + if (_pos > 0) + { + rc = RSA_private_decrypt(static_cast(_pos), _pBuf, output, const_cast(_pRSA), mapPaddingMode(_paddingMode)); + if (rc == -1) + throwError(); + } + return rc; + } +} + + +RSACipherImpl::RSACipherImpl(const RSAKey& key, RSAPaddingMode paddingMode): + _key(key), + _paddingMode(paddingMode) +{ +} + + +RSACipherImpl::~RSACipherImpl() +{ +} + + +CryptoTransform* RSACipherImpl::createEncryptor() +{ + return new RSAEncryptImpl(_key.impl()->getRSA(), _paddingMode); +} + + +CryptoTransform* RSACipherImpl::createDecryptor() +{ + return new RSADecryptImpl(_key.impl()->getRSA(), _paddingMode); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/RSADigestEngine.cpp b/contrib/libpoco/Crypto/src/RSADigestEngine.cpp new file mode 100644 index 00000000000..f0ae5c98067 --- /dev/null +++ b/contrib/libpoco/Crypto/src/RSADigestEngine.cpp @@ -0,0 +1,98 @@ +// +// RSADigestEngine.cpp +// +// $Id: //poco/1.4/Crypto/src/RSADigestEngine.cpp#1 $ +// +// Library: Crypto +// Package: RSA +// Module: RSADigestEngine +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/RSADigestEngine.h" +#include + + +namespace Poco { +namespace Crypto { + + +RSADigestEngine::RSADigestEngine(const RSAKey& key, DigestType digestType): + _key(key), + _engine(digestType == DIGEST_MD5 ? "MD5" : "SHA1") +{ +} + +RSADigestEngine::RSADigestEngine(const RSAKey& key, const std::string &name): + _key(key), + _engine(name) +{ +} + + +RSADigestEngine::~RSADigestEngine() +{ +} + + +std::size_t RSADigestEngine::digestLength() const +{ + return _engine.digestLength(); +} + + +void RSADigestEngine::reset() +{ + _engine.reset(); + _digest.clear(); + _signature.clear(); +} + + +const DigestEngine::Digest& RSADigestEngine::digest() +{ + if (_digest.empty()) + { + _digest = _engine.digest(); + } + return _digest; +} + + +const DigestEngine::Digest& RSADigestEngine::signature() +{ + if (_signature.empty()) + { + digest(); + _signature.resize(_key.size()); + unsigned sigLen = static_cast(_signature.size()); + RSA_sign(_engine.nid(), &_digest[0], static_cast(_digest.size()), &_signature[0], &sigLen, _key.impl()->getRSA()); + // truncate _sig to sigLen + if (sigLen < _signature.size()) + _signature.resize(sigLen); + } + return _signature; +} + + +bool RSADigestEngine::verify(const DigestEngine::Digest& sig) +{ + digest(); + DigestEngine::Digest sigCpy = sig; // copy becausse RSA_verify can modify sigCpy + int ret = RSA_verify(_engine.nid(), &_digest[0], static_cast(_digest.size()), &sigCpy[0], static_cast(sigCpy.size()), _key.impl()->getRSA()); + return ret != 0; +} + + +void RSADigestEngine::updateImpl(const void* data, std::size_t length) +{ + _engine.update(data, length); +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/RSAKey.cpp b/contrib/libpoco/Crypto/src/RSAKey.cpp new file mode 100644 index 00000000000..3dfcd138e9c --- /dev/null +++ b/contrib/libpoco/Crypto/src/RSAKey.cpp @@ -0,0 +1,107 @@ +// +// RSAKey.cpp +// +// $Id: //poco/1.4/Crypto/src/RSAKey.cpp#2 $ +// +// Library: Crypto +// Package: RSA +// Module: RSAKey +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/RSAKey.h" +#include + + +namespace Poco { +namespace Crypto { + + +RSAKey::RSAKey(const X509Certificate& cert): + _pImpl(new RSAKeyImpl(cert)) +{ +} + + +RSAKey::RSAKey(KeyLength keyLength, Exponent exp): + _pImpl(0) +{ + int keyLen = keyLength; + unsigned long expVal = RSA_3; + if (exp == EXP_LARGE) + expVal = RSA_F4; + _pImpl = new RSAKeyImpl(keyLen, expVal); +} + + +RSAKey::RSAKey(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase): + _pImpl(new RSAKeyImpl(publicKeyFile, privateKeyFile, privateKeyPassphrase)) +{ +} + + +RSAKey::RSAKey(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase): + _pImpl(new RSAKeyImpl(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase)) +{ +} + + +RSAKey::~RSAKey() +{ +} + + +int RSAKey::size() const +{ + return _pImpl->size(); +} + + +RSAKeyImpl::ByteVec RSAKey::modulus() const +{ + return _pImpl->modulus(); +} + + +RSAKeyImpl::ByteVec RSAKey::encryptionExponent() const +{ + return _pImpl->encryptionExponent(); +} + + +RSAKeyImpl::ByteVec RSAKey::decryptionExponent() const +{ + return _pImpl->decryptionExponent(); +} + + +void RSAKey::save(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase) +{ + _pImpl->save(publicKeyFile, privateKeyFile, privateKeyPassphrase); +} + + +void RSAKey::save(std::ostream* pPublicKeyStream, std::ostream* pPrivateKeyStream, const std::string& privateKeyPassphrase) +{ + _pImpl->save(pPublicKeyStream, pPrivateKeyStream, privateKeyPassphrase); +} + + +namespace +{ + static const std::string RSA("rsa"); +} + + +const std::string& RSAKey::name() const +{ + return RSA; +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/RSAKeyImpl.cpp b/contrib/libpoco/Crypto/src/RSAKeyImpl.cpp new file mode 100644 index 00000000000..8333453cee0 --- /dev/null +++ b/contrib/libpoco/Crypto/src/RSAKeyImpl.cpp @@ -0,0 +1,336 @@ +// +// RSAKeyImpl.cpp +// +// $Id: //poco/1.4/Crypto/src/RSAKeyImpl.cpp#3 $ +// +// Library: Crypto +// Package: RSA +// Module: RSAKeyImpl +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/RSAKeyImpl.h" +#include "Poco/Crypto/X509Certificate.h" +#include "Poco/FileStream.h" +#include "Poco/StreamCopier.h" +#include +#include +#include +#include +#if OPENSSL_VERSION_NUMBER >= 0x00908000L +#include +#endif + + +namespace Poco { +namespace Crypto { + + +RSAKeyImpl::RSAKeyImpl(const X509Certificate& cert): + _pRSA(0) +{ + const X509* pCert = cert.certificate(); + EVP_PKEY* pKey = X509_get_pubkey(const_cast(pCert)); + _pRSA = EVP_PKEY_get1_RSA(pKey); + EVP_PKEY_free(pKey); +} + + +RSAKeyImpl::RSAKeyImpl(int keyLength, unsigned long exponent): + _pRSA(0) +{ +#if OPENSSL_VERSION_NUMBER >= 0x00908000L + _pRSA = RSA_new(); + int ret = 0; + BIGNUM* bn = 0; + try + { + bn = BN_new(); + BN_set_word(bn, exponent); + ret = RSA_generate_key_ex(_pRSA, keyLength, bn, 0); + BN_free(bn); + } + catch (...) + { + BN_free(bn); + throw; + } + if (!ret) throw Poco::InvalidArgumentException("Failed to create RSA context"); +#else + _pRSA = RSA_generate_key(keyLength, exponent, 0, 0); + if (!_pRSA) throw Poco::InvalidArgumentException("Failed to create RSA context"); +#endif +} + + +RSAKeyImpl::RSAKeyImpl( + const std::string& publicKeyFile, + const std::string& privateKeyFile, + const std::string& privateKeyPassphrase): + _pRSA(0) +{ + poco_assert_dbg(_pRSA == 0); + + _pRSA = RSA_new(); + if (!publicKeyFile.empty()) + { + BIO* bio = BIO_new(BIO_s_file()); + if (!bio) throw Poco::IOException("Cannot create BIO for reading public key", publicKeyFile); + int rc = BIO_read_filename(bio, publicKeyFile.c_str()); + if (rc) + { + RSA* pubKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); + if (!pubKey) + { + int rc = BIO_reset(bio); + // BIO_reset() normally returns 1 for success and 0 or -1 for failure. + // File BIOs are an exception, they return 0 for success and -1 for failure. + if (rc != 0) throw Poco::FileException("Failed to load public key", publicKeyFile); + pubKey = PEM_read_bio_RSA_PUBKEY(bio, &_pRSA, 0, 0); + } + BIO_free(bio); + if (!pubKey) + { + freeRSA(); + throw Poco::FileException("Failed to load public key", publicKeyFile); + } + } + else + { + freeRSA(); + throw Poco::FileNotFoundException("Public key file", publicKeyFile); + } + } + + if (!privateKeyFile.empty()) + { + BIO* bio = BIO_new(BIO_s_file()); + if (!bio) throw Poco::IOException("Cannot create BIO for reading private key", privateKeyFile); + int rc = BIO_read_filename(bio, privateKeyFile.c_str()); + if (rc) + { + RSA* privKey = 0; + if (privateKeyPassphrase.empty()) + privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); + else + privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast(privateKeyPassphrase.c_str())); + BIO_free(bio); + if (!privKey) + { + freeRSA(); + throw Poco::FileException("Failed to load private key", privateKeyFile); + } + } + else + { + freeRSA(); + throw Poco::FileNotFoundException("Private key file", privateKeyFile); + } + } +} + + +RSAKeyImpl::RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase): + _pRSA(0) +{ + poco_assert_dbg(_pRSA == 0); + + _pRSA = RSA_new(); + if (pPublicKeyStream) + { + std::string publicKeyData; + Poco::StreamCopier::copyToString(*pPublicKeyStream, publicKeyData); + BIO* bio = BIO_new_mem_buf(const_cast(publicKeyData.data()), static_cast(publicKeyData.size())); + if (!bio) throw Poco::IOException("Cannot create BIO for reading public key"); + RSA* publicKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); + if (!publicKey) + { + int rc = BIO_reset(bio); + // BIO_reset() normally returns 1 for success and 0 or -1 for failure. + // File BIOs are an exception, they return 0 for success and -1 for failure. + if (rc != 1) throw Poco::FileException("Failed to load public key"); + publicKey = PEM_read_bio_RSA_PUBKEY(bio, &_pRSA, 0, 0); + } + BIO_free(bio); + if (!publicKey) + { + freeRSA(); + throw Poco::FileException("Failed to load public key"); + } + } + + if (pPrivateKeyStream) + { + std::string privateKeyData; + Poco::StreamCopier::copyToString(*pPrivateKeyStream, privateKeyData); + BIO* bio = BIO_new_mem_buf(const_cast(privateKeyData.data()), static_cast(privateKeyData.size())); + if (!bio) throw Poco::IOException("Cannot create BIO for reading private key"); + RSA* privateKey = 0; + if (privateKeyPassphrase.empty()) + privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); + else + privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast(privateKeyPassphrase.c_str())); + BIO_free(bio); + if (!privateKey) + { + freeRSA(); + throw Poco::FileException("Failed to load private key"); + } + } +} + + +RSAKeyImpl::~RSAKeyImpl() +{ + freeRSA(); +} + + +void RSAKeyImpl::freeRSA() +{ + if (_pRSA) + RSA_free(_pRSA); + _pRSA = 0; +} + + +int RSAKeyImpl::size() const +{ + return RSA_size(_pRSA); +} + + +RSAKeyImpl::ByteVec RSAKeyImpl::modulus() const +{ + return convertToByteVec(_pRSA->n); +} + + +RSAKeyImpl::ByteVec RSAKeyImpl::encryptionExponent() const +{ + return convertToByteVec(_pRSA->e); +} + + +RSAKeyImpl::ByteVec RSAKeyImpl::decryptionExponent() const +{ + return convertToByteVec(_pRSA->d); +} + + +void RSAKeyImpl::save(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase) +{ + if (!publicKeyFile.empty()) + { + BIO* bio = BIO_new(BIO_s_file()); + if (!bio) throw Poco::IOException("Cannot create BIO for writing public key file", publicKeyFile); + try + { + if (BIO_write_filename(bio, const_cast(publicKeyFile.c_str()))) + { + if (!PEM_write_bio_RSAPublicKey(bio, _pRSA)) + throw Poco::WriteFileException("Failed to write public key to file", publicKeyFile); + } + else throw Poco::CreateFileException("Cannot create public key file"); + } + catch (...) + { + BIO_free(bio); + throw; + } + BIO_free(bio); + } + + if (!privateKeyFile.empty()) + { + BIO* bio = BIO_new(BIO_s_file()); + if (!bio) throw Poco::IOException("Cannot create BIO for writing private key file", privateKeyFile); + try + { + if (BIO_write_filename(bio, const_cast(privateKeyFile.c_str()))) + { + int rc = 0; + if (privateKeyPassphrase.empty()) + rc = PEM_write_bio_RSAPrivateKey(bio, _pRSA, 0, 0, 0, 0, 0); + else + rc = PEM_write_bio_RSAPrivateKey(bio, _pRSA, EVP_des_ede3_cbc(), + reinterpret_cast(const_cast(privateKeyPassphrase.c_str())), + static_cast(privateKeyPassphrase.length()), 0, 0); + if (!rc) throw Poco::FileException("Failed to write private key to file", privateKeyFile); + } + else throw Poco::CreateFileException("Cannot create private key file", privateKeyFile); + } + catch (...) + { + BIO_free(bio); + throw; + } + BIO_free(bio); + } +} + + +void RSAKeyImpl::save(std::ostream* pPublicKeyStream, std::ostream* pPrivateKeyStream, const std::string& privateKeyPassphrase) +{ + if (pPublicKeyStream) + { + BIO* bio = BIO_new(BIO_s_mem()); + if (!bio) throw Poco::IOException("Cannot create BIO for writing public key"); + if (!PEM_write_bio_RSAPublicKey(bio, _pRSA)) + { + BIO_free(bio); + throw Poco::WriteFileException("Failed to write public key to stream"); + } + char* pData; + long size = BIO_get_mem_data(bio, &pData); + pPublicKeyStream->write(pData, static_cast(size)); + BIO_free(bio); + } + + if (pPrivateKeyStream) + { + BIO* bio = BIO_new(BIO_s_mem()); + if (!bio) throw Poco::IOException("Cannot create BIO for writing public key"); + int rc = 0; + if (privateKeyPassphrase.empty()) + rc = PEM_write_bio_RSAPrivateKey(bio, _pRSA, 0, 0, 0, 0, 0); + else + rc = PEM_write_bio_RSAPrivateKey(bio, _pRSA, EVP_des_ede3_cbc(), + reinterpret_cast(const_cast(privateKeyPassphrase.c_str())), + static_cast(privateKeyPassphrase.length()), 0, 0); + if (!rc) + { + BIO_free(bio); + throw Poco::FileException("Failed to write private key to stream"); + } + char* pData; + long size = BIO_get_mem_data(bio, &pData); + pPrivateKeyStream->write(pData, static_cast(size)); + BIO_free(bio); + } +} + + +RSAKeyImpl::ByteVec RSAKeyImpl::convertToByteVec(const BIGNUM* bn) +{ + int numBytes = BN_num_bytes(bn); + ByteVec byteVector(numBytes); + + ByteVec::value_type* buffer = new ByteVec::value_type[numBytes]; + BN_bn2bin(bn, buffer); + + for (int i = 0; i < numBytes; ++i) + byteVector[i] = buffer[i]; + + delete [] buffer; + + return byteVector; +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/src/X509Certificate.cpp b/contrib/libpoco/Crypto/src/X509Certificate.cpp new file mode 100644 index 00000000000..dd9ebd2cab3 --- /dev/null +++ b/contrib/libpoco/Crypto/src/X509Certificate.cpp @@ -0,0 +1,283 @@ +// +// X509Certificate.cpp +// +// $Id: //poco/1.4/Crypto/src/X509Certificate.cpp#1 $ +// +// Library: Crypto +// Package: Certificate +// Module: X509Certificate +// +// Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Crypto/X509Certificate.h" +#include "Poco/StreamCopier.h" +#include "Poco/String.h" +#include "Poco/DateTimeParser.h" +#include +#include +#include +#include +#include + + +namespace Poco { +namespace Crypto { + + +X509Certificate::X509Certificate(std::istream& istr): + _pCert(0) +{ + load(istr); +} + + +X509Certificate::X509Certificate(const std::string& path): + _pCert(0) +{ + load(path); +} + + +X509Certificate::X509Certificate(X509* pCert): + _pCert(pCert) +{ + poco_check_ptr(_pCert); + + init(); +} + + +X509Certificate::X509Certificate(X509* pCert, bool shared): + _pCert(pCert) +{ + poco_check_ptr(_pCert); + + if (shared) + { + _pCert->references++; + } + + init(); +} + + +X509Certificate::X509Certificate(const X509Certificate& cert): + _issuerName(cert._issuerName), + _subjectName(cert._subjectName), + _pCert(cert._pCert) +{ + _pCert = X509_dup(_pCert); +} + + +X509Certificate& X509Certificate::operator = (const X509Certificate& cert) +{ + X509Certificate tmp(cert); + swap(tmp); + return *this; +} + + +void X509Certificate::swap(X509Certificate& cert) +{ + using std::swap; + swap(cert._issuerName, _issuerName); + swap(cert._subjectName, _subjectName); + swap(cert._pCert, _pCert); +} + + +X509Certificate::~X509Certificate() +{ + X509_free(_pCert); +} + + +void X509Certificate::load(std::istream& istr) +{ + poco_assert (!_pCert); + + std::stringstream certStream; + Poco::StreamCopier::copyStream(istr, certStream); + std::string cert = certStream.str(); + + BIO *pBIO = BIO_new_mem_buf(const_cast(cert.data()), static_cast(cert.size())); + if (!pBIO) throw Poco::IOException("Cannot create BIO for reading certificate"); + _pCert = PEM_read_bio_X509(pBIO, 0, 0, 0); + BIO_free(pBIO); + + if (!_pCert) throw Poco::IOException("Faild to load certificate from stream"); + + init(); +} + + +void X509Certificate::load(const std::string& path) +{ + poco_assert (!_pCert); + + BIO *pBIO = BIO_new(BIO_s_file()); + if (!pBIO) throw Poco::IOException("Cannot create BIO for reading certificate file", path); + if (!BIO_read_filename(pBIO, path.c_str())) + { + BIO_free(pBIO); + throw Poco::OpenFileException("Cannot open certificate file for reading", path); + } + + _pCert = PEM_read_bio_X509(pBIO, 0, 0, 0); + BIO_free(pBIO); + + if (!_pCert) throw Poco::ReadFileException("Faild to load certificate from", path); + + init(); +} + + +void X509Certificate::save(std::ostream& stream) const +{ + BIO *pBIO = BIO_new(BIO_s_mem()); + if (!pBIO) throw Poco::IOException("Cannot create BIO for writing certificate"); + try + { + if (!PEM_write_bio_X509(pBIO, _pCert)) + throw Poco::IOException("Failed to write certificate to stream"); + + char *pData; + long size; + size = BIO_get_mem_data(pBIO, &pData); + stream.write(pData, size); + } + catch (...) + { + BIO_free(pBIO); + throw; + } + BIO_free(pBIO); +} + + +void X509Certificate::save(const std::string& path) const +{ + BIO *pBIO = BIO_new(BIO_s_file()); + if (!pBIO) throw Poco::IOException("Cannot create BIO for reading certificate file", path); + if (!BIO_write_filename(pBIO, const_cast(path.c_str()))) + { + BIO_free(pBIO); + throw Poco::CreateFileException("Cannot create certificate file", path); + } + try + { + if (!PEM_write_bio_X509(pBIO, _pCert)) + throw Poco::WriteFileException("Failed to write certificate to file", path); + } + catch (...) + { + BIO_free(pBIO); + throw; + } + BIO_free(pBIO); +} + + +void X509Certificate::init() +{ + char buffer[NAME_BUFFER_SIZE]; + X509_NAME_oneline(X509_get_issuer_name(_pCert), buffer, sizeof(buffer)); + _issuerName = buffer; + X509_NAME_oneline(X509_get_subject_name(_pCert), buffer, sizeof(buffer)); + _subjectName = buffer; +} + + +std::string X509Certificate::commonName() const +{ + return subjectName(NID_COMMON_NAME); +} + + +std::string X509Certificate::issuerName(NID nid) const +{ + if (X509_NAME* issuer = X509_get_issuer_name(_pCert)) + { + char buffer[NAME_BUFFER_SIZE]; + X509_NAME_get_text_by_NID(issuer, nid, buffer, sizeof(buffer)); + return std::string(buffer); + } + else return std::string(); +} + + +std::string X509Certificate::subjectName(NID nid) const +{ + if (X509_NAME* subj = X509_get_subject_name(_pCert)) + { + char buffer[NAME_BUFFER_SIZE]; + X509_NAME_get_text_by_NID(subj, nid, buffer, sizeof(buffer)); + return std::string(buffer); + } + else return std::string(); +} + + +void X509Certificate::extractNames(std::string& cmnName, std::set& domainNames) const +{ + domainNames.clear(); + if (STACK_OF(GENERAL_NAME)* names = static_cast(X509_get_ext_d2i(_pCert, NID_subject_alt_name, 0, 0))) + { + for (int i = 0; i < sk_GENERAL_NAME_num(names); ++i) + { + const GENERAL_NAME* name = sk_GENERAL_NAME_value(names, i); + if (name->type == GEN_DNS) + { + const char* data = reinterpret_cast(ASN1_STRING_data(name->d.ia5)); + std::size_t len = ASN1_STRING_length(name->d.ia5); + domainNames.insert(std::string(data, len)); + } + } + GENERAL_NAMES_free(names); + } + + cmnName = commonName(); + if (!cmnName.empty() && domainNames.empty()) + { + domainNames.insert(cmnName); + } +} + + +Poco::DateTime X509Certificate::validFrom() const +{ + ASN1_TIME* certTime = X509_get_notBefore(_pCert); + std::string dateTime(reinterpret_cast(certTime->data)); + int tzd; + return DateTimeParser::parse("%y%m%d%H%M%S", dateTime, tzd); +} + + +Poco::DateTime X509Certificate::expiresOn() const +{ + ASN1_TIME* certTime = X509_get_notAfter(_pCert); + std::string dateTime(reinterpret_cast(certTime->data)); + int tzd; + return DateTimeParser::parse("%y%m%d%H%M%S", dateTime, tzd); +} + + +bool X509Certificate::issuedBy(const X509Certificate& issuerCertificate) const +{ + X509* pCert = const_cast(_pCert); + X509* pIssuerCert = const_cast(issuerCertificate.certificate()); + EVP_PKEY* pIssuerPublicKey = X509_get_pubkey(pIssuerCert); + if (!pIssuerPublicKey) throw Poco::InvalidArgumentException("Issuer certificate has no public key"); + int rc = X509_verify(pCert, pIssuerPublicKey); + EVP_PKEY_free(pIssuerPublicKey); + return rc == 1; +} + + +} } // namespace Poco::Crypto diff --git a/contrib/libpoco/Crypto/testsuite/CMakeLists.txt b/contrib/libpoco/Crypto/testsuite/CMakeLists.txt new file mode 100644 index 00000000000..c0d6f8eed48 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/CMakeLists.txt @@ -0,0 +1,24 @@ +set(TESTUNIT "${LIBNAME}-testrunner") + +# Sources +file(GLOB SRCS_G "src/*.cpp") +POCO_SOURCES_AUTO( TEST_SRCS ${SRCS_G}) + +# Headers +file(GLOB_RECURSE HDRS_G "src/*.h" ) +POCO_HEADERS_AUTO( TEST_SRCS ${HDRS_G}) + +POCO_SOURCES_AUTO_PLAT( TEST_SRCS OFF + src/WinDriver.cpp +) + +POCO_SOURCES_AUTO_PLAT( TEST_SRCS WINCE + src/WinCEDriver.cpp +) + +add_executable( ${TESTUNIT} ${TEST_SRCS} ) +add_test(NAME ${LIBNAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${TESTUNIT} -all) +target_link_libraries( ${TESTUNIT} PocoCrypto PocoNetSSL PocoXML PocoUtil PocoFoundation CppUnit ) +if(UNIX) + target_link_libraries( ${TESTUNIT} pthread) +endif(UNIX) diff --git a/contrib/libpoco/Crypto/testsuite/src/CryptoTest.cpp b/contrib/libpoco/Crypto/testsuite/src/CryptoTest.cpp new file mode 100644 index 00000000000..8c403470950 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/CryptoTest.cpp @@ -0,0 +1,276 @@ +// +// CryptoTest.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/CryptoTest.cpp#2 $ +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "CryptoTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Crypto/CipherFactory.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/CipherKey.h" +#include "Poco/Crypto/X509Certificate.h" +#include "Poco/Crypto/CryptoStream.h" +#include "Poco/StreamCopier.h" +#include "Poco/Base64Encoder.h" +#include + + +using namespace Poco::Crypto; + + +static const std::string APPINF_PEM( + "-----BEGIN CERTIFICATE-----\n" + "MIIESzCCAzOgAwIBAgIBATALBgkqhkiG9w0BAQUwgdMxEzARBgNVBAMMCmFwcGlu\n" + "Zi5jb20xNjA0BgNVBAoMLUFwcGxpZWQgSW5mb3JtYXRpY3MgU29mdHdhcmUgRW5n\n" + "aW5lZXJpbmcgR21iSDEUMBIGA1UECwwLRGV2ZWxvcG1lbnQxEjAQBgNVBAgMCUNh\n" + "cmludGhpYTELMAkGA1UEBhMCQVQxHjAcBgNVBAcMFVN0LiBKYWtvYiBpbSBSb3Nl\n" + "bnRhbDEtMCsGCSqGSIb3DQEJARYeZ3VlbnRlci5vYmlsdHNjaG5pZ0BhcHBpbmYu\n" + "Y29tMB4XDTA5MDUwNzE0NTY1NloXDTI5MDUwMjE0NTY1NlowgdMxEzARBgNVBAMM\n" + "CmFwcGluZi5jb20xNjA0BgNVBAoMLUFwcGxpZWQgSW5mb3JtYXRpY3MgU29mdHdh\n" + "cmUgRW5naW5lZXJpbmcgR21iSDEUMBIGA1UECwwLRGV2ZWxvcG1lbnQxEjAQBgNV\n" + "BAgMCUNhcmludGhpYTELMAkGA1UEBhMCQVQxHjAcBgNVBAcMFVN0LiBKYWtvYiBp\n" + "bSBSb3NlbnRhbDEtMCsGCSqGSIb3DQEJARYeZ3VlbnRlci5vYmlsdHNjaG5pZ0Bh\n" + "cHBpbmYuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA89GolWCR\n" + "KtLQclJ2M2QtpFqzNC54hUQdR6n8+DAeruH9WFwLSdWW2fEi+jrtd/WEWCdt4PxX\n" + "F2/eBYeURus7Hg2ZtJGDd3je0+Ygsv7+we4cMN/knaBY7rATqhmnZWk+yBpkf5F2\n" + "IHp9gBxUaJWmt/bq3XrvTtzrDXpCd4zg4zPXZ8IC8ket5o3K2vnkAOsIsgN+Ffqd\n" + "4GjF4dsblG6u6E3VarGRLwGtgB8BAZOA/33mV4FHSMkc4OXpAChaK3tM8YhrLw+m\n" + "XtsfqDiv1825S6OWFCKGj/iX8X2QAkrdB63vXCSpb3de/ByIUfp31PpMlMh6dKo1\n" + "vf7yj0nb2w0utQIDAQABoyowKDAOBgNVHQ8BAf8EBAMCB4AwFgYDVR0lAQH/BAww\n" + "CgYIKwYBBQUHAwMwDQYJKoZIhvcNAQEFBQADggEBAM0cpfb4BgiU/rkYe121P581\n" + "ftg5Ck1PYYda1Fy/FgzbgJh2AwVo/6sn6GF79/QkEcWEgtCMNNO3LMTTddUUApuP\n" + "jnEimyfmUhIThyud/vryzTMNa/eZMwaAqUQWqLf+AwgqjUsBSMenbSHavzJOpsvR\n" + "LI0PQ1VvqB+3UGz0JUnBJiKvHs83Fdm4ewPAf3M5fGcIa+Fl2nU5Plzwzskj84f6\n" + "73ZlEEi3aW9JieNy7RWsMM+1E8Sj2CGRZC4BM9V1Fgnsh4+VHX8Eu7eHucvfeIYx\n" + "3mmLMoK4sCayL/FGhrUDw5AkWb8tKNpRXY+W60Et281yxQSeWLPIbatVzIWI0/M=\n" + "-----END CERTIFICATE-----\n" +); + + +CryptoTest::CryptoTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +CryptoTest::~CryptoTest() +{ +} + + +void CryptoTest::testEncryptDecrypt() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256")); + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_NONE); + std::string result = pCipher->decryptString(out, Cipher::ENC_NONE); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64); + std::string result = pCipher->decryptString(out, Cipher::ENC_BASE64); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BINHEX); + std::string result = pCipher->decryptString(out, Cipher::ENC_BINHEX); + assert (in == result); + } +} + + +void CryptoTest::testEncryptDecryptWithSalt() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256", "simplepwd", "Too much salt")); + Cipher::Ptr pCipher2 = CipherFactory::defaultFactory().createCipher(CipherKey("aes256", "simplepwd", "Too much salt")); + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_NONE); + std::string result = pCipher2->decryptString(out, Cipher::ENC_NONE); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64); + std::string result = pCipher2->decryptString(out, Cipher::ENC_BASE64); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BINHEX); + std::string result = pCipher2->decryptString(out, Cipher::ENC_BINHEX); + assert (in == result); + } +} + + +void CryptoTest::testEncryptDecryptDESECB() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("des-ecb", "password")); + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_NONE); + std::string result = pCipher->decryptString(out, Cipher::ENC_NONE); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64); + std::string result = pCipher->decryptString(out, Cipher::ENC_BASE64); + assert (in == result); + } + + for (std::size_t n = 1; n < MAX_DATA_SIZE; n++) + { + std::string in(n, 'x'); + std::string out = pCipher->encryptString(in, Cipher::ENC_BINHEX); + std::string result = pCipher->decryptString(out, Cipher::ENC_BINHEX); + assert (in == result); + } +} + + +void CryptoTest::testPassword() +{ + CipherKey key("aes256", "password", "salt"); + + std::ostringstream keyStream; + Poco::Base64Encoder base64KeyEnc(keyStream); + base64KeyEnc.write(reinterpret_cast(&key.getKey()[0]), key.keySize()); + base64KeyEnc.close(); + std::string base64Key = keyStream.str(); + assert (base64Key == "hIzxBt58GDd7/6mRp88bewKk42lM4QwaF78ek0FkVoA="); +} + + +void CryptoTest::testEncryptInterop() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256", "password", "salt")); + + const std::string plainText = "This is a secret message."; + const std::string expectedCipherText = "9HITTPaU3A/LaZzldbdnRZ109DKlshouKren/n8BsHc="; + std::string cipherText = pCipher->encryptString(plainText, Cipher::ENC_BASE64); + assert (cipherText == expectedCipherText); +} + + +void CryptoTest::testDecryptInterop() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256", "password", "salt")); + + const std::string expectedPlainText = "This is a secret message."; + const std::string cipherText = "9HITTPaU3A/LaZzldbdnRZ109DKlshouKren/n8BsHc="; + std::string plainText = pCipher->decryptString(cipherText, Cipher::ENC_BASE64); + assert (plainText == expectedPlainText); +} + + +void CryptoTest::testStreams() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256")); + + static const std::string SECRET_MESSAGE = "This is a secret message. Don't tell anyone."; + + std::stringstream sstr; + EncryptingOutputStream encryptor(sstr, *pCipher); + encryptor << SECRET_MESSAGE; + encryptor.close(); + + DecryptingInputStream decryptor(sstr, *pCipher); + std::string result; + Poco::StreamCopier::copyToString(decryptor, result); + + assert (result == SECRET_MESSAGE); + assert (decryptor.eof()); + assert (!decryptor.bad()); + + + std::istringstream emptyStream; + DecryptingInputStream badDecryptor(emptyStream, *pCipher); + Poco::StreamCopier::copyToString(badDecryptor, result); + + assert (badDecryptor.fail()); + assert (badDecryptor.bad()); + assert (!badDecryptor.eof()); +} + + +void CryptoTest::testCertificate() +{ + std::istringstream certStream(APPINF_PEM); + X509Certificate cert(certStream); + + std::string subjectName(cert.subjectName()); + std::string issuerName(cert.issuerName()); + std::string commonName(cert.commonName()); + std::string country(cert.subjectName(X509Certificate::NID_COUNTRY)); + std::string localityName(cert.subjectName(X509Certificate::NID_LOCALITY_NAME)); + std::string stateOrProvince(cert.subjectName(X509Certificate::NID_STATE_OR_PROVINCE)); + std::string organizationName(cert.subjectName(X509Certificate::NID_ORGANIZATION_NAME)); + std::string organizationUnitName(cert.subjectName(X509Certificate::NID_ORGANIZATION_UNIT_NAME)); + + assert (subjectName == "/CN=appinf.com/O=Applied Informatics Software Engineering GmbH/OU=Development/ST=Carinthia/C=AT/L=St. Jakob im Rosental/emailAddress=guenter.obiltschnig@appinf.com"); + assert (issuerName == subjectName); + assert (commonName == "appinf.com"); + assert (country == "AT"); + assert (localityName == "St. Jakob im Rosental"); + assert (stateOrProvince == "Carinthia"); + assert (organizationName == "Applied Informatics Software Engineering GmbH"); + assert (organizationUnitName == "Development"); + + // fails with recent OpenSSL versions: + // assert (cert.issuedBy(cert)); +} + + +void CryptoTest::setUp() +{ +} + + +void CryptoTest::tearDown() +{ +} + + +CppUnit::Test* CryptoTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CryptoTest"); + + CppUnit_addTest(pSuite, CryptoTest, testEncryptDecrypt); + CppUnit_addTest(pSuite, CryptoTest, testEncryptDecryptWithSalt); + CppUnit_addTest(pSuite, CryptoTest, testEncryptDecryptDESECB); + CppUnit_addTest(pSuite, CryptoTest, testPassword); + CppUnit_addTest(pSuite, CryptoTest, testEncryptInterop); + CppUnit_addTest(pSuite, CryptoTest, testDecryptInterop); + CppUnit_addTest(pSuite, CryptoTest, testStreams); + CppUnit_addTest(pSuite, CryptoTest, testCertificate); + + return pSuite; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/CryptoTest.h b/contrib/libpoco/Crypto/testsuite/src/CryptoTest.h new file mode 100644 index 00000000000..18390f67095 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/CryptoTest.h @@ -0,0 +1,52 @@ +// +// CryptoTest.h +// +// $Id: //poco/1.4/Crypto/testsuite/src/CryptoTest.h#2 $ +// +// Definition of the CryptoTest class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef CryptoTest_INCLUDED +#define CryptoTest_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "CppUnit/TestCase.h" + + +class CryptoTest: public CppUnit::TestCase +{ +public: + enum + { + MAX_DATA_SIZE = 10000 + }; + + CryptoTest(const std::string& name); + ~CryptoTest(); + + void testEncryptDecrypt(); + void testEncryptDecryptWithSalt(); + void testEncryptDecryptDESECB(); + void testStreams(); + void testPassword(); + void testEncryptInterop(); + void testDecryptInterop(); + void testCertificate(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // CryptoTest_INCLUDED diff --git a/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.cpp b/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.cpp new file mode 100644 index 00000000000..2cd2396231a --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.cpp @@ -0,0 +1,28 @@ +// +// CryptoTestSuite.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/CryptoTestSuite.cpp#1 $ +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "CryptoTestSuite.h" +#include "CryptoTest.h" +#include "RSATest.h" +#include "DigestEngineTest.h" + + +CppUnit::Test* CryptoTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CryptoTestSuite"); + + pSuite->addTest(CryptoTest::suite()); + pSuite->addTest(RSATest::suite()); + pSuite->addTest(DigestEngineTest::suite()); + + return pSuite; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.h b/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.h new file mode 100644 index 00000000000..0469b7cae0e --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/CryptoTestSuite.h @@ -0,0 +1,29 @@ +// +// CryptoTestSuite.h +// +// $Id: //poco/1.4/Crypto/testsuite/src/CryptoTestSuite.h#1 $ +// +// Definition of the CryptoTestSuite class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef CryptoTestSuite_INCLUDED +#define CryptoTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class CryptoTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // CryptoTestSuite_INCLUDED diff --git a/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.cpp b/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.cpp new file mode 100644 index 00000000000..e1763e6bfa6 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.cpp @@ -0,0 +1,96 @@ +// +// DigestEngineTest.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/DigestEngineTest.cpp#1 $ +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "DigestEngineTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Crypto/DigestEngine.h" + + +using Poco::Crypto::DigestEngine; + + +DigestEngineTest::DigestEngineTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +DigestEngineTest::~DigestEngineTest() +{ +} + + +void DigestEngineTest::testMD5() +{ + DigestEngine engine("MD5"); + + // test vectors from RFC 1321 + + engine.update(""); + assert (DigestEngine::digestToHex(engine.digest()) == "d41d8cd98f00b204e9800998ecf8427e"); + + engine.update("a"); + assert (DigestEngine::digestToHex(engine.digest()) == "0cc175b9c0f1b6a831c399e269772661"); + + engine.update("abc"); + assert (DigestEngine::digestToHex(engine.digest()) == "900150983cd24fb0d6963f7d28e17f72"); + + engine.update("message digest"); + assert (DigestEngine::digestToHex(engine.digest()) == "f96b697d7cb7938d525a2f31aaf161d0"); + + engine.update("abcdefghijklmnopqrstuvwxyz"); + assert (DigestEngine::digestToHex(engine.digest()) == "c3fcd3d76192e4007dfb496cca67e13b"); + + engine.update("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + engine.update("abcdefghijklmnopqrstuvwxyz0123456789"); + assert (DigestEngine::digestToHex(engine.digest()) == "d174ab98d277d9f5a5611c2c9f419d9f"); + + engine.update("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); + assert (DigestEngine::digestToHex(engine.digest()) == "57edf4a22be3c955ac49da2e2107b67a"); +} + +void DigestEngineTest::testSHA1() +{ + DigestEngine engine("SHA1"); + + // test vectors from FIPS 180-1 + + engine.update("abc"); + assert (DigestEngine::digestToHex(engine.digest()) == "a9993e364706816aba3e25717850c26c9cd0d89d"); + + engine.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); + assert (DigestEngine::digestToHex(engine.digest()) == "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); + + for (int i = 0; i < 1000000; ++i) + engine.update('a'); + assert (DigestEngine::digestToHex(engine.digest()) == "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); +} + +void DigestEngineTest::setUp() +{ +} + + +void DigestEngineTest::tearDown() +{ +} + + +CppUnit::Test* DigestEngineTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DigestEngineTest"); + + CppUnit_addTest(pSuite, DigestEngineTest, testMD5); + CppUnit_addTest(pSuite, DigestEngineTest, testSHA1); + + return pSuite; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.h b/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.h new file mode 100644 index 00000000000..89fc24d30f5 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/DigestEngineTest.h @@ -0,0 +1,41 @@ +// +// DigestEngineTest.h +// +// $Id: //poco/1.4/Crypto/testsuite/src/DigestEngineTest.h#1 $ +// +// Definition of the DigestEngineTest class. +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef DigestEngineTest_INCLUDED +#define DigestEngineTest_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "CppUnit/TestCase.h" + + +class DigestEngineTest: public CppUnit::TestCase +{ +public: + DigestEngineTest(const std::string& name); + ~DigestEngineTest(); + + void testMD5(); + void testSHA1(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // DigestEngineTest_INCLUDED diff --git a/contrib/libpoco/Crypto/testsuite/src/Driver.cpp b/contrib/libpoco/Crypto/testsuite/src/Driver.cpp new file mode 100644 index 00000000000..96ff4f22139 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/Driver.cpp @@ -0,0 +1,45 @@ +// +// Driver.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/Driver.cpp#1 $ +// +// Console-based test driver for Poco Crypto. +// +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "CppUnit/TestRunner.h" +#include "CryptoTestSuite.h" +#include "Poco/Crypto/Crypto.h" + + +class CryptoInitializer +{ +public: + CryptoInitializer() + { + Poco::Crypto::initializeCrypto(); + } + + ~CryptoInitializer() + { + Poco::Crypto::uninitializeCrypto(); + } +}; + + +int main(int ac, char **av) +{ + CryptoInitializer ci; + + std::vector args; + for (int i = 0; i < ac; ++i) + args.push_back(std::string(av[i])); + CppUnit::TestRunner runner; + runner.addTest("CryptoTestSuite", CryptoTestSuite::suite()); + return runner.run(args) ? 0 : 1; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/RSATest.cpp b/contrib/libpoco/Crypto/testsuite/src/RSATest.cpp new file mode 100644 index 00000000000..690cfa0968d --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/RSATest.cpp @@ -0,0 +1,279 @@ +// +// RSATest.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/RSATest.cpp#1 $ +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "RSATest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Crypto/RSADigestEngine.h" +#include "Poco/Crypto/CipherFactory.h" +#include "Poco/Crypto/Cipher.h" +#include "Poco/Crypto/X509Certificate.h" +#include + + +using namespace Poco::Crypto; + + +static const std::string anyPem( + "-----BEGIN CERTIFICATE-----\r\n" + "MIICaDCCAdECCQCzfxSsk7yaLjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB\r\n" + "VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV\r\n" + "BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA\r\n" + "aW5mLmNvbTAeFw0wNjAzMDExMzA3MzFaFw0wNjAzMzExMzA3MzFaMH4xCzAJBgNV\r\n" + "BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExETAPBgNVBAcTCFN0IEpha29iMRww\r\n" + "GgYDVQQKExNBcHBsaWVkIEluZm9ybWF0aWNzMQowCAYDVQQDFAEqMR4wHAYJKoZI\r\n" + "hvcNAQkBFg9pbmZvQGFwcGluZi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ\r\n" + "AoGBAJHGyXDHyCYoWz+65ltNwwZbhwOGnxr9P1WMATuFJh0bPBZxKbZRdbTm9KhZ\r\n" + "OlvsEIsfgiYdsxURYIqXfEgISYLZcZY0pQwGEOmB+0NeC/+ENSfOlNSthx6zSVlc\r\n" + "zhJ7+dJOGwepHAiLr1fRuc5jogYLraE+lKTnqAAFfzwvti77AgMBAAEwDQYJKoZI\r\n" + "hvcNAQEFBQADgYEAY/ZoeY1ukkEJX7259NeoVM0oahlulWV0rlCqyaeosOiDORPT\r\n" + "m6X1w/5MTCf9VyaD1zukoSZ4QqNVjHFXcXidbB7Tgt3yRuZ5PC5LIFCDPv9mgPne\r\n" + "mUA70yfctNfza2z3ZiQ6NDkW3mZX+1tmxYIrJQIrkVeYeqf1Gh2nyZrUMcE=\r\n" + "-----END CERTIFICATE-----\r\n" + "-----BEGIN RSA PRIVATE KEY-----\r\n" + "Proc-Type: 4,ENCRYPTED\r\n" + "DEK-Info: DES-EDE3-CBC,E7AE93C9E49184EA\r\n" + "\r\n" + "A2IqzNcWs+I5vzV+i+woDk56+yr58eU0Onw8eEvXkLjnSc58JU4327IF7yUbKWdW\r\n" + "Q7BYGGOkVFiZ7ANOwviDg5SUhxRDWCcW8dS6/p1vfdQ1C3qj2OwJjkpg0aDBIzJn\r\n" + "FzgguT3MF3ama77vxv0S3kOfmCj62MLqPGpj5pQ0/1hefRFbL8oAX8bXUN7/rmGM\r\n" + "Zc0QyzFZv2iQ04dY/6TNclwKPB4H0On4K+8BMs3PRkWA0clCaQaFO2+iwnk3XZfe\r\n" + "+MsKUEbLCpAQeYspYv1cw38dCdWq1KTP5aJk+oXgwjfX5cAaPTz74NTqTIsCcaTD\r\n" + "3vy7ukJYFlDR9Kyo7z8rMazYrKJslhnuRH0BhK9st9McwL957j5tZmrKyraCcmCx\r\n" + "dMAGcsis1va3ayYZpIpFqA4EhYrTM+6N8ZRfUap20+b5IQwHfTQDejUhL6rBwy7j\r\n" + "Ti5yD83/itoOMyXq2sV/XWfVD5zk/P5iv22O1EAQMhhnPB9K/I/JhuSGQJfn3cNh\r\n" + "ykOUYT0+vDeSeEVa+FVEP1W35G0alTbKbNs5Tb8KxJ3iDJUxokM//SvPXZy9hOVX\r\n" + "Y05imB04J15DaGbAHlNzunhuJi7121WV/JRXZRW9diE6hwpD8rwqi3FMuRUmy7U9\r\n" + "aFA5poKRAYlo9YtZ3YpFyjGKB6MfCQcB2opuSnQ/gbugV41m67uQ4CDwWLaNRkTb\r\n" + "GlsMBNcHnidg15Bsat5HaB7l250ukrI13Uw1MYdDUzaS3gPfw9aC4F2w0p3U+DPH\r\n" + "80/zePxtroR7T4/+rI136Rl+aMXDMOEGCX1TVP8rjuZzuRyUSUKC8Q==\r\n" + "-----END RSA PRIVATE KEY-----\r\n" + "-----BEGIN CERTIFICATE-----\r\n" + "MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB\r\n" + "VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV\r\n" + "BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA\r\n" + "aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV\r\n" + "BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP\r\n" + "MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW\r\n" + "C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN\r\n" + "Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm\r\n" + "k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W\r\n" + "jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB\r\n" + "AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a\r\n" + "+ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO\r\n" + "M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP\r\n" + "-----END CERTIFICATE-----\r\n" +); + + +RSATest::RSATest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +RSATest::~RSATest() +{ +} + + +void RSATest::testNewKeys() +{ + RSAKey key(RSAKey::KL_1024, RSAKey::EXP_SMALL); + std::ostringstream strPub; + std::ostringstream strPriv; + key.save(&strPub, &strPriv, "testpwd"); + std::string pubKey = strPub.str(); + std::string privKey = strPriv.str(); + + // now do the round trip + std::istringstream iPub(pubKey); + std::istringstream iPriv(privKey); + RSAKey key2(&iPub, &iPriv, "testpwd"); + + std::istringstream iPriv2(privKey); + RSAKey key3(0, &iPriv2, "testpwd"); + std::ostringstream strPub3; + key3.save(&strPub3); + std::string pubFromPrivate = strPub3.str(); + assert (pubFromPrivate == pubKey); +} + + +void RSATest::testNewKeysNoPassphrase() +{ + RSAKey key(RSAKey::KL_1024, RSAKey::EXP_SMALL); + std::ostringstream strPub; + std::ostringstream strPriv; + key.save(&strPub, &strPriv); + std::string pubKey = strPub.str(); + std::string privKey = strPriv.str(); + + // now do the round trip + std::istringstream iPub(pubKey); + std::istringstream iPriv(privKey); + RSAKey key2(&iPub, &iPriv); + + std::istringstream iPriv2(privKey); + RSAKey key3(0, &iPriv2); + std::ostringstream strPub3; + key3.save(&strPub3); + std::string pubFromPrivate = strPub3.str(); + assert (pubFromPrivate == pubKey); +} + + +void RSATest::testSign() +{ + std::string msg("Test this sign message"); + RSAKey key(RSAKey::KL_2048, RSAKey::EXP_LARGE); + RSADigestEngine eng(key); + eng.update(msg.c_str(), static_cast(msg.length())); + const Poco::DigestEngine::Digest& sig = eng.signature(); + std::string hexDig = Poco::DigestEngine::digestToHex(sig); + + // verify + std::ostringstream strPub; + key.save(&strPub); + std::string pubKey = strPub.str(); + std::istringstream iPub(pubKey); + RSAKey keyPub(&iPub); + RSADigestEngine eng2(keyPub); + eng2.update(msg.c_str(), static_cast(msg.length())); + assert (eng2.verify(sig)); +} + + +void RSATest::testSignSha256() +{ + std::string msg("Test this sign message"); + RSAKey key(RSAKey::KL_2048, RSAKey::EXP_LARGE); + RSADigestEngine eng(key, "SHA256"); + eng.update(msg.c_str(), static_cast(msg.length())); + const Poco::DigestEngine::Digest& sig = eng.signature(); + std::string hexDig = Poco::DigestEngine::digestToHex(sig); + + // verify + std::ostringstream strPub; + key.save(&strPub); + std::string pubKey = strPub.str(); + std::istringstream iPub(pubKey); + RSAKey keyPub(&iPub); + RSADigestEngine eng2(keyPub, "SHA256"); + eng2.update(msg.c_str(), static_cast(msg.length())); + assert (eng2.verify(sig)); +} + + +void RSATest::testSignManipulated() +{ + std::string msg("Test this sign message"); + std::string msgManip("Test that sign message"); + RSAKey key(RSAKey::KL_2048, RSAKey::EXP_LARGE); + RSADigestEngine eng(key); + eng.update(msg.c_str(), static_cast(msg.length())); + const Poco::DigestEngine::Digest& sig = eng.signature(); + std::string hexDig = Poco::DigestEngine::digestToHex(sig); + + // verify + std::ostringstream strPub; + key.save(&strPub); + std::string pubKey = strPub.str(); + std::istringstream iPub(pubKey); + RSAKey keyPub(&iPub); + RSADigestEngine eng2(keyPub); + eng2.update(msgManip.c_str(), static_cast(msgManip.length())); + assert (!eng2.verify(sig)); +} + + +void RSATest::testRSACipher() +{ + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL)); + for (std::size_t n = 1; n <= 1200; n++) + { + std::string val(n, 'x'); + std::string enc = pCipher->encryptString(val); + std::string dec = pCipher->decryptString(enc); + assert (dec == val); + } +} + + +void RSATest::testRSACipherLarge() +{ + std::vector sizes; + sizes.push_back (2047); + sizes.push_back (2048); + sizes.push_back (2049); + sizes.push_back (4095); + sizes.push_back (4096); + sizes.push_back (4097); + sizes.push_back (8191); + sizes.push_back (8192); + sizes.push_back (8193); + sizes.push_back (16383); + sizes.push_back (16384); + sizes.push_back (16385); + + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL)); + for (std::vector::const_iterator it = sizes.begin(); it != sizes.end(); ++it) + { + std::string val(*it, 'x'); + std::string enc = pCipher->encryptString(val); + std::string dec = pCipher->decryptString(enc); + assert (dec == val); + } +} + + +void RSATest::testCertificate() +{ + std::istringstream str(anyPem); + X509Certificate cert(str); + RSAKey publicKey(cert); + std::istringstream str2(anyPem); + RSAKey privateKey(0, &str2, "test"); + Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(publicKey); + Cipher::Ptr pCipher2 = CipherFactory::defaultFactory().createCipher(privateKey); + std::string val("lets do some encryption"); + + std::string enc = pCipher->encryptString(val); + std::string dec = pCipher2->decryptString(enc); + assert (dec == val); +} + + +void RSATest::setUp() +{ +} + + +void RSATest::tearDown() +{ +} + + +CppUnit::Test* RSATest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("RSATest"); + + CppUnit_addTest(pSuite, RSATest, testNewKeys); + CppUnit_addTest(pSuite, RSATest, testNewKeysNoPassphrase); + CppUnit_addTest(pSuite, RSATest, testSign); + CppUnit_addTest(pSuite, RSATest, testSignSha256); + CppUnit_addTest(pSuite, RSATest, testSignManipulated); + CppUnit_addTest(pSuite, RSATest, testRSACipher); + CppUnit_addTest(pSuite, RSATest, testRSACipherLarge); + CppUnit_addTest(pSuite, RSATest, testCertificate); + + return pSuite; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/RSATest.h b/contrib/libpoco/Crypto/testsuite/src/RSATest.h new file mode 100644 index 00000000000..ebef2675349 --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/RSATest.h @@ -0,0 +1,47 @@ +// +// RSATest.h +// +// $Id: //poco/1.4/Crypto/testsuite/src/RSATest.h#1 $ +// +// Definition of the RSATest class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef RSATest_INCLUDED +#define RSATest_INCLUDED + + +#include "Poco/Crypto/Crypto.h" +#include "CppUnit/TestCase.h" + + +class RSATest: public CppUnit::TestCase +{ +public: + RSATest(const std::string& name); + ~RSATest(); + + void testNewKeys(); + void testNewKeysNoPassphrase(); + void testSign(); + void testSignSha256(); + void testSignManipulated(); + void testRSACipher(); + void testRSACipherLarge(); + void testCertificate(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // RSATest_INCLUDED diff --git a/contrib/libpoco/Crypto/testsuite/src/WinCEDriver.cpp b/contrib/libpoco/Crypto/testsuite/src/WinCEDriver.cpp new file mode 100644 index 00000000000..704c23157fd --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/WinCEDriver.cpp @@ -0,0 +1,50 @@ +// +// WinCEDriver.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/WinCEDriver.cpp#1 $ +// +// Console-based test driver for Windows CE. +// +// Copyright (c) 2004-2010, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "CppUnit/TestRunner.h" +#include "CryptoTestSuite.h" +#include "Poco/Crypto/Crypto.h" +#include + + +class CryptoInitializer +{ +public: + CryptoInitializer() + { + Poco::Crypto::initializeCrypto(); + } + + ~CryptoInitializer() + { + Poco::Crypto::uninitializeCrypto(); + } +}; + + +int _tmain(int argc, wchar_t* argv[]) +{ + CryptoInitializer ci; + + std::vector args; + for (int i = 0; i < argc; ++i) + { + char buffer[1024]; + std::wcstombs(buffer, argv[i], sizeof(buffer)); + args.push_back(std::string(buffer)); + } + CppUnit::TestRunner runner; + runner.addTest("CryptoTestSuite", CryptoTestSuite::suite()); + return runner.run(args) ? 0 : 1; +} diff --git a/contrib/libpoco/Crypto/testsuite/src/WinDriver.cpp b/contrib/libpoco/Crypto/testsuite/src/WinDriver.cpp new file mode 100644 index 00000000000..7bbef8179ff --- /dev/null +++ b/contrib/libpoco/Crypto/testsuite/src/WinDriver.cpp @@ -0,0 +1,48 @@ +// +// WinDriver.cpp +// +// $Id: //poco/1.4/Crypto/testsuite/src/WinDriver.cpp#1 $ +// +// Windows test driver for Poco Crypto. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "WinTestRunner/WinTestRunner.h" +#include "CryptoTestSuite.h" +#include "Poco/Crypto/Crypto.h" + + +class CryptoInitializer +{ +public: + CryptoInitializer() + { + Poco::Crypto::initializeCrypto(); + } + + ~CryptoInitializer() + { + Poco::Crypto::uninitializeCrypto(); + } +}; + + +class TestDriver: public CppUnit::WinTestRunnerApp +{ + void TestMain() + { + CryptoInitializer ci; + + CppUnit::WinTestRunner runner; + runner.addTest(CryptoTestSuite::suite()); + runner.run(); + } +}; + + +TestDriver theDriver; diff --git a/contrib/libpoco/DLLVersion.rc b/contrib/libpoco/DLLVersion.rc new file mode 100644 index 00000000000..a8d63b5f652 --- /dev/null +++ b/contrib/libpoco/DLLVersion.rc @@ -0,0 +1,40 @@ +#ifdef APSTUDIO_INVOKED + #error This file is not editable by Visual C++. +#endif //APSTUDIO_INVOKED + +#include "winres.h" + +#define POCO_VERSION 1,6,1,0 +#define POCO_VERSION_STR "1.6.1" + +VS_VERSION_INFO VERSIONINFO + FILEVERSION POCO_VERSION + PRODUCTVERSION POCO_VERSION + FILEFLAGSMASK 0x17L +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "000004b0" + BEGIN + VALUE "CompanyName", "Applied Informatics Software Engineering GmbH" + VALUE "FileDescription", "This file is part of the POCO C++ Libraries." + VALUE "FileVersion", POCO_VERSION_STR + VALUE "InternalName", "POCO" + VALUE "LegalCopyright", "Copyright (C) 2004-2015, Applied Informatics Software Engineering GmbH and Contributors." + VALUE "ProductName", "POCO C++ Libraries - http://pocoproject.org" + VALUE "ProductVersion", POCO_VERSION_STR + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0, 1200 + END +END diff --git a/contrib/libpoco/Data/CMakeLists.txt b/contrib/libpoco/Data/CMakeLists.txt new file mode 100644 index 00000000000..1bdbf6ca55a --- /dev/null +++ b/contrib/libpoco/Data/CMakeLists.txt @@ -0,0 +1,67 @@ +set(LIBNAME "PocoData") +set(POCO_LIBNAME "${LIBNAME}") + +# Sources +file(GLOB SRCS_G "src/*.cpp") +POCO_SOURCES_AUTO( SRCS ${SRCS_G}) + +# Headers +file(GLOB_RECURSE HDRS_G "include/*.h" ) +POCO_HEADERS_AUTO( SRCS ${HDRS_G}) + +if (NOT POCO_STATIC) + add_definitions(-DTHREADSAFE) +endif (NOT POCO_STATIC) + +if(MSVC AND NOT(MSVC_VERSION LESS 1400)) + set_source_files_properties(src/StatementImpl.cpp + PROPERTIES COMPILE_FLAGS "/bigobj") +endif() + +add_library( "${LIBNAME}" ${LIB_MODE} ${SRCS} ) +set_target_properties( "${LIBNAME}" + PROPERTIES + VERSION ${SHARED_LIBRARY_VERSION} SOVERSION ${SHARED_LIBRARY_VERSION} + OUTPUT_NAME ${POCO_LIBNAME} + DEFINE_SYMBOL Data_EXPORTS + ) + +target_link_libraries( "${LIBNAME}" PocoFoundation) + +if(ENABLE_DATA_SQLITE) + # SQlite3 is built in any case + add_subdirectory( SQLite ) +endif(ENABLE_DATA_SQLITE) + +if(ENABLE_DATA_MYSQL) + find_package(MySQL) + if(MYSQL_FOUND) + include_directories("${MYSQL_INCLUDE_DIR}") + message(STATUS "MySQL Support Enabled") + add_subdirectory( MySQL ) + else() + message(STATUS "MySQL Support Disabled - no MySQL library") + endif(MYSQL_FOUND) +endif(ENABLE_DATA_MYSQL) + +if(ENABLE_DATA_ODBC) + find_package(ODBC) + if(WIN32 AND NOT WINCE) + set(ODBC_LIBRARIES "odbc32" "odbccp32") + message(STATUS "Windows native ODBC Support Enabled") + add_subdirectory( ODBC ) + else(WIN32 AND NOT WINCE) + if(ODBC_FOUND) + include_directories("${ODBC_INCLUDE_DIRECTORIES}") + message(STATUS "ODBC Support Enabled") + add_subdirectory( ODBC ) + else() + message(STATUS "ODBC Support Disabled - no ODBC runtime") + endif() + endif(WIN32 AND NOT WINCE) +endif(ENABLE_DATA_ODBC) + +if (ENABLE_TESTS) + add_subdirectory(samples) + add_subdirectory(testsuite) +endif () diff --git a/contrib/libpoco/Data/MySQL/CMakeLists.txt b/contrib/libpoco/Data/MySQL/CMakeLists.txt new file mode 100644 index 00000000000..899eb3e5623 --- /dev/null +++ b/contrib/libpoco/Data/MySQL/CMakeLists.txt @@ -0,0 +1,26 @@ +set(LIBNAME "Poco_DataMySQL") +set(POCO_LIBNAME "${LIBNAME}") + +# Sources +file(GLOB SRCS_G "src/*.cpp") +POCO_SOURCES_AUTO( MYSQL_SRCS ${SRCS_G}) + +# Headers +file(GLOB_RECURSE HDRS_G "include/*.h" ) +POCO_HEADERS_AUTO( MYSQL_SRCS ${HDRS_G}) + +add_definitions(-DTHREADSAFE -DNO_TCL) + +add_library( "${LIBNAME}" ${LIB_MODE} ${MYSQL_SRCS} ) +set_target_properties( "${LIBNAME}" + PROPERTIES + VERSION ${SHARED_LIBRARY_VERSION} SOVERSION ${SHARED_LIBRARY_VERSION} + OUTPUT_NAME ${POCO_LIBNAME} + DEFINE_SYMBOL MySQL_EXPORTS + ) + +target_link_libraries( "${LIBNAME}" PocoFoundation PocoData ${MYSQL_LIB}) + +if (ENABLE_TESTS) + add_subdirectory(testsuite) +endif () diff --git a/contrib/libpoco/Data/MySQL/cmake/PocoDataMySQLConfig.cmake b/contrib/libpoco/Data/MySQL/cmake/PocoDataMySQLConfig.cmake new file mode 100644 index 00000000000..092774b3d21 --- /dev/null +++ b/contrib/libpoco/Data/MySQL/cmake/PocoDataMySQLConfig.cmake @@ -0,0 +1,4 @@ +include(CMakeFindDependencyMacro) +find_dependency(PocoFoundation) +find_dependency(PocoData) +include("${CMAKE_CURRENT_LIST_DIR}/PocoDataMySQLTargets.cmake") diff --git a/contrib/libpoco/Data/MySQL/include/Poco/Data/MySQL/Binder.h b/contrib/libpoco/Data/MySQL/include/Poco/Data/MySQL/Binder.h new file mode 100644 index 00000000000..6f6374acd08 --- /dev/null +++ b/contrib/libpoco/Data/MySQL/include/Poco/Data/MySQL/Binder.h @@ -0,0 +1,260 @@ +// +// Binder.h +// +// $Id: //poco/1.4/Data/MySQL/include/Poco/Data/MySQL/Binder.h#1 $ +// +// Library: Data +// Package: MySQL +// Module: Binder +// +// Definition of the Binder class. +// +// Copyright (c) 2008, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Data_MySQL_Binder_INCLUDED +#define Data_MySQL_Binder_INCLUDED + +#include "Poco/Data/MySQL/MySQL.h" +#include "Poco/Data/AbstractBinder.h" +#include "Poco/Data/LOB.h" +#include "Poco/Data/MySQL/MySQLException.h" +#include + +namespace Poco { +namespace Data { +namespace MySQL { + + +class MySQL_API Binder: public Poco::Data::AbstractBinder + /// Binds placeholders in the sql query to the provided values. Performs data types mapping. +{ +public: + typedef SharedPtr Ptr; + + Binder(); + /// Creates the Binder. + + virtual ~Binder(); + /// Destroys the Binder. + + virtual void bind(std::size_t pos, const Poco::Int8& val, Direction dir); + /// Binds an Int8. + + virtual void bind(std::size_t pos, const Poco::UInt8& val, Direction dir); + /// Binds an UInt8. + + virtual void bind(std::size_t pos, const Poco::Int16& val, Direction dir); + /// Binds an Int16. + + virtual void bind(std::size_t pos, const Poco::UInt16& val, Direction dir); + /// Binds an UInt16. + + virtual void bind(std::size_t pos, const Poco::Int32& val, Direction dir); + /// Binds an Int32. + + virtual void bind(std::size_t pos, const Poco::UInt32& val, Direction dir); + /// Binds an UInt32. + + virtual void bind(std::size_t pos, const Poco::Int64& val, Direction dir); + /// Binds an Int64. + + virtual void bind(std::size_t pos, const Poco::UInt64& val, Direction dir); + /// Binds an UInt64. + +#ifndef POCO_LONG_IS_64_BIT + + virtual void bind(std::size_t pos, const long& val, Direction dir = PD_IN); + /// Binds a long. + + virtual void bind(std::size_t pos, const unsigned long& val, Direction dir = PD_IN); + /// Binds an unsigned long. + +#endif // POCO_LONG_IS_64_BIT + + virtual void bind(std::size_t pos, const bool& val, Direction dir); + /// Binds a boolean. + + virtual void bind(std::size_t pos, const float& val, Direction dir); + /// Binds a float. + + virtual void bind(std::size_t pos, const double& val, Direction dir); + /// Binds a double. + + virtual void bind(std::size_t pos, const char& val, Direction dir); + /// Binds a single character. + + virtual void bind(std::size_t pos, const std::string& val, Direction dir); + /// Binds a string. + + virtual void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir); + /// Binds a BLOB. + + virtual void bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir); + /// Binds a CLOB. + + virtual void bind(std::size_t pos, const DateTime& val, Direction dir); + /// Binds a DateTime. + + virtual void bind(std::size_t pos, const Date& val, Direction dir); + /// Binds a Date. + + virtual void bind(std::size_t pos, const Time& val, Direction dir); + /// Binds a Time. + + virtual void bind(std::size_t pos, const NullData& val, Direction dir); + /// Binds a null. + + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::deque& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::list& val, Direction dir = PD_IN); + + virtual void bind(std::size_t pos, const std::vector