Clean up more code

The *NSYNC linked list API is good enough that it deserves to be part of
the C libray, so this change writes an improved version of it which uses
that offsetof() trick from the Linux Kernel. We vendor all of the *NSYNC
tests in third_party which helped confirm the needed refactoring is safe

This change also deletes more old code that didn't pan out. My goal here
is to work towards a vision where the Cosmopolitan core libraries become
less experimental and more focused on curation. This better reflects the
current level of quality we've managed to achieve.
This commit is contained in:
Justine Tunney 2023-07-06 06:57:28 -07:00
parent 88612a2cd7
commit 0a24b4fc3c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
268 changed files with 632 additions and 8688 deletions

40
third_party/python/Modules/bextra.c vendored Normal file
View file

@ -0,0 +1,40 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "third_party/python/Modules/bextra.h"
unsigned BitFieldExtract(const unsigned *p, size_t i, char b) {
unsigned k, r, w;
w = sizeof(unsigned) * CHAR_BIT;
if (b) {
b &= w - 1;
i *= b;
k = i & (w - 1);
i /= w;
if (k <= w - b) {
return (p[i] >> k) & ((1u << (b - 1)) | ((1u << (b - 1)) - 1));
} else {
r = p[i] >> k;
r |= p[i + 1] << (w - k);
r &= (1ul << b) - 1;
return r;
}
} else {
return 0;
}
}

10
third_party/python/Modules/bextra.h vendored Normal file
View file

@ -0,0 +1,10 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_PYTHON_MODULES_BEXTRA_H_
#define COSMOPOLITAN_THIRD_PARTY_PYTHON_MODULES_BEXTRA_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
unsigned BitFieldExtract(const unsigned *, size_t, char);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_PYTHON_MODULES_BEXTRA_H_ */

View file

@ -7,7 +7,6 @@
#define PY_SSIZE_T_CLEAN
#include "third_party/python/Modules/unicodedata.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "libc/nexgen32e/kompressor.h"
#include "third_party/python/Include/floatobject.h"
#include "third_party/python/Include/import.h"
@ -21,6 +20,7 @@
#include "third_party/python/Include/structmember.h"
#include "third_party/python/Include/ucnhash.h"
#include "third_party/python/Include/yoink.h"
#include "third_party/python/Modules/bextra.h"
#include "third_party/python/Modules/unicodedata_unidata.h"
/* clang-format off */
@ -405,7 +405,7 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
/* high byte is number of hex bytes (usually one or two), low byte
is prefix code (from*/
count = _bextra(_PyUnicode_Decomp, index, _PyUnicode_DecompBits) >> 8;
count = BitFieldExtract(_PyUnicode_Decomp, index, _PyUnicode_DecompBits) >> 8;
/* XXX: could allocate the PyString up front instead
(strlen(prefix) + 5 * count + 1 bytes) */
@ -413,7 +413,7 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
/* Based on how index is calculated above and _PyUnicode_Decomp is
generated from Tools/unicode/makeunicodedata.py, it should not be
possible to overflow _PyUnicode_DecompPrefix. */
prefix_index = _bextra(_PyUnicode_Decomp, index, _PyUnicode_DecompBits) & 255;
prefix_index = BitFieldExtract(_PyUnicode_Decomp, index, _PyUnicode_DecompBits) & 255;
assert(prefix_index < Py_ARRAY_LENGTH(_PyUnicode_DecompPrefix));
/* copy prefix */
@ -425,8 +425,8 @@ unicodedata_UCD_decomposition_impl(PyObject *self, int chr)
decomp[i++] = ' ';
assert(i < sizeof(decomp));
PyOS_snprintf(decomp + i, sizeof(decomp) - i, "%04X",
_bextra(_PyUnicode_Decomp, ++index,
_PyUnicode_DecompBits));
BitFieldExtract(_PyUnicode_Decomp, ++index,
_PyUnicode_DecompBits));
i += strlen(decomp + i);
}
return PyUnicode_FromStringAndSize(decomp, i);

View file

@ -5,11 +5,11 @@
https://docs.python.org/3/license.html │
*/
#include "libc/fmt/fmt.h"
#include "libc/intrin/bits.h"
#include "third_party/python/Include/pyctype.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h"
#include "third_party/python/Modules/bextra.h"
#include "third_party/python/Modules/unicodedata.h"
#include "third_party/python/Modules/unicodedata_unidata.h"
/* clang-format off */
@ -174,7 +174,7 @@ _PyUnicode_GetCode(PyObject *self, const char *name, int namelen, Py_UCS4 *code,
details */
h = (unsigned int)_gethash(name, namelen, _PyUnicode_CodeMagic);
i = ~h & mask;
v = _bextra(_PyUnicode_CodeHash, i, _PyUnicode_CodeHashBits);
v = BitFieldExtract(_PyUnicode_CodeHash, i, _PyUnicode_CodeHashBits);
if (!v)
return 0;
if (_cmpname(self, v, name, namelen))
@ -184,7 +184,7 @@ _PyUnicode_GetCode(PyObject *self, const char *name, int namelen, Py_UCS4 *code,
incr = mask;
for (;;) {
i = (i + incr) & mask;
v = _bextra(_PyUnicode_CodeHash, i, _PyUnicode_CodeHashBits);
v = BitFieldExtract(_PyUnicode_CodeHash, i, _PyUnicode_CodeHashBits);
if (!v)
return 0;
if (_cmpname(self, v, name, namelen))
@ -247,10 +247,10 @@ _PyUnicode_GetUcName(PyObject *self, Py_UCS4 code, char *buffer, int buflen,
}
/* get offset into phrasebook */
offset = _PyUnicode_PhrasebookOffset1[(code>>_PyUnicode_PhrasebookShift)];
offset = _bextra(_PyUnicode_PhrasebookOffset2,
(offset << _PyUnicode_PhrasebookShift) +
(code & ((1 << _PyUnicode_PhrasebookShift) - 1)),
_PyUnicode_PhrasebookOffset2Bits);
offset = BitFieldExtract(_PyUnicode_PhrasebookOffset2,
(offset << _PyUnicode_PhrasebookShift) +
(code & ((1 << _PyUnicode_PhrasebookShift) - 1)),
_PyUnicode_PhrasebookOffset2Bits);
if (!offset)
return 0;
i = 0;
@ -271,8 +271,8 @@ _PyUnicode_GetUcName(PyObject *self, Py_UCS4 code, char *buffer, int buflen,
word has bit 7 set. the last word in a string ends with
0x80 */
w = (_PyUnicode_Lexicon +
_bextra(_PyUnicode_LexiconOffset, word,
_PyUnicode_LexiconOffsetBits));
BitFieldExtract(_PyUnicode_LexiconOffset, word,
_PyUnicode_LexiconOffsetBits));
while (*w < 128) {
if (i >= buflen)
return 0; /* buffer overflow */

View file

@ -5,6 +5,7 @@
https://docs.python.org/3/license.html │
*/
#include "libc/intrin/bits.h"
#include "third_party/python/Modules/bextra.h"
#include "third_party/python/Modules/unicodedata.h"
#include "third_party/python/Modules/unicodedata_unidata.h"
/* clang-format off */
@ -31,7 +32,7 @@ _PyUnicode_GetDecompRecord(PyObject *self,
}
/* high byte is number of hex bytes (usually one or two), low byte
is prefix code (from*/
decomp = _bextra(_PyUnicode_Decomp, *index, _PyUnicode_DecompBits);
decomp = BitFieldExtract(_PyUnicode_Decomp, *index, _PyUnicode_DecompBits);
*count = decomp >> 8;
*prefix = decomp & 255;
(*index)++;

View file

@ -4,10 +4,10 @@
Python 3
https://docs.python.org/3/license.html │
*/
#include "libc/intrin/bits.h"
#include "libc/intrin/likely.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymem.h"
#include "third_party/python/Modules/bextra.h"
#include "third_party/python/Modules/unicodedata.h"
#include "third_party/python/Modules/unicodedata_unidata.h"
/* clang-format off */
@ -115,10 +115,10 @@ _PyUnicode_NfcNfkc(PyObject *self, PyObject *input, int k)
}
index = f * UNIDATA_TOTAL_LAST + l;
index1 = _PyUnicode_CompIndex[index >> _PyUnicode_CompShift];
code = _bextra(_PyUnicode_CompData,
(index1 << _PyUnicode_CompShift)+
(index & ((1 << _PyUnicode_CompShift) - 1)),
_PyUnicode_CompDataBits);
code = BitFieldExtract(_PyUnicode_CompData,
(index1 << _PyUnicode_CompShift)+
(index & ((1 << _PyUnicode_CompShift) - 1)),
_PyUnicode_CompDataBits);
if (code == 0)
goto not_combinable;
/* Replace the original character. */

View file

@ -4,9 +4,9 @@
Python 3
https://docs.python.org/3/license.html │
*/
#include "libc/intrin/bits.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymem.h"
#include "third_party/python/Modules/bextra.h"
#include "third_party/python/Modules/unicodedata.h"
#include "third_party/python/Modules/unicodedata_unidata.h"
/* clang-format off */
@ -97,9 +97,9 @@ _PyUnicode_NfdNfkd(PyObject *self, PyObject *input, int k)
/* Copy decomposition onto the stack, in reverse
order. */
while(count) {
code = _bextra(_PyUnicode_Decomp,
index + (--count),
_PyUnicode_DecompBits);
code = BitFieldExtract(_PyUnicode_Decomp,
index + (--count),
_PyUnicode_DecompBits);
stack[stackptr++] = code;
}
}

View file

@ -156,6 +156,7 @@ THIRD_PARTY_PYTHON_HDRS = \
third_party/python/Include/unicodeobject.h \
third_party/python/Include/warnings.h \
third_party/python/Include/weakrefobject.h \
third_party/python/Modules/bextra.h \
third_party/python/Modules/unicodedata.h \
third_party/python/Modules/unicodedata_unidata.h \
third_party/python/Modules/_decimal/docstrings.h \
@ -417,6 +418,7 @@ THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \
third_party/python/Python/sysmodule.c \
third_party/python/Python/thread.c \
third_party/python/Python/traceback.c \
third_party/python/Modules/bextra.c \
third_party/python/Modules/unicodedata_3.2.0.c \
third_party/python/Modules/unicodedata_bidirectionalnames.c \
third_party/python/Modules/unicodedata_categorynames.c \