Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
Math.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Math.cc (C) 2000-2015 */
9/* */
10/* Fonctions mathématiques diverses. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/utils/Math.h"
17#include "arcane/utils/ArrayView.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22ARCANE_BEGIN_NAMESPACE
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace
28{
29union DoubleInt
30{
31 double dblVal;
32 unsigned long long intVal;
33 explicit DoubleInt(const double x) : dblVal(x) {}
34};
35
36// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
37int digit_to_bits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 41, 31, 26, 21, 16, 12, 11 };
38
39inline double _doTruncate(double x,unsigned int numDiscardBits)
40{
41 DoubleInt num(x);
42 if (numDiscardBits>0){
43 unsigned long long halfBit = (1ULL << (numDiscardBits -1));
44 unsigned long long mantissaDiscardMask = ~(halfBit-1) ^ 0xfff0000000000000ULL;
45 unsigned long long test = num.intVal & mantissaDiscardMask;
46 num.intVal &= 0xfff0000000000000ULL;
47 test += halfBit;
48 if (test== 0x0010000000000000ULL){
49 num.dblVal *= 2.0;
50 }
51 else{
52 num.intVal |= test & ~halfBit;
53 }
54 }
55 return num.dblVal;
56}
57
58inline double _doTruncateDigit(double x,int nb_digit)
59{
60 if (nb_digit<=0)
61 return x;
62 if (nb_digit<=8)
63 return (float)x;
64 if (nb_digit>=15)
65 return x;
66 int nb_bit = digit_to_bits[nb_digit];
67 if (nb_bit==0)
68 return x;
69 return _doTruncate(x,nb_bit);
70}
71
72}
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77extern double math::
78truncateDouble(double v,Integer nb_digit)
79{
80 return _doTruncateDigit(v,nb_digit);
81}
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85
86extern void math::
87truncateDouble(ArrayView<double> values,Integer nb_digit)
88{
89 Integer n = values.size();
90 if (n==0)
91 return;
92
93 if (nb_digit<=0)
94 return;
95 if (nb_digit<=8){
96 for( Integer i=0; i<n; ++i )
97 values[i] = (float)values[i];
98 return;
99 }
100 if (nb_digit>=15)
101 return;
102 int nb_bit = digit_to_bits[nb_digit];
103 if (nb_bit==0)
104 return;
105 for( Integer i=0; i<n; ++i )
106 values[i] = _doTruncate(values[i],nb_bit);
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112ARCANE_END_NAMESPACE
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
Vue modifiable d'un tableau d'un type T.
constexpr Integer size() const noexcept
Retourne la taille du tableau.