15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <cmath> 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <limits> 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/gfx_export.h" 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace gfx { 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline int ClampToInt(float value) { 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value != value) 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 0; // no int NaN. 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value >= std::numeric_limits<int>::max()) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return std::numeric_limits<int>::max(); 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value <= std::numeric_limits<int>::min()) 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return std::numeric_limits<int>::min(); 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return static_cast<int>(value); 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline int ToFlooredInt(float value) { 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ClampToInt(std::floor(value)); 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline int ToCeiledInt(float value) { 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ClampToInt(std::ceil(value)); 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline int ToRoundedInt(float value) { 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float rounded; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value >= 0.0f) 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rounded = std::floor(value + 0.5f); 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) else 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) rounded = std::ceil(value - 0.5f); 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ClampToInt(rounded); 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool IsExpressibleAsInt(float value) { 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value != value) 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; // no int NaN. 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value > std::numeric_limits<int>::max()) 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (value < std::numeric_limits<int>::min()) 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return false; 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return true; 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace gfx 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 55