add API for integer multiplication with overflow detection
parent
ccc0a23183
commit
d1df715971
@ -0,0 +1,33 @@
|
||||
#ifndef _ARRAY_H
|
||||
#define _ARRAY_H
|
||||
|
||||
#include "uint64.h"
|
||||
#include <errno.h>
|
||||
|
||||
typedef struct {
|
||||
char* p;
|
||||
int64 allocated, initialized; /* in bytes */
|
||||
|
||||
/* p and allocated nonzero: array is allocated */
|
||||
/* p zero: array is unallocated */
|
||||
/* allocated < 0: array is failed */
|
||||
} array;
|
||||
|
||||
void* array_allocate(array* x,int64 membersize,int64 pos);
|
||||
void* array_get(array* x,int64 membersize,int64 pos);
|
||||
void* array_start(const array* const x);
|
||||
int64 array_length(const array* const x,int64 membersize);
|
||||
int64 array_bytes(const array* const x);
|
||||
void array_truncate(array* x,int64 membersize,int64 len);
|
||||
void array_trunc(array* x);
|
||||
void array_reset(array* x);
|
||||
void array_fail(array* x);
|
||||
int array_equal(const array* const x,const array* const y);
|
||||
void array_cat(array* to,const array* const from);
|
||||
void array_catb(array* to,const char* from,int64 len);
|
||||
void array_cats(array* to,const char* from);
|
||||
void array_cats0(array* to,const char* from);
|
||||
void array_cat0(array* to);
|
||||
void array_cate(array* to,const array* const from,int64 pos,int64 stop);
|
||||
|
||||
#endif
|
@ -0,0 +1,51 @@
|
||||
#include "array.h"
|
||||
|
||||
#if 0
|
||||
static array x;
|
||||
t *p;
|
||||
int64 pos;
|
||||
|
||||
p = array_allocate(&x,sizeof(t),pos);
|
||||
|
||||
array_allocate makes sure that enough bytes are allocated in x for at
|
||||
least pos+1 objects of type t. (The size of t must be positive;
|
||||
otherwise the effects are undefined.) If not enough bytes are
|
||||
allocated (or x is unallocated), array_allocate allocates more bytes,
|
||||
moving the dynamically allocated region if necessary. array_allocate
|
||||
often allocates somewhat more bytes than necessary, to save time
|
||||
later.
|
||||
|
||||
array_allocate then makes sure that the number of bytes initialized
|
||||
covers at least those pos+1 objects. If not enough bytes are
|
||||
initialized, array_allocate initializes more bytes (setting them to
|
||||
0), up to exactly the end of the pos+1st object.
|
||||
|
||||
array_allocate then returns a pointer to the pos+1st object; i.e.,
|
||||
object number pos, with objects numbered starting at 0. This pointer
|
||||
can be used to change or inspect the object. The pointer can continue
|
||||
to be used through subsequent calls to array_get, array_start,
|
||||
array_length, and array_bytes, but it must not be used after any
|
||||
other operations on this array.
|
||||
|
||||
If something goes wrong, array_allocate returns 0, setting errno
|
||||
appropriately, without touching x. In particular, array_allocate
|
||||
returns 0 if
|
||||
|
||||
* x has failed, or
|
||||
* pos is negative, or
|
||||
* not enough memory is available.
|
||||
|
||||
array_allocate does not change x to have failed; if you want to do
|
||||
that, use array_fail.
|
||||
#endif
|
||||
|
||||
void* array_allocate(array* x,int64 membersize,int64 pos) {
|
||||
int64 wanted;
|
||||
if (membersize<128)
|
||||
wanted=(pos+127)&(-128ll); /* round up to multiple of 128 */
|
||||
else
|
||||
wanted=(pos+4095)&(-4096ll); /* round up to 4k pages */
|
||||
/* detect numeric overflow */
|
||||
if (wanted<0) return 0;
|
||||
wanted=membersize*(pos+1);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int imult16(int16 a,int16 b,int16* c) {
|
||||
int neg=(a<0);
|
||||
if (neg) a=-a;
|
||||
if (b<0) { neg^=1; b=-b; }
|
||||
if (umult16(a,b,c)) return 1;
|
||||
if (neg) *c=-*c;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int imult32(int32 a,int32 b,int32* c) {
|
||||
int neg=(a<0);
|
||||
if (neg) a=-a;
|
||||
if (b<0) { neg^=1; b=-b; }
|
||||
if (umult32(a,b,c)) return 1;
|
||||
if (neg) *c=-*c;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int imult64(int64 a,int64 b,int64* c) {
|
||||
int neg=(a<0);
|
||||
if (neg) a=-a;
|
||||
if (b<0) { neg^=1; b=-b; }
|
||||
if (umult64(a,b,c)) return 1;
|
||||
if (neg) *c=-*c;
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int umult16(uint16 a,uint16 b,uint16* c) {
|
||||
unsigned long x=(unsigned long)a*b;
|
||||
if (x>0xffff) return 1;
|
||||
*c=x&0xffff;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include "safemult.h"
|
||||
|
||||
int umult32(uint32 a,uint32 b,uint32* c) {
|
||||
unsigned long long x=(unsigned long long)a*b;
|
||||
if (x>0xffffffff) return 1;
|
||||
*c=x&0xffffffff;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#include "safemult.h"
|
||||
|
||||
/* return 1 for overflow, 0 for ok */
|
||||
int umult64(uint64 a,uint64 b,uint64* c) {
|
||||
uint32 ahi=a>>32;
|
||||
uint32 alo=(a&0xffffffff);
|
||||
uint32 bhi=b>>32;
|
||||
uint32 blo=(b&0xffffffff);
|
||||
|
||||
// a=ahi*x+alo, b=bhi*x+blo
|
||||
// a*b = (ahi*x+alo) * (bhi*x+blo)
|
||||
// = ahi*x*bhi*x + ahi*x*blo + alo*bhi*x + alo*blo
|
||||
|
||||
// -> overflow if ahi*bhi != zero */
|
||||
if (ahi && bhi) return 1;
|
||||
|
||||
a=(uint64)(ahi)*blo+(uint64)(alo)*bhi;
|
||||
if (a>0xffffffff) return 1;
|
||||
*c=(a<<32)+(uint64)(alo)*blo;
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
#ifndef _SAFEMULT_H
|
||||
#define _SAFEMULT_H
|
||||
|
||||
#include "uint16.h"
|
||||
#include "uint32.h"
|
||||
#include "uint64.h"
|
||||
|
||||
/* return 1 for overflow, 0 for ok */
|
||||
int umult16(uint16 a,uint16 b,uint16* c);
|
||||
int imult16( int16 a, int16 b, int16* c);
|
||||
|
||||
int umult32(uint32 a,uint32 b,uint32* c);
|
||||
int imult32( int32 a, int32 b, int32* c);
|
||||
|
||||
int umult64(uint64 a,uint64 b,uint64* c);
|
||||
int imult64( int64 a, int64 b, int64* c);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue