From 13b2ac642a8476f1ed06234de11428fe6136ee1c Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 1 Mar 2016 21:36:53 +0300 Subject: [PATCH 1/6] Add makefile --- Makefile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..53f0afd --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +CPPFLAGS=-Wall -O2 + +main: main.c From cdb273007752665586549715114bc5dfab6a6f10 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 1 Mar 2016 21:38:50 +0300 Subject: [PATCH 2/6] Fix compilation on 32bit --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 53f0afd..0a9b996 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ -CPPFLAGS=-Wall -O2 +CPPFLAGS=-Wall -O2 -msse2 main: main.c From 47dcbe79491c57be0d3266ba78687fb1f354bc2c Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 1 Mar 2016 22:08:43 +0300 Subject: [PATCH 3/6] add make clean target --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index 0a9b996..3c1b7b5 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ CPPFLAGS=-Wall -O2 -msse2 main: main.c + +.PHONY: clean + +clean: + rm -f main From 3b662e133e81f4bcfc9836c6ae5cdbdd0ddeeeac Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 1 Mar 2016 22:27:35 +0300 Subject: [PATCH 4/6] Compiles on both linux and mach --- main.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/main.c b/main.c index 5218224..31d59c2 100644 --- a/main.c +++ b/main.c @@ -4,10 +4,44 @@ #include #include +#include +#include +#ifdef __MACH__ #include #include +#endif #include +#ifndef NSEC_PER_SEC +#define NSEC_PER_SEC 1000000000ull +#endif + +#ifndef __MACH__ +typedef struct timespec mach_timespec_t; +typedef unsigned int mach_port_t; + +static inline uint64_t mach_absolute_time(void) { + mach_timespec_t tp; + int res = clock_gettime(CLOCK_MONOTONIC_RAW, &tp); + if (res < 0) { + perror("clock_gettime"); + exit(1); + } + uint64_t result = tp.tv_sec * NSEC_PER_SEC; + result += tp.tv_nsec; + return result; +} + +static inline void nanosleep_for(time_t sec, long nsec, mach_timespec_t *remain) { + mach_timespec_t req = { sec, nsec }; + int res = nanosleep(&req, remain); + if (res < 0) { + perror("nanosleep"); + exit(1); + } +} +#endif // __MACH__ + __m128i reg; __m128i reg_zero; __m128i reg_one; @@ -28,17 +62,23 @@ static inline void square_am_signal(float time, float frequency) { _mm_stream_si128(®, reg_one); _mm_stream_si128(®, reg_zero); } +#ifdef __MACH__ clock_sleep_trap(clock_port, TIME_ABSOLUTE, reset / NSEC_PER_SEC, reset % NSEC_PER_SEC, &remain); +#else + nanosleep_for((period / 2) / NSEC_PER_SEC, (period / 2) % NSEC_PER_SEC, &remain); +#endif start = reset; } } int main() { +#ifdef __MACH__ mach_timebase_info_data_t theTimeBaseInfo; mach_timebase_info(&theTimeBaseInfo); puts("TESTING TIME BASE: the following should be 1 / 1"); printf(" Mach base: %u / %u nanoseconds\n\n", theTimeBaseInfo.numer, theTimeBaseInfo.denom); +#endif uint64_t start = mach_absolute_time(); uint64_t end = mach_absolute_time(); From 79e708aaf3a168104129ef4ba607781bb32ce6c9 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 1 Mar 2016 22:29:27 +0300 Subject: [PATCH 5/6] Fix compilation warning --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 31d59c2..39a8345 100644 --- a/main.c +++ b/main.c @@ -82,7 +82,7 @@ int main() uint64_t start = mach_absolute_time(); uint64_t end = mach_absolute_time(); - printf("TESTING TIME TO EXECUTE mach_absolute_time()\n Result: %lld nanoseconds\n\n", end - start); + printf("TESTING TIME TO EXECUTE mach_absolute_time()\n Result: %"PRIu64" nanoseconds\n\n", end - start); reg_zero = _mm_set_epi32(0, 0, 0, 0); reg_one = _mm_set_epi32(-1, -1, -1, -1); From 522abdf570d0e5a949b86f9c2822d5ae4e03a728 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Wed, 2 Mar 2016 20:02:36 +0300 Subject: [PATCH 6/6] Use absolute time as per PR#3. https://github.com/fulldecent/system-bus-radio/pull/3 --- main.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 39a8345..3ff20bb 100644 --- a/main.c +++ b/main.c @@ -17,12 +17,13 @@ #endif #ifndef __MACH__ +#define TIME_ABSOLUTE CLOCK_REALTIME typedef struct timespec mach_timespec_t; typedef unsigned int mach_port_t; static inline uint64_t mach_absolute_time(void) { mach_timespec_t tp; - int res = clock_gettime(CLOCK_MONOTONIC_RAW, &tp); + int res = clock_gettime(CLOCK_REALTIME, &tp); if (res < 0) { perror("clock_gettime"); exit(1); @@ -32,11 +33,12 @@ static inline uint64_t mach_absolute_time(void) { return result; } -static inline void nanosleep_for(time_t sec, long nsec, mach_timespec_t *remain) { +// non-conformant wrapper just for the purposes of this application +static inline void clock_sleep_trap(mach_port_t clock_port, int sleep_type, time_t sec, long nsec, mach_timespec_t *remain) { mach_timespec_t req = { sec, nsec }; - int res = nanosleep(&req, remain); + int res = clock_nanosleep(sleep_type, TIMER_ABSTIME, &req, remain); if (res < 0) { - perror("nanosleep"); + perror("clock_nanosleep"); exit(1); } } @@ -62,11 +64,7 @@ static inline void square_am_signal(float time, float frequency) { _mm_stream_si128(®, reg_one); _mm_stream_si128(®, reg_zero); } -#ifdef __MACH__ clock_sleep_trap(clock_port, TIME_ABSOLUTE, reset / NSEC_PER_SEC, reset % NSEC_PER_SEC, &remain); -#else - nanosleep_for((period / 2) / NSEC_PER_SEC, (period / 2) % NSEC_PER_SEC, &remain); -#endif start = reset; } }