<commit_before>/**
 * @file CosaSwitch.ino
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2014, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * @section Description
 * Testing code generation of switch statements.
 *
 * This file is part of the Arduino Che Cosa project.
 */

#include "Cosa/RTC.hh"
#include "Cosa/Watchdog.hh"
#include "Cosa/Trace.hh"
#include "Cosa/IOStream/Driver/UART.hh"

enum {
  OP_ADD,
  OP_SUB,
  OP_MUL,
  OP_DIV,
  OP_REM,
  OP_LOAD,
  OP_STORE,
  OP_BRA,
  OP_BRZ,
  OP_CALL,
  OP_EXIT
};

// Traditional switch statement will compile to if-else if
#define DISPATCH(op) switch (op)
#define OP(x) case OP_ ## x:
int switch_fn(uint8_t op)
{
  int res = 0;
  DISPATCH(op) {
    OP(ADD)
      res = 1;
    break;
    OP(SUB)
      res = 2;
    break;
    OP(MUL)
      res = 3;
    break;
    OP(DIV)
      res = 4;
    break;
    OP(REM)
      res = 5;
    break;
    OP(LOAD)
      res = 6;
    break;
    OP(STORE)
      res = 7;
    break;
    OP(BRA)
      res = 8;
    break;
    OP(BRZ)
      res = 9;
    break;
    OP(CALL)
      res = 10;
    break;
    OP(EXIT)
      res = 11;
    break;
  }
  return (res);
}
#undef OP
#undef DISPATCH

// Table driven switch statement using gcc goto extension and program memory table
#define OP(x) DO_ ## x:
#define DISPATCH(op) goto *pgm_read_word(&tab[op]); for (uint8_t i = 0; i < 1; i++)
int goto_fn(uint8_t op)
{
  // Label address table
  static const void* tab[] __PROGMEM = {
    &&DO_ADD,
    &&DO_SUB,
    &&DO_MUL,
    &&DO_DIV,
    &&DO_REM,
    &&DO_LOAD,
    &&DO_STORE,
    &&DO_BRA,
    &&DO_BRZ,
    &&DO_CALL,
    &&DO_EXIT
  };
  int res = 0;

  // Switch implementation with table and do-while(0) loop to allow break
  DISPATCH(op) {
    OP(ADD)
      res = 1;
    break;
    OP(SUB)
      res = 2;
    break;
    OP(MUL)
      res = 3;
    break;
    OP(DIV)
      res = 4;
    break;
    OP(REM)
      res = 5;
    break;
    OP(LOAD)
      res = 6;
    break;
    OP(STORE)
      res = 7;
    break;
    OP(BRA)
      res = 8;
    break;
    OP(BRZ)
      res = 9;
    break;
    OP(CALL)
      res = 10;
    break;
    OP(EXIT)
      res = 11;
    break;
  }
  return (res);
}

void setup()
{
  uart.begin(9600);
  trace.begin(&uart, PSTR("CosaGoto: started"));
  Watchdog::begin();
  RTC::begin();
}

void loop()
{
  uint32_t start, stop, us;
  uint8_t* switch_fn_addr = (uint8_t*) switch_fn;
  uint8_t* goto_fn_addr = (uint8_t*) goto_fn;
  uint8_t* loop_addr = (uint8_t*) loop;
  size_t switch_fn_size = goto_fn_addr - switch_fn_addr;
  size_t goto_fn_size = loop_addr - goto_fn_addr;
  int res;

  // Measure traditional switch statement version
  TRACE(switch_fn_addr);
  TRACE(switch_fn_size);
  for (uint8_t op = OP_ADD; op <= OP_EXIT; op++) 
    TRACE(switch_fn(op));
  start = RTC::micros();
  res = 0;
  for (int i = 0; i < 1000; i++)
    for (uint8_t op = OP_ADD; op <= OP_EXIT; op++)
      res += switch_fn(op);<commit_msg>Fixed trace setup banner; wrong example file name.<commit_after>/**
 * @file CosaSwitch.ino
 * @version 1.0
 *
 * @section License
 * Copyright (C) 2014, <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * @section Description
 * Testing code generation of switch statements.
 *
 * This file is part of the Arduino Che Cosa project.
 */

#include "Cosa/RTC.hh"
#include "Cosa/Watchdog.hh"
#include "Cosa/Trace.hh"
#include "Cosa/IOStream/Driver/UART.hh"

enum {
  OP_ADD,
  OP_SUB,
  OP_MUL,
  OP_DIV,
  OP_REM,
  OP_LOAD,
  OP_STORE,
  OP_BRA,
  OP_BRZ,
  OP_CALL,
  OP_EXIT
};

// Traditional switch statement will compile to if-else if
#define DISPATCH(op) switch (op)
#define OP(x) case OP_ ## x:
int switch_fn(uint8_t op)
{
  int res = 0;
  DISPATCH(op) {
    OP(ADD)
      res = 1;
    break;
    OP(SUB)
      res = 2;
    break;
    OP(MUL)
      res = 3;
    break;
    OP(DIV)
      res = 4;
    break;
    OP(REM)
      res = 5;
    break;
    OP(LOAD)
      res = 6;
    break;
    OP(STORE)
      res = 7;
    break;
    OP(BRA)
      res = 8;
    break;
    OP(BRZ)
      res = 9;
    break;
    OP(CALL)
      res = 10;
    break;
    OP(EXIT)
      res = 11;
    break;
  }
  return (res);
}
#undef OP
#undef DISPATCH

// Table driven switch statement using gcc goto extension and program memory table
#define OP(x) DO_ ## x:
#define DISPATCH(op) goto *pgm_read_word(&tab[op]); for (uint8_t i = 0; i < 1; i++)
int goto_fn(uint8_t op)
{
  // Label address table
  static const void* tab[] __PROGMEM = {
    &&DO_ADD,
    &&DO_SUB,
    &&DO_MUL,
    &&DO_DIV,
    &&DO_REM,
    &&DO_LOAD,
    &&DO_STORE,
    &&DO_BRA,
    &&DO_BRZ,
    &&DO_CALL,
    &&DO_EXIT
  };
  int res = 0;

  // Switch implementation with table and do-while(0) loop to allow break
  DISPATCH(op) {
    OP(ADD)
      res = 1;
    break;
    OP(SUB)
      res = 2;
    break;
    OP(MUL)
      res = 3;
    break;
    OP(DIV)
      res = 4;
    break;
    OP(REM)
      res = 5;
    break;
    OP(LOAD)
      res = 6;
    break;
    OP(STORE)
      res = 7;
    break;
    OP(BRA)
      res = 8;
    break;
    OP(BRZ)
      res = 9;
    break;
    OP(CALL)
      res = 10;
    break;
    OP(EXIT)
      res = 11;
    break;
  }
  return (res);
}

void setup()
{
  uart.begin(9600);
  trace.begin(&uart, PSTR("CosaSwitch: started"));
  Watchdog::begin();
  RTC::begin();
}

void loop()
{
  uint32_t start, stop, us;
  uint8_t* switch_fn_addr = (uint8_t*) switch_fn;
  uint8_t* goto_fn_addr = (uint8_t*) goto_fn;
  uint8_t* loop_addr = (uint8_t*) loop;
  size_t switch_fn_size = goto_fn_addr - switch_fn_addr;
  size_t goto_fn_size = loop_addr - goto_fn_addr;
  int res;

  // Measure traditional switch statement version
  TRACE(switch_fn_addr);
  TRACE(switch_fn_size);
  for (uint8_t op = OP_ADD; op <= OP_EXIT; op++) 
    TRACE(switch_fn(op));
  start = RTC::micros();
  res = 0;
  for (int i = 0; i < 1000; i++)
    for (uint8_t op = OP_ADD; op <= OP_EXIT; op++)
      res += switch_fn(op);
{'fraction_non_alphanumeric': 0.11970006816632583, 'fraction_numerical': 0.0207225630538514, 'mean_word_length': 2.525228255646324, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7648098', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2737, 'n_words': 844}