Skip to content

Commit 3c2ab96

Browse files
author
camilo
committed
fixes to some ffmath functions
1 parent ec788c5 commit 3c2ab96

File tree

1 file changed

+102
-8
lines changed

1 file changed

+102
-8
lines changed

src/ffmath.cpp

+102-8
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,120 @@ float ffmath::rCbrt( float x )
288288
/*============================================================================*/
289289
float ffmath::rounding( float x )
290290
{
291-
x += 12582912.0F;
292-
x -= 12582912.0F;
293-
return x;
291+
int32_t i0 = 0, j0;
292+
float ret = x;
293+
294+
cast_reinterpret( i0, x );
295+
/*cstat -MISRAC++2008-5-0-21 -MISRAC++2008-2-13-3 -ATH-neg-check-nonneg -ATH-shift-neg -CERT-INT34-C_c*/
296+
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
297+
if ( j0 < 23 ) {
298+
if ( j0 < 0 ) {
299+
i0 &= static_cast<int32_t>( 0x80000000 );
300+
if ( -1 == j0 ) {
301+
i0 |= 0x3F800000;
302+
}
303+
cast_reinterpret( ret, i0 );
304+
}
305+
else {
306+
const int32_t i = 0x007FFFFF >> j0;
307+
if ( 0 != ( i0 & i ) ) {
308+
i0 += 0x00400000 >> j0;
309+
i0 &= ~i;
310+
cast_reinterpret( ret, i0 );
311+
}
312+
}
313+
}
314+
/*cstat +MISRAC++2008-5-0-21 +MISRAC++2008-2-13-3 +ATH-neg-check-nonneg +ATH-shift-neg +CERT-INT34-C_c*/
315+
return ret;
294316
}
295317
/*============================================================================*/
296318
float ffmath::floor( float x )
297319
{
298-
return ffmath::rounding( x - 0.5F );
320+
int32_t i0 = 0, j0;
321+
float ret = x;
322+
323+
cast_reinterpret( i0, x );
324+
/*cstat -MISRAC++2008-5-0-21 -MISRAC++2008-2-13-3 -ATH-neg-check-nonneg -ATH-shift-neg -CERT-INT34-C_c*/
325+
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
326+
if ( j0 < 23 ) {
327+
if ( j0 < 0 ) {
328+
if ( i0 >= 0 ) {
329+
i0 = 0;
330+
}
331+
else if ( 0 != ( i0 & 0x7FFFFFFF ) ) {
332+
i0 = static_cast<int32_t>( 0xBF800000 );
333+
}
334+
else {
335+
/*nothing to do here*/
336+
}
337+
cast_reinterpret( ret, i0 );
338+
}
339+
else {
340+
const int32_t i = ( 0x007FFFFF ) >> j0;
341+
if ( 0 != ( i0 & i ) ) {
342+
if ( i0 < 0 ) {
343+
i0 += 0x00800000 >> j0;
344+
}
345+
i0 &= (~i);
346+
cast_reinterpret( ret, i0 );
347+
}
348+
}
349+
}
350+
/*cstat +MISRAC++2008-5-0-21 +MISRAC++2008-2-13-3 +ATH-neg-check-nonneg +ATH-shift-neg +CERT-INT34-C_c*/
351+
return ret;
299352
}
300353
/*============================================================================*/
301354
float ffmath::ceil( float x )
302355
{
303-
return ffmath::rounding( x + 0.5F );
356+
int32_t i0 = 0, j0;
357+
float ret = x;
358+
359+
cast_reinterpret( i0, x );
360+
/*cstat -MISRAC++2008-5-0-21 -MISRAC++2008-2-13-3 -ATH-neg-check-nonneg -ATH-shift-neg -CERT-INT34-C_c*/
361+
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
362+
if ( j0 < 23 ) {
363+
if ( j0 < 0 ) {
364+
if ( i0 < 0 ) {
365+
i0 = static_cast<int32_t>( 0x80000000 );
366+
}
367+
else if ( 0 != i0 ) {
368+
i0 = static_cast<int32_t>( 0x3F800000 );
369+
}
370+
else {
371+
/*nothing to do here*/
372+
}
373+
cast_reinterpret( ret, i0 );
374+
}
375+
else {
376+
const int32_t i = ( 0x007FFFFF ) >> j0;
377+
if ( 0 != ( i0 & i ) ) {
378+
if ( i0 > 0 ) {
379+
i0 += 0x00800000 >> j0;
380+
}
381+
i0 &= (~i);
382+
cast_reinterpret( ret, i0 );
383+
}
384+
}
385+
}
386+
/*cstat +MISRAC++2008-5-0-21 +MISRAC++2008-2-13-3 +ATH-neg-check-nonneg +ATH-shift-neg +CERT-INT34-C_c*/
387+
return ret;
304388
}
305389
/*============================================================================*/
306390
float ffmath::trunc( float x )
307391
{
308-
/*cstat -CERT-FLP34-C -CERT-FLP36-C*/
309-
return static_cast<float>( static_cast<int32_t>( x ) );
310-
/*cstat +CERT-FLP34-C +CERT-FLP36-C*/
392+
int32_t i0 = 0, sx, j0;
393+
float ret = x;
394+
395+
cast_reinterpret( i0, x );
396+
/*cstat -MISRAC++2008-5-0-21 -MISRAC++2008-2-13-3 -ATH-neg-check-nonneg -ATH-shift-neg -CERT-INT34-C_c*/
397+
sx = i0 & static_cast<int32_t>( 0x80000000 );
398+
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
399+
if ( j0 < 23 ) {
400+
const int32_t tmp = ( j0 < 0 ) ? sx : ( sx | ( i0 & ~( 0x007FFFFF >> j0 ) ) );
401+
cast_reinterpret( ret, tmp );
402+
}
403+
/*cstat +MISRAC++2008-5-0-21 +MISRAC++2008-2-13-3 +ATH-neg-check-nonneg +ATH-shift-neg +CERT-INT34-C_c*/
404+
return ret;
311405
}
312406
/*============================================================================*/
313407
float ffmath::frac( float x )

0 commit comments

Comments
 (0)