Explorar o código

Added constants to DecimalCtx.
Added codeblocks win32 build for lipmdecimal

mingodad %!s(int64=13) %!d(string=hai) anos
pai
achega
e167955635
Modificáronse 3 ficheiros con 80 adicións e 8 borrados
  1. 17 2
      ext/mpdecimal/mpdecimal.cbp
  2. 61 6
      ext/sq_decimal.cpp
  3. 2 0
      samples/test-decimal.nut

+ 17 - 2
ext/mpdecimal/mpdecimal.cbp

@@ -21,13 +21,28 @@
 			<Target title="Release">
 				<Option output="libmpdecimal" prefix_auto="1" extension_auto="1" />
 				<Option working_dir="" />
-				<Option object_output="/home/mingo/dev/mpdecimal-2.3//.objs" />
+				<Option object_output="/home/mingo/dev/mpdecimal-2.3/.objs" />
 				<Option type="2" />
 				<Option compiler="gcc" />
 				<Option createDefFile="1" />
 				<Compiler>
+					<Add option="-O3" />
+					<Add option="-Wall" />
+				</Compiler>
+				<Linker>
+					<Add option="-s" />
+				</Linker>
+			</Target>
+			<Target title="Release win32">
+				<Option output="libmpdecimal-win32" prefix_auto="1" extension_auto="1" />
+				<Option working_dir="" />
+				<Option object_output="/home/mingo/dev/mpdecimal-2.3/.objs-win32" />
+				<Option type="2" />
+				<Option compiler="mingw32_compiler" />
+				<Option createDefFile="1" />
+				<Compiler>
+					<Add option="-O3" />
 					<Add option="-Wall" />
-					<Add option="-O2" />
 				</Compiler>
 				<Linker>
 					<Add option="-s" />

+ 61 - 6
ext/sq_decimal.cpp

@@ -3,8 +3,10 @@
 #include <string.h>
 SQ_OPT_STRING_STRLEN();
 
+
 static const SQChar sq_decimal_ctx_TAG[] = _SC("DecimalCtx");
 static const SQChar sq_decimal_TAG[] = _SC("Decimal");
+static const SQChar sq_context_static[] = _SC("context");
 
 #define GET_DecimalCtx_INSTANCE(v, idx) SQ_GET_INSTANCE_VAR(v, idx, mpd_context_t, ctx, sq_decimal_ctx_TAG)
 #define GET_Decimal_INSTANCE(v, idx) SQ_GET_INSTANCE_VAR(v, idx, mpd_t, dec, sq_decimal_TAG)
@@ -162,7 +164,7 @@ static SQRegFunction DecimalCtx_methods[] =
 
 static mpd_context_t * sq_get_global_ctx(HSQUIRRELVM v, SQInteger idx)
 {
-    sq_pushliteral(v, _SC("gctx"));
+    sq_pushstring(v, sq_context_static, SIZEOF_SQCHAR_STRING(sq_context_static));
     sq_get(v, idx);
     mpd_context_t *ctx = 0;
     sq_getinstanceup(v, -1, (void**)&ctx, (void*)sq_decimal_ctx_TAG);
@@ -223,7 +225,7 @@ static SQRESULT sq_Decimal_constructor (HSQUIRRELVM v) {
 }
 
 static SQRESULT sq_Decimal_error(HSQUIRRELVM v, uint32_t status) {
-    SQChar *error = _SC("MPD_??");
+    const SQChar *error = _SC("MPD_??");
 #define CASE_ERROR(n) case n: error = #n; break;
     switch(status){
         CASE_ERROR(MPD_Clamped);
@@ -249,7 +251,10 @@ static SQRESULT sq_Decimal_error(HSQUIRRELVM v, uint32_t status) {
 static SQRESULT sq_Decimal_new_for_dec (HSQUIRRELVM v, mpd_t *dec, mpd_context_t *ctx, uint32_t status) {
     //mpd_addstatus_raise(ctx, status);
 	ctx->status |= status;
-	if (status&ctx->traps) return sq_Decimal_error(v, status);
+	if (status&ctx->traps) {
+	    mpd_del(dec);
+	    return sq_Decimal_error(v, status);
+	}
 
     sq_pushstring(v, sq_decimal_TAG, -1);
     sq_getonroottable(v);
@@ -442,6 +447,51 @@ static SQRegFunction Decimal_methods[] =
 };
 #undef _DECL_FUNC
 
+#define CTXC(s)  { _SC(#s), MPD_ ## s },
+
+static const struct {
+    const SQChar* name;
+    int value;
+} ctx_constants[] = {
+    /*Precision and Exponents*/
+    CTXC(MAX_PREC)
+    CTXC(MAX_EMAX)
+    CTXC(MIN_EMIN)
+    /* rounding */
+    CTXC(ROUND_UP)                  /* round away from 0 */
+    CTXC(ROUND_DOWN)                /* round toward 0 (truncate) */
+    CTXC(ROUND_CEILING)             /* round toward +infinity */
+    CTXC(ROUND_FLOOR)               /* round toward -infinity */
+    CTXC(ROUND_HALF_UP)             /* 0.5 is rounded up */
+    CTXC(ROUND_HALF_DOWN)           /* 0.5 is rounded down */
+    CTXC(ROUND_HALF_EVEN)           /* 0.5 is rounded to even */
+    CTXC(ROUND_05UP)                /* round zero or five away from 0  */
+    CTXC(ROUND_TRUNC)               /* truncate, but set infinities  */
+    /* Trap-enabler and Status flags */
+    CTXC(Conversion_syntax)
+    CTXC(Division_impossible)
+    CTXC(Division_undefined)
+    CTXC(Invalid_context)
+    CTXC(Invalid_operation)
+    CTXC(Malloc_error)
+    CTXC(Clamped)
+    CTXC(Division_by_zero)
+    CTXC(Fpu_error)
+    CTXC(Inexact)
+    CTXC(Not_implemented)
+    CTXC(Overflow)
+    CTXC(Rounded)
+    CTXC(Subnormal)
+    CTXC(Underflow)
+    CTXC(IEEE_Invalid_operation)
+    /*IEEE Interchange Formats*/
+    CTXC(IEEE_CONTEXT_MAX_BITS)
+    CTXC(DECIMAL32)
+    CTXC(DECIMAL64)
+    CTXC(DECIMAL128)
+    /* terminator */
+    { NULL, 0 }
+};
 
 #ifdef __cplusplus
 extern "C" {
@@ -449,17 +499,22 @@ extern "C" {
 
 SQRESULT sqext_register_decimal(HSQUIRRELVM v)
 {
-    sq_pushstring(v,sq_decimal_ctx_TAG,-1);
+    sq_pushstring(v,sq_decimal_ctx_TAG,SIZEOF_SQCHAR_STRING(sq_decimal_ctx_TAG));
 	sq_newclass(v,SQFalse);
 	sq_settypetag(v,-1,(void*)sq_decimal_ctx_TAG);
     sq_insert_reg_funcs(v, DecimalCtx_methods);
+    for(int i=0; ctx_constants[i].name; ++i){
+        sq_pushstring(v, ctx_constants[i].name, -1);
+        sq_pushinteger(v, ctx_constants[i].value);
+        sq_newslot(v,-3,SQTrue);
+    }
     sq_newslot(v,-3,SQTrue);
 
-    sq_pushstring(v,sq_decimal_TAG,-1);
+    sq_pushstring(v,sq_decimal_TAG,SIZEOF_SQCHAR_STRING(sq_decimal_TAG));
 	sq_newclass(v,SQFalse);
 	sq_settypetag(v,-1,(void*)sq_decimal_TAG);
 
-	sq_pushliteral(v, _SC("gctx"));
+    sq_pushstring(v, sq_context_static, SIZEOF_SQCHAR_STRING(sq_context_static));
 	sq_pushstring(v,sq_decimal_ctx_TAG,-1);
 	sq_getonroottable(v);
 	sq_pushroottable(v);

+ 2 - 0
samples/test-decimal.nut

@@ -38,6 +38,8 @@ catch(e){
 	print(e);
 }
 print(result);
+print(Decimal.context);
+print(result.context.prec());
 
 dec1 = Decimal("10.0");
 dec2 = Decimal("3.33");