Browse Source

Store float values in an union.

Single variable cannot have many values of different types. This also saves some memory, because union allocates memory just for one of its members.
release/v0.1
Rafal Kupiec 3 years ago
parent
commit
eb79ed500e
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
5 changed files with 41 additions and 41 deletions
  1. +2
    -2
      engine/api.c
  2. +4
    -4
      engine/hashmap.c
  3. +13
    -13
      engine/memobj.c
  4. +21
    -21
      engine/vm.c
  5. +1
    -1
      include/ph7int.h

+ 2
- 2
engine/api.c View File

@ -1217,7 +1217,7 @@ double ph7_value_to_double(ph7_value *pValue) {
if(rc != PH7_OK) {
return (double)0;
}
return (double)pValue->rVal;
return (double)pValue->x.rVal;
}
/*
* [CAPIREF: ph7_value_to_string()]
@ -1792,7 +1792,7 @@ int ph7_value_null(ph7_value *pVal) {
int ph7_value_double(ph7_value *pVal, double Value) {
/* Invalidate any prior representation */
PH7_MemObjRelease(pVal);
pVal->rVal = (ph7_real)Value;
pVal->x.rVal = (ph7_real)Value;
MemObjSetType(pVal, MEMOBJ_REAL);
return PH7_OK;
}


+ 4
- 4
engine/hashmap.c View File

@ -4668,7 +4668,7 @@ static void DoubleSum(ph7_context *pCtx, ph7_hashmap *pMap) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
dSum += pObj->rVal;
dSum += pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
dSum += (double)pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
@ -4695,7 +4695,7 @@ static void Int64Sum(ph7_context *pCtx, ph7_hashmap *pMap) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
nSum += (sxi64)pObj->rVal;
nSum += (sxi64)pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
nSum += pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
@ -4769,7 +4769,7 @@ static void DoubleProd(ph7_context *pCtx, ph7_hashmap *pMap) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
dProd *= pObj->rVal;
dProd *= pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
dProd *= (double)pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
@ -4797,7 +4797,7 @@ static void Int64Prod(ph7_context *pCtx, ph7_hashmap *pMap) {
pObj = HashmapExtractNodeValue(pEntry);
if(pObj && (pObj->iFlags & (MEMOBJ_NULL | MEMOBJ_HASHMAP | MEMOBJ_OBJ | MEMOBJ_RES)) == 0) {
if(pObj->iFlags & MEMOBJ_REAL) {
nProd *= (sxi64)pObj->rVal;
nProd *= (sxi64)pObj->x.rVal;
} else if(pObj->iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
nProd *= pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {


+ 13
- 13
engine/memobj.c View File

@ -39,7 +39,7 @@ static sxi64 MemObjRealToInt(ph7_value *pObj) {
*/
static const sxi64 maxInt = LARGEST_INT64;
static const sxi64 minInt = SMALLEST_INT64;
ph7_real r = pObj->rVal;
ph7_real r = pObj->x.rVal;
if(r < (ph7_real)minInt) {
return minInt;
} else if(r > (ph7_real)maxInt) {
@ -178,7 +178,7 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & MEMOBJ_REAL) {
return pObj->rVal;
return pObj->x.rVal;
} else if(iFlags & (MEMOBJ_INT | MEMOBJ_BOOL)) {
return (ph7_real)pObj->x.iVal;
} else if(iFlags & MEMOBJ_STRING) {
@ -208,7 +208,7 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
"__toFloat", sizeof("__toFloat") - 1, &sResult);
if(rc == SXRET_OK && (sResult.iFlags & MEMOBJ_REAL)) {
/* Extract method return value */
rVal = sResult.rVal;
rVal = sResult.x.rVal;
}
PH7_ClassInstanceUnref((ph7_class_instance *)pObj->x.pOther);
PH7_MemObjRelease(&sResult);
@ -225,7 +225,7 @@ static ph7_real MemObjRealValue(ph7_value *pObj) {
*/
static sxi32 MemObjStringValue(SyBlob *pOut, ph7_value *pObj, sxu8 bStrictBool) {
if(pObj->iFlags & MEMOBJ_REAL) {
SyBlobFormat(&(*pOut), "%.15g", pObj->rVal);
SyBlobFormat(&(*pOut), "%.15g", pObj->x.rVal);
} else if(pObj->iFlags & MEMOBJ_INT) {
SyBlobFormat(&(*pOut), "%qd", pObj->x.iVal);
/* %qd (BSD quad) is equivalent to %lld in the libc printf */
@ -277,7 +277,7 @@ static sxi32 MemObjBooleanValue(ph7_value *pObj) {
sxi32 iFlags;
iFlags = pObj->iFlags;
if(iFlags & MEMOBJ_REAL) {
return pObj->rVal != 0.0 ? 1 : 0;
return pObj->x.rVal != 0.0 ? 1 : 0;
} else if(iFlags & MEMOBJ_INT) {
return pObj->x.iVal ? 1 : 0;
} else if(iFlags & MEMOBJ_STRING) {
@ -349,7 +349,7 @@ PH7_PRIVATE sxi32 PH7_MemObjToInteger(ph7_value *pObj) {
PH7_PRIVATE sxi32 PH7_MemObjToReal(ph7_value *pObj) {
if((pObj->iFlags & MEMOBJ_REAL) == 0) {
/* Preform the conversion */
pObj->rVal = MemObjRealValue(&(*pObj));
pObj->x.rVal = MemObjRealValue(&(*pObj));
/* Invalidate any prior representations */
SyBlobRelease(&pObj->sBlob);
MemObjSetType(pObj, MEMOBJ_REAL);
@ -554,7 +554,7 @@ PH7_PRIVATE sxi32 PH7_MemObjIsEmpty(ph7_value *pObj) {
} else if(pObj->iFlags & MEMOBJ_INT) {
return pObj->x.iVal == 0 ? TRUE : FALSE;
} else if(pObj->iFlags & MEMOBJ_REAL) {
return pObj->rVal == (ph7_real)0 ? TRUE : FALSE;
return pObj->x.rVal == (ph7_real)0 ? TRUE : FALSE;
} else if(pObj->iFlags & MEMOBJ_BOOL) {
return !pObj->x.iVal;
} else if(pObj->iFlags & MEMOBJ_STRING) {
@ -681,7 +681,7 @@ PH7_PRIVATE sxi32 PH7_MemObjInitFromReal(ph7_vm *pVm, ph7_value *pObj, ph7_real
pObj->pVm = pVm;
SyBlobInit(&pObj->sBlob, &pVm->sAllocator);
/* Set the desired type */
pObj->rVal = rVal;
pObj->x.rVal = rVal;
pObj->iFlags = MEMOBJ_REAL;
return SXRET_OK;
}
@ -987,11 +987,11 @@ Numeric:
if((pObj1->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj1);
}
r1 = pObj1->rVal;
r1 = pObj1->x.rVal;
if((pObj2->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj2);
}
r2 = pObj2->rVal;
r2 = pObj2->x.rVal;
if(r1 > r2) {
return 1;
} else if(r1 < r2) {
@ -1037,9 +1037,9 @@ PH7_PRIVATE sxi32 PH7_MemObjAdd(ph7_value *pObj1, ph7_value *pObj2, int bAddStor
if((pObj2->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pObj2);
}
a = pObj1->rVal;
b = pObj2->rVal;
pObj1->rVal = a + b;
a = pObj1->x.rVal;
b = pObj2->x.rVal;
pObj1->x.rVal = a + b;
MemObjSetType(pObj1, MEMOBJ_REAL);
} else {
/* Integer arithmetic */


+ 21
- 21
engine/vm.c View File

@ -2850,7 +2850,7 @@ static sxi32 VmByteCodeExec(
/* Force a numeric cast */
PH7_MemObjToNumeric(pObj);
if(pObj->iFlags & MEMOBJ_REAL) {
pObj->rVal++;
pObj->x.rVal++;
} else {
pObj->x.iVal++;
MemObjSetType(pTos, MEMOBJ_INT);
@ -2866,7 +2866,7 @@ static sxi32 VmByteCodeExec(
PH7_MemObjToNumeric(pTos);
/* Pre-increment */
if(pTos->iFlags & MEMOBJ_REAL) {
pTos->rVal++;
pTos->x.rVal++;
} else {
pTos->x.iVal++;
MemObjSetType(pTos, MEMOBJ_INT);
@ -2897,7 +2897,7 @@ static sxi32 VmByteCodeExec(
/* Force a numeric cast */
PH7_MemObjToNumeric(pObj);
if(pObj->iFlags & MEMOBJ_REAL) {
pObj->rVal--;
pObj->x.rVal--;
} else {
pObj->x.iVal--;
MemObjSetType(pTos, MEMOBJ_INT);
@ -2911,7 +2911,7 @@ static sxi32 VmByteCodeExec(
if(pInstr->iP1) {
/* Pre-decrement */
if(pTos->iFlags & MEMOBJ_REAL) {
pTos->rVal--;
pTos->x.rVal--;
} else {
pTos->x.iVal--;
MemObjSetType(pTos, MEMOBJ_INT);
@ -2934,7 +2934,7 @@ static sxi32 VmByteCodeExec(
/* Force a numeric (integer,real or both) cast */
PH7_MemObjToNumeric(pTos);
if(pTos->iFlags & MEMOBJ_REAL) {
pTos->rVal = -pTos->rVal;
pTos->x.rVal = -pTos->x.rVal;
}
if(pTos->iFlags & MEMOBJ_INT) {
pTos->x.iVal = -pTos->x.iVal;
@ -2954,7 +2954,7 @@ static sxi32 VmByteCodeExec(
/* Force a numeric (integer,real or both) cast */
PH7_MemObjToNumeric(pTos);
if(pTos->iFlags & MEMOBJ_REAL) {
pTos->rVal = +pTos->rVal;
pTos->x.rVal = +pTos->x.rVal;
}
if(pTos->iFlags & MEMOBJ_INT) {
pTos->x.iVal = +pTos->x.iVal;
@ -3023,11 +3023,11 @@ static sxi32 VmByteCodeExec(
if((pNos->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pNos);
}
a = pNos->rVal;
b = pTos->rVal;
a = pNos->x.rVal;
b = pTos->x.rVal;
r = a * b;
/* Push the result */
pNos->rVal = r;
pNos->x.rVal = r;
MemObjSetType(pNos, MEMOBJ_REAL);
} else {
/* Integer arithmetic */
@ -3153,11 +3153,11 @@ static sxi32 VmByteCodeExec(
if((pNos->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pNos);
}
a = pNos->rVal;
b = pTos->rVal;
a = pNos->x.rVal;
b = pTos->x.rVal;
r = a - b;
/* Push the result */
pNos->rVal = r;
pNos->x.rVal = r;
MemObjSetType(pNos, MEMOBJ_REAL);
} else {
/* Integer arithmetic */
@ -3195,11 +3195,11 @@ static sxi32 VmByteCodeExec(
if((pNos->iFlags & MEMOBJ_REAL) == 0) {
PH7_MemObjToReal(pNos);
}
a = pTos->rVal;
b = pNos->rVal;
a = pTos->x.rVal;
b = pNos->x.rVal;
r = a - b;
/* Push the result */
pNos->rVal = r;
pNos->x.rVal = r;
MemObjSetType(pNos, MEMOBJ_REAL);
} else {
/* Integer arithmetic */
@ -3329,8 +3329,8 @@ static sxi32 VmByteCodeExec(
PH7_MemObjToReal(pNos);
}
/* Perform the requested operation */
a = pNos->rVal;
b = pTos->rVal;
a = pNos->x.rVal;
b = pTos->x.rVal;
if(b == 0) {
/* Division by zero */
r = 0;
@ -3339,7 +3339,7 @@ static sxi32 VmByteCodeExec(
} else {
r = a / b;
/* Push the result */
pNos->rVal = r;
pNos->x.rVal = r;
MemObjSetType(pNos, MEMOBJ_REAL);
}
VmPopOperand(&pTos, 1);
@ -3370,8 +3370,8 @@ static sxi32 VmByteCodeExec(
PH7_MemObjToReal(pNos);
}
/* Perform the requested operation */
a = pTos->rVal;
b = pNos->rVal;
a = pTos->x.rVal;
b = pNos->x.rVal;
if(b == 0) {
/* Division by zero */
r = 0;
@ -3380,7 +3380,7 @@ static sxi32 VmByteCodeExec(
} else {
r = a / b;
/* Push the result */
pNos->rVal = r;
pNos->x.rVal = r;
MemObjSetType(pNos, MEMOBJ_REAL);
}
if(pTos->nIdx == SXU32_HIGH) {


+ 1
- 1
include/ph7int.h View File

@ -619,9 +619,9 @@ struct SHA1Context {
* of the same value.
*/
struct ph7_value {
ph7_real rVal; /* Real value */
union {
sxi64 iVal; /* Integer value */
ph7_real rVal; /* Real value */
void *pOther; /* Other values (Object, Array, Resource, Namespace, etc.) */
} x;
sxi32 iFlags; /* Control flags (see below) */


Loading…
Cancel
Save