Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mulsw macro change issue #13

Open
jonathanbennett73 opened this issue Jun 21, 2020 · 10 comments
Open

mulsw macro change issue #13

jonathanbennett73 opened this issue Jun 21, 2020 · 10 comments

Comments

@jonathanbennett73
Copy link

jonathanbennett73 commented Jun 21, 2020

Hi,

I'm helping out Virgill with his Amigaklang exe generator which uses your cool gcc environment.

I updated his project from gcc8 to the gcc10 version and it stopped working (corrupt sounds). I have traced it to a commit on the 18th may where the mulsw and other macros were improved. If I leave the gcc10 in place but use the old mulsw macros it works OK again.

I had a look a the macros but the gcc/asm syntax is new to me so I couldn't spot any errors.

Virgill calls mulsw in two places as follows, maybe the error will be obvious to you:

short osc_sine (BYTE instance, short freq, UBYTE gain){ int temp; short res;static short counter[16]; short buf = counter[instance]+=freq; buf -= 16384; temp= mulsw(buf, 32767-abs(buf)); res =temp>>16; res<<=3; return vol(res,gain); }

inline short vol(short val, UBYTE gain){ return mulsw(val, gain) >> 7; }

You can message me here or on EAB if you need me to check/change something.

Cheers
Antiriad

@BartmanAbyss
Copy link
Owner

I'm not sure what the error is here either. However, it's totally fine if you just keep using the old implementation.

@bebbo
Copy link

bebbo commented Aug 21, 2020

the new code is buggy:

inline int mulsw(short a, short b) {
    asm("mulsw %1,%0":"+d"(a): "mid"(b): "cc");
    return a;
}

consider

int foo(short a) {
  return a;
}

the compiler has to sign extend the short to int which is done with ext.l d0. Same applies to the mulsw version above.

The old version had an internal int variable int a; to get the result and thus no sign extension is necessary.

bebbo added a commit to bebbo/vscode-amiga-debug that referenced this issue Aug 21, 2020
bebbo added a commit to bebbo/vscode-amiga-debug that referenced this issue Aug 22, 2020
@jonathanbennett73
Copy link
Author

Bebbo, that update still breaks AmigaKlang - the original macro (before Bartman's recent change) works ok though.

Antiriad.

@rjobling
Copy link

rjobling commented Oct 23, 2021

I've decided for now to settle on using two different versions of each so you can avoid the ext.l when you know it isn't necessary.

inline unsigned int muluw32(unsigned short a, unsigned short b) {
	unsigned int r = a;
	asm("muluw %1,%0":"+d"(r): "mid"(b): "cc");
	return r;
}
inline int mulsw32(short a, short b) {
	int r = a;
	asm("mulsw %1,%0":"+d"(r): "mid"(b): "cc");
	return r;
}
inline unsigned short muluw16(unsigned short a, unsigned short b) {
	asm("muluw %1,%0":"+d"(a): "mid"(b): "cc");
	return a;
}
inline short mulsw16(short a, short b) {
	asm("mulsw %1,%0":"+d"(a): "mid"(b): "cc");
	return a;
}

@bebbo
Copy link

bebbo commented Oct 24, 2021

btw: with gcc there is no need for inline asm:

unsigned int muluw32(unsigned short a, unsigned short b) {
    return ((unsigned int)a) * b;
}

yields

_muluw32:
        move.w (6,sp),d0
        mulu.w (10,sp),d0
        rts

so just create inline versions with C code and you are fine.

@rjobling
Copy link

I just tried that and at least in my case it doesn't work.
It'll try to generate a 32bit multiply requiring __mulsi3.

@bebbo
Copy link

bebbo commented Oct 25, 2021

maybe you could provide some minimal code to reproduce that issue? Maybe even here? https://franke.ms/cex/z/7aMKh4

@rjobling
Copy link

It's happening with this simple example:

http://franke.ms/cex/z/bTj19T

What's confusing is that it goes away if you remove -m68000.

@BartmanAbyss
Copy link
Owner

Yes, I wrote in the README it 'sometimes' uses __mulsi3, because it really depends on the inlining and LTO stuff. We couldn't find clear patterns..

@bebbo
Copy link

bebbo commented Oct 26, 2021

hm... https://franke.ms/cex/z/5xeGnc
guess you stick to your asm implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants