mercurial/bitmanipulation.h
author Raphaël Gomès <rgomes@octobus.net>
Mon, 12 Jun 2023 16:51:08 +0200
changeset 50695 1c31b343e514
parent 48280 d86908050375
permissions -rw-r--r--
match: add `filepath:` pattern to match an exact filepath relative to the root It's useful in certain automated workflows to make sure we recurse in directories whose name conflicts with files in other revisions. In addition it makes it possible to avoid building a potentially costly regex, improving performance when the set of files to match explicitly is large. The benchmark below are run in the following configuration : # data-env-vars.name = mozilla-central-2018-08-01-zstd-sparse-revlog # benchmark.name = files # benchmark.variants.rev = tip # benchmark.variants.files = all-list-filepath-sorted # bin-env-vars.hg.flavor = no-rust It also includes timings using the re2 engine (through the `google-re2` module) to show how much can be saved by just using a better regexp engine. Pattern time (seconds) time using re2 ----------------------------------------------------------- just "." 0.4 0.4 list of "filepath:…" 1.3 1.3 list of "path:…" 25.7 3.9 list of patterns 29.7 10.4 As you can see, Without re2, using "filepath:" instead of "path:" is a huge win. With re2, it is still about three times faster to not have to build the regex.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48280
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46720
diff changeset
     1
#ifndef HG_BITMANIPULATION_H
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46720
diff changeset
     2
#define HG_BITMANIPULATION_H
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     3
32669
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     4
#include <string.h>
b4356d1cf3e4 bitmanipulation: add missing include of string.h
Martin von Zweigbergk <martinvonz@google.com>
parents: 29444
diff changeset
     5
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     6
#include "compat.h"
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
     7
46720
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
     8
/* Reads a 64 bit integer from big-endian bytes. Assumes that the data is long
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
     9
 enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    10
static inline uint64_t getbe64(const char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    11
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    12
	const unsigned char *d = (const unsigned char *)c;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    13
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    14
	return ((((uint64_t)d[0]) << 56) | (((uint64_t)d[1]) << 48) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    15
	        (((uint64_t)d[2]) << 40) | (((uint64_t)d[3]) << 32) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    16
	        (((uint64_t)d[4]) << 24) | (((uint64_t)d[5]) << 16) |
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    17
	        (((uint64_t)d[6]) << 8) | (d[7]));
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    18
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    19
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    20
static inline uint32_t getbe32(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    21
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    22
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    23
38312
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    24
	return ((((uint32_t)d[0]) << 24) | (((uint32_t)d[1]) << 16) |
1fb2510cf8c8 bitmanipulation: fix undefined behavior in bit shift in getbe32
Augie Fackler <augie@google.com>
parents: 34697
diff changeset
    25
	        (((uint32_t)d[2]) << 8) | (d[3]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    26
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    27
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    28
static inline int16_t getbeint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    29
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    30
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    31
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32669
diff changeset
    32
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    33
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    34
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    35
static inline uint16_t getbeuint16(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    36
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    37
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    38
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32669
diff changeset
    39
	return ((d[0] << 8) | (d[1]));
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    40
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    41
46720
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    42
/* Writes a 64 bit integer to bytes in a big-endian format.
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    43
 Assumes that the buffer is long enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    44
static inline void putbe64(uint64_t x, char *c)
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    45
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    46
	c[0] = (x >> 56) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    47
	c[1] = (x >> 48) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    48
	c[2] = (x >> 40) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    49
	c[3] = (x >> 32) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    50
	c[4] = (x >> 24) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    51
	c[5] = (x >> 16) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    52
	c[6] = (x >> 8) & 0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    53
	c[7] = (x)&0xff;
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    54
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Raphaël Gomès <rgomes@octobus.net>
parents: 38312
diff changeset
    55
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    56
static inline void putbe32(uint32_t x, char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    57
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    58
	c[0] = (x >> 24) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    59
	c[1] = (x >> 16) & 0xff;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    60
	c[2] = (x >> 8) & 0xff;
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32669
diff changeset
    61
	c[3] = (x)&0xff;
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    62
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    63
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    64
static inline double getbefloat64(const char *c)
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    65
{
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    66
	const unsigned char *d = (const unsigned char *)c;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    67
	double ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    68
	int i;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    69
	uint64_t t = 0;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    70
	for (i = 0; i < 8; i++) {
34697
ce77b0563228 bitmanipulation: reformat with clang-format
Augie Fackler <augie@google.com>
parents: 32669
diff changeset
    71
		t = (t << 8) + d[i];
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    72
	}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    73
	memcpy(&ret, &t, sizeof(t));
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    74
	return ret;
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    75
}
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    76
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
diff changeset
    77
#endif