From 7c37134702a662f82ab2749a6a59727de8d5e460 Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 07:52:06 +0200 Subject: [PATCH 1/6] Added ARM mikroc round function --- ARM/mikroC/std_library/cmath.c | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/ARM/mikroC/std_library/cmath.c b/ARM/mikroC/std_library/cmath.c index fd9ccf321..cd43656cb 100644 --- a/ARM/mikroC/std_library/cmath.c +++ b/ARM/mikroC/std_library/cmath.c @@ -127,6 +127,65 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + { + return -1.0; + } + else + { + return 0.0; + } + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + { + return num; + } + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if( i >= 0 ) + { + if( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) \ From 5d06495487e860438121678a669dabe7c6658b1b Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 07:56:54 +0200 Subject: [PATCH 2/6] Added AVR mikroC round function --- ARM/mikroC/std_library/cmath.c | 14 +++------ AVR/mikroC/std_library/cmath.c | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/ARM/mikroC/std_library/cmath.c b/ARM/mikroC/std_library/cmath.c index cd43656cb..aed893659 100644 --- a/ARM/mikroC/std_library/cmath.c +++ b/ARM/mikroC/std_library/cmath.c @@ -137,26 +137,20 @@ double round( double num ) if ( expon < 0 ) { if ( num < 0.0 ) - { return -1.0; - } else - { return 0.0; - } } if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) - { return num; - } i = _FRNDINT( num ); double diff_down; double diff_up; - if( i < 0 ) + if ( i < 0 ) { diff_down = i - num; diff_up = i - 1.0 - num; @@ -170,16 +164,16 @@ double round( double num ) diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); - if( i >= 0 ) + if ( i >= 0 ) { - if( diff_down < diff_up ) + if ( diff_down < diff_up ) return i; else return i + 1.0; } else { - if( diff_down >= diff_up ) + if ( diff_down >= diff_up ) return i - 1.0; else return i; diff --git a/AVR/mikroC/std_library/cmath.c b/AVR/mikroC/std_library/cmath.c index 0969c04b8..8b367c4bb 100644 --- a/AVR/mikroC/std_library/cmath.c +++ b/AVR/mikroC/std_library/cmath.c @@ -123,6 +123,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) From 58721115cddc67601eb2ef972592e81ef1ed1dc3 Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 07:59:40 +0200 Subject: [PATCH 3/6] Added dsPIC mikroC round function --- dsPIC/mikroC/std_library/cmath.c | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/dsPIC/mikroC/std_library/cmath.c b/dsPIC/mikroC/std_library/cmath.c index 9780aca62..e74116308 100644 --- a/dsPIC/mikroC/std_library/cmath.c +++ b/dsPIC/mikroC/std_library/cmath.c @@ -123,6 +123,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) From 5b13a0c442e5804c95fa0b7e9c45c9a66c61795b Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 08:01:58 +0200 Subject: [PATCH 4/6] Added PIC mikroC round function --- PIC/mikroC/std_library/cmath.c | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/PIC/mikroC/std_library/cmath.c b/PIC/mikroC/std_library/cmath.c index aacb7cc3f..cb13304b3 100644 --- a/PIC/mikroC/std_library/cmath.c +++ b/PIC/mikroC/std_library/cmath.c @@ -123,6 +123,61 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + { + return num; + } + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) From 6b7fbfa2bcd5e394a606930b097f165a22760da2 Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 08:04:59 +0200 Subject: [PATCH 5/6] Added PIC32 mikroC round function --- PIC/mikroC/std_library/cmath.c | 2 -- PIC32/mikroC/std_library/cmath.c | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/PIC/mikroC/std_library/cmath.c b/PIC/mikroC/std_library/cmath.c index cb13304b3..1393ecb86 100644 --- a/PIC/mikroC/std_library/cmath.c +++ b/PIC/mikroC/std_library/cmath.c @@ -139,9 +139,7 @@ double round( double num ) } if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) - { return num; - } i = _FRNDINT( num ); diff --git a/PIC32/mikroC/std_library/cmath.c b/PIC32/mikroC/std_library/cmath.c index e6fee03ba..57be603d9 100644 --- a/PIC32/mikroC/std_library/cmath.c +++ b/PIC32/mikroC/std_library/cmath.c @@ -129,6 +129,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) \ From 8b33626a8f3b2d2835292036e78b80b507000156 Mon Sep 17 00:00:00 2001 From: Ivan Ruzavin Date: Tue, 30 Jul 2024 08:10:36 +0200 Subject: [PATCH 6/6] Added round function to all mikroC headers --- ARM/mikroC/std_library/include/math.h | 11 +++++++++++ AVR/mikroC/std_library/include/math.h | 11 +++++++++++ PIC/mikroC/std_library/include/math.h | 11 +++++++++++ PIC32/mikroC/std_library/include/math.h | 11 +++++++++++ dsPIC/mikroC/std_library/include/math.h | 11 +++++++++++ 5 files changed, 55 insertions(+) diff --git a/ARM/mikroC/std_library/include/math.h b/ARM/mikroC/std_library/include/math.h index 4e4727528..14886c5f3 100644 --- a/ARM/mikroC/std_library/include/math.h +++ b/ARM/mikroC/std_library/include/math.h @@ -67,6 +67,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * The frexp function breaks a floating-point number into a normalized diff --git a/AVR/mikroC/std_library/include/math.h b/AVR/mikroC/std_library/include/math.h index 8ac70baaf..52910e338 100644 --- a/AVR/mikroC/std_library/include/math.h +++ b/AVR/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The frexp function breaks a floating-point number into a normalized diff --git a/PIC/mikroC/std_library/include/math.h b/PIC/mikroC/std_library/include/math.h index 7975930e2..c982e67c3 100644 --- a/PIC/mikroC/std_library/include/math.h +++ b/PIC/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The frexp function breaks a floating-point number into a normalized diff --git a/PIC32/mikroC/std_library/include/math.h b/PIC32/mikroC/std_library/include/math.h index 9654818d8..4f35f3513 100644 --- a/PIC32/mikroC/std_library/include/math.h +++ b/PIC32/mikroC/std_library/include/math.h @@ -68,6 +68,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details frexp function breaks a floating-point number into a normalized diff --git a/dsPIC/mikroC/std_library/include/math.h b/dsPIC/mikroC/std_library/include/math.h index faf463ef1..8fe86ab0d 100644 --- a/dsPIC/mikroC/std_library/include/math.h +++ b/dsPIC/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The function breaks a floating-point number into a normalized