Skip to content

Commit 0762960

Browse files
committed
Create BYTES_REMAINING_IN_WORD()
This macro encapsulates the task of finding how far until the next word boundary the passed-in address is. There are several places that could use this, but instead of converting use this in those places, the next commit will create macros that depend on this one and those places will instead convert to use those other new macros.
1 parent 93ed852 commit 0762960

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

inline.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,14 +1459,21 @@ Perl_valid_utf8_to_uv(const U8 *s, STRLEN *retlen)
14591459
# define PERL_WORDSIZE sizeof(PERL_UINTMAX_T)
14601460
# define PERL_WORD_BOUNDARY_MASK (PERL_WORDSIZE - 1)
14611461

1462-
/* Evaluates to 0 if 'x' is at a word boundary; otherwise evaluates to 1, by
1463-
* or'ing together the lowest bits of 'x'. Hopefully the final term gets
1464-
* optimized out completely on a 32-bit system, and its mask gets optimized out
1465-
* on a 64-bit system */
1466-
# define PERL_IS_SUBWORD_ADDR(x) (1 & ( PTR2nat(x) \
1467-
| ( PTR2nat(x) >> 1) \
1468-
| ( ( (PTR2nat(x) \
1469-
& PERL_WORD_BOUNDARY_MASK) >> 2))))
1462+
/* Given an address of a byte 'x', how many bytes away is that address to the
1463+
* following closest full word boundary. */
1464+
# define BYTES_REMAINING_IN_WORD(x) \
1465+
( (PERL_WORDSIZE - (PTR2nat(x) & PERL_WORD_BOUNDARY_MASK)) \
1466+
& PERL_WORD_BOUNDARY_MASK)
1467+
/* For example, consider two addresses in an 8 byte word size (the dots are
1468+
* don't cares):
1469+
* 0b...............010 0b...............000
1470+
* ((8 - (0b1101010 & 0x7)) & 0x7) ((8 - (0b1101000 & 0x7)) & 0x7)
1471+
* ((8 - 0b10) & 0x7) ((8 - 0) & 0x7)
1472+
* (6 & 0x7) (8 & 0x7)
1473+
* 6 0 */
1474+
1475+
/* Evaluates to 0 if 'x' is at a word boundary; otherwise evaluates to 1 */
1476+
# define PERL_IS_SUBWORD_ADDR(x) (BYTES_REMAINING_IN_WORD(x) != 0)
14701477

14711478
/*
14721479
=for apidoc is_utf8_invariant_string
@@ -2116,6 +2123,7 @@ S_variant_under_utf8_count(const U8* const s, const U8* const e)
21162123
# undef PERL_COUNT_MULTIPLIER
21172124
# undef PERL_WORD_BOUNDARY_MASK
21182125
# undef PERL_VARIANTS_WORD_MASK
2126+
# undef BYTES_REMAINING_IN_WORD
21192127
#endif
21202128

21212129
#define is_utf8_string(s, len) is_utf8_string_loclen(s, len, NULL, NULL)

0 commit comments

Comments
 (0)