Skip to content

Commit

Permalink
Convert ext/bcmath to HNI
Browse files Browse the repository at this point in the history
Converts ext/bcmath to HNI and adds support for scaling

Closes #1534

Reviewed By: @sgolemon

Differential Revision: D1123805

Pulled By: @scannell
  • Loading branch information
simonwelsh authored and sgolemon committed Jan 10, 2014
1 parent 3d9cfbe commit 8dfad03
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 336 deletions.
2 changes: 2 additions & 0 deletions hphp/hhvm/anchor-syms.cpp
Expand Up @@ -24,13 +24,15 @@ namespace HPHP {
extern Extension s_zip_extension;
extern Extension s_fileinfo_extension;
extern Extension s_intl_extension;
extern Extension s_bcmath_extension;
#ifdef HAVE_UODBC
extern Extension s_odbc_extension;
#endif
const Extension *g_anchor_extensions[] = {
&s_zip_extension,
&s_fileinfo_extension,
&s_intl_extension,
&s_bcmath_extension,
#ifdef HAVE_UODBC
&s_odbc_extension,
#endif
Expand Down
Expand Up @@ -15,11 +15,11 @@
+----------------------------------------------------------------------+
*/

#include "hphp/runtime/ext/ext_bcmath.h"
#include "hphp/runtime/base/base-includes.h"
#include "hphp/runtime/ext/bcmath/bcmath.h"
#include "hphp/runtime/base/ini-setting.h"

namespace HPHP {
IMPLEMENT_DEFAULT_EXTENSION(bcmath);
///////////////////////////////////////////////////////////////////////////////

class bcmath_data {
Expand Down Expand Up @@ -48,13 +48,13 @@ static void php_str2num(bc_num *num, const char *str) {
}
}

bool f_bcscale(int64_t scale) {
static bool HHVM_FUNCTION(bcscale, int64_t scale) {
BCG(bc_precision) = scale < 0 ? 0 : scale;
return true;
}

String f_bcadd(const String& left, const String& right,
int64_t scale /* = -1 */) {
static String HHVM_FUNCTION(bcadd, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, result;
bc_init_num(&first);
Expand All @@ -73,8 +73,8 @@ String f_bcadd(const String& left, const String& right,
return ret;
}

String f_bcsub(const String& left, const String& right,
int64_t scale /* = -1 */) {
static String HHVM_FUNCTION(bcsub, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, result;
bc_init_num(&first);
Expand All @@ -93,8 +93,8 @@ String f_bcsub(const String& left, const String& right,
return ret;
}

int64_t f_bccomp(const String& left, const String& right,
int64_t scale /* = -1 */) {
static int64_t HHVM_FUNCTION(bccomp, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second;
bc_init_num(&first);
Expand All @@ -107,8 +107,8 @@ int64_t f_bccomp(const String& left, const String& right,
return ret;
}

String f_bcmul(const String& left, const String& right,
int64_t scale /* = -1 */) {
static String HHVM_FUNCTION(bcmul, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, result;
bc_init_num(&first);
Expand All @@ -127,7 +127,7 @@ String f_bcmul(const String& left, const String& right,
return ret;
}

String f_bcdiv(const String& left, const String& right,
static Variant HHVM_FUNCTION(bcdiv, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, result;
Expand All @@ -138,7 +138,7 @@ String f_bcdiv(const String& left, const String& right,
php_str2num(&second, (char*)right.data());
if (bc_divide(first, second, &result, scale) == -1) {
raise_warning("Division by zero");
return String();
return Variant();
}
String ret(bc_num2str(result), AttachString);
bc_free_num(&first);
Expand All @@ -147,7 +147,7 @@ String f_bcdiv(const String& left, const String& right,
return ret;
}

String f_bcmod(const String& left, const String& right) {
static Variant HHVM_FUNCTION(bcmod, const String& left, const String& right) {
bc_num first, second, result;
bc_init_num(&first);
bc_init_num(&second);
Expand All @@ -156,7 +156,7 @@ String f_bcmod(const String& left, const String& right) {
php_str2num(&second, (char*)right.data());
if (bc_modulo(first, second, &result, 0) == -1) {
raise_warning("Division by zero");
return String();
return Variant();
}
String ret(bc_num2str(result), AttachString);
bc_free_num(&first);
Expand All @@ -165,8 +165,8 @@ String f_bcmod(const String& left, const String& right) {
return ret;
}

String f_bcpow(const String& left, const String& right,
int64_t scale /* = -1 */) {
static String HHVM_FUNCTION(bcpow, const String& left, const String& right,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, result;
bc_init_num(&first);
Expand All @@ -185,8 +185,8 @@ String f_bcpow(const String& left, const String& right,
return ret;
}

Variant f_bcpowmod(const String& left, const String& right,
const String& modulus, int64_t scale /* = -1 */) {
static Variant HHVM_FUNCTION(bcpowmod, const String& left, const String& right,
const String& modulus, int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num first, second, mod, result;
bc_init_num(&first);
Expand All @@ -210,7 +210,8 @@ Variant f_bcpowmod(const String& left, const String& right,
return ret;
}

Variant f_bcsqrt(const String& operand, int64_t scale /* = -1 */) {
static Variant HHVM_FUNCTION(bcsqrt, const String& operand,
int64_t scale /* = -1 */) {
if (scale < 0) scale = BCG(bc_precision);
bc_num result;
bc_init_num(&result);
Expand All @@ -230,6 +231,29 @@ Variant f_bcsqrt(const String& operand, int64_t scale /* = -1 */) {

///////////////////////////////////////////////////////////////////////////////

class bcmathExtension : public Extension {
public:
bcmathExtension() : Extension("bcmath") {}
virtual void moduleInit() {
IniSetting::Bind("bcmath.scale", "0", ini_on_update_long, ini_get_long,
&BCG(bc_precision));

HHVM_FE(bcscale);
HHVM_FE(bcadd);
HHVM_FE(bcsub);
HHVM_FE(bccomp);
HHVM_FE(bcmul);
HHVM_FE(bcdiv);
HHVM_FE(bcmod);
HHVM_FE(bcpow);
HHVM_FE(bcpowmod);
HHVM_FE(bcsqrt);
loadSystemlib();
}
} s_bcmath_extension;

///////////////////////////////////////////////////////////////////////////////

extern "C" {
struct BCMathGlobals *get_bcmath_globals() {
return &HPHP::s_globals.get()->data;
Expand Down
146 changes: 146 additions & 0 deletions hphp/runtime/ext/bcmath/ext_bcmath.php
@@ -0,0 +1,146 @@
<?hh

/**
* bcscale() - http://php.net/function.bcscale
*
* @param int $scale - The scale factor.
*
* @return bool - Returns TRUE on success or FALSE on failure.
*/
<<__Native>>
function bcscale(int $scale): bool;

/**
* bcadd() - http://php.net/function.bcadd
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return string - The sum of the two operands, as a string.
*/
<<__Native>>
function bcadd(string $left, string $right, int $scale = -1): string;

/**
* bcsub() - http://php.net/function.bcsub
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return string - The result of the subtraction, as a string.
*/
<<__Native>>
function bcsub(string $left, string $right, int $scale = -1): string;

/**
* bccomp() - http://php.net/function.bccomp
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return int - Returns 0 if the two operands are equal, 1 if the left_operand
* is larger than the right_operand, -1 otherwise.
*/
<<__Native>>
function bccomp(string $left, string $right, int $scale = -1): int;

/**
* bcmul() - http://php.net/function.bcmul
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return string - Returns the result as a string.
*/
<<__Native>>
function bcmul(string $left, string $right, int $scale = -1): string;

/**
* bcdiv() - http://php.net/function.bcdiv
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return string - Returns the result of the division as a string, or NULL if
* right_operand is 0.
*/
<<__Native>>
function bcdiv(string $left, string $right, int $scale = -1): ?string;

/**
* bcmod() - http://php.net/function.bcmod
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
*
* @return string - Returns the modulus as a string, or NULL if modulus is 0.
*/
<<__Native>>
function bcmod(string $left, string $right): ?string;

/**
* bcpow() - http://php.net/function.bcpow
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You can
* also set the global default scale for all functions
* by using bcscale().
*
* @return string - Returns the result as a string.
*/
<<__Native>>
function bcpow(string $left, string $right, int $scale = -1): string;

/**
* bcpowmod() - http://php.net/function.bcpowmod
*
* @param string $left - The left operand, as a string.
* @param string $right - The right operand, as a string.
* @param string $modulus - The modulus, as a string
* @param int $scale - This optional parameter is used to set the number of
* digits after the decimal place in the result. You
* can also set the global default scale for all
* functions by using bcscale().
*
* @return string - Returns the result as a string, or NULL if modulus is 0.
*/
<<__Native>>
function bcpowmod(string $left, string $right, string $modulus,
int $scale = -1): ?string;

/**
* bcsqrt() - http://php.net/function.bcsqrt
*
* @param string $operand - The operand, as a string.
* @param int $scale - This optional parameter is used to set the number
* of digits after the decimal place in the result.
* You can also set the global default scale for all
* functions by using bcscale().
*
* @return string - Returns the square root as a string, or NULL if operand is
* negative.
*/
<<__Native>>
function bcsqrt(string $operand, int $scale = -1): ?string;
1 change: 0 additions & 1 deletion hphp/runtime/ext/ext.h
Expand Up @@ -24,7 +24,6 @@
#include "hphp/runtime/ext/ext_apc.h"
#include "hphp/runtime/ext/ext_array.h"
#include "hphp/runtime/ext/ext_asio.h"
#include "hphp/runtime/ext/ext_bcmath.h"
#include "hphp/runtime/ext/ext_bzip2.h"
#include "hphp/runtime/ext/ext_class.h"
#include "hphp/runtime/ext/ext_closure.h"
Expand Down
43 changes: 0 additions & 43 deletions hphp/runtime/ext/ext_bcmath.h

This file was deleted.

0 comments on commit 8dfad03

Please sign in to comment.