File: php2.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (51 lines) | stat: -rw-r--r-- 1,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdint.h>

typedef long zend_long;
typedef double zend_double;

/* Mock of PHP zval struct */
typedef struct {
    zend_long lval;
    zend_double dval;
} zval;

/* Mock access macros */
#define Z_LVAL_P(z) ((z)->lval)
#define Z_DVAL_P(z) ((z)->dval)

int main(void) {
    zval _op1 = {123456789, 0.0};
    zval _op2 = {987654321, 0.0};
    zval _result = {0, 0.0};
    int overflow = 0;

    /* Pointers to simulate real PHP usage */
    zval *op1 = &_op1;
    zval *op2 = &_op2;
    zval *result = &_result;

    /* x86-64 version of ZEND_SIGNED_MULTIPLY_LONG */
#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \
    zend_long __tmpvar;                                           \
    __asm__ ("imul %3,%0\n"                                       \
             "adc $0,%1"                                          \
             : "=r"(__tmpvar), "=r"(usedval)                     \
             : "0"(a), "r"(b), "1"(0));                          \
    if (usedval) (dval) = (double) (a) * (double) (b);           \
    else (lval) = __tmpvar;                                       \
} while (0)

    /* Perform the multiplication as PHP does */
    ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1), Z_LVAL_P(op2),
                              Z_LVAL_P(result), Z_DVAL_P(result),
                              overflow);

    /* Print result */
    if (overflow)
        printf("Overflow detected, double result: %f\n", Z_DVAL_P(result));
    else
        printf("Result fits in long: %ld\n", Z_LVAL_P(result));

    return 0;
}