64-bit bitwise manipulation functions, by Gautam Tirumala.
This commit is contained in:
parent
ee1b608bcf
commit
6c66933f90
3 changed files with 110 additions and 0 deletions
|
@ -34,6 +34,11 @@ int cmp64ul(u64_t i, unsigned long j);
|
|||
unsigned long ex64lo(u64_t i);
|
||||
unsigned long ex64hi(u64_t i);
|
||||
u64_t make64(unsigned long lo, unsigned long hi);
|
||||
u64_t rrotate64(u64_t x, unsigned short b);
|
||||
u64_t rshift64(u64_t x, unsigned short b);
|
||||
u64_t xor64(u64_t a, u64_t b);
|
||||
u64_t and64(u64_t a, u64_t b);
|
||||
u64_t not64(u64_t a);
|
||||
|
||||
#define is_zero64(i) ((i).lo == 0 && (i).hi == 0)
|
||||
#define make_zero64(i) do { (i).lo = (i).hi = 0; } while(0)
|
||||
|
|
76
lib/libc/other/u64util.c
Normal file
76
lib/libc/other/u64util.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/* Few u64 utils implemented in C
|
||||
* Author: Gautam BT
|
||||
*/
|
||||
#include <minix/u64.h>
|
||||
|
||||
u64_t rrotate64(u64_t x, unsigned short b)
|
||||
{
|
||||
u64_t r, t;
|
||||
|
||||
b %= 64;
|
||||
|
||||
if(b == 32) {
|
||||
r.lo = x.hi;
|
||||
r.hi = x.lo;
|
||||
return r;
|
||||
}else if(b < 32) {
|
||||
r.lo = (x.lo >> b) | (x.hi << (32 - b));
|
||||
r.hi = (x.hi >> b) | (x.lo << (32 - b));
|
||||
return r;
|
||||
}else {
|
||||
/* Rotate by 32 bits first then rotate by remaining */
|
||||
t.lo = x.hi;
|
||||
t.hi = x.lo;
|
||||
b = b - 32;
|
||||
r.lo = (t.lo >> b) | (t.hi << (32 - b));
|
||||
r.hi = (t.hi >> b) | (t.lo << (32 - b));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
u64_t rshift64(u64_t x, unsigned short b)
|
||||
{
|
||||
u64_t r;
|
||||
|
||||
if(b >= 64)
|
||||
return make64(0,0);
|
||||
|
||||
if(b >= 32) {
|
||||
r.hi = 0;
|
||||
r.lo = x.hi >> (b - 32);
|
||||
}else {
|
||||
r.lo = (x.lo >> b) | (x.hi << (32 - b));
|
||||
r.hi = (x.hi >> b);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
u64_t xor64(u64_t a, u64_t b)
|
||||
{
|
||||
u64_t r;
|
||||
r.hi = a.hi ^ b.hi;
|
||||
r.lo = a.lo ^ b.lo;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
u64_t and64(u64_t a, u64_t b)
|
||||
{
|
||||
u64_t r;
|
||||
r.hi = a.hi & b.hi;
|
||||
r.lo = a.lo & b.lo;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
u64_t not64(u64_t a)
|
||||
{
|
||||
u64_t r;
|
||||
|
||||
r.hi = ~a.hi;
|
||||
r.lo = ~a.lo;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
|
@ -31,6 +31,11 @@ int cmp64ul(u64_t \fIi\fP, unsigned long \fIj\fP)
|
|||
unsigned long ex64lo(u64_t \fIi\fP)
|
||||
unsigned long ex64hi(u64_t \fIi\fP)
|
||||
u64_t make64(unsigned long \fIlo\fP, unsigned long \fIhi\fP)
|
||||
u64_t rrotate64(u64_t \fIi\fP, unsigned short \fIb\fP)
|
||||
u64_t rshift64(u64_t \fIi\fP, unsigned short \fIb\fP)
|
||||
u64_t xor64(u64_t \fIi\fP, u64_t \fIj\fP)
|
||||
u64_t and64(u64_t \fIi\fP, u64_t \fIj\fP)
|
||||
u64_t not64(u64_t \fIi\fP)
|
||||
.fi
|
||||
.ft P
|
||||
.SH DESCRIPTION
|
||||
|
@ -186,9 +191,33 @@ if
|
|||
.I i
|
||||
>
|
||||
.IR j .
|
||||
|
||||
.TP
|
||||
.B "u64_t rrotate64(u64_t \fIi\fP, unsigned short \fIb\fP)"
|
||||
Rotate first 64-bit argument to the right by \fIb\fP bits and
|
||||
return the result.
|
||||
|
||||
.TP
|
||||
.B "u64_t rshift64(u64_t \fIi\fP, unsigned short \fIb\fP)"
|
||||
Shift first 64-bit argument to the right by \fIb\fP bits and
|
||||
return the result.
|
||||
|
||||
.TP
|
||||
.B "u64_t xor64(u64_t \fIi\fP, u64_t \fIj\fP)"
|
||||
Return the 64-bit bitwise xor of the 64-bit \fIi\fP and \fIj\fP arguments.
|
||||
|
||||
.TP
|
||||
.B "u64_t and64(u64_t \fIi\fP, u64_t \fIj\fP)"
|
||||
Return the 64-bit bitwise and of the 64-bit \fIi\fP and \fIj\fP arguments.
|
||||
|
||||
.TP
|
||||
.B "u64_t not64(u64_t \fIi\fP)"
|
||||
Return the 64-bit bitwise not of the 64-bit \fIi\fP argument.
|
||||
|
||||
.TP
|
||||
.B "int cmp64u(u64_t \fIi\fP, unsigned \fIj\fP)"
|
||||
Likewise compare a 64 bit number with an unsigned.
|
||||
|
||||
.TP
|
||||
.B "int cmp64ul(u64_t \fIi\fP, unsigned long \fIj\fP)"
|
||||
Likewise compare a 64 bit number with an unsigned long.
|
||||
|
|
Loading…
Reference in a new issue