From a66eebf57c212363f9f430a4c8c9a4f3ddf57cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Thu, 19 Mar 2020 07:52:35 +0200 Subject: MDEV-21981 Replace arithmetic + with bitwise OR when possible Several macros such as sint2korr() and uint4korr() are using the arithmetic + operator while a bitwise or operator would suffice. GCC 5 and clang 5 and later can detect patterns consisting of bitwise or and shifts by multiples of 8 bits, such as those used in the InnoDB function mach_read_from_4(). They actually translate that verbose low-level code into high-level machine language (i486 bswap instruction or fused into the Haswell movbe instruction). We should do the same for MariaDB Server code that is outside InnoDB. Note: The Microsoft C compiler is lacking this optimization. There, we might consider using _byteswap_ushort(), _byteswap_ulong(), _byteswap_uint64(). But, those would lead to unaligned reads, which are bad for reasons stated in MDEV-20277. Besides, outside InnoDB, most data is already being stored in the native little-endian format of that compiler. --- include/myisampack.h | 65 ++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) (limited to 'include/myisampack.h') diff --git a/include/myisampack.h b/include/myisampack.h index eb5baf5fc56..6bfe1958fbc 100644 --- a/include/myisampack.h +++ b/include/myisampack.h @@ -2,6 +2,7 @@ #define MYISAMPACK_INCLUDED /* Copyright (c) 2000-2002, 2004 MySQL AB, 2009 Sun Microsystems, Inc. + Copyright (c) 2020, MariaDB Corporation. Use is subject to license terms. This program is free software; you can redistribute it and/or modify @@ -28,7 +29,7 @@ #define mi_sint1korr(A) ((int8)(*A)) #define mi_uint1korr(A) ((uint8)(*A)) -#define mi_sint2korr(A) ((int16) (((int16) (((const uchar*) (A))[1])) +\ +#define mi_sint2korr(A) ((int16) (((int16) (((const uchar*) (A))[1])) |\ ((int16) ((int16) ((const char*) (A))[0]) << 8))) #define mi_sint3korr(A) ((int32) (((((const uchar*) (A))[0]) & 128) ? \ (((uint32) 255L << 24) | \ @@ -38,58 +39,58 @@ (((uint32) ((const uchar*) (A))[0]) << 16) |\ (((uint32) ((const uchar*) (A))[1]) << 8) | \ ((uint32) ((const uchar*) (A))[2]))) -#define mi_sint4korr(A) ((int32) (((int32) (((const uchar*) (A))[3])) +\ - ((int32) (((const uchar*) (A))[2]) << 8) +\ - ((int32) (((const uchar*) (A))[1]) << 16) +\ +#define mi_sint4korr(A) ((int32) (((int32) (((const uchar*) (A))[3])) |\ + ((int32) (((const uchar*) (A))[2]) << 8) |\ + ((int32) (((const uchar*) (A))[1]) << 16) |\ ((int32) ((int16) ((const char*) (A))[0]) << 24))) #define mi_sint8korr(A) ((longlong) mi_uint8korr(A)) -#define mi_uint2korr(A) ((uint16) (((uint16) (((const uchar*) (A))[1])) +\ +#define mi_uint2korr(A) ((uint16) (((uint16) (((const uchar*) (A))[1])) |\ ((uint16) (((const uchar*) (A))[0]) << 8))) -#define mi_uint3korr(A) ((uint32) (((uint32) (((const uchar*) (A))[2])) +\ - (((uint32) (((const uchar*) (A))[1])) << 8) +\ +#define mi_uint3korr(A) ((uint32) (((uint32) (((const uchar*) (A))[2])) |\ + (((uint32) (((const uchar*) (A))[1])) << 8) |\ (((uint32) (((const uchar*) (A))[0])) << 16))) -#define mi_uint4korr(A) ((uint32) (((uint32) (((const uchar*) (A))[3])) +\ - (((uint32) (((const uchar*) (A))[2])) << 8) +\ - (((uint32) (((const uchar*) (A))[1])) << 16) +\ +#define mi_uint4korr(A) ((uint32) (((uint32) (((const uchar*) (A))[3])) |\ + (((uint32) (((const uchar*) (A))[2])) << 8) |\ + (((uint32) (((const uchar*) (A))[1])) << 16) |\ (((uint32) (((const uchar*) (A))[0])) << 24))) #ifndef HAVE_mi_uint5korr -#define mi_uint5korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[4])) +\ - (((uint32) (((const uchar*) (A))[3])) << 8) +\ - (((uint32) (((const uchar*) (A))[2])) << 16) +\ - (((uint32) (((const uchar*) (A))[1])) << 24)) +\ +#define mi_uint5korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[4])) |\ + (((uint32) (((const uchar*) (A))[3])) << 8) |\ + (((uint32) (((const uchar*) (A))[2])) << 16) |\ + (((uint32) (((const uchar*) (A))[1])) << 24)) |\ (((ulonglong) (((const uchar*) (A))[0])) << 32)) #endif /* HAVE_mi_uint5korr */ #ifndef HAVE_mi_uint6korr -#define mi_uint6korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[5])) +\ - (((uint32) (((const uchar*) (A))[4])) << 8) +\ - (((uint32) (((const uchar*) (A))[3])) << 16) +\ - (((uint32) (((const uchar*) (A))[2])) << 24)) +\ - (((ulonglong) (((uint32) (((const uchar*) (A))[1])) +\ +#define mi_uint6korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[5])) |\ + (((uint32) (((const uchar*) (A))[4])) << 8) |\ + (((uint32) (((const uchar*) (A))[3])) << 16) |\ + (((uint32) (((const uchar*) (A))[2])) << 24)) |\ + (((ulonglong) (((uint32) (((const uchar*) (A))[1])) |\ (((uint32) (((const uchar*) (A))[0]) << 8)))) <<\ 32)) #endif /* HAVE_mi_uint6korr */ #ifndef HAVE_mi_uint7korr -#define mi_uint7korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[6])) +\ - (((uint32) (((const uchar*) (A))[5])) << 8) +\ - (((uint32) (((const uchar*) (A))[4])) << 16) +\ - (((uint32) (((const uchar*) (A))[3])) << 24)) +\ - (((ulonglong) (((uint32) (((const uchar*) (A))[2])) +\ - (((uint32) (((const uchar*) (A))[1])) << 8) +\ +#define mi_uint7korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[6])) |\ + (((uint32) (((const uchar*) (A))[5])) << 8) |\ + (((uint32) (((const uchar*) (A))[4])) << 16) |\ + (((uint32) (((const uchar*) (A))[3])) << 24)) |\ + (((ulonglong) (((uint32) (((const uchar*) (A))[2])) |\ + (((uint32) (((const uchar*) (A))[1])) << 8) |\ (((uint32) (((const uchar*) (A))[0])) << 16))) <<\ 32)) #endif /* HAVE_mi_uint7korr */ #ifndef HAVE_mi_uint8korr -#define mi_uint8korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[7])) +\ - (((uint32) (((const uchar*) (A))[6])) << 8) +\ - (((uint32) (((const uchar*) (A))[5])) << 16) +\ - (((uint32) (((const uchar*) (A))[4])) << 24)) +\ - (((ulonglong) (((uint32) (((const uchar*) (A))[3])) +\ - (((uint32) (((const uchar*) (A))[2])) << 8) +\ - (((uint32) (((const uchar*) (A))[1])) << 16) +\ +#define mi_uint8korr(A) ((ulonglong)(((uint32) (((const uchar*) (A))[7])) |\ + (((uint32) (((const uchar*) (A))[6])) << 8) |\ + (((uint32) (((const uchar*) (A))[5])) << 16) |\ + (((uint32) (((const uchar*) (A))[4])) << 24)) |\ + (((ulonglong) (((uint32) (((const uchar*) (A))[3])) |\ + (((uint32) (((const uchar*) (A))[2])) << 8) |\ + (((uint32) (((const uchar*) (A))[1])) << 16) |\ (((uint32) (((const uchar*) (A))[0])) << 24))) <<\ 32)) #endif /* HAVE_mi_uint8korr */ -- cgit v1.2.1