-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Use bulk conversion in BCMath of BCD/CHAR where possible #14103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <nielsdos@php.net> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#include "bcmath.h" | ||
#include "convert.h" | ||
|
||
#define SWAR_ONES (~((size_t) 0) / 0xFF) | ||
#define SWAR_REPEAT(x) (SWAR_ONES * (x)) | ||
|
||
static char *bc_copy_and_shift_numbers(char *dest, const char *source, const char *source_end, unsigned char shift, bool add) | ||
{ | ||
size_t bulk_shift = SWAR_REPEAT(shift); | ||
if (!add) { | ||
bulk_shift = -bulk_shift; | ||
shift = -shift; | ||
} | ||
|
||
while (source + sizeof(size_t) <= source_end) { | ||
size_t bytes; | ||
memcpy(&bytes, source, sizeof(bytes)); | ||
|
||
bytes += bulk_shift; | ||
memcpy(dest, &bytes, sizeof(bytes)); | ||
|
||
source += sizeof(size_t); | ||
dest += sizeof(size_t); | ||
} | ||
|
||
while (source < source_end) { | ||
*dest = *source + shift; | ||
dest++; | ||
source++; | ||
} | ||
|
||
return dest; | ||
} | ||
|
||
char *bc_copy_ch_val(char *dest, const char *source, const char *source_end) | ||
{ | ||
return bc_copy_and_shift_numbers(dest, source, source_end, '0', false); | ||
} | ||
|
||
char *bc_copy_bcd_val(char *dest, const char *source, const char *source_end) | ||
{ | ||
return bc_copy_and_shift_numbers(dest, source, source_end, '0', true); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Authors: Niels Dossche <nielsdos@php.net> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#ifndef BCMATH_CONVERT_H | ||
#define BCMATH_CONVERT_H | ||
|
||
char *bc_copy_ch_val(char *dest, const char *source, const char *source_end); | ||
char *bc_copy_bcd_val(char *dest, const char *source, const char *source_end); | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible add a
restrict
keyword fordest
andsource
? As those APIs are only used within other C files, so we don't need to have compatibility with C++?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I'll add the restrict keyword.
I'll explain what the code does.
The intention is to copy each byte from source to dest, but subtract or add '0' to each byte.
The idea of this patch is to try to read+write 8 bytes at once, adding/subtracting '0' to each byte also in parallel.
SWAR_ONES will be of the form 0x01010101 for 32-bit or 0x0101010101010101 for 64-bit.
Example: SWAR_ONES * 0xAB will therefore be equal to 0xABABABAB for 32-bit or 0xABABABABABABABAB for 64-bit.
So in this case, for SWAR_REPEAT('0'), it will be a 32/64-bit word where each byte is equal to '0', i.e. 0x303030...
Since we know that subtract/add overflow from one byte to another can't occur, we can subtract/add with 0x303030... to the entire 4/8 bytes which will be equivalent to adding 0x30 to each byte individually.
And to be complete: SWAR stands for "SIMD Within A Register"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh okay, well could you please write this in a comment in the file? :D