Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/CPUInfo/CPUInfo.cpp
Views: 1798
/*1test CPU speed2Andrew Tridgell September 20113*/45#define ALLOW_DOUBLE_MATH_FUNCTIONS67#include <cmath>89#include <AP_HAL/AP_HAL.h>10#include <AP_Common/AP_Common.h>11#include <AP_Math/AP_Math.h>12#include <AP_Math/div1000.h>13#include <AP_ESC_Telem/AP_ESC_Telem.h>14#include "EKF_Maths.h"1516#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS17#if HAL_WITH_DSP18#include <arm_math.h>19#endif20#include <hrt.h>21#include <ch.h>22#endif // HAL_BOARD_CHIBIOS2324void setup();25void loop();2627const AP_HAL::HAL& hal = AP_HAL::get_HAL();2829// On H750 we want to measure external flash to ram performance30#if defined(EXT_FLASH_SIZE_MB) && EXT_FLASH_SIZE_MB>0 && defined(STM32H7)31#include "ch.h"32#define DISABLE_CACHES33#endif3435#ifdef STM32_SYS_CK36static uint32_t sysclk = STM32_SYS_CK;37#elif defined(STM32_SYSCLK)38static uint32_t sysclk = STM32_SYSCLK;39#else40static uint32_t sysclk = 0;41#endif4243static EKF_Maths ekf;4445HAL_Semaphore sem;46#if HAL_WITH_ESC_TELEM47AP_ESC_Telem telem;48#endif4950void setup() {51#ifdef DISABLE_CACHES52#if !HAL_XIP_ENABLED // can't disable DCache in memory-mapped mode53SCB_DisableDCache();54#endif55SCB_DisableICache();56#endif57ekf.init();58}5960static void show_sizes(void)61{62hal.console->printf("SYSCLK %uMHz\n", unsigned(sysclk/1000000U));6364hal.console->printf("Type sizes:\n");65hal.console->printf("char : %lu\n", (unsigned long)sizeof(char));66hal.console->printf("short : %lu\n", (unsigned long)sizeof(short));67hal.console->printf("int : %lu\n", (unsigned long)sizeof(int));68hal.console->printf("long : %lu\n", (unsigned long)sizeof(long));69hal.console->printf("long long : %lu\n", (unsigned long)sizeof(long long));70hal.console->printf("bool : %lu\n", (unsigned long)sizeof(bool));71hal.console->printf("void* : %lu\n", (unsigned long)sizeof(void *));7273hal.console->printf("printing NaN: %f\n", (double)sqrtf(-1.0f));74hal.console->printf("printing +Inf: %f\n", (double)(1.0f/0.0f));75hal.console->printf("printing -Inf: %f\n", (double)(-1.0f/0.0f));76}7778#define TENTIMES(x) do { x; x; x; x; x; x; x; x; x; x; } while (0)79#define FIFTYTIMES(x) do { TENTIMES(x); TENTIMES(x); TENTIMES(x); TENTIMES(x); TENTIMES(x); } while (0)8081#define TIMEIT(name, op, count) do { \82uint16_t us_end, us_start; \83us_start = AP_HAL::micros16(); \84for (uint8_t i = 0; i < count; i++) { \85FIFTYTIMES(op); \86} \87us_end = AP_HAL::micros16(); \88uint16_t dt_us = us_end - us_start; \89hal.console->printf("%-10s %7.4f usec/call\n", name, double(dt_us) / double(count * 50.0)); \90hal.scheduler->delay(10); \91} while (0)9293volatile float v_f = 1.0;94volatile float v_out;95volatile double v_d = 1.0;96volatile double v_out_d;97volatile uint32_t v_32 = 1;98volatile uint32_t v_out_32 = 1;99volatile uint16_t v_16 = 1;100volatile uint16_t v_out_16 = 1;101volatile uint8_t v_8 = 1;102volatile uint8_t v_out_8 = 1;103volatile uint8_t mbuf1[128], mbuf2[128];104volatile uint64_t v_64 = 1;105volatile uint64_t v_out_64 = 1;106107#pragma GCC diagnostic error "-Wframe-larger-than=2000"108static void show_timings(void)109{110111v_f = 1+(AP_HAL::micros() % 5);112v_out = 1+(AP_HAL::micros() % 3);113114v_32 = AP_HAL::millis();115v_32 = 1+(AP_HAL::micros() % 5);116v_out_32 = 1+(AP_HAL::micros() % 3);117118v_16 = 1+(AP_HAL::micros() % 5);119v_out_16 = 1+(AP_HAL::micros() % 3);120121v_8 = 1+(AP_HAL::micros() % 5);122v_out_8 = 1+(AP_HAL::micros() % 3);123124125hal.console->printf("Operation timings:\n");126hal.console->printf("Note: timings for some operations are very data dependent\n");127128TIMEIT("nop", asm volatile("nop"::), 255);129130TIMEIT("micros()", AP_HAL::micros(), 200);131TIMEIT("micros16()", AP_HAL::micros16(), 200);132TIMEIT("millis()", AP_HAL::millis(), 200);133TIMEIT("millis16()", AP_HAL::millis16(), 200);134TIMEIT("micros64()", AP_HAL::micros64(), 200);135136#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS137TIMEIT("hrt_micros32()", hrt_micros32(), 200);138TIMEIT("hrt_micros64()", hrt_micros64(), 200);139TIMEIT("hrt_millis32()", hrt_millis32(), 200);140TIMEIT("hrt_millis64()", hrt_millis64(), 200);141#endif142143TIMEIT("fadd", v_out += v_f, 100);144TIMEIT("fsub", v_out -= v_f, 100);145TIMEIT("fmul", v_out *= v_f, 100);146TIMEIT("fdiv /=", v_out /= v_f, 100);147TIMEIT("fdiv 2/x", v_out = 2.0f/v_f, 100);148149TIMEIT("dadd", v_out_d += v_d, 100);150TIMEIT("dsub", v_out_d -= v_d, 100);151TIMEIT("dmul", v_out_d *= v_d, 100);152TIMEIT("ddiv", v_out_d /= v_d, 100);153154TIMEIT("sinf()", v_out = sinf(v_f), 100);155TIMEIT("cosf()", v_out = cosf(v_f), 100);156#if HAL_WITH_DSP && CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS157TIMEIT("arm_sin_f32()", v_out = arm_sin_f32(v_f), 100);158TIMEIT("arm_cos_f32()", v_out = arm_cos_f32(v_f), 100);159#endif160TIMEIT("tanf()", v_out = tanf(v_f), 100);161TIMEIT("acosf()", v_out = acosf(v_f * 0.2), 100);162TIMEIT("asinf()", v_out = asinf(v_f * 0.2), 100);163TIMEIT("atan2f()", v_out = atan2f(v_f * 0.2, v_f * 0.3), 100);164TIMEIT("sqrtf()",v_out = sqrtf(v_f), 100);165166TIMEIT("sin()", v_out = sin(v_f), 100);167TIMEIT("cos()", v_out = cos(v_f), 100);168TIMEIT("tan()", v_out = tan(v_f), 100);169TIMEIT("acos()", v_out = acos(v_f * 0.2), 100);170TIMEIT("asin()", v_out = asin(v_f * 0.2), 100);171TIMEIT("atan2()", v_out = atan2(v_f * 0.2, v_f * 0.3), 100);172TIMEIT("sqrt()",v_out = sqrt(v_f), 100);173#if HAL_WITH_DSP && CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS174TIMEIT("arm_sqrt_f32()", arm_sqrt_f32(v_f, (float32_t*)&v_out), 100);175#endif176TIMEIT("sq()",v_out = sq(v_f), 100);177TIMEIT("powf(v,2)",v_out = powf(v_f, 2), 100);178TIMEIT("powf(v,3.1)",v_out = powf(v_f, 3.1), 100);179TIMEIT("EKF",v_out = ekf.test(), 5);180181TIMEIT("iadd8", v_out_8 += v_8, 100);182TIMEIT("isub8", v_out_8 -= v_8, 100);183TIMEIT("imul8", v_out_8 *= v_8, 100);184TIMEIT("idiv8", v_out_8 /= v_8, 100);185186TIMEIT("iadd16", v_out_16 += v_16, 100);187TIMEIT("isub16", v_out_16 -= v_16, 100);188TIMEIT("imul16", v_out_16 *= v_16, 100);189TIMEIT("idiv16", v_out_16 /= v_16, 100);190191TIMEIT("iadd32", v_out_32 += v_32, 100);192TIMEIT("isub32", v_out_32 -= v_32, 100);193TIMEIT("imul32", v_out_32 *= v_32, 100);194TIMEIT("idiv32", v_out_32 /= v_32, 100);195196TIMEIT("iadd64", v_out_64 += v_64, 100);197TIMEIT("isub64", v_out_64 -= v_64, 100);198TIMEIT("imul64", v_out_64 *= v_64, 100);199TIMEIT("idiv64", v_out_64 /= v_64, 100);200201TIMEIT("memcpy128", memcpy((void*)mbuf1, (const void *)mbuf2, sizeof(mbuf1)); v_out_8 += mbuf1[0], 200);202TIMEIT("memset128", memset((void*)mbuf1, 1, sizeof(mbuf1)); v_out_8 += mbuf1[0], 200);203TIMEIT("delay(1)", hal.scheduler->delay(1), 5);204205TIMEIT("SEM", { WITH_SEMAPHORE(sem); v_out_32 += v_32;}, 100);206}207208static void test_div1000(void)209{210hal.console->printf("Testing div1000\n");211for (uint32_t i=0; i<2000000; i++) {212uint64_t v = 0;213if (!hal.util->get_random_vals((uint8_t*)&v, sizeof(v))) {214AP_HAL::panic("ERROR: div1000 no random");215break;216}217uint64_t v1 = v / 1000ULL;218uint64_t v2 = uint64_div1000(v);219if (v1 != v2) {220AP_HAL::panic("ERROR: 0x%llx v1=0x%llx v2=0x%llx",221(unsigned long long)v, (unsigned long long)v1, (unsigned long long)v2);222return;223}224}225#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS226// test from locked context227for (uint32_t i=0; i<2000000; i++) {228uint64_t v = 0;229if (!hal.util->get_random_vals((uint8_t*)&v, sizeof(v))) {230AP_HAL::panic("ERROR: div1000 no random");231break;232}233chSysLock();234uint64_t v1 = v / 1000ULL;235uint64_t v2 = uint64_div1000(v);236chSysUnlock();237if (v1 != v2) {238AP_HAL::panic("ERROR: 0x%llx v1=0x%llx v2=0x%llx",239(unsigned long long)v, (unsigned long long)v1, (unsigned long long)v2);240return;241}242}243#endif244hal.console->printf("div1000 OK\n");245}246247void loop()248{249show_sizes();250hal.console->printf("\n");251show_timings();252test_div1000();253hal.console->printf("\n");254hal.scheduler->delay(3000);255}256257AP_HAL_MAIN();258259260