Move is_valid_ascii() to ascii.h.

This function requires simd.h, which is a rather large dependency
for a widely-used header file like pg_wchar.h.  Furthermore, there
is a report of a third-party tool that is struggling to use
pg_wchar.h due to its dependence on simd.h (presumably because
simd.h uses several intrinsics).  Moving the function to the much
less popular ascii.h resolves these issues for now.

This commit is back-patched for the benefit of the aforementioned
third-party tool.  The simd.h dependency was only added in v16,
but we've opted to back-patch to v15 so that is_valid_ascii() lives
in the same file for all versions where it exists.  This could
break existing third-party code that uses the function, but we
couldn't find any examples of such code.  It should be possible to
fix any code that this commit breaks by including ascii.h in the
file that uses is_valid_ascii().

Author: Jubilee Young
Reviewed-by: Tom Lane, John Naylor, Andres Freund, Eric Ridge
Discussion: https://postgr.es/m/CAPNHn3oKJJxMsYq%2BqLYzVJOFrUcOr4OF1EC-KtFT-qh8nOOOtQ%40mail.gmail.com
Backpatch-through: 15
This commit is contained in:
Nathan Bossart 2024-01-29 12:08:57 -06:00
parent 400928b83b
commit 97287bdfae
3 changed files with 69 additions and 69 deletions

View File

@ -13,6 +13,7 @@
#include "c.h"
#include "mb/pg_wchar.h"
#include "utils/ascii.h"
/*

View File

@ -22,8 +22,6 @@
#ifndef PG_WCHAR_H
#define PG_WCHAR_H
#include "port/simd.h"
/*
* The pg_wchar type
*/
@ -722,71 +720,4 @@ extern int mic2latin_with_table(const unsigned char *mic, unsigned char *p,
extern WCHAR *pgwin32_message_to_UTF16(const char *str, int len, int *utf16len);
#endif
/*
* Verify a chunk of bytes for valid ASCII.
*
* Returns false if the input contains any zero bytes or bytes with the
* high-bit set. Input len must be a multiple of the chunk size (8 or 16).
*/
static inline bool
is_valid_ascii(const unsigned char *s, int len)
{
const unsigned char *const s_end = s + len;
Vector8 chunk;
Vector8 highbit_cum = vector8_broadcast(0);
#ifdef USE_NO_SIMD
Vector8 zero_cum = vector8_broadcast(0x80);
#endif
Assert(len % sizeof(chunk) == 0);
while (s < s_end)
{
vector8_load(&chunk, s);
/* Capture any zero bytes in this chunk. */
#ifdef USE_NO_SIMD
/*
* First, add 0x7f to each byte. This sets the high bit in each byte,
* unless it was a zero. If any resulting high bits are zero, the
* corresponding high bits in the zero accumulator will be cleared.
*
* If none of the bytes in the chunk had the high bit set, the max
* value each byte can have after the addition is 0x7f + 0x7f = 0xfe,
* and we don't need to worry about carrying over to the next byte. If
* any input bytes did have the high bit set, it doesn't matter
* because we check for those separately.
*/
zero_cum &= (chunk + vector8_broadcast(0x7F));
#else
/*
* Set all bits in each lane of the highbit accumulator where input
* bytes are zero.
*/
highbit_cum = vector8_or(highbit_cum,
vector8_eq(chunk, vector8_broadcast(0)));
#endif
/* Capture all set bits in this chunk. */
highbit_cum = vector8_or(highbit_cum, chunk);
s += sizeof(chunk);
}
/* Check if any high bits in the high bit accumulator got set. */
if (vector8_is_highbit_set(highbit_cum))
return false;
#ifdef USE_NO_SIMD
/* Check if any high bits in the zero accumulator got cleared. */
if (zero_cum != vector8_broadcast(0x80))
return false;
#endif
return true;
}
#endif /* PG_WCHAR_H */

View File

@ -11,6 +11,74 @@
#ifndef _ASCII_H_
#define _ASCII_H_
#include "port/simd.h"
extern void ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz);
/*
* Verify a chunk of bytes for valid ASCII.
*
* Returns false if the input contains any zero bytes or bytes with the
* high-bit set. Input len must be a multiple of the chunk size (8 or 16).
*/
static inline bool
is_valid_ascii(const unsigned char *s, int len)
{
const unsigned char *const s_end = s + len;
Vector8 chunk;
Vector8 highbit_cum = vector8_broadcast(0);
#ifdef USE_NO_SIMD
Vector8 zero_cum = vector8_broadcast(0x80);
#endif
Assert(len % sizeof(chunk) == 0);
while (s < s_end)
{
vector8_load(&chunk, s);
/* Capture any zero bytes in this chunk. */
#ifdef USE_NO_SIMD
/*
* First, add 0x7f to each byte. This sets the high bit in each byte,
* unless it was a zero. If any resulting high bits are zero, the
* corresponding high bits in the zero accumulator will be cleared.
*
* If none of the bytes in the chunk had the high bit set, the max
* value each byte can have after the addition is 0x7f + 0x7f = 0xfe,
* and we don't need to worry about carrying over to the next byte. If
* any input bytes did have the high bit set, it doesn't matter
* because we check for those separately.
*/
zero_cum &= (chunk + vector8_broadcast(0x7F));
#else
/*
* Set all bits in each lane of the highbit accumulator where input
* bytes are zero.
*/
highbit_cum = vector8_or(highbit_cum,
vector8_eq(chunk, vector8_broadcast(0)));
#endif
/* Capture all set bits in this chunk. */
highbit_cum = vector8_or(highbit_cum, chunk);
s += sizeof(chunk);
}
/* Check if any high bits in the high bit accumulator got set. */
if (vector8_is_highbit_set(highbit_cum))
return false;
#ifdef USE_NO_SIMD
/* Check if any high bits in the zero accumulator got cleared. */
if (zero_cum != vector8_broadcast(0x80))
return false;
#endif
return true;
}
#endif /* _ASCII_H_ */