Skip to content

bug 79747: crc32 optimizations #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/mysqlbinlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static void warning(const char *format, ...)
#include "sql_string.h"
#include "my_decimal.h"
#include "rpl_constants.h"
#include "ut0crc32.h"

#include <algorithm>
#include <utility>
Expand Down Expand Up @@ -3321,6 +3322,7 @@ int main(int argc, char** argv)
DBUG_ENTER("main");
DBUG_PROCESS(argv[0]);

ut_crc32_init();
my_init_time(); // for time functions
tzset(); // set tzname
/*
Expand Down
3 changes: 1 addition & 2 deletions cmake/zlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ MACRO (MYSQL_CHECK_ZLIB_WITH_COMPRESS)
IF(ZLIB_FOUND)
INCLUDE(CheckFunctionExists)
SET(CMAKE_REQUIRED_LIBRARIES z)
CHECK_FUNCTION_EXISTS(crc32 HAVE_CRC32)
CHECK_FUNCTION_EXISTS(compressBound HAVE_COMPRESSBOUND)
CHECK_FUNCTION_EXISTS(deflateBound HAVE_DEFLATEBOUND)
SET(CMAKE_REQUIRED_LIBRARIES)
IF(HAVE_CRC32 AND HAVE_COMPRESSBOUND AND HAVE_DEFLATEBOUND)
IF(HAVE_COMPRESSBOUND AND HAVE_DEFLATEBOUND)
SET(ZLIB_LIBRARY ${ZLIB_LIBRARIES} CACHE INTERNAL "System zlib library")
SET(WITH_ZLIB "system" CACHE STRING
"Which zlib to use (possible values are 'bundled' or 'system')")
Expand Down
14 changes: 0 additions & 14 deletions extra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,12 @@ IF(WITH_INNOBASE_STORAGE_ENGINE)
ADD_DEFINITIONS("-DUNIV_INNOCHECKSUM")
SET(INNOBASE_SOURCES
../storage/innobase/buf/buf0checksum.cc
../storage/innobase/ut/ut0crc32.cc
../storage/innobase/ut/ut0ut.cc
../storage/innobase/buf/buf0buf.cc
../storage/innobase/page/page0zip.cc
../storage/innobase/os/os0file.cc
)

# Avoid generating Hardware Capabilities due to crc32 instructions
IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND
CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
MY_CHECK_CXX_COMPILER_FLAG("-Wa,-nH" HAVE_WA_NH)
IF(HAVE_WA_NH)
ADD_COMPILE_FLAGS(
../storage/innobase/ut/ut0crc32.cc
COMPILE_FLAGS "-Wa,-nH"
)
ENDIF()
ENDIF()

MYSQL_ADD_EXECUTABLE(innochecksum innochecksum.cc ${INNOBASE_SOURCES})
TARGET_LINK_LIBRARIES(innochecksum mysys mysys_ssl ${LZ4_LIBRARY})
ADD_DEPENDENCIES(innochecksum GenError)
Expand Down
2 changes: 2 additions & 0 deletions extra/comp_err.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <my_getopt.h>
#include <assert.h>
#include <my_dir.h>
#include <ut0crc32.h>
#include <mysql_version.h>

#define MAX_ROWS 2000
Expand Down Expand Up @@ -174,6 +175,7 @@ int main(int argc, char *argv[])
struct languages *lang_head;
DBUG_ENTER("main");

ut_crc32_init();
charsets_dir= DEFAULT_CHARSET_DIR;
my_umask_dir= 0777;
if (get_options(&argc, &argv))
Expand Down
36 changes: 26 additions & 10 deletions storage/innobase/include/ut0crc32.h → include/ut0crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ Created Aug 10, 2011 Vasil Dimov
#ifndef ut0crc32_h
#define ut0crc32_h

#include "univ.i"
#include <my_global.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/********************************************************************//**
Initializes the data structures used by ut_crc32*(). Does not do any
allocations, would not hurt if called twice, but would be pointless. */
/* from UNIV_INTERN in storage/innobase/include/univ.i */
void
ut_crc32_init();
/*===========*/
Expand All @@ -41,20 +46,31 @@ Calculates CRC32.
@param len - data length in bytes.
@return CRC32 (CRC-32C, using the GF(2) primitive polynomial 0x11EDC6F41,
or 0x1EDC6F41 without the high-order bit) */
typedef uint32_t (*ut_crc32_func_t)(const byte* ptr, ulint len);
typedef uint32_t (*ut_crc32_func_t)(const uint8* ptr, my_ulonglong len);

/** Pointer to CRC32 calculation function. */
extern ut_crc32_func_t ut_crc32;
/** Pointer to CRC32C calculation function. */
extern ut_crc32_func_t ut_crc32c;

/** Pointer to CRC32 calculation function, which uses big-endian byte order
/** Pointer to CRC32C calculation function, which uses big-endian byte order
when converting byte strings to integers internally. */
extern ut_crc32_func_t ut_crc32_legacy_big_endian;
extern ut_crc32_func_t ut_crc32c_legacy_big_endian;

/** Pointer to CRC32-byte-by-byte calculation function (byte order agnostic,
/** Pointer to CRC32C-byte-by-byte calculation function (byte order agnostic,
but very slow). */
extern ut_crc32_func_t ut_crc32_byte_by_byte;
extern ut_crc32_func_t ut_crc32c_byte_by_byte;

/* Pointer to CRC32 calculation function - polynominal 0x04C11DB7 */
extern ut_crc32_func_t ut_crc32;

typedef uint32 (*ut_crc32_ex_func_t)(uint32, const uint8* ptr, my_ulonglong len);
/* extended CRC32 function taking the partial CRC32 as an input */
extern ut_crc32_ex_func_t ut_crc32_ex;

/** Text description of CRC32(C) implementation */
extern const char *ut_crc32_implementation;

/** Flag that tells whether the CPU supports CRC32 or not */
extern bool ut_crc32_sse2_enabled;
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* ut0crc32_h */
19 changes: 0 additions & 19 deletions libbinlogevents/include/binlog_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
*/
#include "byteorder.h"
#include "wrapper_functions.h"
#include <zlib.h> //for checksum calculations
#include <cstdio>
#include <iostream>
#include <climits>
Expand Down Expand Up @@ -405,24 +404,6 @@ enum enum_binlog_checksum_alg
#define BINLOG_CHECKSUM_ALG_DESC_LEN 1 /* 1 byte checksum alg descriptor */
#define LOG_EVENT_HEADER_SIZE 20

/**
Calculate a long checksum for a memoryblock.

@param crc start value for crc
@param pos pointer to memory block
@param length length of the block

@return checksum for a memory block
*/
inline uint32_t checksum_crc32(uint32_t crc, const unsigned char *pos,
size_t length)
{
BAPI_ASSERT(length <= UINT_MAX);
return static_cast<uint32_t>(crc32(static_cast<unsigned int>(crc), pos,
static_cast<unsigned int>(length)));
}


/*
Reads string from buf.

Expand Down
7 changes: 3 additions & 4 deletions libbinlogevents/src/binlog_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "binary_log_types.h"

#include "statement_events.h"
#include "ut0crc32.h"

#include <algorithm>
#include <stdint.h>
Expand Down Expand Up @@ -253,11 +254,9 @@ bool Log_event_footer::event_checksum_test(unsigned char *event_buf,
event_buf + event_len - BINLOG_CHECKSUM_LEN, sizeof(incoming));
incoming= le32toh(incoming);

computed= checksum_crc32(0L, NULL, 0);
/* checksum the event content but not the checksum part itself */
computed= binary_log::checksum_crc32(computed,
(const unsigned char*) event_buf,
event_len - BINLOG_CHECKSUM_LEN);
computed= ut_crc32((const unsigned char*) event_buf,
event_len - BINLOG_CHECKSUM_LEN);

if (flags != 0)
{
Expand Down
6 changes: 6 additions & 0 deletions mysql-test/r/func_math.result
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,9 @@ number Round(number, num_digits) > 0 Round(number, 3) > 0
1 1 1
0 0 0
DROP PROCEDURE test_round_fn;
#
# CRC32 tests
#
select CRC32(NULL), CRC32(''), CRC32('MySQL'), CRC32('mysql'), CRC32('01234567'), CRC32('012345678');
CRC32(NULL) CRC32('') CRC32('MySQL') CRC32('mysql') CRC32('01234567') CRC32('012345678')
NULL 0 3259397556 2501908538 763378421 939184570
7 changes: 7 additions & 0 deletions mysql-test/t/func_math.test
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,10 @@ DELIMITER ;|

CALL test_round_fn();
DROP PROCEDURE test_round_fn;


--echo #
--echo # CRC32 tests
--echo #

select CRC32(NULL), CRC32(''), CRC32('MySQL'), CRC32('mysql'), CRC32('01234567'), CRC32('012345678');
26 changes: 25 additions & 1 deletion mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ SET(MYSYS_SOURCES array.c charset-def.c charset.c checksum.c
thr_rwlock.c tree.c typelib.c base64.c my_memmem.c
lf_alloc-pin.c lf_dynarray.c lf_hash.c
my_rdtsc.c psi_noop.c my_syslog.c
my_chmod.c my_thread.c)
my_chmod.c my_thread.c ut0crc32.cc)

IF(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le")
enable_language(ASM)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/mysys/crc32_power8)
LIST(APPEND MYSYS_SOURCES
crc32_power8/crc32.S
crc32_power8/crc32c.S
crc32_power8/crc32_wrapper.c
crc32_power8/crc32c_wrapper.c
)
ENDIF()

IF (WIN32)
LIST(APPEND MYSYS_SOURCES
Expand All @@ -60,6 +71,19 @@ IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND CMAKE_C_COMPILER_ID MATCHES "SunPro")
PROPERTIES COMPILE_FLAGS "${CMAKE_CURRENT_SOURCE_DIR}/my_timer_cycles.il")
ENDIF()

# Avoid generating Hardware Capabilities due to crc32 instructions
IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND
CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
MY_CHECK_CXX_COMPILER_FLAG("-Wa,-nH" HAVE_WA_NH)
IF(HAVE_WA_NH)
ADD_COMPILE_FLAGS(
ut0crc32.cc
COMPILE_FLAGS "-Wa,-nH"
)
ENDIF()
ENDIF()

IF(HAVE_LINUX_LARGE_PAGES)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} my_largepage.c)
ENDIF()
Expand Down
4 changes: 2 additions & 2 deletions mysys/checksum.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <my_global.h>
#include <my_sys.h>
#include <zlib.h>
#include "ut0crc32.h"

/*
Calculate a long checksum for a memoryblock.
Expand All @@ -30,6 +30,6 @@

ha_checksum my_checksum(ha_checksum crc, const uchar *pos, size_t length)
{
return (ha_checksum)crc32((uint)crc, pos, (uint)length);
return (ha_checksum)ut_crc32_ex((uint)crc, pos, (uint)length);
}

14 changes: 14 additions & 0 deletions mysys/crc32_power8/crc32.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifdef __powerpc__

#define CONSTANTS .crc32_constants
#define SHORT_CONSTANTS .crc32_short_constants
#define BARRETT_CONSTANTS .crc32_barrett_constants

#include "crc32_constants.h"

#define __F __crc32_vpmsum

#include "crc32.iS"

#endif

Loading