Path: blob/master/drivers/gpu/drm/amd/display/dc/basics/conversion.c
26552 views
/*1* Copyright 2012-15 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21* Authors: AMD22*23*/2425#include "dm_services.h"26#include "basics/conversion.h"2728#define DIVIDER 100002930/* S2D13 value in [-3.00...0.9999] */31#define S2D13_MIN (-3 * DIVIDER)32#define S2D13_MAX (3 * DIVIDER)3334uint16_t fixed_point_to_int_frac(35struct fixed31_32 arg,36uint8_t integer_bits,37uint8_t fractional_bits)38{39int32_t numerator;40int32_t divisor = 1 << fractional_bits;4142uint16_t result;4344uint16_t d = (uint16_t)dc_fixpt_floor(45dc_fixpt_abs(46arg));4748if (d <= (uint16_t)(1 << integer_bits) - (1 / (uint16_t)divisor))49numerator = (uint16_t)dc_fixpt_round(50dc_fixpt_mul_int(51arg,52divisor));53else {54numerator = dc_fixpt_floor(55dc_fixpt_sub(56dc_fixpt_from_int(571LL << integer_bits),58dc_fixpt_recip(59dc_fixpt_from_int(60divisor))));61}6263if (numerator >= 0)64result = (uint16_t)numerator;65else66result = (uint16_t)(67(1 << (integer_bits + fractional_bits + 1)) + numerator);6869if ((result != 0) && dc_fixpt_lt(70arg, dc_fixpt_zero))71result |= 1 << (integer_bits + fractional_bits);7273return result;74}75/*76* convert_float_matrix - This converts a double into HW register spec defined format S2D13.77*/78void convert_float_matrix(79uint16_t *matrix,80struct fixed31_32 *flt,81uint32_t buffer_size)82{83const struct fixed31_32 min_2_13 =84dc_fixpt_from_fraction(S2D13_MIN, DIVIDER);85const struct fixed31_32 max_2_13 =86dc_fixpt_from_fraction(S2D13_MAX, DIVIDER);87uint32_t i;8889for (i = 0; i < buffer_size; ++i) {90uint32_t reg_value =91fixed_point_to_int_frac(92dc_fixpt_clamp(93flt[i],94min_2_13,95max_2_13),962,9713);9899matrix[i] = (uint16_t)reg_value;100}101}102103static struct fixed31_32 int_frac_to_fixed_point(uint16_t arg,104uint8_t integer_bits,105uint8_t fractional_bits)106{107struct fixed31_32 result;108uint16_t sign_mask = 1 << (fractional_bits + integer_bits);109uint16_t value_mask = sign_mask - 1;110111result.value = (long long)(arg & value_mask) <<112(FIXED31_32_BITS_PER_FRACTIONAL_PART - fractional_bits);113114if (arg & sign_mask)115result = dc_fixpt_neg(result);116117return result;118}119120/**121* convert_hw_matrix - converts HW values into fixed31_32 matrix.122* @matrix: fixed point 31.32 matrix123* @reg: array of register values124* @buffer_size: size of the array of register values125*126* Converts HW register spec defined format S2D13 into a fixed-point 31.32127* matrix.128*/129void convert_hw_matrix(struct fixed31_32 *matrix,130uint16_t *reg,131uint32_t buffer_size)132{133for (int i = 0; i < buffer_size; ++i)134matrix[i] = int_frac_to_fixed_point(reg[i], 2, 13);135}136137static uint32_t find_gcd(uint32_t a, uint32_t b)138{139uint32_t remainder;140141while (b != 0) {142remainder = a % b;143a = b;144b = remainder;145}146return a;147}148149void reduce_fraction(uint32_t num, uint32_t den,150uint32_t *out_num, uint32_t *out_den)151{152uint32_t gcd = 0;153154gcd = find_gcd(num, den);155*out_num = num / gcd;156*out_den = den / gcd;157}158159160