Example #1
0
void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf) {
	msg_init(msg, type, handle->device);
	format_int(&(msg[2]), handle->device->read_buffer_size, 2, MP_LITTLE_ENDIAN);
	format_int(&(msg[4]), addr, 3, MP_LITTLE_ENDIAN);
	msg_send(handle, msg, 18);
	msg_recv(handle, buf, handle->device->read_buffer_size);
}
Example #2
0
void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf) {
	msg_init(msg, type, handle->device);
	format_int(&(msg[2]), handle->device->write_buffer_size, 2, MP_LITTLE_ENDIAN);
	format_int(&(msg[4]), addr, 3, MP_LITTLE_ENDIAN);
	memcpy(&(msg[7]), buf, handle->device->write_buffer_size);
	msg_send(handle, msg, 7 + handle->device->write_buffer_size);
}
Example #3
0
void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf, unsigned int len) {
	msg_init(msg, type, handle->device, handle->icsp);
	format_int(&(msg[2]), len, 2, MP_LITTLE_ENDIAN);
	format_int(&(msg[4]), addr, 3, MP_LITTLE_ENDIAN);
	memcpy(&(msg[7]), buf, len);
	msg_send(handle, msg, 7 + len);
}
Example #4
0
int stlink2_swim_write_range(programmer_t *pgm, stm8_device_t *device, unsigned char *buffer, unsigned int start, unsigned int length, const memtype_t memtype) {
	stlink2_init_session(pgm);

	stlink2_write_byte(pgm, 0x00, device->regs.CLK_CKDIVR);
    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x00, device->regs.FLASH_IAPSR);
    }

    if(memtype == FLASH) {
        stlink2_write_byte(pgm, 0x56, device->regs.FLASH_PUKR);
        stlink2_write_byte(pgm, 0xae, device->regs.FLASH_PUKR); 
    }
    if(memtype == EEPROM || memtype == OPT) {
        stlink2_write_byte(pgm, 0xae, device->regs.FLASH_DUKR);
        stlink2_write_byte(pgm, 0x56, device->regs.FLASH_DUKR);
    }

    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x56, device->regs.FLASH_IAPSR); // mov 0x56, FLASH_IAPSR
    }

	int i;
	int BLOCK_SIZE = device->flash_block_size;
	for(i = 0; i < length; i+=BLOCK_SIZE) {
        if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
            // stlink2_write_word(pgm, 0x01fe, device->regs.FLASH_CR2); // mov 0x01fe, FLASH_CR2
            stlink2_write_byte(pgm, 0x01, device->regs.FLASH_CR2); // mov 0x01fe, FLASH_CR2; 0x817e - enable write OPT bytes
            if(device->regs.FLASH_NCR2 != 0) { // Device have FLASH_NCR2 register
                stlink2_write_byte(pgm, 0xFE, device->regs.FLASH_NCR2);
            }
        }

		// The first 8 packet bytes are getting transmitted
		// with the same USB bulk transfer as the command itself
		msg_init(cmd_buf, 0xf40a);
		format_int(&(cmd_buf[2]), BLOCK_SIZE, 2, MP_BIG_ENDIAN);
		format_int(&(cmd_buf[6]), start + i, 2, MP_BIG_ENDIAN);
		memcpy(&(cmd_buf[8]), &(buffer[i]), 8);
		msg_send(pgm, cmd_buf, sizeof(cmd_buf));

		// Transmitting the rest
		msg_send(pgm, &(buffer[i + 8]), BLOCK_SIZE - 8);

		// Waiting for the transfer to process
		TRY(128, HI(stlink2_get_status(pgm)) == BLOCK_SIZE);

        if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
            stlink2_wait_until_transfer_completes(pgm, device);
        }
	}
    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x56, device->regs.FLASH_IAPSR); // mov 0x56, FLASH_IAPSR
    }
	stlink2_write_byte(pgm, 0x00, 0x7f80);
	stlink2_write_byte(pgm, 0xb6, 0x7f80);
	stlink2_finish_session(pgm);
	return(length);
}
format_fixedpoint::format_fixedpoint(t_int64 p_val,unsigned p_point)
{
	unsigned div = 1;
	for(unsigned n=0;n<p_point;n++) div *= 10;

	if (p_val < 0) {m_buffer << "-";p_val = -p_val;}

	
	m_buffer << format_int(p_val / div) << "." << format_int(p_val % div, p_point);
}
Example #6
0
void __assert_int_equal(const char* spec, int expected, int actual, int line, const char* file){
    char buf1[31];
    char buf2[31];
    if(expected!=actual){
        format_int(expected, buf1, 31);
        format_int(actual, buf2, 31);
        printf(fail_message_equal, spec, buf1, buf2, line, file);
    }
    int pass = expected == actual;
    _TEST_CASE_REPORT_RESULT(pass);
}
Example #7
0
static void msg_init(unsigned char *out_buf, unsigned char cmd, device_t *device) {
	out_buf[0] = cmd;
	out_buf[1] = device->protocol_id;
	out_buf[2] = device->variant;
	out_buf[3] = 0x00;
	out_buf[4] = device->data_memory_size >> 8 & 0xFF;

	format_int(&(msg[5]), device->opts1, 2, MP_LITTLE_ENDIAN);
	msg[8] = msg[6];
	format_int(&(msg[6]), device->opts2, 2, MP_LITTLE_ENDIAN);
	format_int(&(msg[9]), device->opts3, 2, MP_LITTLE_ENDIAN);

}
/*
 * Note: This code assumes that 'nanos' has the same sign as 'sec',
 * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
 * and not -0.8 seconds.  This is a pretty pedantic point, as we're
 * unlikely to encounter many real files created before Jan 1, 1970,
 * much less ones with timestamps recorded to sub-second resolution.
 */
static void
add_pax_attr_time(struct archive_string *as, const char *key,
    int64_t sec, unsigned long nanos)
{
	int digit, i;
	char *t;
	/*
	 * Note that each byte contributes fewer than 3 base-10
	 * digits, so this will always be big enough.
	 */
	char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];

	tmp[sizeof(tmp) - 1] = 0;
	t = tmp + sizeof(tmp) - 1;

	/* Skip trailing zeros in the fractional part. */
	for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
		digit = nanos % 10;
		nanos /= 10;
	}

	/* Only format the fraction if it's non-zero. */
	if (i > 0) {
		while (i > 0) {
			*--t = "0123456789"[digit];
			digit = nanos % 10;
			nanos /= 10;
			i--;
		}
		*--t = '.';
	}
	t = format_int(t, sec);

	add_pax_attr(as, key, t);
}
Example #9
0
void minipro_prepare_writing(minipro_handle_t *handle) {
	unsigned char buf[10];
	msg_init(msg, MP_PREPARE_WRITING, handle->device, handle->icsp);
	format_int(&(msg[2]), 0x03, 2, MP_LITTLE_ENDIAN);
	msg[2] = handle->device->write_unlock;
	msg_send(handle, msg, 15);
	msg_recv(handle, buf, 10);
}
static void
add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
{
	char tmp[1 + 3 * sizeof(value)];

	tmp[sizeof(tmp) - 1] = 0;
	add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
}
Example #11
0
void native_formatter_invoke(uint8_t mref) {
  char * fmt = stack_peek_addr(0);
  char res[50];
  
  formatDescr fmtdscr;
  make_format_descr(&fmtdscr, fmt);

  int len = 0;
  if(mref == NATIVE_METHOD_formatI) {
    nvm_int_t val = stack_peek_int(1);
    len = format_int(res, &fmtdscr, val);
  } else if(mref == NATIVE_METHOD_formatZ) {
    nvm_int_t val = stack_peek_int(1);
    len = format_bool(res, &fmtdscr, val);
  } else if(mref == NATIVE_METHOD_formatF) {
    nvm_float_t val = stack_peek_float(1);
    len = format_float(res, &fmtdscr, val);
  } else
    error(ERROR_NATIVE_UNKNOWN_METHOD);
    

  uint8_t add = 0;
  if (fmtdscr.width>len)
    add = (fmtdscr.width-len);
    
  // allocate heap and realign strings (address may be changed by gc...)
  heap_id_t id = heap_alloc(FALSE, len + add + fmtdscr.pre_len + fmtdscr.post_len + 1);
  int memoffset = (char*)stack_peek_addr(0)-(char*)fmt;
  fmt+=memoffset;
  fmtdscr.post+=memoffset;
  char * dst = heap_get_addr(id);
  
  // build result string
  native_strncpy(dst, fmt, fmtdscr.pre_len);
  dst+=fmtdscr.pre_len;

  if (!(fmtdscr.flags&0x01)){
    while(add--)
      *dst++=' ';
  }

  native_strncpy(dst, res, len);
  dst+=len;
  
  if (fmtdscr.flags&0x01){
    while(add--)
      *dst++=' ';
  }
  native_strncpy(dst, fmtdscr.post, fmtdscr.post_len);
  dst+=fmtdscr.post_len;
  *dst=0;
  stack_pop();
  stack_pop();
  stack_push(NVM_TYPE_HEAP | id);
}
void
ResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param)
{
    Method* m = new Method;
        m->modifiers = PUBLIC;
        m->returnType = VOID_TYPE;
        m->returnTypeDimension = 0;
        m->name = name;
        m->statements = new StatementBlock;
    *param = new Variable(BYTE_TYPE, "result", 1);
    m->parameters.push_back(*param);
    this->elements.push_back(m);
    *method = m;

    Case* c = new Case(format_int(index));
    c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam));
    c->statements->Add(new Break());

    this->methodSwitch->cases.push_back(c);
}
Example #13
0
static void
print_constant (FILE *out, JCF *jcf, int index, int verbosity)
{
  int j, n;
  jlong num;
  const char *str;
  int kind = JPOOL_TAG (jcf, index);
  switch (kind)
    {
    case CONSTANT_Class:
      n = JPOOL_USHORT1 (jcf, index);
      if (verbosity > 0)
	{
	  if (verbosity > 1)
	    fprintf (out, "Class name: %d=", n);
	  else
	    fprintf (out, "Class ");
	}
      if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, n))
	fprintf (out, "<out of range>");
      else if (verbosity < 2 && JPOOL_TAG (jcf, n) == CONSTANT_Utf8)
	{
	  int len = JPOOL_UTF_LENGTH (jcf, n);
	  jcf_print_utf8_replace (out, JPOOL_UTF_DATA(jcf,n), len, '/', '.');
	}
      else
	print_constant_terse (out, jcf, n, CONSTANT_Utf8);
      break;
    case CONSTANT_Fieldref:
      str = "Field"; goto field_or_method;
    case CONSTANT_Methodref:
      str = "Method"; goto field_or_method;
    case CONSTANT_InterfaceMethodref:
      str = "InterfaceMethod"; goto field_or_method;
    field_or_method:
      {
	uint16 tclass = JPOOL_USHORT1 (jcf, index);
	uint16 name_and_type = JPOOL_USHORT2 (jcf, index);
	if (verbosity == 2)
	  fprintf (out, "%sref class: %d=", str, tclass);
	else if (verbosity > 0)
	    fprintf (out, "%s ", str);
	print_constant_terse (out, jcf, tclass, CONSTANT_Class);
	if (verbosity < 2)
	  fprintf (out, ".");
	else
	  fprintf (out, " name_and_type: %d=<", name_and_type);
	print_constant_terse (out, jcf, name_and_type, CONSTANT_NameAndType);
	if (verbosity == 2)
	  fputc ('>', out);
      }
      break;
    case CONSTANT_String:
      j = JPOOL_USHORT1 (jcf, index);
      if (verbosity > 0)
	{
	  if (verbosity > 1)
	    fprintf (out, "String %d=", j);
	  else
	    fprintf (out, "String ");
	}
      print_constant_terse (out, jcf, j, CONSTANT_Utf8);
      break;
    case CONSTANT_Integer:
      if (verbosity > 0)
	fprintf (out, "Integer ");
      num = JPOOL_INT (jcf, index);
      goto integer;
    case CONSTANT_Long:
      if (verbosity > 0)
	fprintf (out, "Long ");
      num = JPOOL_LONG (jcf, index);
      goto integer;
    integer:
      {
	char buffer[25];
	format_int (buffer, num, 10);
	fprintf (out, "%s", buffer);
	if (verbosity > 1)
	  {
	    format_uint (buffer, (uint64)num, 16);
	    fprintf (out, "=0x%s", buffer);
	  }
      }
      break;
    case CONSTANT_Float:
      {
	jfloat fnum = JPOOL_FLOAT (jcf, index);

	if (verbosity > 0)
	  fputs ("Float ", out);

	if (fnum.negative)
	  putc ('-', out);

	if (JFLOAT_FINITE (fnum))
	  {
	    int dummy;
	    int exponent = fnum.exponent - JFLOAT_EXP_BIAS;
	    double f;
	    uint32 mantissa = fnum.mantissa;
	    if (fnum.exponent == 0)
	      /* Denormal.  */
	      exponent++;
	    else
	      /* Normal; add the implicit bit.  */
	      mantissa |= ((uint32)1 << 23);
	    
	    f = frexp (mantissa, &dummy);
	    f = ldexp (f, exponent + 1);
	    fprintf (out, "%.10g", f);
	  }
	else
	  {
	    if (fnum.mantissa == 0)
	      fputs ("Inf", out);
	    else if (fnum.mantissa & JFLOAT_QNAN_MASK)
	      fprintf (out, "QNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
	    else
	      fprintf (out, "SNaN(%u)", (fnum.mantissa & ~JFLOAT_QNAN_MASK));
	  }

	if (verbosity > 1)
	  fprintf (out, ", bits = 0x%08lx", (long) JPOOL_UINT (jcf, index));
	
	break;
      }
    case CONSTANT_Double:
      {
	jdouble dnum = JPOOL_DOUBLE (jcf, index);

	if (verbosity > 0)
	  fputs ("Double ", out);

	if (dnum.negative)
	  putc ('-', out);

	if (JDOUBLE_FINITE (dnum))
	  {
	    int dummy;
	    int exponent = dnum.exponent - JDOUBLE_EXP_BIAS;
	    double d;
	    uint64 mantissa = ((((uint64) dnum.mantissa0) << 32)
			       + dnum.mantissa1);
	    if (dnum.exponent == 0)
	      /* Denormal.  */
	      exponent++;
	    else
	      /* Normal; add the implicit bit.  */
	      mantissa |= ((uint64)1 << 52);

	    d = frexp (mantissa, &dummy);
	    d = ldexp (d, exponent + 1);
	    fprintf (out, "%.20g", d);
	  }
	else
	  {
	    uint64 mantissa = dnum.mantissa0 & ~JDOUBLE_QNAN_MASK;
	    mantissa = (mantissa << 32) + dnum.mantissa1;

	    if (dnum.mantissa0 == 0 && dnum.mantissa1 == 0)
	      fputs ("Inf", out);
	    else if (dnum.mantissa0 & JDOUBLE_QNAN_MASK)
	      fprintf (out, "QNaN(%llu)", (unsigned long long)mantissa);
	    else
	      fprintf (out, "SNaN(%llu)", (unsigned long long)mantissa);
	  }
	if (verbosity > 1)
	  {
	    int32 hi, lo;
	    hi = JPOOL_UINT (jcf, index);
	    lo = JPOOL_UINT (jcf, index + 1);
	    fprintf (out, ", bits = 0x%08lx%08lx", (long) hi, (long) lo);
	  }
	break;
      }
    case CONSTANT_NameAndType:
      {
	uint16 name = JPOOL_USHORT1 (jcf, index);
	uint16 sig = JPOOL_USHORT2 (jcf, index);
	if (verbosity > 0)
	  {
	    if (verbosity > 1)
	      fprintf (out, "NameAndType name: %d=", name);
	    else
	      fprintf (out, "NameAndType ");
	  }
	print_name (out, jcf, name);
	if (verbosity <= 1)
	  fputc (' ', out);
	else
	  fprintf (out, ", signature: %d=", sig);
	print_signature (out, jcf, sig, 0);
      }
      break;
    case CONSTANT_Utf8:
      {
	const unsigned char *str = JPOOL_UTF_DATA (jcf, index);
	int length = JPOOL_UTF_LENGTH (jcf, index);
	if (verbosity > 0)
	  { /* Print as 8-bit bytes. */
	    fputs ("Utf8: \"", out);
	    while (--length >= 0)
	      jcf_print_char (out, *str++);
	  }
	else
	  { /* Print as Unicode. */
	    fputc ('\"', out);
	    jcf_print_utf8 (out, str, length);
	  }
	fputc ('\"', out);
      }
      break;
    default:
      fprintf (out, "(Unknown constant type %d)", kind);
    }
}
static void
generate_proxy_method(const method_type* method, RpcProxyClass* proxyClass,
        ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
{
    arg_type* arg;
    Method* proxyMethod = new Method;
        proxyMethod->comment = gather_comments(method->comments_token->extra);
        proxyMethod->modifiers = PUBLIC;
        proxyMethod->returnType = VOID_TYPE;
        proxyMethod->returnTypeDimension = 0;
        proxyMethod->name = method->name.data;
        proxyMethod->statements = new StatementBlock;
    proxyClass->elements.push_back(proxyMethod);

    // The local variables
    Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
    proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));

    // Add the arguments
    arg = method->args;
    while (arg != NULL) {
        if (convert_direction(arg->direction.data) & IN_PARAMETER) {
            // Function signature
            Type* t = NAMES.Search(arg->type.type.data);
            Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
            proxyMethod->parameters.push_back(v);

            // Input parameter marshalling
            generate_write_to_data(t, proxyMethod->statements,
                    new StringLiteralExpression(arg->name.data), v, _data);
        }
        arg = arg->next;
    }

    // If there is a results interface for this class
    Expression* resultParameter;
    if (resultsInterfaceType != NULL) {
        // Result interface parameter
        Variable* resultListener = new Variable(resultsInterfaceType, "_result");
        proxyMethod->parameters.push_back(resultListener);

        // Add the results dispatcher callback
        resultsDispatcherClass->needed = true;
        resultParameter = new NewExpression(resultsDispatcherClass->type, 2,
                new LiteralExpression(format_int(index)), resultListener);
    } else {
        resultParameter = NULL_VALUE;
    }

    // All proxy methods take an error parameter
    Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors");
    proxyMethod->parameters.push_back(errorListener);

    // Call the broker
    proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"),
                "sendRpc", 5,
                proxyClass->endpoint,
                new StringLiteralExpression(method->name.data),
                new MethodCall(_data, "serialize"),
                resultParameter,
                errorListener));
}
Example #15
0
void LSTMLayer<Dtype>::FillUnrolledNet(NetParameter* net_param) const {
  const int num_output = this->layer_param_.recurrent_param().num_output();
  CHECK_GT(num_output, 0) << "num_output must be positive";
  const FillerParameter& weight_filler =
      this->layer_param_.recurrent_param().weight_filler();
  const FillerParameter& bias_filler =
      this->layer_param_.recurrent_param().bias_filler();

  // Add generic LayerParameter's (without bottoms/tops) of layer types we'll
  // use to save redundant code.
  LayerParameter hidden_param;
  hidden_param.set_type("InnerProduct");
  hidden_param.mutable_inner_product_param()->set_num_output(num_output * 4);
  hidden_param.mutable_inner_product_param()->set_bias_term(false);
  hidden_param.mutable_inner_product_param()->set_axis(2);
  hidden_param.mutable_inner_product_param()->
      mutable_weight_filler()->CopyFrom(weight_filler);

  LayerParameter biased_hidden_param(hidden_param);
  biased_hidden_param.mutable_inner_product_param()->set_bias_term(true);
  biased_hidden_param.mutable_inner_product_param()->
      mutable_bias_filler()->CopyFrom(bias_filler);

  LayerParameter sum_param;
  sum_param.set_type("Eltwise");
  sum_param.mutable_eltwise_param()->set_operation(
      EltwiseParameter_EltwiseOp_SUM);

  LayerParameter scale_param;
  scale_param.set_type("Scale");
  scale_param.mutable_scale_param()->set_axis(0);

  LayerParameter slice_param;
  slice_param.set_type("Slice");
  slice_param.mutable_slice_param()->set_axis(0);

  LayerParameter split_param;
  split_param.set_type("Split");

  vector<BlobShape> input_shapes;
  RecurrentInputShapes(&input_shapes);
  CHECK_EQ(2, input_shapes.size());

  LayerParameter* input_layer_param = net_param->add_layer();
  input_layer_param->set_type("Input");
  InputParameter* input_param = input_layer_param->mutable_input_param();

  input_layer_param->add_top("c_0");
  input_param->add_shape()->CopyFrom(input_shapes[0]);

  input_layer_param->add_top("h_0");
  input_param->add_shape()->CopyFrom(input_shapes[1]);

  LayerParameter* cont_slice_param = net_param->add_layer();
  cont_slice_param->CopyFrom(slice_param);
  cont_slice_param->set_name("cont_slice");
  cont_slice_param->add_bottom("cont");
  cont_slice_param->mutable_slice_param()->set_axis(0);

  // Add layer to transform all timesteps of x to the hidden state dimension.
  //     W_xc_x = W_xc * x + b_c
  {
    LayerParameter* x_transform_param = net_param->add_layer();
    x_transform_param->CopyFrom(biased_hidden_param);
    x_transform_param->set_name("x_transform");
    x_transform_param->add_param()->set_name("W_xc");
    x_transform_param->add_param()->set_name("b_c");
    x_transform_param->add_bottom("x");
    x_transform_param->add_top("W_xc_x");
    x_transform_param->add_propagate_down(true);
  }

  if (this->static_input_) {
    // Add layer to transform x_static to the gate dimension.
    //     W_xc_x_static = W_xc_static * x_static
    LayerParameter* x_static_transform_param = net_param->add_layer();
    x_static_transform_param->CopyFrom(hidden_param);
    x_static_transform_param->mutable_inner_product_param()->set_axis(1);
    x_static_transform_param->set_name("W_xc_x_static");
    x_static_transform_param->add_param()->set_name("W_xc_static");
    x_static_transform_param->add_bottom("x_static");
    x_static_transform_param->add_top("W_xc_x_static_preshape");
    x_static_transform_param->add_propagate_down(true);

    LayerParameter* reshape_param = net_param->add_layer();
    reshape_param->set_type("Reshape");
    BlobShape* new_shape =
         reshape_param->mutable_reshape_param()->mutable_shape();
    new_shape->add_dim(1);  // One timestep.
    // Should infer this->N as the dimension so we can reshape on batch size.
    new_shape->add_dim(-1);
    new_shape->add_dim(
        x_static_transform_param->inner_product_param().num_output());
    reshape_param->set_name("W_xc_x_static_reshape");
    reshape_param->add_bottom("W_xc_x_static_preshape");
    reshape_param->add_top("W_xc_x_static");
  }

  LayerParameter* x_slice_param = net_param->add_layer();
  x_slice_param->CopyFrom(slice_param);
  x_slice_param->add_bottom("W_xc_x");
  x_slice_param->set_name("W_xc_x_slice");

  LayerParameter output_concat_layer;
  output_concat_layer.set_name("h_concat");
  output_concat_layer.set_type("Concat");
  output_concat_layer.add_top("h");
  output_concat_layer.mutable_concat_param()->set_axis(0);

  for (int t = 1; t <= this->T_; ++t) {
    string tm1s = format_int(t - 1);
    string ts = format_int(t);

    cont_slice_param->add_top("cont_" + ts);
    x_slice_param->add_top("W_xc_x_" + ts);

    // Add layers to flush the hidden state when beginning a new
    // sequence, as indicated by cont_t.
    //     h_conted_{t-1} := cont_t * h_{t-1}
    //
    // Normally, cont_t is binary (i.e., 0 or 1), so:
    //     h_conted_{t-1} := h_{t-1} if cont_t == 1
    //                       0   otherwise
    {
      LayerParameter* cont_h_param = net_param->add_layer();
      cont_h_param->CopyFrom(scale_param);
      cont_h_param->set_name("h_conted_" + tm1s);
      cont_h_param->add_bottom("h_" + tm1s);
      cont_h_param->add_bottom("cont_" + ts);
      cont_h_param->add_top("h_conted_" + tm1s);
    }

    // Add layer to compute
    //     W_hc_h_{t-1} := W_hc * h_conted_{t-1}
    {
      LayerParameter* w_param = net_param->add_layer();
      w_param->CopyFrom(hidden_param);
      w_param->set_name("transform_" + ts);
      w_param->add_param()->set_name("W_hc");
      w_param->add_bottom("h_conted_" + tm1s);
      w_param->add_top("W_hc_h_" + tm1s);
      w_param->mutable_inner_product_param()->set_axis(2);
    }

    // Add the outputs of the linear transformations to compute the gate input.
    //     gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c
    //                   = W_hc_h_{t-1} + W_xc_x_t + b_c
    {
      LayerParameter* input_sum_layer = net_param->add_layer();
      input_sum_layer->CopyFrom(sum_param);
      input_sum_layer->set_name("gate_input_" + ts);
      input_sum_layer->add_bottom("W_hc_h_" + tm1s);
      input_sum_layer->add_bottom("W_xc_x_" + ts);
      if (this->static_input_) {
        input_sum_layer->add_bottom("W_xc_x_static");
      }
      input_sum_layer->add_top("gate_input_" + ts);
    }

    // Add LSTMUnit layer to compute the cell & hidden vectors c_t and h_t.
    // Inputs: c_{t-1}, gate_input_t = (i_t, f_t, o_t, g_t), cont_t
    // Outputs: c_t, h_t
    //     [ i_t' ]
    //     [ f_t' ] := gate_input_t
    //     [ o_t' ]
    //     [ g_t' ]
    //         i_t := \sigmoid[i_t']
    //         f_t := \sigmoid[f_t']
    //         o_t := \sigmoid[o_t']
    //         g_t := \tanh[g_t']
    //         c_t := cont_t * (f_t .* c_{t-1}) + (i_t .* g_t)
    //         h_t := o_t .* \tanh[c_t]
    {
      LayerParameter* lstm_unit_param = net_param->add_layer();
      lstm_unit_param->set_type("LSTMUnit");
      lstm_unit_param->add_bottom("c_" + tm1s);
      lstm_unit_param->add_bottom("gate_input_" + ts);
      lstm_unit_param->add_bottom("cont_" + ts);
      lstm_unit_param->add_top("c_" + ts);
      lstm_unit_param->add_top("h_" + ts);
      lstm_unit_param->set_name("unit_" + ts);
    }
    output_concat_layer.add_bottom("h_" + ts);
  }  // for (int t = 1; t <= this->T_; ++t)

  {
    LayerParameter* c_T_copy_param = net_param->add_layer();
    c_T_copy_param->CopyFrom(split_param);
    c_T_copy_param->add_bottom("c_" + format_int(this->T_));
    c_T_copy_param->add_top("c_T");
  }
  net_param->add_layer()->CopyFrom(output_concat_layer);
}
Example #16
0
void process_item (int rectype, int reclen, unsigned char *rec) {
	if (rectype != CONTINUE && prev_rectype == SST) {
		/* we have accumulated  unparsed SST, and now encountered
		 * another record, which indicates that SST is ended */
		/* fprintf(stderr,"parse sst!\n");*/
		parse_sst(sstBuffer,sstBytes);
	}	
	switch (rectype) {
	case FILEPASS: {
		fprintf(stderr,"File is encrypted\n");
		exit(69);
		break;
	}
	case WRITEPROT: 
		/* File is write protected, but we only read it */
		break;
	case 0x42: {
		if (source_charset) break;
		codepage=getshort(rec,0);
		/*fprintf(stderr,"CODEPAGE %d\n",codepage); */
		if (codepage!=1200) {
			const char *cp = charset_from_codepage(codepage); 
			source_charset=read_charset(cp);
		}		 
		break;
	}  
	case FORMAT: {
		int format_code;
		format_code=getshort(rec,0);
		SetFormatIdxUsed(format_code);
		/* this debug code prints format string */
		/*							  
			int i;
			char *ptr;
			fprintf(stderr,"Format %x \"",format_code);
			if (rec[2] == reclen - 3 && rec[3] != 0) {
			for (i=0,ptr=rec+3;i<rec[2];i++,ptr++) {
			fputc(*ptr,stderr);
			}
			} else {
			for (i=0,ptr=rec+5;i<rec[2];i++,ptr+=2) {
			fputc(*ptr,stderr);
			}
			}
			fprintf (stderr,"\"\n");
		*/
		break;
	}			 
	case SST: {
		/* Just copy SST into buffer, and wait until we get
		 * all CONTINUE records
		 */
/* 		fprintf(stderr,"SST\n"); */
		/* If exists first SST entry, then just drop it and start new*/
		if (sstBuffer != NULL) 
			free(sstBuffer);
		if (sst != NULL)
			free(sst);
		
		sstBuffer=(unsigned char*)malloc(reclen);
		sstBytes = reclen;
		if (sstBuffer == NULL ) {
			perror("SSTptr alloc error! ");
			exit(1);
		}	  
		memcpy(sstBuffer,rec,reclen);
		break;
	}	
	case CONTINUE: {
		if (prev_rectype != SST) {
			return; /* to avoid changing of prev_rectype;*/
		}    
		sstBuffer=realloc(sstBuffer,sstBytes+reclen);
		if (sstBuffer == NULL ) {
			perror("SSTptr realloc error! ");
			exit(1);
		}	  
		memcpy(sstBuffer+sstBytes,rec,reclen);
		sstBytes+=reclen;
		return;
	}			   
	case LABEL: {
		int row,col;
		unsigned char **pcell;
		unsigned char *src=(unsigned char *)rec+6;

		saved_reference=NULL;
		row = getshort(rec,0); 
		col = getshort(rec,2);
		/* 		fprintf(stderr,"LABEL!\n"); */
		pcell=allocate(row,col);
		*pcell=copy_unicode_string(&src);
		break;
	}     
	case BLANK: { int row,col;unsigned char **pcell;
			row = getshort(rec,0);
			col = getshort(rec,2);
			pcell=allocate(row,col);
			*pcell=NULL;
			break;
	}
	case MULBLANK: {
		int row, startcol,endcol;
		unsigned char **pcell;
		row = getshort(rec,0);
		startcol = getshort(rec,2);
		endcol=getshort(rec,reclen-2);
		pcell=allocate(row,endcol);
		*pcell=NULL;
		(void)startcol;
		break;
	}	   
	case CONSTANT_STRING: {
		int row = getshort(rec,0); 
		int col = getshort(rec,2);
		unsigned char **pcell;
		int string_no=getshort(rec,6);
		if (!sst) {
			fprintf(stderr,"CONSTANT_STRING before SST parsed\n");
			exit(1);
		}    
		/* 									fprintf(stderr,"col=%d row=%d no=%d\n",col,row,string_no); */
									
		saved_reference=NULL;
		pcell=allocate(row,col);
		if (string_no>=sstsize|| string_no < 0 ) {
			fprintf(stderr,"string index out of boundary\n"); 
			exit(1);	 
		} else if (sst[string_no] !=NULL) {	
			int len;
			unsigned char *outptr;
			len=strlen((char *)sst[string_no]);
			outptr=*pcell=malloc(len+1);
			strcpy((char *)outptr,(char *)sst[string_no]);
		} else {
			*pcell=malloc(1);
			**pcell = 0;
		}	
		break;
	}
	case 0x03:
	case 0x103:
	case 0x303:
	case NUMBER: {
		int row,col;
		unsigned char **pcell;

		saved_reference=NULL;
		row = getshort(rec,0)-startrow; 
		col = getshort(rec,2);
		pcell=allocate(row,col);
		*pcell=(unsigned char *)strdup(format_double(rec,6,getshort(rec,4)));
		break;
	}
	case INTEGER_CELL: {
		int row,col;
		unsigned char **pcell;

		row = getshort(rec,0)-startrow;
		col = getshort(rec,2);
		pcell=allocate(row,col);
		*pcell=(unsigned char *)strdup(format_int(getshort(rec,7),getshort(rec,4)));		  
		break;

	}				  
	case RK: {
		int row,col,format_code;
		unsigned char **pcell;

		saved_reference=NULL;
		row = getshort(rec,0)-startrow; 
		col = getshort(rec,2);
		pcell=allocate(row,col);
		format_code = getshort(rec,4);
		*pcell=(unsigned char *)strdup(format_rk(rec+6,format_code));
		break;
	}
	case MULRK: {
		int row,col,startcol,endcol,offset,format_code;
		unsigned char **pcell;
		row = getshort(rec,0)-startrow; 
		startcol = getshort(rec,2);
		endcol = getshort(rec,reclen-2);
		saved_reference=NULL;

		for (offset=4,col=startcol;col<=endcol;offset+=6,col++) { 
			pcell=allocate(row,col);
			format_code=getshort(rec,offset);
			*pcell=(unsigned char *)strdup(format_rk(rec+offset+2,format_code));

		}		 
		break;
	} 
	case FORMULA: { 
		int row,col;
		unsigned char **pcell;
		saved_reference=NULL;
		row = getshort(rec,0)-startrow; 
		col = getshort(rec,2);
		pcell=allocate(row,col);
		if (((unsigned char)rec[12]==0xFF)&&(unsigned char)rec[13]==0xFF) {
			/* not a floating point value */
			if (rec[6]==1) {
				/*boolean*/
				char buf[2]="0";
				buf[0]+=rec[9];
				*pcell=(unsigned char *)strdup(buf);
			} else if (rec[6]==2) {
				/*error*/
				char buf[6]="ERROR";
				*pcell=(unsigned char *)strdup(buf);
			} else if (rec[6]==0) {
				saved_reference=pcell;
			}   
		} else {
			int format_code=getshort(rec,4);
			*pcell=(unsigned char *)strdup(format_double(rec,6,format_code));
		}		 
		break;
	}
	case STRING: {
		unsigned char *src=(unsigned char *)rec;
		if (!saved_reference) {
			fprintf(stderr,"String record without preceeding string formula\n");
			break;
		}
		*saved_reference=copy_unicode_string(&src);
		break;
	}	    
	case BOF: {
		if (rowptr) {
			fprintf(stderr,"BOF when current sheet is not flushed\n");
			free_sheet();
		}
		break;
	}	  
	case XF:
	case 0x43: /*from perl module Spreadsheet::ParseExecel */		  
		{
			short int formatIndex = getshort(rec,2);
			/* we are interested only in format index here */ 
			if (formatTableIndex >= formatTableSize) {
				formatTable=realloc(formatTable,
														(formatTableSize+=16)*sizeof(short int));
					  	  
				if (!formatTable) {
					fprintf(stderr,"Out of memory for format table");
					exit (1);
				}	  
			}	
			formatTable[formatTableIndex++] = formatIndex;
			break;
		} 
	case MS1904: /* Macintosh 1904 date system */ 
		date_shift=24107.0; 
		break;	 
						 
					 
	case MSEOF: {
		if (!rowptr) break;
		print_sheet();
		free_sheet();
		break;
	}
	case ROW: { 
		/*  		fprintf(stderr,"Row! %d %d %d\n",getshort(rec,0), getshort(rec+2,0),getshort(rec+4,0));  */
		break; 
	} 
	case INDEX: {
		/* 		fprintf(stderr,"INDEX! %d %d\n", getlong(rec+4,0), getlong(rec+8,0));  */
		break;
	}
	default: {
#if	0	
		fprintf(stderr,"Unknown record 0x%x\n length %d\n",rectype,reclen);		
#endif 
	}
	}
	prev_rectype=rectype; 
}  
Example #17
0
void LSTMLayer<Dtype>::RecurrentOutputBlobNames(vector<string>* names) const {
  names->resize(2);
  (*names)[0] = "h_" + format_int(this->T_);
  (*names)[1] = "c_T";
}
Example #18
0
void printf(const char* format, ...){
    char convertBuffer[BUF_SIZE];
    va_list vars;

    int iVal = 0;
    float fVal = 0.0f;
    char* strVal = NULL;
    unsigned int uiVal = 0;
    int length = 0;
    va_start(vars, format);

    int skip = 0;
    while( *format != '\0' ){
        for(; *format!='\0' && *format!='%' ; format++) skip++;
        puts_n(format-skip, skip);
        skip = 0;
        if(*format == '%'){
            format++;
            switch(*format){
                case 'd':
                    iVal = va_arg(vars, int);
                    length = format_int(iVal, convertBuffer, BUF_SIZE);
                    puts_n(convertBuffer, length);
                    iVal = 0;
                    format++;
                    break;
                case 'e':
                    fVal = va_arg(vars, double);
                    length = format_float(fVal, convertBuffer, BUF_SIZE);
                    puts_n(convertBuffer, length);
                    fVal = 0.0f;
                    format++;
                    break;
                case 's':
                    strVal = va_arg(vars, char*);
                    puts(strVal);
                    strVal = NULL;
                    format++;
                    break;
                case 'c':
                    iVal = va_arg(vars, int);
                    putchar(iVal);
                    iVal = 0;
                    format++;
                    break;
                case 'x':
                    iVal = va_arg(vars, int);
                    format++;
                    break;
                case 'u':
                    uiVal = va_arg(vars, unsigned int);
                    format++;
                    break;
                case '%':
                    putchar('%');
                    format++;
                    break;
                default:
                    break;
            }
        }
    }
void PedestrianDetector::runDetection() {
    // open the video stream
    VideoStream *vid = read_video_stream(config["dataset"]);
    vid->open();

    std::vector<BoundingBox> candidates;
    double hit_threshold = config["detector_opts"]["hit_threshold"].asDouble();

    std::ofstream out_file;
    if (config["output"]["save_log"].asBool()) {
        //std::cout << "It's going to save a log file in " << config.logFilename << std::endl;
        out_file.open(config["output"]["out_filename"].asString());
        if (out_file.fail()) {
            std::cerr << "open failure: " << strerror(errno) << '\n';
        }
    }

    while (!vid->has_ended()) {
        cv::Mat frame = vid->get_next_frame();

        // resize frame if needed.
        if (config["detector_opts"]["resize_image"].asDouble() != 1.0) {
            double f = config["detector_opts"]["resize_image"].asDouble();
            cv::resize(frame, frame, cv::Size(), f, f);
        }

        // begin timer
        clock_t frame_start = clock();

        std::vector<BoundingBox> detections;
        // if calibration is required and the candidates were not build yet
        if (config["detector_opts"]["use_calibration"].asBool()) {
            if (candidates.size() == 0) {
                candidates = generateCandidatesWCalibration(frame.rows, frame.cols, NULL);
                std::cout << "Number of candidates: " << candidates.size() << std::endl; 
            }

            // std::cout << "Its going to debug..." << std::endl;
            
            std::vector<cv::Mat> pyramid_images;
            std::vector<float> pyramid_scales;
            pyramid_images = computeImagePyramid(frame, pyramid_scales, 1.05);
            associateScaleToCandidates(candidates, pyramid_scales, frame.rows);

            // debugCandidates(frame, candidates, pyramid_scales);

            detections = detectWCandidates(candidates, pyramid_images, pyramid_scales, hit_threshold);

            std::cout << "Number of detections: " << detections.size() << std::endl;
            
        }
        else {
            std::vector<cv::Mat> pyramid_images;
            std::vector<float> pyramid_scales;
            pyramid_images = computeImagePyramid(frame, pyramid_scales, 1.05);

            detections = detectBaseline(pyramid_images, pyramid_scales, hit_threshold);
        }

        showDetections(frame, detections, cv::Scalar(0,200,0));
        detections = nonMaxSuppression(detections);
        showDetections(frame, detections, cv::Scalar(0,0,200));

        clock_t frame_end = clock();
        std::cout << "TIME: " << (double(frame_end - frame_start) / CLOCKS_PER_SEC) << std::endl;
        // end timer
        
        int f = vid->get_current_frame_number();
        if (config["output"]["save_frames"].asBool()) {
            std::stringstream ss;
            ss << config["output"]["out_folder"].asString() << format_int(f, 4) << ".jpg";
            std::cout << "Saving frame " << ss.str() << std::endl;
            cv::imwrite(ss.str(), frame);

        }

        if (config["output"]["save_log"].asBool()) {
            for (int i = 0; i < detections.size(); ++i) {
                out_file << f << " " << detections[i].bb.x << " " 
                         << detections[i].bb.y << " "  
                         << detections[i].bb.height << " "
                         << detections[i].bb.width << " "
                         << detections[i].score << std::endl;
            }
        }
    }
}
Example #20
0
static void msg_init(unsigned char *out, unsigned int cmd) {
	memset(out, 0, 16);
	format_int(out, cmd, 2, MP_BIG_ENDIAN);
}
Example #21
0
stcp_log& stcp_log::operator<<(int v)
{
	format_int(v);
	return * this;
}