Multiplication is performed after converting to uint32_t/uint64_t, making calculations faster.
---------
Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
Co-authored-by: Gina Peter Banyard <girgias@php.net>
The code for _bc_do_add and _bc_do_sub were written slightly differently for
similar processing (and add was slower than sub), so I changed the code to one
similar to sub.
Also, _bc_do_add has been changed to use SIMD to perform faster calculations
when possible.
Changed to count trailing zeros using SIMD when converting a string to
a bc_num structure if possible.
Removed unnecessary pointer resetting.
Added UNEXPECTED to some branches.
This simplifies the code, and also might indirectly improve performance
due to a decrease in instruction cache pressure. Although the latter is
probably negligible.
This works because 0x30 has no overlapping bits with [0, 9].
Also avoid some memsets where we do call bc_new_num.
After:
```
1.2066178321838
1.5389559268951
1.6050860881805
```
Before:
```
1.3858470916748
1.6806011199951
1.9091980457306
```
Using SIMD to accelerate the validation.
Using the benchmark from #14076.
After:
```
1.3504369258881
1.6206321716309
1.6845638751984
```
Before:
```
1.4750170707703
1.9039781093597
1.9632289409637
```
Since freeing can deal with NULL, we can avoid calling bc_init_num and
avoid resetting the number during parsing.
Using benchmark from #14076.
Before:
```
1.544440984726
2.0288550853729
2.092139005661
```
After:
```
1.5324399471283
1.9081380367279
2.065819978714
```
On my i7-4790 with benchmark from #14076, on top of #14101 I obtain the
following results:
before (with #14101):
```
1.672737121582
2.3618471622467
2.3474779129028
```
after (with #14101 + this):
```
1.5878579616547
2.0568618774414
2.0204811096191
```
Since the two allocations are tied together anyway, we can just use a
single allocation. Moreover, this actually seemed like the intention
because the bc_struct allocation already accounted for the length and
scale.
power is a copy of base and returns early if base->n_scale is non-zero. Since
scale is size_t, it is always greater than or equal to 0, so rscale is always
the value of scale.
Re-formats the BCmath extension to have consistent formatting.
Mostly, it adds the spaces in calculations to have them more readable.
Also:
- removes unused headers
- removes few variables which are used only once in the code
Co-authored-by: George Peter Banyard <girgias@php.net>
* ext/bcmath: coding style: use indentation
And add braces to block statements, as the current code was pretty much unreadable with how inconsistent it was.
* ext/bcmath: Remove some useless header inclusions
* ext/bcmath: Use standard C99 bool type instead of char
* ext/bcmath: Include specific headers instead of config.h
* Restructure definitions to reduce header inclusions
* Use size_t as a more appropriate type
* Remove unused variable full_scale
* Refactor bc_raisemod() to get rid of Zend dependencies
This separates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsibility of the library
* Refactor bc_raise() to get rid of Zend dependencies
This separates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsibility of the library
* Refactor bc_divmod() and bc_modulo() to return bool
Return false on division by 0 attempt instead of -1 and true on success instead of 0
* Refactor bc_divide() to return bool
Return false on division by 0 attempt instead of -1 and true on success instead of 0
* Zend/zend_enum: make `forbidden_methods` static+const
* main/php_syslog: make `xdigits` static
* sapi/fpm: make several globals `const`
* sapi/phpdbg: make `OPTIONS` static
* sapi/phpdbg/help: make help texts const
* sapi/cli: make `template_map` const
* ext/ffi: make `zend_ffi_types` static
* ext/bcmath: make `ref_str` const
* ext/phar: make several globals static+const