turns out the imult routines (which I never used) were incorrect.
Noted by Matthew Dempsky open_* from open.h now open in large file modemaster
parent
46dcfbdce1
commit
19202d2320
@ -1,11 +1,8 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int imult16(int16 a,int16 b,int16* c) {
|
||||
int neg=(a<0);
|
||||
uint16 d;
|
||||
if (neg) a=-a;
|
||||
if (b<0) { neg^=1; b=-b; }
|
||||
if (umult16(a,b,&d)) return 0;
|
||||
*c=(neg?-d:d);
|
||||
int32 x=(int32)a*b;
|
||||
if ((int16)x != x) return 0;
|
||||
*c=x;
|
||||
return 1;
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int imult32(int32 a,int32 b,int32* c) {
|
||||
int neg=(a<0);
|
||||
uint32 d;
|
||||
if (neg) a=-a;
|
||||
if (b<0) { neg^=1; b=-b; }
|
||||
if (umult32(a,b,&d)) return 0;
|
||||
*c=(neg?-d:d);
|
||||
int64 x=(int64)a*b;
|
||||
if ((int32)x != x) return 0;
|
||||
*c=x;
|
||||
return 1;
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
#include <assert.h>
|
||||
#include "safemult.h"
|
||||
|
||||
int main() {
|
||||
int16 a;
|
||||
int32 b;
|
||||
int64 c;
|
||||
uint16 d;
|
||||
uint32 e;
|
||||
uint64 f;
|
||||
|
||||
assert(imult16(4,10000,&a)==0);
|
||||
assert(imult16(-4,10000,&a)==0);
|
||||
assert(imult16(5,10,&a)==1 && a==50);
|
||||
assert(imult16(-3,10000,&a)==1 && a==-30000);
|
||||
|
||||
assert(imult32(0x40000000,2,&b)==0);
|
||||
assert(imult32(-0x40000000,2,&b)==1 && b==-0x80000000);
|
||||
assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
|
||||
|
||||
assert(imult64(0x4000000000000000ll,2,&c)==0);
|
||||
assert(imult64(-0x4000000000000000ll,2,&c)==1 && c==-0x8000000000000000ll);
|
||||
assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue