...
 
Commits (2)
# Note: this should match what's in lib/nacl-20110221/do.
short_hostname := $(shell hostname | sed 's/\..*//' | tr -cd '[a-z][A-Z][0-9]')
NACL_ABI := $(shell $(top_srcdir)/lib/nacl-20110221/printabi.debian)
NACL_DIR = $(top_builddir)/lib/nacl-20110221/build/$(short_hostname)
NACL_LIB_DIR = $(NACL_DIR)/lib/$(NACL_ABI)
AM_CPPFLAGS = \
-I$(top_srcdir)/src \
-I$(NACL_DIR)/include/$(NACL_ABI)
-I$(top_srcdir)/src
AM_LDFLAGS = \
-L$(top_builddir)/src \
-L$(NACL_LIB_DIR)
-L$(top_builddir)/src
NACL_LIBS = $(NACL_LIB_DIR)/libnacl.a $(NACL_LIB_DIR)/librandombytes.a
SSO_LIBS = $(top_builddir)/src/sso/libsso.la $(NACL_LIBS)
SSO_LIBS = $(top_builddir)/src/sso/libsso.la
LDADD = $(SSO_LIBS)
......@@ -23,6 +23,13 @@ ACX_PTHREAD([have_pthread=yes], [heve_pthread=no])
CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"
dnl libsodium
PKG_CHECK_MODULES(SODIUM, libsodium)
AC_SUBST(SODIUM_CFLAGS)
AC_SUBST(SODIUM_LIBS)
CXXFLAGS="$CXXFLAGS $SODIUM_CFLAGS"
LIBS="$LIBS $SODIUM_LIBS"
dnl Checks for headers.
AC_HEADER_STDC
AC_HEADER_TIME
......@@ -128,7 +135,6 @@ dnl Final stage
AC_OUTPUT(
Makefile
lib/Makefile
lib/nacl-20110221/Makefile
lib/gtest/Makefile
src/Makefile
src/sso/Makefile
......
SUBDIRS = nacl-20110221 gtest
SUBDIRS = gtest
crypto_verify
crypto_verify_BYTES
crypto_core
crypto_core_OUTPUTBYTES
crypto_core_INPUTBYTES
crypto_core_KEYBYTES
crypto_core_CONSTBYTES
crypto_hashblocks
crypto_hashblocks_STATEBYTES
crypto_hashblocks_BLOCKBYTES
crypto_hash
crypto_hash_BYTES
crypto_stream
crypto_stream_xor
crypto_stream_beforenm
crypto_stream_afternm
crypto_stream_xor_afternm
crypto_stream_KEYBYTES
crypto_stream_NONCEBYTES
crypto_stream_BEFORENMBYTES
crypto_onetimeauth
crypto_onetimeauth_verify
crypto_onetimeauth_BYTES
crypto_onetimeauth_KEYBYTES
crypto_auth
crypto_auth_verify
crypto_auth_BYTES
crypto_auth_KEYBYTES
crypto_secretbox
crypto_secretbox_open
crypto_secretbox_KEYBYTES
crypto_secretbox_NONCEBYTES
crypto_secretbox_ZEROBYTES
crypto_secretbox_BOXZEROBYTES
crypto_scalarmult
crypto_scalarmult_base
crypto_scalarmult_BYTES
crypto_scalarmult_SCALARBYTES
crypto_box
crypto_box_open
crypto_box_keypair
crypto_box_beforenm
crypto_box_afternm
crypto_box_open_afternm
crypto_box_PUBLICKEYBYTES
crypto_box_SECRETKEYBYTES
crypto_box_BEFORENMBYTES
crypto_box_NONCEBYTES
crypto_box_ZEROBYTES
crypto_box_BOXZEROBYTES
crypto_sign
crypto_sign_open
crypto_sign_keypair
crypto_sign_BYTES
crypto_sign_PUBLICKEYBYTES
crypto_sign_SECRETKEYBYTES
include $(top_srcdir)/Makefile.defs
all: .build-stamp .librandombytes-stamp
.build-stamp:
$(srcdir)/do
-touch $@
.librandombytes-stamp: .build-stamp
$(AR) cru $(NACL_LIB_DIR)/librandombytes.a \
$(NACL_LIB_DIR)/randombytes.o
$(RANLIB) $(NACL_LIB_DIR)/librandombytes.a
-touch $@
clean:
-rm -f .build-stamp
crypto_verify
crypto_core
crypto_hashblocks
crypto_hash
crypto_stream
crypto_onetimeauth
crypto_auth
crypto_secretbox
crypto_scalarmult
crypto_box
crypto_sign
extern int crypto_verify(const unsigned char *,const unsigned char *);
extern int crypto_core(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_hashblocks(unsigned char *,const unsigned char *,unsigned long long);
extern int crypto_hash(unsigned char *,const unsigned char *,unsigned long long);
extern int crypto_stream(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_beforenm(unsigned char *,const unsigned char *);
extern int crypto_stream_afternm(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_xor_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_onetimeauth(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_onetimeauth_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_auth(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_auth_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_secretbox(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_secretbox_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_scalarmult(unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_scalarmult_base(unsigned char *,const unsigned char *);
extern int crypto_box(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_keypair(unsigned char *,unsigned char *);
extern int crypto_box_beforenm(unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_box_open_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_sign(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_sign_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_sign_keypair(unsigned char *,unsigned char *);
extern std::string crypto_auth(const std::string &,const std::string &);
extern void crypto_auth_verify(const std::string &,const std::string &,const std::string &);
extern std::string crypto_box(const std::string &,const std::string &,const std::string &,const std::string &);
extern std::string crypto_box_open(const std::string &,const std::string &,const std::string &,const std::string &);
extern std::string crypto_box_keypair(std::string *);
extern std::string crypto_hash(const std::string &);
extern std::string crypto_onetimeauth(const std::string &,const std::string &);
extern void crypto_onetimeauth_verify(const std::string &,const std::string &,const std::string &);
extern std::string crypto_scalarmult(const std::string &,const std::string &);
extern std::string crypto_scalarmult_base(const std::string &);
extern std::string crypto_secretbox(const std::string &,const std::string &,const std::string &);
extern std::string crypto_secretbox_open(const std::string &,const std::string &,const std::string &);
extern std::string crypto_stream(size_t,const std::string &,const std::string &);
extern std::string crypto_stream_xor(const std::string &,const std::string &,const std::string &);
extern std::string crypto_sign(const std::string &,const std::string &);
extern std::string crypto_sign_open(const std::string &,const std::string &);
extern std::string crypto_sign_keypair(std::string *);
/*
commandline/nacl-sha256.c version 20080713
D. J. Bernstein
Public domain.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "crypto_hash_sha256.h"
unsigned char *input;
unsigned long long inputalloc;
unsigned long long inputlen;
unsigned char h[crypto_hash_sha256_BYTES];
void h_print(void)
{
int i;
for (i = 0;i < crypto_hash_sha256_BYTES;++i) printf("%02x",255 & (int) h[i]);
printf("\n");
}
int main()
{
struct stat st;
int ch;
if (fstat(0,&st) == 0) {
input = mmap(0,st.st_size,PROT_READ,MAP_SHARED,0,0);
if (input != MAP_FAILED) {
crypto_hash_sha256(h,input,st.st_size);
h_print();
return 0;
}
}
input = 0;
inputalloc = 0;
inputlen = 0;
while ((ch = getchar()) != EOF) {
if (inputlen >= inputalloc) {
void *newinput;
while (inputlen >= inputalloc)
inputalloc = inputalloc * 2 + 1;
if (posix_memalign(&newinput,16,inputalloc) != 0) return 111;
memcpy(newinput,input,inputlen);
free(input);
input = newinput;
}
input[inputlen++] = ch;
}
crypto_hash_sha256(h,input,inputlen);
h_print();
return 0;
}
/*
commandline/nacl-sha512.c version 20080713
D. J. Bernstein
Public domain.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "crypto_hash_sha512.h"
unsigned char *input;
unsigned long long inputalloc;
unsigned long long inputlen;
unsigned char h[crypto_hash_sha512_BYTES];
void h_print(void)
{
int i;
for (i = 0;i < crypto_hash_sha512_BYTES;++i) printf("%02x",255 & (int) h[i]);
printf("\n");
}
int main()
{
struct stat st;
int ch;
if (fstat(0,&st) == 0) {
input = mmap(0,st.st_size,PROT_READ,MAP_SHARED,0,0);
if (input != MAP_FAILED) {
crypto_hash_sha512(h,input,st.st_size);
h_print();
return 0;
}
}
input = 0;
inputalloc = 0;
inputlen = 0;
while ((ch = getchar()) != EOF) {
if (inputlen >= inputalloc) {
void *newinput;
while (inputlen >= inputalloc)
inputalloc = inputalloc * 2 + 1;
if (posix_memalign(&newinput,16,inputalloc) != 0) return 111;
memcpy(newinput,input,inputlen);
free(input);
input = newinput;
}
input[inputlen++] = ch;
}
crypto_hash_sha512(h,input,inputlen);
h_print();
return 0;
}
/*
cpucycles/alpha.c version 20060316
D. J. Bernstein
Public domain.
*/
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static long long tod(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static long long rpcc(void)
{
unsigned long long t;
asm volatile("rpcc %0" : "=r"(t));
return t & 0xffffffff;
}
static long long firstrpcc;
static long long firsttod;
static long long lastrpcc;
static long long lasttod;
static double mhz = 0;
static void init(void)
{
firstrpcc = rpcc();
firsttod = tod();
do {
lastrpcc = rpcc();
lasttod = tod();
} while (lasttod - firsttod < 10000);
lastrpcc -= firstrpcc; lastrpcc &= 0xffffffff;
lasttod -= firsttod;
mhz = (double) lastrpcc / (double) lasttod;
}
long long cpucycles_alpha(void)
{
double x;
long long y;
if (!mhz) init();
lastrpcc = rpcc();
lasttod = tod();
lastrpcc -= firstrpcc; lastrpcc &= 0xffffffff;
lasttod -= firsttod;
/* Number of cycles since firstrpcc is lastrpcc + 2^32 y for unknown y. */
/* Number of microseconds since firsttod is lasttod. */
x = (lasttod * mhz - lastrpcc) * 0.00000000023283064365386962890625;
y = x;
while (x > y + 0.5) y += 1;
while (x < y - 0.5) y -= 1;
y *= 4294967296ULL;
lastrpcc += y;
mhz = (double) lastrpcc / (double) lasttod;
return firstrpcc + lastrpcc;
}
long long cpucycles_alpha_persecond(void)
{
if (!mhz) init();
return 1000000.0 * mhz;
}
/*
cpucycles alpha.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_alpha_h
#define CPUCYCLES_alpha_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_alpha(void);
extern long long cpucycles_alpha_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "alpha"
#define cpucycles cpucycles_alpha
#define cpucycles_persecond cpucycles_alpha_persecond
#endif
#endif
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_amd64cpuinfo(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64cpuinfo_persecond(void)
{
return osfreq();
}
/*
cpucycles amd64cpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_amd64cpuinfo_h
#define CPUCYCLES_amd64cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64cpuinfo(void);
extern long long cpucycles_amd64cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64cpuinfo"
#define cpucycles cpucycles_amd64cpuinfo
#define cpucycles_persecond cpucycles_amd64cpuinfo_persecond
#endif
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
long long cpucycles_amd64cpuspeed(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64cpuspeed_persecond(void)
{
int oid[2];
int val;
size_t size;
oid[0] = CTL_HW;
oid[1] = HW_CPUSPEED;
size = sizeof val;
if (sysctl(oid,2,&val,&size,0,0) == -1) return 0;
if (size != sizeof val) return 0;
return val * 1000000LL;
}
/*
cpucycles amd64cpuspeed.h version 20090716
Matthew Dempsky
Public domain.
*/
#ifndef CPUCYCLES_amd64cpuspeed_h
#define CPUCYCLES_amd64cpuspeed_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64cpuspeed(void);
extern long long cpucycles_amd64cpuspeed_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64cpuspeed"
#define cpucycles cpucycles_amd64cpuspeed
#define cpucycles_persecond cpucycles_amd64cpuspeed_persecond
#endif
#endif
#include <stdio.h>
#include <sys/types.h>
long long cpucycles_amd64tscfreq(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64tscfreq_persecond(void)
{
long result = 0;
size_t resultlen = sizeof(long);
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
return result;
}
/*
cpucycles amd64tscfreq.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_amd64tscfreq_h
#define CPUCYCLES_amd64tscfreq_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64tscfreq(void);
extern long long cpucycles_amd64tscfreq_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64tscfreq"
#define cpucycles cpucycles_amd64tscfreq
#define cpucycles_persecond cpucycles_amd64tscfreq_persecond
#endif
#endif
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <spu_mfcio.h>
static long myround(double u)
{
long result = u;
while (result + 0.5 < u) result += 1;
while (result - 0.5 > u) result -= 1;
return result;
}
static long long microseconds(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static long long timebase(void)
{
unsigned long long result;
result = -spu_read_decrementer();
return 0xffffffff & result;
}
static double cpufrequency = 0;
static long tbcycles = 0;
static double guesstbcycles(void)
{
long long tb0; long long us0;
long long tb1; long long us1;
tb0 = timebase();
us0 = microseconds();
do {
tb1 = timebase();
us1 = microseconds();
} while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
if (tb1 <= tb0) return 0;
tb1 -= tb0;
us1 -= us0;
return (cpufrequency * 0.000001 * (double) us1) / (double) tb1;
}
static void init(void)
{
int loop;
double guess1;
double guess2;
spu_write_decrementer(0xffffffff);
cpufrequency = 3192000000.0;
for (loop = 0;loop < 100;++loop) {
guess1 = guesstbcycles();
guess2 = guesstbcycles();
tbcycles = myround(guess1);
if (guess1 - tbcycles > 0.1) continue;
if (tbcycles - guess1 > 0.1) continue;
if (guess2 - tbcycles > 0.1) continue;
if (tbcycles - guess2 > 0.1) continue;
return;
}
tbcycles = 0;
}
long long cpucycles_celllinux(void)
{
if (!tbcycles) init();
return timebase() * tbcycles;
}
long long cpucycles_celllinux_persecond(void)
{
if (!tbcycles) init();
return cpufrequency;
}
/*
cpucycles celllinux.h version 20081201
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_celllinux_h
#define CPUCYCLES_celllinux_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_celllinux(void);
extern long long cpucycles_celllinux_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "celllinux"
#define cpucycles cpucycles_celllinux
#define cpucycles_persecond cpucycles_celllinux_persecond
#endif
#endif
/*
cpucycles/cortex.c version 20101203
D. J. Bernstein
Public domain.
*/
#define SCALE 1
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static int enabled = 0;
static int prev[3];
static unsigned long long prevcycles = 0;
static int now[3];
static long long cyclespersec = 0;
static void readticks(unsigned int *result)
{
struct timeval t;
unsigned int cc;
if (!enabled) {
asm volatile("mcr p15, 0, %0, c9, c12, 0" :: "r"(17));
asm volatile("mcr p15, 0, %0, c9, c12, 1" :: "r"(0x8000000f));
asm volatile("mcr p15, 0, %0, c9, c12, 3" :: "r"(0x8000000f));
enabled = 1;
}
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(cc));
gettimeofday(&t,(struct timezone *) 0);
result[0] = cc;
result[1] = t.tv_usec;
result[2] = t.tv_sec;
}
long long cpucycles_cortex(void)
{
unsigned long long delta4;
int deltan;
int deltas;
unsigned long long guesscycles;
readticks(now);
delta4 = (unsigned int) (now[0] - prev[0]); /* unsigned change in number of cycles mod 2^32 */
deltan = now[1] - prev[1]; /* signed change in number of nanoseconds mod 10^9 */
deltas = now[2] - prev[2]; /* signed change in number of seconds */
if ((deltas == 0 && deltan < 200000) || (deltas == 1 && deltan < -800000))
return (prevcycles + delta4) * SCALE;
prev[0] = now[0];
prev[1] = now[1];
prev[2] = now[2];
if ((deltas == 0 && deltan < 300000) || (deltas == 1 && deltan < -700000)) {
// actual number of cycles cannot have increased by 2^32 in <0.3ms
cyclespersec = 1000000 * (unsigned long long) delta4;
cyclespersec /= deltan + 1000000 * (long long) deltas;
} else {
guesscycles = deltas * cyclespersec;
guesscycles += (deltan * cyclespersec) / 1000000;
while (delta4 + 2147483648ULL < guesscycles) delta4 += 4294967296ULL;
/* XXX: could do longer-term extrapolation here */
}
prevcycles += delta4;
return prevcycles * SCALE;
}
long long cpucycles_cortex_persecond(void)
{
while (!cyclespersec) cpucycles_cortex();
return cyclespersec * SCALE;
}
/*
cpucycles cortex.h version 20100912
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_cortex_h
#define CPUCYCLES_cortex_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_cortex(void);
extern long long cpucycles_cortex_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "cortex"
#define cpucycles cpucycles_cortex
#define cpucycles_persecond cpucycles_cortex_persecond
#endif
#endif
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
static int fddev = -1;
static int prev[3];
static unsigned long long prevcycles = 0;
static int now[3];
static long long cyclespersec = 0;
static void readdev(unsigned int *result)
{
if (read(fddev,result,12) == 12) return;
result[0] = result[1] = result[2] = 0;
}
long long cpucycles_dev4ns(void)
{
unsigned long long delta4;
int deltan;
int deltas;
unsigned long long guesscycles;
if (fddev == -1) {
fddev = open("/dev/cpucycles4ns",O_RDONLY);
readdev(prev);
}
readdev(now);
delta4 = (unsigned int) (now[0] - prev[0]); /* unsigned change in number of cycles mod 2^32 */
deltan = now[1] - prev[1]; /* signed change in number of nanoseconds mod 10^9 */
deltas = now[2] - prev[2]; /* signed change in number of seconds */
if ((deltas == 0 && deltan < 200000000) || (deltas == 1 && deltan < -800000000))
return prevcycles + delta4;
prev[0] = now[0];
prev[1] = now[1];
prev[2] = now[2];
if ((deltas == 0 && deltan < 300000000) || (deltas == 1 && deltan < -700000000)) {
// actual number of cycles cannot have increased by 2^32 in <0.3ms
cyclespersec = 1000000000 * (unsigned long long) delta4;
cyclespersec /= deltan + 1000000000 * (long long) deltas;
} else {
guesscycles = deltas * cyclespersec;
guesscycles += (deltan * cyclespersec) / 1000000000;
while (delta4 + 2147483648ULL < guesscycles) delta4 += 4294967296ULL;
/* XXX: could do longer-term extrapolation here */
}
prevcycles += delta4;
return prevcycles;
}
long long cpucycles_dev4ns_persecond(void)
{
while (!cyclespersec) cpucycles_dev4ns();
return cyclespersec;
}
/*
cpucycles dev4ns.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_dev4ns_h
#define CPUCYCLES_dev4ns_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_dev4ns(void);
extern long long cpucycles_dev4ns_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "dev4ns"
#define cpucycles cpucycles_dev4ns
#define cpucycles_persecond cpucycles_dev4ns_persecond
#endif
#endif
#!/bin/sh -e
okabi | (
while read abi
do
rm -f cpucycles.o cpucycles.h
(
case "$abi" in
ppc*)
echo powerpccpuinfo
echo powerpcmacos
;;
amd64*)
echo amd64tscfreq
echo amd64cpuinfo
echo amd64cpuspeed
;;
x86*)
echo x86tscfreq
echo x86cpuinfo
echo x86cpuspeed
echo x86estimate
;;
cell*)
echo celllinux
;;
sparc*)
echo sparccpuinfo
echo sparc32cpuinfo
;;
mips*)
echo mips
;;
hppa*)
echo hppapstat
;;
alpha*)
echo alpha
;;
sgi*)
echo sgi
;;
arm*)
echo cortex
echo dev4ns
;;
esac
echo amd64tscfreq
echo amd64cpuinfo
echo amd64cpuspeed
echo x86tscfreq
echo x86cpuinfo
echo x86cpuspeed
echo x86estimate
echo ia64cpuinfo
echo powerpccpuinfo
echo powerpcmacos
echo celllinux
echo sparccpuinfo
echo sparc32cpuinfo
echo mips
echo hppapstat
echo alpha
echo sgi
echo cortex
echo dev4ns
echo monotoniccpuinfo
echo monotonic
echo gettimeofday
) | (
while read n
do
okc-$abi | (
while read c
do
echo "=== `date` === Trying $n.c with $c..." >&2
rm -f test cpucycles-impl.o cpucycles-impl.h cpucycles-impl.c
cp $n.c cpucycles-impl.c || continue
cp $n.h cpucycles-impl.h || continue
$c -c cpucycles-impl.c || continue
$c -o test test.c cpucycles-impl.o || continue
./test || continue
echo "=== `date` === Success. Using $n.c." >&2
mkdir -p lib/$abi
mv cpucycles-impl.o lib/$abi/cpucycles.o
mkdir -p include/$abi
mv cpucycles-impl.h include/$abi/cpucycles.h
exit 0
done
exit 111
) && exit 0
done
exit 111
) || (
echo ===== Giving up. >&2
rm -f test cpucycles-impl.o cpucycles-impl.h cpucycles-impl.c
exit 111
) || exit 0
done
exit 0
) || exit 111
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "osfreq.c"
static double cpufrequency = 0;
static void init(void)
{
cpufrequency = osfreq();
}
long long cpucycles_gettimeofday(void)
{
double result;
struct timeval t;
if (!cpufrequency) init();
gettimeofday(&t,(struct timezone *) 0);
result = t.tv_usec;
result *= 0.000001;
result += (double) t.tv_sec;
result *= cpufrequency;
return result;
}
long long cpucycles_gettimeofday_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}
/*
cpucycles gettimeofday.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_gettimeofday_h
#define CPUCYCLES_gettimeofday_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_gettimeofday(void);
extern long long cpucycles_gettimeofday_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "gettimeofday"
#define cpucycles cpucycles_gettimeofday
#define cpucycles_persecond cpucycles_gettimeofday_persecond
#endif
#endif
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/pstat.h>
#include <machine/inline.h>
long long cpucycles_hppapstat(void)
{
register long long result;
_MFCTL(16,result);
return result;
}
long long cpucycles_hppapstat_persecond(void)
{
struct pst_processor pst;
union pstun pu;
double result;
pu.pst_processor = &pst;
if (pstat(PSTAT_PROCESSOR,pu,sizeof(pst),1,0) < 0) return 0<