Skip to content

Commit

Permalink
Move hexdec & dechex builtins to runtime-common (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov authored Dec 10, 2024
1 parent 60eca25 commit 2398687
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 67 deletions.
12 changes: 8 additions & 4 deletions builtin-functions/kphp-light/math.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ define('M_SQRT1_2', 0.70710678118654752440);
define('M_LNPI', 1.14472988584940017414);
define('M_EULER', 0.57721566490153286061);

function mt_rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;
function mt_rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD): int;

function mt_getrandmax() ::: int;
function mt_getrandmax(): int;

function mt_srand ($seed ::: int = PHP_INT_MIN) ::: void;
function mt_srand ($seed ::: int = PHP_INT_MIN): void;

function rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD) ::: int;
function rand ($l ::: int = TODO_OVERLOAD, $r ::: int = TODO_OVERLOAD): int;

function dechex ($number ::: int): string;

function hexdec ($number ::: string): int;

// === UNSUPPORTED ===

Expand Down
6 changes: 0 additions & 6 deletions builtin-functions/kphp-light/unsupported/misc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ function bindec ($number ::: string) ::: int;

/** @kphp-extern-func-info generate-stub */
function decbin ($number ::: int) ::: string;
/** @kphp-extern-func-info generate-stub */
function dechex ($number ::: int) ::: string;

/** @kphp-extern-func-info generate-stub */
function hexdec ($number ::: string) ::: int;


/** @kphp-extern-func-info generate-stub */
function crc32 ($s ::: string) ::: int;
Expand Down
50 changes: 50 additions & 0 deletions runtime-common/stdlib/math/math-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2020 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime-common/stdlib/math/math-functions.h"

#include <cstdint>

#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/string/string-context.h"
#include "runtime-common/stdlib/string/string-functions.h"

string f$dechex(int64_t number) noexcept {
auto v = static_cast<uint64_t>(number);

char s[17];
int i = 16;

const auto &lhex_digits{StringLibConstants::get().lhex_digits};
do {
s[--i] = lhex_digits[v & 15];
v >>= 4;
} while (v > 0);

return {s + i, static_cast<string::size_type>(16 - i)};
}

int64_t f$hexdec(const string &number) noexcept {
uint64_t v = 0;
bool bad_str_param = number.empty();
bool overflow = false;
for (string::size_type i = 0; i < number.size(); i++) {
const uint8_t d = hex_to_int(number[i]);
if (unlikely(d == 16)) {
bad_str_param = true;
} else {
v = math_functions_impl_::mult_and_add<16>(v, d, overflow);
}
}

if (unlikely(bad_str_param)) {
php_warning("Wrong parameter \"%s\" in function hexdec", number.c_str());
}
if (unlikely(overflow)) {
php_warning("Integer overflow on converting '%s' in function hexdec, "
"the result will be different from PHP",
number.c_str());
}
return v;
}
17 changes: 17 additions & 0 deletions runtime-common/stdlib/math/math-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
#pragma once

#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>

#include "runtime-common/core/runtime-core.h"

namespace math_functions_impl_ {

template<uint8_t M>
uint64_t mult_and_add(uint64_t x, uint8_t y, bool &overflow) noexcept {
const uint64_t r = x * M + y;
overflow = overflow || r < x || r > static_cast<uint64_t>(std::numeric_limits<int64_t>::max());
return r;
}

} // namespace math_functions_impl_

inline double f$ceil(double v) noexcept;

inline double f$cos(double v) noexcept;
Expand Down Expand Up @@ -177,3 +190,7 @@ inline double f$sqrt(double v) noexcept {
}
return sqrt(v);
}

int64_t f$hexdec(const string &number) noexcept;

string f$dechex(int64_t number) noexcept;
5 changes: 3 additions & 2 deletions runtime-common/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
prepend(STDLIB_ARRAY stdlib/array/ array-functions.cpp)
prepend(STDLIB_MATH stdlib/math/ math-functions.cpp)
prepend(STDLIB_SERIALIZATION stdlib/serialization/ json-functions.cpp
json-writer.cpp serialize-functions.cpp)
prepend(STDLIB_STRING stdlib/string/ mbstring-functions.cpp
string-functions.cpp)
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp)

set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_SERIALIZATION} ${STDLIB_STRING}
${STDLIB_SERVER})
set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_MATH} ${STDLIB_SERIALIZATION}
${STDLIB_STRING} ${STDLIB_SERVER})
47 changes: 1 addition & 46 deletions runtime/math_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,11 @@
#include "common/cycleclock.h"
#include "runtime-common/stdlib/string/string-context.h"
#include "runtime-common/stdlib/math/random-functions.h"
#include "runtime-common/stdlib/string/string-functions.h"
#include "runtime/allocator.h"
#include "runtime/critical_section.h"
#include "server/php-engine-vars.h"

namespace {
template<uint8_t M>
uint64_t mult_and_add(uint64_t x, uint8_t y, bool &overflow) noexcept {
const uint64_t r = x * M + y;
overflow = overflow || r < x || r > static_cast<uint64_t>(std::numeric_limits<int64_t>::max());
return r;
}

int64_t secure_rand_buf(char * const buf, int64_t length) noexcept {
#if defined(__APPLE__)
arc4random_buf(static_cast<void*>(buf), static_cast<size_t>(length));
Expand All @@ -49,7 +41,7 @@ int64_t f$bindec(const string &number) noexcept {
for (string::size_type i = 0; i < number.size(); i++) {
const char c = number[i];
if (likely(vk::any_of_equal(c, '0', '1'))) {
v = mult_and_add<2>(v, static_cast<uint8_t>(c - '0'), overflow);
v = math_functions_impl_::mult_and_add<2>(v, static_cast<uint8_t>(c - '0'), overflow);
} else {
bad_str_param = true;
}
Expand Down Expand Up @@ -79,43 +71,6 @@ string f$decbin(int64_t number) noexcept {
return {s + i, static_cast<string::size_type>(65 - i)};
}

string f$dechex(int64_t number) noexcept {
auto v = static_cast<uint64_t>(number);

char s[17];
int i = 16;

do {
s[--i] = StringLibConstants::get().lhex_digits[v & 15];
v >>= 4;
} while (v > 0);

return {s + i, static_cast<string::size_type>(16 - i)};
}

int64_t f$hexdec(const string &number) noexcept {
uint64_t v = 0;
bool bad_str_param = number.empty();
bool overflow = false;
for (string::size_type i = 0; i < number.size(); i++) {
const uint8_t d = hex_to_int(number[i]);
if (unlikely(d == 16)) {
bad_str_param = true;
} else {
v = mult_and_add<16>(v, d, overflow);
}
}

if (unlikely(bad_str_param)) {
php_warning("Wrong parameter \"%s\" in function hexdec", number.c_str());
}
if (unlikely(overflow)) {
php_warning("Integer overflow on converting '%s' in function hexdec, "
"the result will be different from PHP", number.c_str());
}
return v;
}

double f$lcg_value() {
dl::enter_critical_section();//OK

Expand Down
5 changes: 0 additions & 5 deletions runtime/math_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ int64_t f$bindec(const string &number) noexcept;

string f$decbin(int64_t number) noexcept;

string f$dechex(int64_t number) noexcept;

int64_t f$hexdec(const string &number) noexcept;

double f$lcg_value();


void f$mt_srand(int64_t seed = std::numeric_limits<int64_t>::min()) noexcept;

int64_t f$mt_rand(int64_t l, int64_t r) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/435_base64_encode.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php

function filesBinFromBase64($str) { // for storage-engine: secret packing
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/455_inc_xor_str.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php
function inc_xor_str($a, $b) {
$len = strlen($a);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/488_ipv6.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php

function md5LastBits($x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/489_hexdec.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok k2_skip
@ok
<?php
var_dump(hexdec("See"));
var_dump(hexdec("ee"));
Expand Down

0 comments on commit 2398687

Please sign in to comment.