--- gforth/prim 2006/04/09 08:24:47 1.193 +++ gforth/prim 2006/10/22 16:54:00 1.198 @@ -809,12 +809,21 @@ n = n1*n2; / ( n1 n2 -- n ) core slash n = n1/n2; -if(FLOORED_DIV && ((n1^n2) < 0) && (n1%n2 != 0)) n--; +if (CHECK_DIVISION && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); +if (FLOORED_DIV && ((n1^n2) < 0) && (n1%n2 != 0)) + n--; : /mod nip ; mod ( n1 n2 -- n ) core n = n1%n2; +if (CHECK_DIVISION && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); if(FLOORED_DIV && ((n1^n2) < 0) && n!=0) n += n2; : /mod drop ; @@ -822,6 +831,10 @@ if(FLOORED_DIV && ((n1^n2) < 0) && n!=0) /mod ( n1 n2 -- n3 n4 ) core slash_mod n4 = n1/n2; n3 = n1%n2; /* !! is this correct? look into C standard! */ +if (CHECK_DIVISION && n2 == 0) + throw(BALL_DIVZERO); +if (CHECK_DIVISION && n2 == -1 && n1 == CELL_MIN) + throw(BALL_RESULTRANGE); if (FLOORED_DIV && ((n1^n2) < 0) && n3!=0) { n4--; n3+=n2; @@ -831,6 +844,7 @@ if (FLOORED_DIV && ((n1^n2) < 0) && n3!= */mod ( n1 n2 n3 -- n4 n5 ) core star_slash_mod ""n1*n2=n3*n5+n4, with the intermediate result (n1*n2) being double."" +DCell d5; #ifdef BUGGY_LL_MUL DCell d = mmul(n1,n2); #else @@ -842,18 +856,24 @@ n4=DHI(r); n5=DLO(r); #else /* assumes that the processor uses either floored or symmetric division */ -n5 = d/n3; +d5 = d/n3; n4 = d%n3; +if (CHECK_DIVISION && n3 == 0) + throw(BALL_DIVZERO); if (FLOORED_DIV && ((DHI(d)^n3)<0) && n4!=0) { - n5--; + d5--; n4+=n3; } +n5 = d5; +if (d5 != n5) + throw(BALL_RESULTRANGE); #endif : >r m* r> fm/mod ; */ ( n1 n2 n3 -- n4 ) core star_slash ""n4=(n1*n2)/n3, with the intermediate result being double."" +DCell d4; #ifdef BUGGY_LL_MUL DCell d = mmul(n1,n2); #else @@ -864,8 +884,14 @@ DCell r = fmdiv(d,n3); n4=DLO(r); #else /* assumes that the processor uses either floored or symmetric division */ -n4 = d/n3; -if (FLOORED_DIV && ((DHI(d)^n3)<0) && (d%n3)!=0) n4--; +d4 = d/n3; +if (CHECK_DIVISION && n3 == 0) + throw(BALL_DIVZERO); +if (FLOORED_DIV && ((DHI(d)^n3)<0) && (d%n3)!=0) + d4--; +n4 = d4; +if (d4 != n4) + throw(BALL_RESULTRANGE); #endif : */mod nip ; @@ -893,6 +919,8 @@ fm/mod ( d1 n1 -- n2 n3 ) core f_m_sla #ifdef ASM_SM_SLASH_REM ASM_SM_SLASH_REM(d1.lo, d1.hi, n1, n2, n3); if (((DHI(d1)^n1)<0) && n2!=0) { + if (n3 == CELL_MIN) + throw(BALL_RESULTRANGE); n3--; n2+=n1; } @@ -905,18 +933,25 @@ n3=DLO(r); #ifdef ASM_SM_SLASH_REM4 ASM_SM_SLASH_REM4(d1, n1, n2, n3); if (((DHI(d1)^n1)<0) && n2!=0) { + if (n3 == CELL_MIN) + throw(BALL_RESULTRANGE); n3--; n2+=n1; } #else /* !defined(ASM_SM_SLASH_REM4) */ /* assumes that the processor uses either floored or symmetric division */ -n3 = d1/n1; +DCell d3 = d1/n1; n2 = d1%n1; +if (CHECK_DIVISION && n1 == 0) + throw(BALL_DIVZERO); /* note that this 1%-3>0 is optimized by the compiler */ if (1%-3>0 && ((DHI(d1)^n1)<0) && n2!=0) { - n3--; + d3--; n2+=n1; } +n3 = d3; +if (d3 != n3) + throw(BALL_RESULTRANGE); #endif /* !defined(ASM_SM_SLASH_REM4) */ #endif : @@ -940,13 +975,18 @@ n3=DLO(r); ASM_SM_SLASH_REM4(d1, n1, n2, n3); #else /* !defined(ASM_SM_SLASH_REM4) */ /* assumes that the processor uses either floored or symmetric division */ -n3 = d1/n1; +DCell d3 = d1/n1; n2 = d1%n1; +if (CHECK_DIVISION && n1 == 0) + throw(BALL_DIVZERO); /* note that this 1%-3<0 is optimized by the compiler */ if (1%-3<0 && ((DHI(d1)^n1)<0) && n2!=0) { - n3++; + d3++; n2-=n1; } +n3 = d3; +if (d3 != n3) + throw(BALL_RESULTRANGE); #endif /* !defined(ASM_SM_SLASH_REM4) */ #endif : @@ -996,8 +1036,13 @@ u3=DLO(r); #ifdef ASM_UM_SLASH_MOD4 ASM_UM_SLASH_MOD4(ud, u1, u2, u3); #else /* !defined(ASM_UM_SLASH_MOD4) */ -u3 = ud/u1; +UDCell ud3 = ud/u1; u2 = ud%u1; +if (CHECK_DIVISION && u1 == 0) + throw(BALL_DIVZERO); +u3 = ud3; +if (ud3 != u3) + throw(BALL_RESULTRANGE); #endif /* !defined(ASM_UM_SLASH_MOD4) */ #endif : @@ -2698,7 +2743,7 @@ w = ffi_prep_closure((ffi_closure *)a_cl ffi-2@ ( a_addr -- d ) gforth ffi_2fetch #ifdef BUGGY_LONG_LONG -DLO_IS(d, (Cell*)(*a_addr)); +DLO_IS(d, *(Cell*)(*a_addr)); DHI_IS(d, 0); #else d = *(DCell*)(a_addr); @@ -2714,14 +2759,25 @@ ffi-2! ( d a_addr -- ) gforth ffi_2store ffi-arg-int ( -- w ) gforth ffi_arg_int w = *(int *)(*gforth_clist++); +ffi-arg-long ( -- w ) gforth ffi_arg_long +w = *(long *)(*gforth_clist++); + ffi-arg-longlong ( -- d ) gforth ffi_arg_longlong #ifdef BUGGY_LONG_LONG -DLO_IS(d, (Cell*)(*gforth_clist++)); -DHI_IS(d, 0); +DLO_IS(d, *(Cell*)(*gforth_clist++)); +DHI_IS(d, -(*(Cell*)(*gforth_clist++)<0)); #else d = *(DCell*)(*gforth_clist++); #endif +ffi-arg-dlong ( -- d ) gforth ffi_arg_dlong +#ifdef BUGGY_LONG_LONG +DLO_IS(d, *(Cell*)(*gforth_clist++)); +DHI_IS(d, -(*(Cell*)(*gforth_clist++)<0)); +#else +d = *(Cell*)(*gforth_clist++); +#endif + ffi-arg-ptr ( -- c_addr ) gforth ffi_arg_ptr c_addr = *(Char **)(*gforth_clist++); @@ -2746,6 +2802,18 @@ ffi-ret-longlong ( d -- ) gforth ffi_ret #endif return 0; +ffi-ret-dlong ( d -- ) gforth ffi_ret_dlong +#ifdef BUGGY_LONG_LONG +*(Cell*)(gforth_ritem) = DLO(d); +#else +*(Cell*)(gforth_ritem) = d; +#endif +return 0; + +ffi-ret-long ( n -- ) gforth ffi_ret_long +*(Cell*)(gforth_ritem) = n; +return 0; + ffi-ret-ptr ( c_addr -- ) gforth ffi_ret_ptr *(Char **)(gforth_ritem) = c_addr; return 0;