mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-02-20 09:26:37 +00:00
cleaned up NaCl build to just compile using make. #17
This commit is contained in:
parent
55c9cff8cd
commit
a40fcfec66
11
Android.mk
11
Android.mk
@ -85,11 +85,10 @@ SERVALD_LOCAL_STATIC_LIBRARIES = sqlite3 nacl
|
||||
# Build NACL
|
||||
include $(CLEAR_VARS)
|
||||
# Work out where NACL is
|
||||
NACL_VER=$(shell cat $(LOCAL_PATH)/serval-dna/nacl/nacl-version)
|
||||
NACL_BASE=serval-dna/nacl/$(NACL_VER)/build_android
|
||||
NACL_INC=$(LOCAL_PATH)/$(NACL_BASE)
|
||||
NACL_BASE=serval-dna/nacl/src
|
||||
NACL_INC=$(LOCAL_PATH)/$(NACL_BASE)/../include
|
||||
# Find sources
|
||||
include $(LOCAL_PATH)/$(NACL_BASE)/sources.mk
|
||||
include $(LOCAL_PATH)/$(NACL_BASE)/nacl.mk
|
||||
LOCAL_MODULE:= nacl
|
||||
LOCAL_SRC_FILES:= $(NACL_SOURCES)
|
||||
LOCAL_CFLAGS += -g -I$(NACL_INC)
|
||||
@ -100,9 +99,9 @@ include $(CLEAR_VARS)
|
||||
# Find SQLITE3 headers
|
||||
SQLITE3_INC=$(LOCAL_PATH)/sqlite3
|
||||
# Get the list of sources
|
||||
include $(LOCAL_PATH)/serval-dna/nacl/$(NACL_VER)/build_android/sources.mk
|
||||
include $(LOCAL_PATH)/serval-dna/nacl/src/nacl.mk
|
||||
LOCAL_SRC_FILES:= $(SERVALD_SRC_FILES)
|
||||
LOCAL_CFLAGS += $(SERVALD_LOCAL_CFLAGS)
|
||||
LOCAL_CFLAGS += $(SERVALD_LOCAL_CFLAGS) -Iserval-dna/nacl/include
|
||||
LOCAL_LDLIBS := $(SERVALD_LOCAL_LDLIBS)
|
||||
LOCAL_STATIC_LIBRARIES := $(SERVALD_LOCAL_STATIC_LIBRARIES)
|
||||
LOCAL_MODULE:= serval
|
||||
|
@ -1,4 +1,6 @@
|
||||
SRCS= \
|
||||
NACL_BASE= nacl/src
|
||||
include $(NACL_BASE)/nacl.mk
|
||||
SRCS= $(NACL_SOURCES) \
|
||||
audiodevices.c \
|
||||
audio_reflector.c \
|
||||
batman.c \
|
||||
@ -121,7 +123,7 @@ HDRS= fifo.h \
|
||||
|
||||
LDFLAGS=@LDFLAGS@ @LIBS@ @PORTAUDIO_LIBS@ @SRC_LIBS@ @SPANDSP_LIBS@ @CODEC2_LIBS@ @PTHREAD_LIBS@
|
||||
|
||||
CFLAGS= -Isqlite-amalgamation-3070900 @CPPFLAGS@ @CFLAGS@ @PORTAUDIO_CFLAGS@ @SRC_CFLAGS@ @SPANDSP_CFLAGS@ @PTHREAD_CFLAGS@ $(VOIPTEST_CFLAGS)
|
||||
CFLAGS= -Isqlite-amalgamation-3070900 @CPPFLAGS@ @CFLAGS@ @PORTAUDIO_CFLAGS@ @SRC_CFLAGS@ @SPANDSP_CFLAGS@ @PTHREAD_CFLAGS@ $(VOIPTEST_CFLAGS) -Inacl/include
|
||||
CFLAGS+=-fPIC
|
||||
CFLAGS+=-Wall -Wno-unused-value
|
||||
# Solaris magic
|
||||
@ -140,7 +142,7 @@ sqlite-amalgamation-3070900/sqlite3.o: sqlite-amalgamation-3070900/sqlite3.c
|
||||
|
||||
%.o: %.c $(HDRS)
|
||||
@echo CC $<
|
||||
@$(CC) $(CFLAGS) $(DEFS) -c $<
|
||||
@$(CC) $(CFLAGS) $(DEFS) -c $< -o $@
|
||||
|
||||
servald: $(OBJS)
|
||||
@echo LINK $@
|
||||
|
24
configure.in
24
configure.in
@ -105,30 +105,6 @@ AC_CHECK_HEADER([alsa/asoundlib.h], [have_alsa=1], [have_alsa=0])
|
||||
AS_IF([test x"$have_alsa" = "x1"], [AC_DEFINE([HAVE_ALSA_ASOUNDLIB_H])])
|
||||
AS_IF([test x"$have_alsa" = "x1"], [AC_SUBST(HAVE_ALSA,1)], [AC_SUBST(HAVE_ALSA,0)])
|
||||
|
||||
echo "Fetching and building NaCl if required."
|
||||
echo "(this can take HOURS to build depending on your architecture,"
|
||||
echo " but fortunately it only needs to happen once.)"
|
||||
cd nacl
|
||||
./nacl-gcc-prep
|
||||
cd ..
|
||||
NACL_INC=`cat nacl/naclinc.txt`
|
||||
NACL_LIB=`cat nacl/nacllib.txt`
|
||||
CPPFLAGS="$CPPFLAGS -Inacl/$NACL_INC"
|
||||
LDFLAGS="$LDFLAGS nacl/$NACL_LIB"
|
||||
AC_CHECK_HEADER(crypto_sign_edwards25519sha512batch_ref/ge25519.h,
|
||||
AC_DEFINE([HAVE_CRYPTO_SIGN_NACL_GE25519_H]),
|
||||
[
|
||||
dnl A kludge to get to the NaCl ge25519 functions, which are not included in the public API
|
||||
dnl in a native build (but are available in the Android build).
|
||||
oCPPFLAGS="$CPPFLAGS"
|
||||
CPPFLAGS="$CPPFLAGS -Inacl/$NACL_INC -Inacl/${NACL_INC%%/*}/crypto_sign"
|
||||
AC_CHECK_HEADER([edwards25519sha512batch/ref/ge25519.h],
|
||||
AC_DEFINE([HAVE_KLUDGE_NACL_GE25519_H]),
|
||||
CPPFLAGS="$oCPPFLAGS"
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
AC_CHECK_LIB(m,sqrtf,[LDFLAGS="$LDFLAGS -lm"])
|
||||
AC_CHECK_LIB(nsl,callrpc,[LDFLAGS="$LDFLAGS -lnsl"])
|
||||
AC_CHECK_LIB(socket,socket,[LDFLAGS="$LDFLAGS -lsocket"])
|
||||
|
@ -1,11 +0,0 @@
|
||||
crypto_verify
|
||||
crypto_core
|
||||
crypto_hashblocks
|
||||
crypto_hash
|
||||
crypto_stream
|
||||
crypto_onetimeauth
|
||||
crypto_auth
|
||||
crypto_secretbox
|
||||
crypto_scalarmult
|
||||
crypto_box
|
||||
crypto_sign
|
@ -1,56 +0,0 @@
|
||||
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
|
@ -1,26 +0,0 @@
|
||||
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 *);
|
@ -1,17 +0,0 @@
|
||||
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 *);
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
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;
|
||||
}
|
@ -1,64 +0,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;
|
||||
}
|
@ -1,80 +0,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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,16 +0,0 @@
|
||||
#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();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,25 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,18 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,83 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,73 +0,0 @@
|
||||
/*
|
||||
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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,62 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,105 +0,0 @@
|
||||
#!/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
|
@ -1,32 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
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
|
@ -1,26 +0,0 @@
|
||||
#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;
|
||||
result = pst.psp_iticksperclktick;
|
||||
result *= (double) sysconf(_SC_CLK_TCK);
|
||||
return result;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles hppapstat.h version 20060319
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_hppapstat_h
|
||||
#define CPUCYCLES_hppapstat_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_hppapstat(void);
|
||||
extern long long cpucycles_hppapstat_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "hppapstat"
|
||||
#define cpucycles cpucycles_hppapstat
|
||||
#define cpucycles_persecond cpucycles_hppapstat_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,15 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
long long cpucycles_ia64cpuinfo(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile("mov %0=ar.itc" : "=r"(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_ia64cpuinfo_persecond(void)
|
||||
{
|
||||
return osfreq();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles ia64cpuinfo.h version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_ia64cpuinfo_h
|
||||
#define CPUCYCLES_ia64cpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_ia64cpuinfo(void);
|
||||
extern long long cpucycles_ia64cpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "ia64cpuinfo"
|
||||
#define cpucycles cpucycles_ia64cpuinfo
|
||||
#define cpucycles_persecond cpucycles_ia64cpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
cpucycles/mips.c version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#define SCALE 2
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
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;
|
||||
asm volatile(".byte 59; .byte 16; .byte 2; .byte 124; move %0,$2" : "=r"(cc) : : "$2");
|
||||
gettimeofday(&t,(struct timezone *) 0);
|
||||
result[0] = cc;
|
||||
result[1] = t.tv_usec;
|
||||
result[2] = t.tv_sec;
|
||||
}
|
||||
|
||||
long long cpucycles_mips(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_mips_persecond(void)
|
||||
{
|
||||
while (!cyclespersec) cpucycles_mips();
|
||||
return cyclespersec * SCALE;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles mips.h version 20100802
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_mips_h
|
||||
#define CPUCYCLES_mips_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_mips(void);
|
||||
extern long long cpucycles_mips_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "mips"
|
||||
#define cpucycles cpucycles_mips
|
||||
#define cpucycles_persecond cpucycles_mips_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,34 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
static double cpufrequency = 0;
|
||||
|
||||
static void init(void)
|
||||
{
|
||||
long result = 0; size_t resultlen = sizeof(long);
|
||||
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
|
||||
cpufrequency = result;
|
||||
}
|
||||
|
||||
long long cpucycles_monotonic(void)
|
||||
{
|
||||
double result;
|
||||
struct timespec t;
|
||||
if (!cpufrequency) init();
|
||||
clock_gettime(CLOCK_MONOTONIC,&t);
|
||||
result = t.tv_nsec;
|
||||
result *= 0.000000001;
|
||||
result += (double) t.tv_sec;
|
||||
result *= cpufrequency;
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_monotonic_persecond(void)
|
||||
{
|
||||
if (!cpufrequency) init();
|
||||
return cpufrequency;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles monotonic.h version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_monotonic_h
|
||||
#define CPUCYCLES_monotonic_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_monotonic(void);
|
||||
extern long long cpucycles_monotonic_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "monotonic"
|
||||
#define cpucycles cpucycles_monotonic
|
||||
#define cpucycles_persecond cpucycles_monotonic_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,33 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
static double cpufrequency = 0;
|
||||
|
||||
static void init(void)
|
||||
{
|
||||
cpufrequency = osfreq();
|
||||
}
|
||||
|
||||
long long cpucycles_monotoniccpuinfo(void)
|
||||
{
|
||||
double result;
|
||||
struct timespec t;
|
||||
if (!cpufrequency) init();
|
||||
clock_gettime(CLOCK_MONOTONIC,&t);
|
||||
result = t.tv_nsec;
|
||||
result *= 0.000000001;
|
||||
result += (double) t.tv_sec;
|
||||
result *= cpufrequency;
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_monotoniccpuinfo_persecond(void)
|
||||
{
|
||||
if (!cpufrequency) init();
|
||||
return cpufrequency;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles monotoniccpuinfo.h version 20100804
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_monotoniccpuinfo_h
|
||||
#define CPUCYCLES_monotoniccpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_monotoniccpuinfo(void);
|
||||
extern long long cpucycles_monotoniccpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "monotoniccpuinfo"
|
||||
#define cpucycles cpucycles_monotoniccpuinfo
|
||||
#define cpucycles_persecond cpucycles_monotoniccpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
static double osfreq(void)
|
||||
{
|
||||
FILE *f;
|
||||
double result;
|
||||
int s;
|
||||
|
||||
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
|
||||
if (f) {
|
||||
s = fscanf(f,"%lf",&result);
|
||||
fclose(f);
|
||||
if (s > 0) return 1000.0 * result;
|
||||
}
|
||||
|
||||
f = fopen("/sys/devices/system/cpu/cpu0/clock_tick", "r");
|
||||
if (f) {
|
||||
s = fscanf(f,"%lf",&result);
|
||||
fclose(f);
|
||||
if (s > 0) return result;
|
||||
}
|
||||
|
||||
f = fopen("/proc/cpuinfo","r");
|
||||
if (f) {
|
||||
for (;;) {
|
||||
s = fscanf(f,"cpu MHz : %lf",&result);
|
||||
if (s > 0) break;
|
||||
if (s == 0) s = fscanf(f,"%*[^\n]\n");
|
||||
if (s < 0) { result = 0; break; }
|
||||
}
|
||||
fclose(f);
|
||||
if (result) return 1000000.0 * result;
|
||||
}
|
||||
|
||||
f = fopen("/proc/cpuinfo","r");
|
||||
if (f) {
|
||||
for (;;) {
|
||||
s = fscanf(f,"clock : %lf",&result);
|
||||
if (s > 0) break;
|
||||
if (s == 0) s = fscanf(f,"%*[^\n]\n");
|
||||
if (s < 0) { result = 0; break; }
|
||||
}
|
||||
fclose(f);
|
||||
if (result) return 1000000.0 * result;
|
||||
}
|
||||
|
||||
f = popen("/usr/sbin/lsattr -E -l proc0 -a frequency 2>/dev/null","r");
|
||||
if (f) {
|
||||
s = fscanf(f,"frequency %lf",&result);
|
||||
pclose(f);
|
||||
if (s > 0) return result;
|
||||
}
|
||||
|
||||
f = popen("/usr/sbin/psrinfo -v 2>/dev/null","r");
|
||||
if (f) {
|
||||
for (;;) {
|
||||
s = fscanf(f," The %*s processor operates at %lf MHz",&result);
|
||||
if (s > 0) break;
|
||||
if (s == 0) s = fscanf(f,"%*[^\n]\n");
|
||||
if (s < 0) { result = 0; break; }
|
||||
}
|
||||
pclose(f);
|
||||
if (result) return 1000000.0 * result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
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 int tbshift = 0;
|
||||
|
||||
static long long timebase(void)
|
||||
{
|
||||
unsigned long high;
|
||||
unsigned long low;
|
||||
unsigned long newhigh;
|
||||
unsigned long long result;
|
||||
asm volatile(
|
||||
"7:mftbu %0;mftb %1;mftbu %2;cmpw %0,%2;bne 7b"
|
||||
: "=r" (high), "=r" (low), "=r" (newhigh)
|
||||
);
|
||||
result = high;
|
||||
result <<= 32;
|
||||
result |= low;
|
||||
return result >> tbshift;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
cpufrequency = osfreq();
|
||||
if (!cpufrequency) return;
|
||||
|
||||
for (tbshift = 0;tbshift < 10;++tbshift) {
|
||||
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_powerpccpuinfo(void)
|
||||
{
|
||||
if (!tbcycles) init();
|
||||
return timebase() * tbcycles;
|
||||
}
|
||||
|
||||
long long cpucycles_powerpccpuinfo_persecond(void)
|
||||
{
|
||||
if (!tbcycles) init();
|
||||
return cpufrequency;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles powerpccpuinfo.h version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_powerpccpuinfo_h
|
||||
#define CPUCYCLES_powerpccpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_powerpccpuinfo(void);
|
||||
extern long long cpucycles_powerpccpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "powerpccpuinfo"
|
||||
#define cpucycles cpucycles_powerpccpuinfo
|
||||
#define cpucycles_persecond cpucycles_powerpccpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,42 +0,0 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
#define timebase mach_absolute_time
|
||||
|
||||
static int cpumib[2] = { CTL_HW, HW_CPU_FREQ } ;
|
||||
static int tbmib[2] = { CTL_HW, HW_TB_FREQ } ;
|
||||
|
||||
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 tbcycles = 0;
|
||||
|
||||
static void init(void)
|
||||
{
|
||||
unsigned int cpufrequency = 0; size_t cpufrequencylen = sizeof(unsigned int);
|
||||
unsigned int tbfrequency = 0; size_t tbfrequencylen = sizeof(unsigned int);
|
||||
sysctl(cpumib,2,&cpufrequency,&cpufrequencylen,0,0);
|
||||
sysctl(tbmib,2,&tbfrequency,&tbfrequencylen,0,0);
|
||||
if (tbfrequency > 0)
|
||||
tbcycles = myround((double) (unsigned long long) cpufrequency
|
||||
/ (double) (unsigned long long) tbfrequency);
|
||||
}
|
||||
|
||||
long long cpucycles_powerpcmacos(void)
|
||||
{
|
||||
if (!tbcycles) init();
|
||||
return timebase() * tbcycles;
|
||||
}
|
||||
|
||||
long long cpucycles_powerpcmacos_persecond(void)
|
||||
{
|
||||
unsigned int result = 0; size_t resultlen = sizeof(unsigned int);
|
||||
sysctl(cpumib,2,&result,&resultlen,0,0);
|
||||
return (unsigned long long) result;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles powerpcmacos.h version 20060319
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_powerpcmacos_h
|
||||
#define CPUCYCLES_powerpcmacos_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_powerpcmacos(void);
|
||||
extern long long cpucycles_powerpcmacos_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "powerpcmacos"
|
||||
#define cpucycles cpucycles_powerpcmacos
|
||||
#define cpucycles_persecond cpucycles_powerpcmacos_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,38 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
static double cpufrequency = 0;
|
||||
|
||||
static void init(void)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
f = popen("hinv -c processor | awk '{if ($3==\"MHZ\") print $2*1000000}'","r");
|
||||
if (!f) return;
|
||||
if (fscanf(f,"%lf",&cpufrequency) < 1) cpufrequency = 0;
|
||||
pclose(f);
|
||||
if (!cpufrequency) return;
|
||||
}
|
||||
|
||||
long long cpucycles_sgi(void)
|
||||
{
|
||||
double result;
|
||||
struct timespec t;
|
||||
if (!cpufrequency) init();
|
||||
clock_gettime(CLOCK_SGI_CYCLE,&t);
|
||||
result = t.tv_nsec;
|
||||
result *= 0.000000001;
|
||||
result += (double) t.tv_sec;
|
||||
result *= cpufrequency;
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_sgi_persecond(void)
|
||||
{
|
||||
if (!cpufrequency) init();
|
||||
return cpufrequency;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles sgi.h version 20070916
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_sgi_h
|
||||
#define CPUCYCLES_sgi_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_sgi(void);
|
||||
extern long long cpucycles_sgi_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "sgi"
|
||||
#define cpucycles cpucycles_sgi
|
||||
#define cpucycles_persecond cpucycles_sgi_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,16 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
long long cpucycles_sparc32cpuinfo(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile(".word 2202075136; .word 2570088480; srl %%g1,0,%L0; mov %%o4,%H0"
|
||||
: "=r" (result) : : "g1","o4");
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_sparc32cpuinfo_persecond(void)
|
||||
{
|
||||
return osfreq();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles sparc32cpuinfo.h version 20100804
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_sparc32cpuinfo_h
|
||||
#define CPUCYCLES_sparc32cpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_sparc32cpuinfo(void);
|
||||
extern long long cpucycles_sparc32cpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "sparc32cpuinfo"
|
||||
#define cpucycles cpucycles_sparc32cpuinfo
|
||||
#define cpucycles_persecond cpucycles_sparc32cpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,15 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
long long cpucycles_sparccpuinfo(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile("rd %%tick,%0" : "=r" (result));
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_sparccpuinfo_persecond(void)
|
||||
{
|
||||
return osfreq();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles sparccpuinfo.h version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_sparccpuinfo_h
|
||||
#define CPUCYCLES_sparccpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_sparccpuinfo(void);
|
||||
extern long long cpucycles_sparccpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "sparccpuinfo"
|
||||
#define cpucycles cpucycles_sparccpuinfo
|
||||
#define cpucycles_persecond cpucycles_sparccpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,77 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include "cpucycles-impl.h"
|
||||
|
||||
static long long tod(void)
|
||||
{
|
||||
struct timeval t;
|
||||
gettimeofday(&t,(struct timezone *) 0);
|
||||
return t.tv_sec * (long long) 1000000 + t.tv_usec;
|
||||
}
|
||||
|
||||
long long todstart;
|
||||
long long todend;
|
||||
long long cpustart;
|
||||
long long cpuend;
|
||||
|
||||
long long cyclespersecond;
|
||||
long long cyclespertod;
|
||||
|
||||
long long t[1001];
|
||||
|
||||
int main()
|
||||
{
|
||||
int j;
|
||||
int i;
|
||||
|
||||
if (!cpucycles()) {
|
||||
fprintf(stderr,"cpucycles() = %lld\n",cpucycles());
|
||||
return 100;
|
||||
}
|
||||
for (i = 0;i <= 1000;++i) t[i] = cpucycles();
|
||||
for (i = 0;i < 1000;++i) if (t[i] > t[i + 1]) {
|
||||
fprintf(stderr,"t[%d] = %lld\n",i,t[i]);
|
||||
fprintf(stderr,"t[%d] = %lld\n",i + 1,t[i + 1]);
|
||||
fprintf(stderr,"cpucycles_persecond() = %lld\n",cpucycles_persecond());
|
||||
return 100;
|
||||
}
|
||||
if (t[0] == t[1000]) {
|
||||
fprintf(stderr,"t[%d] = %lld\n",0,t[0]);
|
||||
fprintf(stderr,"t[%d] = %lld\n",1000,t[1000]);
|
||||
fprintf(stderr,"cpucycles_persecond() = %lld\n",cpucycles_persecond());
|
||||
return 100;
|
||||
}
|
||||
|
||||
cyclespersecond = cpucycles_persecond();
|
||||
|
||||
if (cyclespersecond <= 0) {
|
||||
fprintf(stderr,"cpucycles_persecond() = %lld\n",cyclespersecond);
|
||||
return 100;
|
||||
}
|
||||
|
||||
todstart = tod();
|
||||
cpustart = cpucycles();
|
||||
for (j = 0;j < 1000;++j) for (i = 0;i <= 1000;++i) t[i] = t[i] + i + j;
|
||||
todend = tod();
|
||||
cpuend = cpucycles();
|
||||
|
||||
todend -= todstart;
|
||||
cpuend -= cpustart;
|
||||
|
||||
cyclespertod = (long long) (((double) cpuend) * 1000000.0 / (double) todend);
|
||||
|
||||
if (cyclespertod > 10 * cyclespersecond) {
|
||||
fprintf(stderr,"cyclespertod = %lld, cyclespersecond = %lld\n",cyclespertod,cyclespersecond);
|
||||
return 100;
|
||||
}
|
||||
|
||||
for (i = 0;i <= 1000;++i) t[i] = cpucycles();
|
||||
printf("%s",cpucycles_implementation);
|
||||
printf(" %lld",cyclespersecond);
|
||||
printf(" %lld",cyclespertod);
|
||||
for (i = 0;i < 64;++i) printf(" %lld",t[i + 1] - t[i]);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "osfreq.c"
|
||||
|
||||
long long cpucycles_x86cpuinfo(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile(".byte 15;.byte 49" : "=A" (result));
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_x86cpuinfo_persecond(void)
|
||||
{
|
||||
return osfreq();
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles x86cpuinfo.h version 20100803
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_x86cpuinfo_h
|
||||
#define CPUCYCLES_x86cpuinfo_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_x86cpuinfo(void);
|
||||
extern long long cpucycles_x86cpuinfo_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "x86cpuinfo"
|
||||
#define cpucycles cpucycles_x86cpuinfo
|
||||
#define cpucycles_persecond cpucycles_x86cpuinfo_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,24 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
long long cpucycles_x86cpuspeed(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile(".byte 15;.byte 49" : "=A" (result));
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_x86cpuspeed_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;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles x86cpuspeed.h version 20090716
|
||||
Matthew Dempsky
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_x86cpuspeed_h
|
||||
#define CPUCYCLES_x86cpuspeed_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_x86cpuspeed(void);
|
||||
extern long long cpucycles_x86cpuspeed_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "x86cpuspeed"
|
||||
#define cpucycles cpucycles_x86cpuspeed
|
||||
#define cpucycles_persecond cpucycles_x86cpuspeed_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,59 +0,0 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
long long cpucycles_x86estimate(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile(".byte 15;.byte 49" : "=A" (result));
|
||||
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 double guessfreq(void)
|
||||
{
|
||||
long long tb0; long long us0;
|
||||
long long tb1; long long us1;
|
||||
|
||||
tb0 = cpucycles_x86estimate();
|
||||
us0 = microseconds();
|
||||
do {
|
||||
tb1 = cpucycles_x86estimate();
|
||||
us1 = microseconds();
|
||||
} while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
|
||||
if (tb1 <= tb0) return 0;
|
||||
tb1 -= tb0;
|
||||
us1 -= us0;
|
||||
return ((double) tb1) / (0.000001 * (double) us1);
|
||||
}
|
||||
|
||||
static double cpufrequency = 0;
|
||||
|
||||
static void init(void)
|
||||
{
|
||||
double guess1;
|
||||
double guess2;
|
||||
int loop;
|
||||
|
||||
for (loop = 0;loop < 100;++loop) {
|
||||
guess1 = guessfreq();
|
||||
guess2 = guessfreq();
|
||||
if (guess1 > 1.01 * guess2) continue;
|
||||
if (guess2 > 1.01 * guess1) continue;
|
||||
cpufrequency = 0.5 * (guess1 + guess2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
long long cpucycles_x86estimate_persecond(void)
|
||||
{
|
||||
if (!cpufrequency) init();
|
||||
return cpufrequency;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles x86estimate.h version 20070121
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_x86estimate_h
|
||||
#define CPUCYCLES_x86estimate_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_x86estimate(void);
|
||||
extern long long cpucycles_x86estimate_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "x86estimate"
|
||||
#define cpucycles cpucycles_x86estimate
|
||||
#define cpucycles_persecond cpucycles_x86estimate_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,17 +0,0 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
long long cpucycles_x86tscfreq(void)
|
||||
{
|
||||
long long result;
|
||||
asm volatile(".byte 15;.byte 49" : "=A" (result));
|
||||
return result;
|
||||
}
|
||||
|
||||
long long cpucycles_x86tscfreq_persecond(void)
|
||||
{
|
||||
long result = 0;
|
||||
size_t resultlen = sizeof(long);
|
||||
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
|
||||
return result;
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/*
|
||||
cpucycles x86tscfreq.h version 20060318
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
#ifndef CPUCYCLES_x86tscfreq_h
|
||||
#define CPUCYCLES_x86tscfreq_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern long long cpucycles_x86tscfreq(void);
|
||||
extern long long cpucycles_x86tscfreq_persecond(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef cpucycles_implementation
|
||||
#define cpucycles_implementation "x86tscfreq"
|
||||
#define cpucycles cpucycles_x86tscfreq
|
||||
#define cpucycles_persecond cpucycles_x86tscfreq_persecond
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,16 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char ch;
|
||||
int loop = 0;
|
||||
while (scanf("%c",&ch) == 1) {
|
||||
printf("0x%02x,",255 & (int) ch);
|
||||
if (++loop == 16) {
|
||||
loop = 0;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
printf("0x00\n");
|
||||
return 0;
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void nope()
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned long x[4];
|
||||
unsigned long y[4];
|
||||
int i;
|
||||
int j;
|
||||
char c;
|
||||
|
||||
signal(SIGILL,nope);
|
||||
|
||||
x[0] = 0;
|
||||
x[1] = 0;
|
||||
x[2] = 0;
|
||||
x[3] = 0;
|
||||
|
||||
asm volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0) );
|
||||
if (!x[0]) return 0;
|
||||
asm volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1) );
|
||||
|
||||
for (i = 1;i < 4;++i)
|
||||
for (j = 0;j < 4;++j) {
|
||||
c = x[i] >> (8 * j);
|
||||
if (c < 32) c = 32;
|
||||
if (c > 126) c = 126;
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
printf("-%08x-%08x\n",y[0],y[3]);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
#!/bin/sh -e
|
||||
|
||||
mkdir include
|
||||
|
||||
(
|
||||
echo x86
|
||||
echo unknown
|
||||
) | (
|
||||
while read n
|
||||
do
|
||||
okabi | (
|
||||
while read abi
|
||||
do
|
||||
okc-$abi | (
|
||||
while read c
|
||||
do
|
||||
echo "=== `date` === Trying $n.c with $c..." >&2
|
||||
rm -f cpuid.c
|
||||
cp $n.c cpuid.c || continue
|
||||
$c -o cpuid cpuid.c || continue
|
||||
$c -o cbytes cbytes.c || continue
|
||||
./cpuid > cpuid.out || continue
|
||||
echo 'static const char cpuid[] = {' > cpuid.h || continue
|
||||
./cbytes < cpuid.out >> cpuid.h || continue
|
||||
echo '} ;' >> cpuid.h || continue
|
||||
cp cpuid.h include/cpuid.h || continue
|
||||
cat cpuid.out
|
||||
exit 0
|
||||
done
|
||||
exit 111
|
||||
) && exit 0
|
||||
done
|
||||
exit 111
|
||||
) && exit 0
|
||||
done
|
||||
exit 111
|
||||
)
|
@ -1,7 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
main()
|
||||
{
|
||||
printf("unknown CPU ID\n");
|
||||
return 0;
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void nope()
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned long x[4];
|
||||
unsigned long y[4];
|
||||
int i;
|
||||
int j;
|
||||
char c;
|
||||
|
||||
signal(SIGILL,nope);
|
||||
|
||||
x[0] = 0;
|
||||
x[1] = 0;
|
||||
x[2] = 0;
|
||||
x[3] = 0;
|
||||
|
||||
asm volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0) );
|
||||
if (!x[0]) return 0;
|
||||
asm volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1) );
|
||||
|
||||
for (i = 1;i < 4;++i)
|
||||
for (j = 0;j < 4;++j) {
|
||||
c = x[i] >> (8 * j);
|
||||
if (c < 32) c = 32;
|
||||
if (c > 126) c = 126;
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
printf("-%08x-%08x\n",(unsigned int) y[0],(unsigned int) y[3]);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1 +0,0 @@
|
||||
3bd7abd4f4dce04396f2ac7cb1cff70607f692411c49a1563b037d31e1662632
|
@ -1,2 +0,0 @@
|
||||
#define CRYPTO_BYTES 32
|
||||
#define CRYPTO_KEYBYTES 32
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* 20080913
|
||||
* D. J. Bernstein
|
||||
* Public domain.
|
||||
* */
|
||||
|
||||
#include "crypto_hashblocks_sha256.h"
|
||||
#include "crypto_auth.h"
|
||||
|
||||
#define blocks crypto_hashblocks_sha256
|
||||
|
||||
typedef unsigned int uint32;
|
||||
|
||||
static const char iv[32] = {
|
||||
0x6a,0x09,0xe6,0x67,
|
||||
0xbb,0x67,0xae,0x85,
|
||||
0x3c,0x6e,0xf3,0x72,
|
||||
0xa5,0x4f,0xf5,0x3a,
|
||||
0x51,0x0e,0x52,0x7f,
|
||||
0x9b,0x05,0x68,0x8c,
|
||||
0x1f,0x83,0xd9,0xab,
|
||||
0x5b,0xe0,0xcd,0x19,
|
||||
} ;
|
||||
|
||||
int crypto_auth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
|
||||
{
|
||||
unsigned char h[32];
|
||||
unsigned char padded[128];
|
||||
int i;
|
||||
unsigned long long bits = 512 + (inlen << 3);
|
||||
|
||||
for (i = 0;i < 32;++i) h[i] = iv[i];
|
||||
|
||||
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x36;
|
||||
for (i = 32;i < 64;++i) padded[i] = 0x36;
|
||||
|
||||
blocks(h,padded,64);
|
||||
blocks(h,in,inlen);
|
||||
in += inlen;
|
||||
inlen &= 63;
|
||||
in -= inlen;
|
||||
|
||||
for (i = 0;i < inlen;++i) padded[i] = in[i];
|
||||
padded[inlen] = 0x80;
|
||||
|
||||
if (inlen < 56) {
|
||||
for (i = inlen + 1;i < 56;++i) padded[i] = 0;
|
||||
padded[56] = bits >> 56;
|
||||
padded[57] = bits >> 48;
|
||||
padded[58] = bits >> 40;
|
||||
padded[59] = bits >> 32;
|
||||
padded[60] = bits >> 24;
|
||||
padded[61] = bits >> 16;
|
||||
padded[62] = bits >> 8;
|
||||
padded[63] = bits;
|
||||
blocks(h,padded,64);
|
||||
} else {
|
||||
for (i = inlen + 1;i < 120;++i) padded[i] = 0;
|
||||
padded[120] = bits >> 56;
|
||||
padded[121] = bits >> 48;
|
||||
padded[122] = bits >> 40;
|
||||
padded[123] = bits >> 32;
|
||||
padded[124] = bits >> 24;
|
||||
padded[125] = bits >> 16;
|
||||
padded[126] = bits >> 8;
|
||||
padded[127] = bits;
|
||||
blocks(h,padded,128);
|
||||
}
|
||||
|
||||
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x5c;
|
||||
for (i = 32;i < 64;++i) padded[i] = 0x5c;
|
||||
for (i = 0;i < 32;++i) padded[64 + i] = h[i];
|
||||
|
||||
for (i = 0;i < 32;++i) out[i] = iv[i];
|
||||
|
||||
for (i = 32;i < 64;++i) padded[64 + i] = 0;
|
||||
padded[64 + 32] = 0x80;
|
||||
padded[64 + 62] = 3;
|
||||
|
||||
blocks(out,padded,128);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#include "crypto_verify_32.h"
|
||||
#include "crypto_auth.h"
|
||||
|
||||
int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
|
||||
{
|
||||
unsigned char correct[32];
|
||||
crypto_auth(correct,in,inlen,k);
|
||||
return crypto_verify_32(h,correct);
|
||||
}
|
@ -1 +0,0 @@
|
||||
2f5e8a6a0cac012d8d001351d7d583e69f91390df46305c3608e0c2893491886
|
@ -1,2 +0,0 @@
|
||||
#define CRYPTO_BYTES 32
|
||||
#define CRYPTO_KEYBYTES 32
|
@ -1,86 +0,0 @@
|
||||
/*
|
||||
* 20080913
|
||||
* D. J. Bernstein
|
||||
* Public domain.
|
||||
* */
|
||||
|
||||
#include "crypto_hashblocks_sha512.h"
|
||||
#include "crypto_auth.h"
|
||||
|
||||
#define blocks crypto_hashblocks_sha512
|
||||
|
||||
typedef unsigned long long uint64;
|
||||
|
||||
static const unsigned char iv[64] = {
|
||||
0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
|
||||
0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
|
||||
0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
|
||||
0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
|
||||
0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
|
||||
0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
|
||||
0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
|
||||
0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
|
||||
} ;
|
||||
|
||||
int crypto_auth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
|
||||
{
|
||||
unsigned char h[64];
|
||||
unsigned char padded[256];
|
||||
int i;
|
||||
unsigned long long bytes = 128 + inlen;
|
||||
|
||||
for (i = 0;i < 64;++i) h[i] = iv[i];
|
||||
|
||||
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x36;
|
||||
for (i = 32;i < 128;++i) padded[i] = 0x36;
|
||||
|
||||
blocks(h,padded,128);
|
||||
blocks(h,in,inlen);
|
||||
in += inlen;
|
||||
inlen &= 127;
|
||||
in -= inlen;
|
||||
|
||||
for (i = 0;i < inlen;++i) padded[i] = in[i];
|
||||
padded[inlen] = 0x80;
|
||||
|
||||
if (inlen < 112) {
|
||||
for (i = inlen + 1;i < 119;++i) padded[i] = 0;
|
||||
padded[119] = bytes >> 61;
|
||||
padded[120] = bytes >> 53;
|
||||
padded[121] = bytes >> 45;
|
||||
padded[122] = bytes >> 37;
|
||||
padded[123] = bytes >> 29;
|
||||
padded[124] = bytes >> 21;
|
||||
padded[125] = bytes >> 13;
|
||||
padded[126] = bytes >> 5;
|
||||
padded[127] = bytes << 3;
|
||||
blocks(h,padded,128);
|
||||
} else {
|
||||
for (i = inlen + 1;i < 247;++i) padded[i] = 0;
|
||||
padded[247] = bytes >> 61;
|
||||
padded[248] = bytes >> 53;
|
||||
padded[249] = bytes >> 45;
|
||||
padded[250] = bytes >> 37;
|
||||
padded[251] = bytes >> 29;
|
||||
padded[252] = bytes >> 21;
|
||||
padded[253] = bytes >> 13;
|
||||
padded[254] = bytes >> 5;
|
||||
padded[255] = bytes << 3;
|
||||
blocks(h,padded,256);
|
||||
}
|
||||
|
||||
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x5c;
|
||||
for (i = 32;i < 128;++i) padded[i] = 0x5c;
|
||||
|
||||
for (i = 0;i < 64;++i) padded[128 + i] = h[i];
|
||||
for (i = 0;i < 64;++i) h[i] = iv[i];
|
||||
|
||||
for (i = 64;i < 128;++i) padded[128 + i] = 0;
|
||||
padded[128 + 64] = 0x80;
|
||||
padded[128 + 126] = 6;
|
||||
|
||||
blocks(h,padded,256);
|
||||
for (i = 0;i < 32;++i) out[i] = h[i];
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#include "crypto_verify_32.h"
|
||||
#include "crypto_auth.h"
|
||||
|
||||
int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
|
||||
{
|
||||
unsigned char correct[32];
|
||||
crypto_auth(correct,in,inlen,k);
|
||||
return crypto_verify_32(h,correct);
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
#include "crypto_auth.h"
|
||||
#include "randombytes.h"
|
||||
#include "cpucycles.h"
|
||||
|
||||
extern void printentry(long long,const char *,long long *,long long);
|
||||
extern unsigned char *alignedcalloc(unsigned long long);
|
||||
extern const char *primitiveimplementation;
|
||||
extern const char *implementationversion;
|
||||
extern const char *sizenames[];
|
||||
extern const long long sizes[];
|
||||
extern void allocate(void);
|
||||
extern void measure(void);
|
||||
|
||||
const char *primitiveimplementation = crypto_auth_IMPLEMENTATION;
|
||||
const char *implementationversion = crypto_auth_VERSION;
|
||||
const char *sizenames[] = { "outputbytes", "keybytes", 0 };
|
||||
const long long sizes[] = { crypto_auth_BYTES, crypto_auth_KEYBYTES };
|
||||
|
||||
#define MAXTEST_BYTES 4096
|
||||
#ifdef SUPERCOP
|
||||
#define MGAP 8192
|
||||
#else
|
||||
#define MGAP 8
|
||||
#endif
|
||||
|
||||
static unsigned char *k;
|
||||
static unsigned char *m;
|
||||
static unsigned char *h;
|
||||
|
||||
void preallocate(void)
|
||||
{
|
||||
}
|
||||
|
||||
void allocate(void)
|
||||
{
|
||||
k = alignedcalloc(crypto_auth_KEYBYTES);
|
||||
m = alignedcalloc(MAXTEST_BYTES);
|
||||
h = alignedcalloc(crypto_auth_BYTES);
|
||||
}
|
||||
|
||||
#define TIMINGS 15
|
||||
static long long cycles[TIMINGS + 1];
|
||||
|
||||
void measure(void)
|
||||
{
|
||||
int i;
|
||||
int loop;
|
||||
int mlen;
|
||||
|
||||
for (loop = 0;loop < LOOPS;++loop) {
|
||||
for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / MGAP) {
|
||||
randombytes(k,crypto_auth_KEYBYTES);
|
||||
randombytes(m,mlen);
|
||||
randombytes(h,crypto_auth_BYTES);
|
||||
for (i = 0;i <= TIMINGS;++i) {
|
||||
cycles[i] = cpucycles();
|
||||
crypto_auth(h,m,mlen,k);
|
||||
}
|
||||
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
|
||||
printentry(mlen,"cycles",cycles,TIMINGS);
|
||||
for (i = 0;i <= TIMINGS;++i) {
|
||||
cycles[i] = cpucycles();
|
||||
crypto_auth_verify(h,m,mlen,k);
|
||||
}
|
||||
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
|
||||
printentry(mlen,"verify_cycles",cycles,TIMINGS);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,119 +0,0 @@
|
||||
/*
|
||||
* crypto_auth/try.c version 20090118
|
||||
* D. J. Bernstein
|
||||
* Public domain.
|
||||
*/
|
||||
|
||||
#include "crypto_hash_sha256.h"
|
||||
#include "crypto_auth.h"
|
||||
|
||||
extern unsigned char *alignedcalloc(unsigned long long);
|
||||
|
||||
const char *primitiveimplementation = crypto_auth_IMPLEMENTATION;
|
||||
|
||||
#define MAXTEST_BYTES 10000
|
||||
#define CHECKSUM_BYTES 4096
|
||||
#define TUNE_BYTES 1536
|
||||
|
||||
static unsigned char *h;
|
||||
static unsigned char *m;
|
||||
static unsigned char *k;
|
||||
static unsigned char *h2;
|
||||
static unsigned char *m2;
|
||||
static unsigned char *k2;
|
||||
|
||||
void preallocate(void)
|
||||
{
|
||||
}
|
||||
|
||||
void allocate(void)
|
||||
{
|
||||
h = alignedcalloc(crypto_auth_BYTES);
|
||||
m = alignedcalloc(MAXTEST_BYTES);
|
||||
k = alignedcalloc(crypto_auth_KEYBYTES);
|
||||
h2 = alignedcalloc(crypto_auth_BYTES);
|
||||
m2 = alignedcalloc(MAXTEST_BYTES + crypto_auth_BYTES);
|
||||
k2 = alignedcalloc(crypto_auth_KEYBYTES + crypto_auth_BYTES);
|
||||
}
|
||||
|
||||
void predoit(void)
|
||||
{
|
||||
}
|
||||
|
||||
void doit(void)
|
||||
{
|
||||
crypto_auth(h,m,TUNE_BYTES,k);
|
||||
crypto_auth_verify(h,m,TUNE_BYTES,k);
|
||||
}
|
||||
|
||||
char checksum[crypto_auth_BYTES * 2 + 1];
|
||||
|
||||
const char *checksum_compute(void)
|
||||
{
|
||||
long long i;
|
||||
long long j;
|
||||
|
||||
for (i = 0;i < CHECKSUM_BYTES;++i) {
|
||||
long long mlen = i;
|
||||
long long klen = crypto_auth_KEYBYTES;
|
||||
long long hlen = crypto_auth_BYTES;
|
||||
|
||||
for (j = -16;j < 0;++j) h[j] = random();
|
||||
for (j = -16;j < 0;++j) k[j] = random();
|
||||
for (j = -16;j < 0;++j) m[j] = random();
|
||||
for (j = hlen;j < hlen + 16;++j) h[j] = random();
|
||||
for (j = klen;j < klen + 16;++j) k[j] = random();
|
||||
for (j = mlen;j < mlen + 16;++j) m[j] = random();
|
||||
for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
|
||||
for (j = -16;j < klen + 16;++j) k2[j] = k[j];
|
||||
for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
|
||||
|
||||
if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
|
||||
|
||||
for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
|
||||
for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
|
||||
for (j = -16;j < 0;++j) if (h[j] != h2[j]) return "crypto_auth writes before output";
|
||||
for (j = hlen;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth writes after output";
|
||||
|
||||
for (j = -16;j < 0;++j) h[j] = random();
|
||||
for (j = -16;j < 0;++j) k[j] = random();
|
||||
for (j = -16;j < 0;++j) m[j] = random();
|
||||
for (j = hlen;j < hlen + 16;++j) h[j] = random();
|
||||
for (j = klen;j < klen + 16;++j) k[j] = random();
|
||||
for (j = mlen;j < mlen + 16;++j) m[j] = random();
|
||||
for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
|
||||
for (j = -16;j < klen + 16;++j) k2[j] = k[j];
|
||||
for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
|
||||
|
||||
if (crypto_auth(m2,m2,mlen,k) != 0) return "crypto_auth returns nonzero";
|
||||
for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_auth does not handle m overlap";
|
||||
for (j = 0;j < hlen;++j) m2[j] = m[j];
|
||||
if (crypto_auth(k2,m2,mlen,k2) != 0) return "crypto_auth returns nonzero";
|
||||
for (j = 0;j < hlen;++j) if (k2[j] != h[j]) return "crypto_auth does not handle k overlap";
|
||||
for (j = 0;j < hlen;++j) k2[j] = k[j];
|
||||
|
||||
if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
|
||||
|
||||
for (j = -16;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth overwrites h";
|
||||
for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
|
||||
for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
|
||||
|
||||
crypto_hash_sha256(h2,h,hlen);
|
||||
for (j = 0;j < klen;++j) k[j] ^= h2[j % 32];
|
||||
if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
|
||||
if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
|
||||
|
||||
crypto_hash_sha256(h2,h,hlen);
|
||||
for (j = 0;j < mlen;++j) m[j] ^= h2[j % 32];
|
||||
m[mlen] = h2[0];
|
||||
}
|
||||
if (crypto_auth(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth returns nonzero";
|
||||
if (crypto_auth_verify(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth_verify returns nonzero";
|
||||
|
||||
for (i = 0;i < crypto_auth_BYTES;++i) {
|
||||
checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
|
||||
checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
|
||||
}
|
||||
checksum[2 * i] = 0;
|
||||
return 0;
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
#include <string>
|
||||
using std::string;
|
||||
#include "crypto_auth.h"
|
||||
|
||||
string crypto_auth(const string &m,const string &k)
|
||||
{
|
||||
if (k.size() != crypto_auth_KEYBYTES) throw "incorrect key length";
|
||||
unsigned char a[crypto_auth_BYTES];
|
||||
crypto_auth(a,(const unsigned char *) m.c_str(),m.size(),(const unsigned char *) k.c_str());
|
||||
return string((char *) a,crypto_auth_BYTES);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user