"unsigned >" comparison of 32bit integers failing ??

Bug reports (if you don't/won't have a Github account)
Post Reply
7alken
New member
Posts: 5
Joined: Sat Jun 17, 2023 2:26 am

"unsigned >" comparison of 32bit integers failing ??

Post by 7alken »

Hi, please, say I have this code snippet from my experimental VMEX engine interpreting modified RISC-V
(sw hex-menemo optimized opcodes, 16bit immediates, generally RV32EM asm-src compatible, but different encoding)

and I have tested on MANY compilers, 32/16/8 bit including cc65 this engine, as it has tests battery included. Everywhere is anything related to code below without modification okay, but for BOTH compilers from z88dk for z80/cpm/zxs I experienced issue with simple "unsigned >" comparison of dereferenced pointers - as I debugged test fail, I found that "unsigned >" doest work well here below, so I reversed the operation to use "<" operator

( REGU_TYPE is finally uint32_t and types or _rs1_ _rs2_ are int32_t* )

BLTU - branch less than unsigned - okay
BGTU - branch greater than unsigned (z88dk code FAILs in case natural ">" is used)

Code: Select all

            case OPSBA_BLTU:
                if ((REGU_TYPE) *_rs1_ < (REGU_TYPE) *_rs2_) { core.pc += imm << LSHIFT; }
                break;

            case OPSBB_BLEU: //pseudo
                if ((REGU_TYPE) *_rs1_ <= (REGU_TYPE) *_rs2_) { core.pc += imm << LSHIFT; }
                break;

            case OPSBC_BGEU:
                if ((REGU_TYPE) *_rs1_ >= (REGU_TYPE) *_rs2_) { core.pc += imm << LSHIFT; }
                break;

            case OPSBD_BGTU: //pseudo
                //HACK for Z88DK Z80 compiler - "unsigned >" FAILS !!!
                //HACK - we use reversed registers and "<" here !!!
                if ((REGU_TYPE) *_rs2_ < (REGU_TYPE) *_rs1_) { core.pc += imm << LSHIFT; }
                break;
As I told anywhere else is usage of ">" operator okay, but through z88dk I had this issue - may it be really bug or not ???

Petr
7alken
New member
Posts: 5
Joined: Sat Jun 17, 2023 2:26 am

Re: "unsigned >" comparison of 32bit integers failing ??

Post by 7alken »

entire project is here:
https://github.com/apws/230326-VMEX

and I compiled it for z80/cpm on windows this way:

Code: Select all

set PATH=%PATH%;c:\z88dk\bin\
ZCCCFG=c:\z88dk\lib\config
set SRC=..\..\src

zcc +cpm %SRC%\VMEX.c -I%SRC% -o VMEX.bin -create-app
7alken
New member
Posts: 5
Joined: Sat Jun 17, 2023 2:26 am

Re: "unsigned >" comparison of 32bit integers failing ??

Post by 7alken »

I am trying to isolate this to minimal example for github bugreport.
7alken
New member
Posts: 5
Joined: Sat Jun 17, 2023 2:26 am

Re: "unsigned >" comparison of 32bit integers failing ??

Post by 7alken »

Using minimal example, I can confirm that Z88DK, ONLY sccz80 compiler has bug in some optimizations around "unsigned >" comparison in such cases:
(code shows how expression assigned to result is okay, but expression evaluated inside if () is wrong)

Code: Select all


 #include <stdio.h>
 
 int main()
 {
     signed short i = 0;
     
     signed long a = 0;
     signed long b = 0;
     
     i=0x1000; a = (signed long) i;
     i=0x9000; b = (signed long) i;
             
     printf("a=%ld, b=%ld \n\n", a, b);
     
     signed long *_a_ = &a;
     signed long *_b_ = &b;
     
     
     printf("Z88DK BADLY optimized(?) if condition??: \n");
     if ((unsigned long) *_a_ >  (unsigned long) *_b_) 
       printf("%ld is 'unsigned >' %ld \n", a, b);    
     else
        printf("%ld is 'unsigned <=' %ld \n", a, b);
         
     printf("OKAY result expression: \n");
     int result = ((unsigned long) *_a_ >  (unsigned long) *_b_);
     if (result) 
       printf("%ld is 'unsigned >' %ld \n", a, b);    
     else
        printf("%ld is 'unsigned <=' %ld \n", a, b);
         
 }
teste compilation this way for CP/M (testscc.com) and ZX Spectrum, both compilers (testscc.tap, testsdcc.tap):

Code: Select all

zcc +cpm main.c -o testscc.bin -create-app
zcc +zx -v -startup=0 -clib=sdcc_iy -zorg=24800 main.c -o testsdcc.bin -create-app
zcc +zx -v -clib=new -O3 -startup=1 main.c -I%SRC% -o testscc.bin -create-app
7alken
New member
Posts: 5
Joined: Sat Jun 17, 2023 2:26 am

Re: "unsigned >" comparison of 32bit integers failing ??

Post by 7alken »

reported as github issue
User avatar
dom
Well known member
Posts: 2319
Joined: Sun Jul 15, 2007 10:01 pm

Re: "unsigned >" comparison of 32bit integers failing ??

Post by dom »

Thanks for the report - an interesting case not covered by the test cases and only on the z80 implementation (so 8080, 8085 and gbz80 were correct).

This should be fixed in the next nightly, so 20230618 onwards

It's an interesting project! It's always seem to be CPU emulators that show up problems. There's a m68k emulator in https://gitlab.com/strandgames/brahman/ ... t_magnetic that caused a bit of trouble a few years ago.
Post Reply