bpo-46906: Add PyFloat_Pack8() to the C API (GH-31657)

Add new functions to pack and unpack C double (serialize and
deserialize):

* PyFloat_Pack2(), PyFloat_Pack4(), PyFloat_Pack8()
* PyFloat_Unpack2(), PyFloat_Unpack4(), PyFloat_Unpack8()

Document these functions and add unit tests.

Rename private functions and move them from the internal C API
to the public C API:

* _PyFloat_Pack2() => PyFloat_Pack2()
* _PyFloat_Pack4() => PyFloat_Pack4()
* _PyFloat_Pack8() => PyFloat_Pack8()
* _PyFloat_Unpack2() => PyFloat_Unpack2()
* _PyFloat_Unpack4() => PyFloat_Unpack4()
* _PyFloat_Unpack8() => PyFloat_Unpack8()

Replace the "unsigned char*" type with "char*" which is more common
and easy to use.
This commit is contained in:
Victor Stinner 2022-03-12 00:10:02 +01:00 committed by GitHub
parent ecfff63e06
commit 882d8096c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 294 additions and 91 deletions

View file

@ -2033,7 +2033,7 @@ _PyFloat_DebugMallocStats(FILE *out)
/*----------------------------------------------------------------------------
* _PyFloat_{Pack,Unpack}{2,4,8}. See floatobject.h.
* PyFloat_{Pack,Unpack}{2,4,8}. See floatobject.h.
* To match the NPY_HALF_ROUND_TIES_TO_EVEN behavior in:
* https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/halffloat.c
* We use:
@ -2044,8 +2044,9 @@ _PyFloat_DebugMallocStats(FILE *out)
*/
int
_PyFloat_Pack2(double x, unsigned char *p, int le)
PyFloat_Pack2(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
double f;
@ -2148,8 +2149,9 @@ _PyFloat_Pack2(double x, unsigned char *p, int le)
}
int
_PyFloat_Pack4(double x, unsigned char *p, int le)
PyFloat_Pack4(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (float_format == unknown_format) {
unsigned char sign;
int e;
@ -2255,8 +2257,9 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
}
int
_PyFloat_Pack8(double x, unsigned char *p, int le)
PyFloat_Pack8(double x, char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (double_format == unknown_format) {
unsigned char sign;
int e;
@ -2384,8 +2387,9 @@ _PyFloat_Pack8(double x, unsigned char *p, int le)
}
double
_PyFloat_Unpack2(const unsigned char *p, int le)
PyFloat_Unpack2(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
unsigned char sign;
int e;
unsigned int f;
@ -2446,8 +2450,9 @@ _PyFloat_Unpack2(const unsigned char *p, int le)
}
double
_PyFloat_Unpack4(const unsigned char *p, int le)
PyFloat_Unpack4(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (float_format == unknown_format) {
unsigned char sign;
int e;
@ -2524,8 +2529,9 @@ _PyFloat_Unpack4(const unsigned char *p, int le)
}
double
_PyFloat_Unpack8(const unsigned char *p, int le)
PyFloat_Unpack8(const char *data, int le)
{
unsigned char *p = (unsigned char *)data;
if (double_format == unknown_format) {
unsigned char sign;
int e;