From 0d68b7078cbb1fd709d354addcfbbcb9e27e4523 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 27 Sep 2019 19:47:25 +0530 Subject: [PATCH] Check for float conversion issues on build Useful to catch any errors left over from the migration of times from double to int64_t --- glfw/wl_init.c | 4 ++-- glfw/wl_window.c | 4 ++-- glfw/x11_init.c | 2 +- glfw/x11_window.c | 4 ++-- kitty/child-monitor.c | 4 ++-- kitty/fonts.c | 6 +++--- kitty/freetype.c | 18 +++++++++--------- kitty/glfw.c | 4 ++-- kitty/monotonic.h | 2 +- kitty/shaders.c | 2 +- kitty/state.c | 15 ++++++++++----- setup.py | 2 +- 12 files changed, 36 insertions(+), 31 deletions(-) diff --git a/glfw/wl_init.c b/glfw/wl_init.c index e9d1312a8..9024efa02 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -285,8 +285,8 @@ static void pointerHandleButton(void* data UNUSED, { xdg_toplevel_show_window_menu(window->wl.xdg.toplevel, _glfw.wl.seat, serial, - window->wl.cursorPosX, - window->wl.cursorPosY); + (int32_t)window->wl.cursorPosX, + (int32_t)window->wl.cursorPosY); return; } } diff --git a/glfw/wl_window.c b/glfw/wl_window.c index d6b9fce03..15c030e3f 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -546,9 +546,9 @@ static void xdgToplevelHandleConfigure(void* data, aspectRatio = (float)width / (float)height; targetRatio = (float)window->numer / (float)window->denom; if (aspectRatio < targetRatio) - height = width / targetRatio; + height = (int32_t)((float)width / targetRatio); else if (aspectRatio > targetRatio) - width = height * targetRatio; + width = (int32_t)((float)height * targetRatio); } } } diff --git a/glfw/x11_init.c b/glfw/x11_init.c index a13c78a66..33e9054b8 100644 --- a/glfw/x11_init.c +++ b/glfw/x11_init.c @@ -468,7 +468,7 @@ void _glfwGetSystemContentScaleX11(float* xscale, float* yscale, bool bypass_cac if (XrmGetResource(db, "Xft.dpi", "Xft.Dpi", &type, &value)) { if (type && strcmp(type, "String") == 0) - xdpi = ydpi = atof(value.addr); + xdpi = ydpi = (float)atof(value.addr); } XrmDestroyDatabase(db); diff --git a/glfw/x11_window.c b/glfw/x11_window.c index 71c8fc003..b1ca03981 100644 --- a/glfw/x11_window.c +++ b/glfw/x11_window.c @@ -509,8 +509,8 @@ static bool createNativeWindow(_GLFWwindow* window, if (wndconfig->scaleToMonitor) { - width *= _glfw.x11.contentScaleX; - height *= _glfw.x11.contentScaleY; + width *= (int)_glfw.x11.contentScaleX; + height *= (int)_glfw.x11.contentScaleY; } // Create a colormap based on the visual used by the current context diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 89ca8d92c..a226dcf34 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -588,8 +588,8 @@ render_os_window(OSWindow *os_window, monotonic_t now, unsigned int active_windo bool static_live_resize_in_progress = os_window->live_resize.in_progress && OPT(resize_draw_strategy) == RESIZE_DRAW_STATIC; float x_ratio = 1, y_ratio = 1; if (static_live_resize_in_progress) { - x_ratio = os_window->viewport_width / (double) os_window->live_resize.width; - y_ratio = os_window->viewport_height / (double) os_window->live_resize.height; + x_ratio = (float) os_window->viewport_width / (float) os_window->live_resize.width; + y_ratio = (float) os_window->viewport_height / (float) os_window->live_resize.height; } if (!static_live_resize_in_progress) { draw_borders(br->vao_idx, br->num_border_rects, br->rect_buf, br->is_dirty, os_window->viewport_width, os_window->viewport_height, active_window_bg, num_visible_windows, all_windows_have_same_bg, os_window); diff --git a/kitty/fonts.c b/kitty/fonts.c index f2530c995..5dd5b4b04 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -409,9 +409,9 @@ calc_cell_metrics(FontGroup *fg) { unsigned int before_cell_height = cell_height; int cw = cell_width, ch = cell_height; if (OPT(adjust_line_height_px) != 0) ch += OPT(adjust_line_height_px); - if (OPT(adjust_line_height_frac) != 0.f) ch *= OPT(adjust_line_height_frac); + if (OPT(adjust_line_height_frac) != 0.f) ch = (int)(ch * OPT(adjust_line_height_frac)); if (OPT(adjust_column_width_px != 0)) cw += OPT(adjust_column_width_px); - if (OPT(adjust_column_width_frac) != 0.f) cw *= OPT(adjust_column_width_frac); + if (OPT(adjust_column_width_frac) != 0.f) cw = (int)(cw * OPT(adjust_column_width_frac)); #define MAX_DIM 1000 #define MIN_WIDTH 2 #define MIN_HEIGHT 4 @@ -1107,7 +1107,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, glyph_index glyph_id = glyph_id_for_codepoint(font->face, cpu_cell->ch); int width = get_glyph_width(font->face, glyph_id); - desired_cells = ceilf((float)width / fg->cell_width); + desired_cells = (unsigned int)ceilf((float)width / fg->cell_width); } unsigned int num_spaces = 0; diff --git a/kitty/freetype.c b/kitty/freetype.c index e9ee22b5c..90ee84a9a 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -77,12 +77,12 @@ static FT_Library library; static inline int font_units_to_pixels_y(Face *self, int x) { - return ceil((double)FT_MulFix(x, self->face->size->metrics.y_scale) / 64.0); + return (int)ceil((double)FT_MulFix(x, self->face->size->metrics.y_scale) / 64.0); } static inline int font_units_to_pixels_x(Face *self, int x) { - return ceil((double)FT_MulFix(x, self->face->size->metrics.x_scale) / 64.0); + return (int)ceil((double)FT_MulFix(x, self->face->size->metrics.x_scale) / 64.0); } @@ -140,7 +140,7 @@ set_font_size(Face *self, FT_F26Dot6 char_width, FT_F26Dot6 char_height, FT_UInt if (!error) { unsigned int ch = calc_cell_height(self, false); if (desired_height && ch != desired_height) { - FT_F26Dot6 h = floor((double)char_height * (double)desired_height / (double) ch); + FT_F26Dot6 h = (FT_F26Dot6)floor((double)char_height * (double)desired_height / (double) ch); return set_font_size(self, 0, h, xdpi, ydpi, 0, cell_height); } self->char_width = char_width; self->char_height = char_height; self->xdpi = xdpi; self->ydpi = ydpi; @@ -160,8 +160,8 @@ set_font_size(Face *self, FT_F26Dot6 char_width, FT_F26Dot6 char_height, FT_UInt int32_t min_diff = INT32_MAX; if (desired_height == 0) desired_height = cell_height; if (desired_height == 0) { - desired_height = ceil(((double)char_height / 64.) * (double)ydpi / 72.); - desired_height += ceil(0.2 * desired_height); + desired_height = (unsigned int)ceil(((double)char_height / 64.) * (double)ydpi / 72.); + desired_height += (unsigned int)ceil(0.2 * desired_height); } FT_Int strike_index = -1; for (FT_Int i = 0; i < self->face->num_fixed_sizes; i++) { @@ -190,7 +190,7 @@ set_size_for_face(PyObject *s, unsigned int desired_height, bool force, FONTS_DA FT_F26Dot6 w = (FT_F26Dot6)(ceil(fg->font_sz_in_pts * 64.0)); FT_UInt xdpi = (FT_UInt)fg->logical_dpi_x, ydpi = (FT_UInt)fg->logical_dpi_y; if (!force && (self->char_width == w && self->char_height == w && self->xdpi == xdpi && self->ydpi == ydpi)) return true; - ((Face*)self)->size_in_pts = fg->font_sz_in_pts; + ((Face*)self)->size_in_pts = (float)fg->font_sz_in_pts; return set_font_size(self, w, w, xdpi, ydpi, desired_height, fg->cell_height); } @@ -438,7 +438,7 @@ downsample_bitmap(ProcessedBitmap *bm, unsigned int width, unsigned int cell_hei // better with bi-cubic or lanczos, but at these small sizes I don't think // it matters float ratio = MAX((float)bm->width / width, (float)bm->rows / cell_height); - int factor = ceilf(ratio); + int factor = (int)ceilf(ratio); uint8_t *dest = calloc(4, width * cell_height); if (dest == NULL) fatal("Out of memory"); uint8_t *d = dest; @@ -499,8 +499,8 @@ render_color_bitmap(Face *self, int glyph_id, ProcessedBitmap *ans, unsigned int ans->rows = bitmap->rows; ans->pixel_mode = bitmap->pixel_mode; if (ans->width > num_cells * cell_width + 2) downsample_bitmap(ans, num_cells * cell_width, cell_height); - ans->bitmap_top = (float)self->face->glyph->bitmap_top / ans->factor; - ans->bitmap_left = (float)self->face->glyph->bitmap_left / ans->factor; + ans->bitmap_top = (int)((float)self->face->glyph->bitmap_top / ans->factor); + ans->bitmap_left = (int)((float)self->face->glyph->bitmap_left / ans->factor); detect_right_edge(ans); return true; } diff --git a/kitty/glfw.c b/kitty/glfw.c index 641075ee6..7978910df 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -816,8 +816,8 @@ get_physical_dpi(GLFWmonitor *m) { if (width == 0 || height == 0) { PyErr_SetString(PyExc_ValueError, "Failed to get primary monitor size"); return NULL; } const GLFWvidmode *vm = glfwGetVideoMode(m); if (vm == NULL) { PyErr_SetString(PyExc_ValueError, "Failed to get video mode for monitor"); return NULL; } - float dpix = vm->width / (width / 25.4); - float dpiy = vm->height / (height / 25.4); + float dpix = (float)(vm->width / (width / 25.4)); + float dpiy = (float)(vm->height / (height / 25.4)); return Py_BuildValue("ff", dpix, dpiy); } diff --git a/kitty/monotonic.h b/kitty/monotonic.h index b25677c0b..3efbd5534 100644 --- a/kitty/monotonic.h +++ b/kitty/monotonic.h @@ -54,7 +54,7 @@ static inline monotonic_t ms_to_monotonic_t(monotonic_t time) { } static inline int monotonic_t_to_ms(monotonic_t time) { - return time / 1000ll / 1000ll; + return (int)(time / 1000ll / 1000ll); } static inline double monotonic_t_to_s_double(monotonic_t time) { diff --git a/kitty/shaders.c b/kitty/shaders.c index 28c312c0c..ae38553aa 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -502,7 +502,7 @@ draw_cells(ssize_t vao_idx, ssize_t gvao_idx, GLfloat xstart, GLfloat ystart, GL bind_vao_uniform_buffer(vao_idx, uniform_buffer, cell_program_layouts[CELL_PROGRAM].render_data.index); bind_vertex_array(vao_idx); - float current_inactive_text_alpha = (!can_be_focused || screen->cursor_render_info.is_focused) && is_active_window ? 1.0 : OPT(inactive_text_alpha); + float current_inactive_text_alpha = (!can_be_focused || screen->cursor_render_info.is_focused) && is_active_window ? 1.0f : (float)OPT(inactive_text_alpha); set_cell_uniforms(current_inactive_text_alpha); GLfloat w = (GLfloat)screen->columns * dx, h = (GLfloat)screen->lines * dy; // The scissor limits below are calculated to ensure that they do not diff --git a/kitty/state.c b/kitty/state.c index 1e2f20cc7..c066c29e0 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -365,6 +365,11 @@ set_special_keys(PyObject *dict) { }} } +static inline float +PyFloat_AsFloat(PyObject *o) { + return (float)PyFloat_AsDouble(o); +} + PYWRAP0(next_window_id) { return PyLong_FromUnsignedLongLong(global_state.window_id_counter + 1); } @@ -400,11 +405,11 @@ PYWRAP1(set_options) { S(focus_follows_mouse, PyObject_IsTrue); S(cursor_blink_interval, parse_s_double_to_monotonic_t); S(cursor_stop_blinking_after, parse_s_double_to_monotonic_t); - S(background_opacity, PyFloat_AsDouble); - S(dim_opacity, PyFloat_AsDouble); + S(background_opacity, PyFloat_AsFloat); + S(dim_opacity, PyFloat_AsFloat); S(dynamic_background_opacity, PyObject_IsTrue); - S(inactive_text_alpha, PyFloat_AsDouble); - S(window_padding_width, PyFloat_AsDouble); + S(inactive_text_alpha, PyFloat_AsFloat); + S(window_padding_width, PyFloat_AsFloat); S(scrollback_pager_history_size, PyLong_AsUnsignedLong); S(cursor_shape, PyLong_AsLong); S(url_style, PyLong_AsUnsignedLong); @@ -436,7 +441,7 @@ PYWRAP1(set_options) { S(macos_show_window_title_in, window_title_in); S(macos_window_resizable, PyObject_IsTrue); S(macos_hide_from_tasks, PyObject_IsTrue); - S(macos_thicken_font, PyFloat_AsDouble); + S(macos_thicken_font, PyFloat_AsFloat); S(tab_bar_min_tabs, PyLong_AsUnsignedLong); S(disable_ligatures, PyLong_AsLong); S(resize_draw_strategy, PyLong_AsLong); diff --git a/setup.py b/setup.py index 68e474118..29020a557 100755 --- a/setup.py +++ b/setup.py @@ -212,7 +212,7 @@ def init_env( cppflags.append('-DDEBUG_{}'.format(el.upper().replace('-', '_'))) cflags = os.environ.get( 'OVERRIDE_CFLAGS', ( - '-Wextra -Wno-missing-field-initializers -Wall -Wstrict-prototypes -std=c11' + '-Wextra -Wfloat-conversion -Wno-missing-field-initializers -Wall -Wstrict-prototypes -std=c11' ' -pedantic-errors -Werror {} {} -fwrapv {} {} -pipe {} -fvisibility=hidden' ).format( optimize,