Skip to content

Commit b61ecc7

Browse files
committed
Move S_uiv_2buf from sv.c to sv_inline.h
This allows other parts of core to do integer stringification using the same, fast function.
1 parent 48972dd commit b61ecc7

File tree

5 files changed

+96
-95
lines changed

5 files changed

+96
-95
lines changed

embed.fnc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,6 +3641,11 @@ EXop |bool |try_amagic_bin |int method \
36413641
|int flags
36423642
EXop |bool |try_amagic_un |int method \
36433643
|int flags
3644+
ERTi |char * |uiv_2buf |NN char * const buf \
3645+
|const IV iv \
3646+
|UV uv \
3647+
|const int is_uv \
3648+
|NN char ** const peob
36443649
Adp |SSize_t|unpackstring |NN const char *pat \
36453650
|NN const char *patend \
36463651
|NN const char *s \
@@ -5883,11 +5888,6 @@ ST |STRLEN |sv_pos_u2b_midway \
58835888
|const STRLEN uend
58845889
i |void |sv_unglob |NN SV * const sv \
58855890
|U32 flags
5886-
RTi |char * |uiv_2buf |NN char * const buf \
5887-
|const IV iv \
5888-
|UV uv \
5889-
|const int is_uv \
5890-
|NN char ** const peob
58915891
S |void |utf8_mg_len_cache_update \
58925892
|NN SV * const sv \
58935893
|NN MAGIC ** const mgp \

embed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@
18551855
# define skipspace_flags(a,b) Perl_skipspace_flags(aTHX_ a,b)
18561856
# define sv_magicext_mglob(a) Perl_sv_magicext_mglob(aTHX_ a)
18571857
# define sv_only_taint_gmagic Perl_sv_only_taint_gmagic
1858+
# define uiv_2buf S_uiv_2buf
18581859
# define utf16_to_utf8_base(a,b,c,d,e,f) Perl_utf16_to_utf8_base(aTHX_ a,b,c,d,e,f)
18591860
# define utf8_to_utf16_base(a,b,c,d,e,f) Perl_utf8_to_utf16_base(aTHX_ a,b,c,d,e,f)
18601861
# define validate_proto(a,b,c,d) Perl_validate_proto(aTHX_ a,b,c,d)
@@ -2178,7 +2179,6 @@
21782179
# define sv_pos_u2b_forwards S_sv_pos_u2b_forwards
21792180
# define sv_pos_u2b_midway S_sv_pos_u2b_midway
21802181
# define sv_unglob(a,b) S_sv_unglob(aTHX_ a,b)
2181-
# define uiv_2buf S_uiv_2buf
21822182
# define utf8_mg_len_cache_update(a,b,c) S_utf8_mg_len_cache_update(aTHX_ a,b,c)
21832183
# define utf8_mg_pos_cache_update(a,b,c,d,e) S_utf8_mg_pos_cache_update(aTHX_ a,b,c,d,e)
21842184
# define visit(a,b,c) S_visit(aTHX_ a,b,c)

proto.h

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sv.c

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,88 +2766,6 @@ Perl_sv_2num(pTHX_ SV *const sv)
27662766
return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
27672767
}
27682768

2769-
/* int2str_table: lookup table containing string representations of all
2770-
* two digit numbers. For example, int2str_table.arr[0] is "00" and
2771-
* int2str_table.arr[12*2] is "12".
2772-
*
2773-
* We are going to read two bytes at a time, so we have to ensure that
2774-
* the array is aligned to a 2 byte boundary. That's why it was made a
2775-
* union with a dummy U16 member. */
2776-
static const union {
2777-
char arr[200];
2778-
U16 dummy;
2779-
} int2str_table = {{
2780-
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
2781-
'0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
2782-
'1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
2783-
'2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
2784-
'2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
2785-
'3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
2786-
'4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
2787-
'4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
2788-
'5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
2789-
'6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
2790-
'7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
2791-
'7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
2792-
'8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
2793-
'9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
2794-
'9', '8', '9', '9'
2795-
}};
2796-
2797-
/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2798-
* UV as a string towards the end of buf, and return pointers to start and
2799-
* end of it.
2800-
*
2801-
* We assume that buf is at least TYPE_CHARS(UV) long.
2802-
*/
2803-
2804-
PERL_STATIC_INLINE char *
2805-
S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2806-
{
2807-
char *ptr = buf + TYPE_CHARS(UV);
2808-
char * const ebuf = ptr;
2809-
int sign;
2810-
U16 *word_ptr, *word_table;
2811-
2812-
PERL_ARGS_ASSERT_UIV_2BUF;
2813-
2814-
/* ptr has to be properly aligned, because we will cast it to U16* */
2815-
assert(PTR2nat(ptr) % 2 == 0);
2816-
/* we are going to read/write two bytes at a time */
2817-
word_ptr = (U16*)ptr;
2818-
word_table = (U16*)int2str_table.arr;
2819-
2820-
if (UNLIKELY(is_uv))
2821-
sign = 0;
2822-
else if (iv >= 0) {
2823-
uv = iv;
2824-
sign = 0;
2825-
} else {
2826-
/* Using 0- here to silence bogus warning from MS VC */
2827-
uv = (UV) (0 - (UV) iv);
2828-
sign = 1;
2829-
}
2830-
2831-
while (uv > 99) {
2832-
*--word_ptr = word_table[uv % 100];
2833-
uv /= 100;
2834-
}
2835-
ptr = (char*)word_ptr;
2836-
2837-
if (uv < 10)
2838-
*--ptr = (char)uv + '0';
2839-
else {
2840-
*--word_ptr = word_table[uv];
2841-
ptr = (char*)word_ptr;
2842-
}
2843-
2844-
if (sign)
2845-
*--ptr = '-';
2846-
2847-
*peob = ebuf;
2848-
return ptr;
2849-
}
2850-
28512769
/* Helper for sv_2pv_flags and sv_vcatpvfn_flags. If the NV is an
28522770
* infinity or a not-a-number, writes the appropriate strings to the
28532771
* buffer, including a zero byte. On success returns the written length,

sv_inline.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,89 @@ Perl_sv_setpv_freshbuf(pTHX_ SV *const sv)
10031003
return SvPVX(sv);
10041004
}
10051005

1006+
/* Perl_int2str_table: lookup table containing string representations
1007+
* of all two digit numbers. For example, int2str_table.arr[0] is "00"
1008+
* and int2str_table.arr[12*2] is "12".
1009+
*
1010+
* We are going to read two bytes at a time, so we have to ensure that
1011+
* the array is aligned to a 2 byte boundary. That's why it was made a
1012+
* union with a dummy U16 member. */
1013+
static const union {
1014+
char arr[200];
1015+
U16 dummy;
1016+
} Perl_int2str_table = {{
1017+
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
1018+
'0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
1019+
'1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
1020+
'2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
1021+
'2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
1022+
'3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
1023+
'4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
1024+
'4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
1025+
'5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
1026+
'6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
1027+
'7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
1028+
'7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
1029+
'8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
1030+
'9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
1031+
'9', '8', '9', '9'
1032+
}};
1033+
1034+
/* uiv_2buf() was originally a private routine in sv.c for use by
1035+
* sv_2pv_flags(), but its usefulness elsewhere was noted, and it was
1036+
* moved out here. It prints an IV or UV as a string towards the end
1037+
* of buf, and return pointers to start and end of it.
1038+
*
1039+
* We assume that buf is at least TYPE_CHARS(UV) long.
1040+
*/
1041+
1042+
PERL_STATIC_INLINE char *
1043+
S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
1044+
{
1045+
char *ptr = buf + TYPE_CHARS(UV);
1046+
char * const ebuf = ptr;
1047+
int sign;
1048+
U16 *word_ptr, *word_table;
1049+
1050+
PERL_ARGS_ASSERT_UIV_2BUF;
1051+
1052+
/* ptr has to be properly aligned, because we will cast it to U16* */
1053+
assert(PTR2nat(ptr) % 2 == 0);
1054+
/* we are going to read/write two bytes at a time */
1055+
word_ptr = (U16*)ptr;
1056+
word_table = (U16*)Perl_int2str_table.arr;
1057+
1058+
if (UNLIKELY(is_uv))
1059+
sign = 0;
1060+
else if (iv >= 0) {
1061+
uv = iv;
1062+
sign = 0;
1063+
} else {
1064+
/* Using 0- here to silence bogus warning from MS VC */
1065+
uv = (UV) (0 - (UV) iv);
1066+
sign = 1;
1067+
}
1068+
1069+
while (uv > 99) {
1070+
*--word_ptr = word_table[uv % 100];
1071+
uv /= 100;
1072+
}
1073+
ptr = (char*)word_ptr;
1074+
1075+
if (uv < 10)
1076+
*--ptr = (char)uv + '0';
1077+
else {
1078+
*--word_ptr = word_table[uv];
1079+
ptr = (char*)word_ptr;
1080+
}
1081+
1082+
if (sign)
1083+
*--ptr = '-';
1084+
1085+
*peob = ebuf;
1086+
return ptr;
1087+
}
1088+
10061089
/*
10071090
* ex: set ts=8 sts=4 sw=4 et:
10081091
*/

0 commit comments

Comments
 (0)