mercurial/bitmanipulation.h
author Matt Harbison <matt_harbison@yahoo.com>
Sat, 28 Sep 2024 19:12:18 -0400
changeset 51929 f2832de2a46c
parent 48274 d86908050375
permissions -rw-r--r--
interfaces: introduce and use a protocol class for the `bdiff` module This is allowed by PEP 544[1], and we basically follow the example there. The class here is copied from `mercurial.pure.bdiff`, and the implementation removed. There are several modules that have a few different implementations, and the implementation chosen is controlled by `HGMODULEPOLICY`. The module is loaded via `mercurial/policy.py`, and has been inferred by pytype as `Any` up to this point. Therefore it and PyCharm were blind to all functions on the module, and their signatures. Also, having multiple instances of the same module allows their signatures to get out of sync. Introducing a protocol class allows the loaded module that is stored in a variable to be given type info, which cascades through the various places it is used. This change alters 11 *.pyi files, for example. In theory, this would also allow us to ensure the various implementations of the same module are kept in alignment- simply import the module in a test module, attempt to pass it to a function that uses the corresponding protocol as an argument, and run pytype on it. In practice, this doesn't work (yet). PyCharm (erroneously) flags imported modules being passed where a protocol class is used[2]. Pytype has problems the other way- it fails to detect when a module that doesn't adhere to the protocol is passed to a protocol argument. The good news is that mypy properly detects this case. The bad news is that mypy spews a bunch of other errors when importing even simple modules, like the various `bdiff` modules. Therefore I'm punting on the tests for now because the type info around a loaded module in PyCharm is a clear win by itself. [1] https://peps.python.org/pep-0544/#modules-as-implementations-of-protocols [2] https://youtrack.jetbrains.com/issue/PY-58679/Support-modules-implementing-protocols
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48274
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
diff changeset
     1
#ifndef HG_BITMANIPULATION_H
d86908050375 hg: remove reserved identifiers
David Demelier <markand@malikania.fr>
parents: 46707
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
32646
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
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
diff changeset
     9
 enough */
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
diff changeset
    11
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
diff changeset
    13
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
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: 38303
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: 38303
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: 38303
diff changeset
    18
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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
38303
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: 32646
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: 32646
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
46707
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
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: 38303
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: 38303
diff changeset
    45
{
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 38303
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: 38303
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: 38303
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: 38303
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: 38303
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: 38303
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: 38303
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: 38303
diff changeset
    54
}
eed42f1c22d6 bitmanipulation: add utils to read/write bigendian 64bit integers
Rapha?l Gom?s <rgomes@octobus.net>
parents: 38303
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: 32646
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: 32646
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