File: imult32.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (31 lines) | stat: -rw-r--r-- 540 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#if defined(__GNUC__) && (__GNUC__ >= 5)

#include "uint32.h"

int imult32( int32 a, int32 b, int32* c) { return !__builtin_mul_overflow(a,b,c); }

#else

#include "safemult.h"

int imult32(int32 a,int32 b,int32* c) {
  int64 x=(int64)a*b;
  if ((int32)x != x) return 0;
  *c=x;
  return 1;
}

#endif

#ifdef UNITTEST
#include <assert.h>

int main() {
  int32 b;

  assert(imult32(0x40000000,2,&b)==0);
  assert(imult32(-0x40000000,2,&b)==1 && b==-0x80000000ll);
  assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);
  return 0;
}
#endif