将C ++函数转换为涉及日志函数的PHP谜语

时间:2015-11-16 23:49:00

标签: php c++

这是涉及日志功能的C ++到PHP之谜。

我需要将以下功能翻译成PHP ...这是一个很长的故事,但我们确信我们有很多想法...但我可以使用另一双眼睛。

我有一个可能的PHP实现......

我们有C ++方面的数据示例,这些数据产生了我们无法在PHP端重现的校验和......令人沮丧。

=============================================== =============

#define SYNC_CHECKSUM_MULT (0x01000193)

/**
 * Calculate a reasonable checksum for floating point numbers.
 */
static void oi_AccumulateFloat64Checksum(oiInt& checksum, oiFloat64 f)
{
    // j = round(f * 2^k), where j is a 12-bit number

    if ( oi_IsNaN(f) || oi_IsInf(f) )
    {
        checksum ^= 1;
        checksum *= SYNC_CHECKSUM_MULT;
        return;
    }

    oiInt exponent = (oiInt)logb(f);

    oiInt k = 12 - exponent;

    oiFloat64 jUnrounded = scalb(f, k);

    oiFloat64 jRounded = floor(jUnrounded + 0.5);

    oiInt kChange = (oiInt)logb(jRounded) - 12;

    k += kChange;

    oiFloat64 jFloat = scalb(jRounded, kChange);

    oiInt j = (oiInt)jFloat;

    checksum ^= j;
    checksum *= SYNC_CHECKSUM_MULT;

    checksum ^= k;
    checksum *= SYNC_CHECKSUM_MULT;
}

============= PHP实施=========================

这是在我们调用这个函数之前执行的。$ intChecksum = 0;

public function AccumulateFloatChecksum(&$intChecksum, $floatValue) {
    // j = round(f * 2^k), where j is a 12-bit number

    $TRIVIAL_CHECKSUM_MULT = 0x01000193;

    if ( !is_numeric($floatValue) || $floatValue == 0 || is_nan($floatValue) || is_infinite($floatValue) )
    {
        $intChecksum ^= 1;
        $intChecksum *= $TRIVIAL_CHECKSUM_MULT;
        $intChecksum &= 0xFFFFFFFF;
        return;
    }

    //var_dump('$floatValue: '.$floatValue);

    // oiInt exponent = (oiInt)logb(f);
    $exponent = intval(log($floatValue, 2));

    //var_dump('$exponent: '.$exponent);

    // oiInt k = 12 - exponent;
    $k = 12 - $exponent;

    //var_dump('$k 1: '.$k);

    // oiFloat64 jUnrounded = scalb(f, k);
    $jUnrounded = $floatValue * pow(2, $k);

    //var_dump('$jUnrounded: '.$jUnrounded);

    // oiFloat64 jRounded = floor(jUnrounded + 0.5);
    $jRounded = floor($jUnrounded + 0.5);

    //var_dump('$jRounded: '.$jRounded);

    // oiInt kChange = (oiInt)logb(jRounded) - 12;
    $kChange = intval(log($jRounded, 2)) - 12;

    //var_dump('$kChange: '.$kChange);

    // k += kChange;
    $k += $kChange;


    //var_dump('$k 2: '.$k);

    // oiFloat64 jFloat = scalb(jRounded, kChange);
    $jFloat = $jRounded * pow(2, $kChange);

    //var_dump('$jFloat: '.$jFloat);

    $j = intval($jFloat);

    //var_dump('$j: '.$j);
    //var_dump('===============================');

    $intChecksum ^= $j;
    $intChecksum *= $TRIVIAL_CHECKSUM_MULT;
    $intChecksum &= 0xFFFFFFFF;

    $intChecksum ^= $k;
    $intChecksum *= $TRIVIAL_CHECKSUM_MULT;
    $intChecksum &= 0xFFFFFFFF;
}

0 个答案:

没有答案