Exemplo n.º 1
0
void __assert_float_equal(const char* spec, float expected, float actual, int line, const char* file){
    char buf1[31];
    char buf2[31];
    if(expected!=actual){
        format_float(expected, buf1, 31);
        format_float(actual, buf2, 31);
        printf(fail_message_equal, spec, buf1, buf2, line, file);
    }
    int pass = expected == actual;
    _TEST_CASE_REPORT_RESULT(pass);
}
Exemplo n.º 2
0
 Ustring format_int(T t, uint64_t flags, int prec) {
     static constexpr auto float_flags = Format::digits | Format::exp | Format::fixed | Format::general | Format::stripz;
     static constexpr auto int_flags = Format::binary | Format::decimal | Format::hex | Format::roman;
     static constexpr auto sign_flags = Format::sign | Format::signz;
     if ((flags & float_flags) && ! (flags & int_flags))
         return format_float(t, flags, prec);
     if (ibits(flags & int_flags) > 1 || ibits(flags & sign_flags) > 1)
         throw std::invalid_argument("Inconsistent integer formatting flags");
     char sign = 0;
     if (t > static_cast<T>(0)) {
         if (flags & (Format::sign | Format::signz))
             sign = '+';
     } else if (t == static_cast<T>(0)) {
         if (flags & Format::sign)
             sign = '+';
     } else {
         t = RS_Detail::SimpleAbs<T>()(t);
         sign = '-';
     }
     Ustring s;
     if (flags & Format::binary)
         s = format_radix(t, 2, prec);
     else if (flags & Format::roman)
         s = roman(unsigned(t));
     else if (flags & Format::hex)
         s = format_radix(t, 16, prec);
     else
         s = format_radix(t, 10, prec);
     if (sign)
         s.insert(s.begin(), sign);
     return s;
 }
Exemplo n.º 3
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);
}
Exemplo n.º 4
0
STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_complex_t *o = o_in;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
    char buf[32];
    if (o->real == 0) {
        format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
        print(env, "%sj", buf);
    } else {
        format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
        print(env, "(%s+", buf);
        format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
        print(env, "%sj)", buf);
    }
#else
    if (o->real == 0) {
        print(env, "%.8gj",  (double) o->imag);
    } else {
        print(env, "(%.8g+%.8gj)", (double) o->real, (double) o->imag);
    }
#endif
}
Exemplo n.º 5
0
STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_float_t *o = o_in;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
    char buf[32];
    format_float(o->value, buf, sizeof(buf), 'g', 6, '\0');
    print(env, "%s", buf);
    if (strchr(buf, '.') == NULL) {
        // Python floats always have decimal point
        print(env, ".0");
    }
#else
    char buf[32];
    sprintf(buf, "%.17g", (double) o->value);
    print(env, buf);
    if (strchr(buf, '.') == NULL) {
        // Python floats always have decimal point
        print(env, ".0");
    }
#endif
}
Exemplo n.º 6
0
int pfenv_print_float(const pfenv_t *pfenv, mp_float_t f, char fmt, int flags, char fill, int width, int prec) {
    char buf[32];
    char sign = '\0';
    int chrs = 0;

    if (flags & PF_FLAG_SHOW_SIGN) {
        sign = '+';
    }
    else
    if (flags & PF_FLAG_SPACE_SIGN) {
        sign = ' ';
    }
    int len;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
    len = format_float(f, buf, sizeof(buf), fmt, prec, sign);
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
    char fmt_buf[6];
    char *fmt_s = fmt_buf;

    *fmt_s++ = '%';
    if (sign) {
        *fmt_s++ = sign;
    }
    *fmt_s++ = '.';
    *fmt_s++ = '*';
    *fmt_s++ = fmt;
    *fmt_s = '\0';

    len = snprintf(buf, sizeof(buf), fmt_buf, prec, f);
#else
#error Unknown MICROPY FLOAT IMPL
#endif
    char *s = buf;

    if ((flags & PF_FLAG_ADD_PERCENT) && (len + 1) < sizeof(buf)) {
        buf[len++] = '%';
        buf[len] = '\0';
    }

    // buf[0] < '0' returns true if the first character is space, + or -
    if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') {
        // We have a sign character
        s++;
        if (*s <= '9' || (flags & PF_FLAG_PAD_NAN_INF)) {
            // We have a number, or we have a inf/nan and PAD_NAN_INF is set
            // With '{:06e}'.format(float('-inf')) you get '-00inf'
            chrs += pfenv_print_strn(pfenv, &buf[0], 1, 0, 0, 1);
            width--;
            len--;
        }
    }

    if (*s > 'A' && (flags & PF_FLAG_PAD_NAN_INF) == 0) {
        // We have one of the inf or nan variants, suppress zero fill.
        // With printf, if you use: printf("%06e", -inf) then you get "  -inf"
        // so suppress the zero fill.
        fill = ' ';
    }
    chrs += pfenv_print_strn(pfenv, s, len, flags, fill, width);

    return chrs;
}
Exemplo n.º 7
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;
            }
        }
    }
Exemplo n.º 8
0
/*
* @function    fw_vsprintf
*
* @brief   Firmware user-define sprintf function 
* @date    Feb. 24th, 2009
*/
static inline int32u fw_vsprintf(int8u *buf, int32u buf_len, const int8u *format, va_list arg)
{
	float64 ft_val;
	int32u pre_flag = 0;
	int32u width = 0;
	int32u prec = 6;// default decimal precesion is 6 bits
	int32s int_val;
	int64s long_val;
	int32u res = 0, len;
	int8u *p = (int8u *)format;
	int8u *str_val;
	
	while ((*p != '\0') && (res <= buf_len)) {
		if (pre_flag == 0) { // search '%' or '\' in format string
			if (*p == '%') {
				pre_flag = PRT_PRE_PERCENT;
				width = 0;
			}
			else {
				*(buf++) = *p;
				res++;
			}
		}
		else { // scan format string
			switch (*p) {
				case '0':
					pre_flag |= PRT_FILL_ZERO;
					
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					do {
						width = width*10 + (*p)-'0';
						p++;
					} while (*p>='0' && *p<='9');
					pre_flag |= PRT_LEFT_FILL;
					p--; // go back to last position in format string
					break;
				case 'l':
					pre_flag |= PRT_LONG_FLAG;
					break;
				case 'd':
				case 'x':
				case 'X':
				case 'c':
				case 'u':
					if (pre_flag & PRT_LONG_FLAG)
					{
						long_val = va_arg(arg, int64s);
						len = format_long_integer(buf, long_val, *p, width, pre_flag);
					}
					else
					{
						int_val = va_arg(arg, int32s);
						len = format_integer(buf, int_val, *p, width, pre_flag);
					}
					buf += len;
					res += len;
					pre_flag = 0;
					break;
					
				case 'f':
				case 'F':
					ft_val = va_arg(arg, float64);
					len = format_float(buf, ft_val, width, prec, pre_flag);
					buf += len;
					res += len;
					pre_flag = 0;
					break;
				
				case 's':
					str_val = va_arg(arg, int8u*);
					len = format_string(buf, str_val, width, pre_flag);
					buf += len;
					res += len;
					pre_flag = 0;
					break;

				case '.':
					p++;
					pre_flag |= PRT_PREC_FLAG;
					prec = 0;
					do {
						prec = prec*10 + (*p)-'0';
						p++;
					} while (*p>='0' && *p<='9');
					p--; // go back to last position in format string
					break;
					
				default:
					return 0;
			}
		}

		p++;
	}

	return res;
}
Exemplo n.º 9
0
void texture_select_callback(GObject* obj, gpointer user_data) {
    GtkTreeView* param_tree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "selected_texture_treeview"));
    GtkTreeStore* param_store = GTK_TREE_STORE(gtk_tree_view_get_model(param_tree));
    
    GtkTreeView* image_tree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "selected_texture_images"));
    GtkTreeStore* image_store = GTK_TREE_STORE(gtk_tree_view_get_model(image_tree));
    
    if (!param_store)
        return;
    if (!image_store)
        return;
    
    gtk_tree_store_clear(image_store);
    gtk_tree_store_clear(param_store);
    
    GtkTreePath* path;
    GtkSpinButton* layer_spinbutton = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "layer_spinbutton"));
    
    gtk_tree_view_get_cursor(GTK_TREE_VIEW(obj), &path, NULL);
    if (!path) {
        gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(layer_spinbutton), 0);
        return;
    }
    
    //Initialize params
    size_t tex_index = gtk_tree_path_get_indices(path)[0];
    inspect_texture_t* tex = get_inspect_tex_vec(inspector->textures, tex_index);
    if (!tex) return;
    
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(layer_spinbutton), tex->layer_count-1);
    
    inspect_gl_tex_params_t params = tex->params;
    
    GtkTreeIter row;
    #define VAL(name, val) gtk_tree_store_append(param_store, &row, NULL);\
    gtk_tree_store_set(param_store, &row, 0, (name), 1, (val), -1);
    if (tex->type) {
        VAL("Type", static_format("%s", get_enum_str("TextureTarget", tex->type)));
        VAL("Depth Stencil Mode", static_format("%s", get_enum_str(NULL, params.depth_stencil_mode)));
        VAL("Min Filter", static_format("%s", get_enum_str("TextureMinFilter", params.min_filter)));
        VAL("Mag Filter", static_format("%s", get_enum_str("TextureMagFilter", params.mag_filter)));
        VAL("Min LOD", static_format("%s", format_float(params.min_lod)));
        VAL("Max LOD", static_format("%s", format_float(params.max_lod)));
        VAL("LOD bias", static_format("%s", format_float(params.lod_bias)));
        VAL("Base Level", static_format("%d", params.base_level));
        VAL("Max Level", static_format("%d", params.max_level));
        VAL("Swizzle", static_format("[%s, %s, %s, %s]",
                                     get_enum_str(NULL, params.swizzle[0]),
                                     get_enum_str(NULL, params.swizzle[1]),
                                     get_enum_str(NULL, params.swizzle[2]),
                                     get_enum_str(NULL, params.swizzle[3])));
        VAL("Wrap S", static_format("%s", get_enum_str("TextureWrapMode", params.wrap[0])));
        VAL("Wrap T", static_format("%s", get_enum_str("TextureWrapMode", params.wrap[0])));
        VAL("Wrap R", static_format("%s", get_enum_str("TextureWrapMode", params.wrap[0])));
        VAL("Border Color", static_format("[%s, %s, %s, %s]",
                                          format_float(params.border_color[0]),
                                          format_float(params.border_color[1]),
                                          format_float(params.border_color[2]),
                                          format_float(params.border_color[3])));
        VAL("Compare Mode", static_format("%s", get_enum_str(NULL, params.compare_mode)));
        VAL("Compare Func", static_format("%s", get_enum_str("DepthFunction", params.compare_func)));
        VAL("View Min Level", static_format("%d", params.view_min_level));
        VAL("View Num Levels", static_format("%u", params.view_num_levels));
        VAL("View Min Layer", static_format("%d", params.view_min_layer));
        VAL("View Num Layers", static_format("%u", params.view_num_layers));
        VAL("Immutable levels", static_format("%u", params.immutable_levels));
        VAL("Image Format Compat Type", static_format("%s", get_enum_str(NULL, params.image_format_compatibility_type)));
        VAL("Immutable Format", static_format("%s", get_enum_str(NULL, params.immutable_format)));
    }
    
    if (tex->mipmaps) {
        VAL("Width", static_format("%zu", tex->width));
        VAL("Height", static_format("%zu", tex->height));
        VAL("Depth", static_format("%zu", tex->depth));
        VAL("Mipmap Count", static_format("%zu", tex->mipmap_count));
        VAL("Layer Count", static_format("%zu", tex->layer_count));
        #undef VAL
        
        GtkSpinButton* layer_button = GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "layer_spinbutton"));
        uint32_t layer = gtk_spin_button_get_value(GTK_SPIN_BUTTON(layer_button));
        
        GtkComboBox* face_combobox = GTK_COMBO_BOX(gtk_builder_get_object(builder, "face_combobox"));
        gint face = gtk_combo_box_get_active(GTK_COMBO_BOX(face_combobox));
        
        //Initialize images
        size_t w = tex->width;
        size_t h = tex->height;
        for (size_t level = 0; level < tex->mipmap_count; level++) {
            inspect_image_t* img = inspect_get_tex_mipmap(tex, level, layer, face);
            if (img->has_data) {
                uint32_t* data = (uint32_t*)malloc(w*h*4);
                inspect_get_image_data(img, data);
                
                GtkTreeIter row;
                gtk_tree_store_append(image_store, &row, NULL);
                GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, w, h);
                uint32_t* dest = (uint32_t*)gdk_pixbuf_get_pixels(pixbuf);
                for (size_t y = 0; y < h; y++) {
                    for (size_t x = 0; x < w; x++) {
                        dest[(h-1-y)*w+x] = data[y*w+x];
                    }
                }
                gtk_tree_store_set(image_store, &row, 0, static_format("%u", level), 1, pixbuf, -1);
                g_object_unref(pixbuf);
                
                free(data);
            }
            
            w /= 2;
            h /= 2;
        }
    }
}