summaryrefslogtreecommitdiffstats
path: root/libgcc/config/avr/lib2funcs.c
blob: 774d14cedad8625b38f1745e3773accc9a96e0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Copyright (C) 2013-2014 Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */


/* This file supplies implementations for some AVR-specific builtin
   functions so that code like the following works as expected:

   int (*f (void))(_Fract)
   {
       return __builtin_avr_countlsr;
   }

   In this specific case, the generated code is:

   f:
       ldi r24,lo8(gs(__countlsHI))
       ldi r25,hi8(gs(__countlsHI))
       ret
*/

/* Map fixed-point suffix to the corresponding fixed-point type.  */

typedef short _Fract fx_hr_t;
typedef _Fract fx_r_t;
typedef long _Fract fx_lr_t;
typedef long long _Fract fx_llr_t;

typedef unsigned short _Fract fx_uhr_t;
typedef unsigned _Fract fx_ur_t;
typedef unsigned long _Fract fx_ulr_t;
typedef unsigned long long _Fract fx_ullr_t;

typedef short _Accum fx_hk_t;
typedef _Accum fx_k_t;
typedef long _Accum fx_lk_t;
typedef long long _Accum fx_llk_t;

typedef unsigned short _Accum fx_uhk_t;
typedef unsigned _Accum fx_uk_t;
typedef unsigned long _Accum fx_ulk_t;
typedef unsigned long long _Accum fx_ullk_t;

/* Map fixed-point suffix to the corresponding natural integer type.  */

typedef char int_hr_t;
typedef int int_r_t;
typedef long int_lr_t;
typedef long long int_llr_t;

typedef unsigned char int_uhr_t;
typedef unsigned int int_ur_t;
typedef unsigned long int_ulr_t;
typedef unsigned long long int_ullr_t;

typedef int int_hk_t;
typedef long int_k_t;
typedef long long int_lk_t;
typedef long long int_llk_t;

typedef unsigned int int_uhk_t;
typedef unsigned long int_uk_t;
typedef unsigned long long int_ulk_t;
typedef unsigned long long int_ullk_t;

/* Map mode to the corresponding integer type.  */

typedef char int_qi_t;
typedef int int_hi_t;
typedef long int_si_t;
typedef long long int_di_t;

typedef unsigned char uint_qi_t;
typedef unsigned int uint_hi_t;
typedef unsigned long uint_si_t;
typedef unsigned long long uint_di_t;



/************************************************************************/

/* Supply implementations / symbols for __builtin_roundFX ASM_NAME.  */

#ifdef L_round

#define ROUND1(FX)                              \
  ROUND2 (FX)

#define ROUND2(FX)                                                      \
  extern fx_## FX ##_t __round## FX (fx_## FX ##_t x, int rpoint);      \
                                                                        \
  fx_## FX ##_t                                                         \
  __round## FX (fx_## FX ##_t x, int rpoint)                            \
  {                                                                     \
    return __builtin_avr_round ##FX (x, rpoint);                        \
  }

ROUND1(L_LABEL)

#endif /* L_round */



/*********************************************************************/

/* Implement some count-leading-redundant-sign-bits to be used with
   coundlsFX implementation.  */

#ifdef L__clrsbqi
extern int __clrsbqi2 (char x);

int
__clrsbqi2 (char x)
{
  int ret;

  if (x < 0)
    x = ~x;

  if (x == 0)
    return 8 * sizeof (x) -1;

  ret = __builtin_clz (x << 8);
  return ret - 1;
}
#endif /* L__clrsbqi */


#ifdef L__clrsbdi
extern int __clrsbdi2 (long long x);

int
__clrsbdi2 (long long x)
{
  int ret;

  if (x < 0LL)
    x = ~x;

  if (x == 0LL)
    return 8 * sizeof (x) -1;

  ret = __builtin_clzll ((unsigned long long) x);
  return ret - 1;
}
#endif /* L__clrsbdi */



/*********************************************************************/

/* Supply implementations / symbols for __builtin_avr_countlsFX.  */

/* Signed */

#ifdef L_countls

#define COUNTLS1(MM)                            \
  COUNTLS2 (MM)

#define COUNTLS2(MM)                                                    \
  extern int __countls## MM ##2 (int_## MM ##_t);                       \
  extern int __clrsb## MM ##2 (int_## MM ##_t);                         \
                                                                        \
  int                                                                   \
  __countls## MM ##2 (int_## MM ##_t x)                                 \
  {                                                                     \
    if (x == 0)                                                         \
      return __INT8_MAX__;                                              \
                                                                        \
    return __clrsb## MM ##2 (x);                                        \
  }

COUNTLS1(L_LABEL)

#endif /* L_countls */

/* Unsigned */

#ifdef L_countlsu

#define clz_qi2 __builtin_clz /* unused, avoid warning */
#define clz_hi2 __builtin_clz
#define clz_si2 __builtin_clzl
#define clz_di2 __builtin_clzll

#define COUNTLS1(MM)                            \
  COUNTLS2 (MM)

#define COUNTLS2(MM)                                                    \
  extern int __countlsu## MM ##2 (uint_## MM ##_t);                     \
                                                                        \
  int                                                                   \
  __countlsu## MM ##2 (uint_## MM ##_t x)                               \
  {                                                                     \
    if (x == 0)                                                         \
      return __INT8_MAX__;                                              \
                                                                        \
    if (sizeof (x) == 1)                                                \
      return clz_hi2 (x << 8);                                          \
    else                                                                \
      return clz_## MM ##2 (x);                                         \
  }

COUNTLS1(L_LABEL)

#endif /* L_countlsu */
OpenPOWER on IntegriCloud