xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/intrinsics/mvbits.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg /* Implementation of the MVBITS intrinsic
2*b1e83836Smrg    Copyright (C) 2004-2022 Free Software Foundation, Inc.
3181254a7Smrg    Contributed by Tobias Schlüter
4181254a7Smrg 
5181254a7Smrg This file is part of the GNU Fortran 95 runtime library (libgfortran).
6181254a7Smrg 
7181254a7Smrg Libgfortran is free software; you can redistribute it and/or
8181254a7Smrg modify it under the terms of the GNU General Public
9181254a7Smrg License as published by the Free Software Foundation; either
10181254a7Smrg version 3 of the License, or (at your option) any later version.
11181254a7Smrg 
12181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
13181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15181254a7Smrg GNU General Public License for more details.
16181254a7Smrg 
17181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
18181254a7Smrg permissions described in the GCC Runtime Library Exception, version
19181254a7Smrg 3.1, as published by the Free Software Foundation.
20181254a7Smrg 
21181254a7Smrg You should have received a copy of the GNU General Public License and
22181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
23181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24181254a7Smrg <http://www.gnu.org/licenses/>.  */
25181254a7Smrg 
26181254a7Smrg /* TODO: This should be replaced by a compiler builtin.  */
27181254a7Smrg 
28181254a7Smrg #ifndef SUB_NAME
29181254a7Smrg #include <libgfortran.h>
30181254a7Smrg #endif
31181254a7Smrg 
32181254a7Smrg #ifdef SUB_NAME
33181254a7Smrg /* MVBITS copies LEN bits starting at bit position FROMPOS from FROM
34181254a7Smrg    into TO, starting at bit position TOPOS.  */
35181254a7Smrg 
36181254a7Smrg extern void SUB_NAME (const TYPE *, const int *, const int *, TYPE *,
37181254a7Smrg 		      const int *);
38181254a7Smrg export_proto(SUB_NAME);
39181254a7Smrg 
40181254a7Smrg void
SUB_NAME(const TYPE * from,const int * frompos,const int * len,TYPE * to,const int * topos)41181254a7Smrg SUB_NAME (const TYPE *from, const int *frompos, const int *len, TYPE *to,
42181254a7Smrg 	  const int *topos)
43181254a7Smrg {
44181254a7Smrg   TYPE oldbits, newbits, lenmask;
45181254a7Smrg 
46181254a7Smrg   lenmask = (*len == sizeof (TYPE)*8) ? ~(TYPE)0 : ((TYPE)1 << *len) - 1;
47181254a7Smrg   newbits = (((UTYPE)(*from) >> *frompos) & lenmask) << *topos;
48181254a7Smrg   oldbits = *to & (~(lenmask << *topos));
49181254a7Smrg 
50181254a7Smrg   *to = newbits | oldbits;
51181254a7Smrg }
52181254a7Smrg #endif
53181254a7Smrg 
54181254a7Smrg #ifndef SUB_NAME
55181254a7Smrg #  define TYPE GFC_INTEGER_1
56181254a7Smrg #  define UTYPE GFC_UINTEGER_1
57181254a7Smrg #  define SUB_NAME mvbits_i1
58181254a7Smrg #  include "mvbits.c"
59181254a7Smrg #  undef SUB_NAME
60181254a7Smrg #  undef TYPE
61181254a7Smrg #  undef UTYPE
62181254a7Smrg 
63181254a7Smrg #  define TYPE GFC_INTEGER_2
64181254a7Smrg #  define UTYPE GFC_UINTEGER_2
65181254a7Smrg #  define SUB_NAME mvbits_i2
66181254a7Smrg #  include "mvbits.c"
67181254a7Smrg #  undef SUB_NAME
68181254a7Smrg #  undef TYPE
69181254a7Smrg #  undef UTYPE
70181254a7Smrg 
71181254a7Smrg #  define TYPE GFC_INTEGER_4
72181254a7Smrg #  define UTYPE GFC_UINTEGER_4
73181254a7Smrg #  define SUB_NAME mvbits_i4
74181254a7Smrg #  include "mvbits.c"
75181254a7Smrg #  undef SUB_NAME
76181254a7Smrg #  undef TYPE
77181254a7Smrg #  undef UTYPE
78181254a7Smrg 
79181254a7Smrg #  define TYPE GFC_INTEGER_8
80181254a7Smrg #  define UTYPE GFC_UINTEGER_8
81181254a7Smrg #  define SUB_NAME mvbits_i8
82181254a7Smrg #  include "mvbits.c"
83181254a7Smrg #  undef SUB_NAME
84181254a7Smrg #  undef TYPE
85181254a7Smrg #  undef UTYPE
86181254a7Smrg 
87181254a7Smrg #if defined (HAVE_GFC_INTEGER_16)
88181254a7Smrg #  define TYPE GFC_INTEGER_16
89181254a7Smrg #  define UTYPE GFC_UINTEGER_16
90181254a7Smrg #  define SUB_NAME mvbits_i16
91181254a7Smrg #  include "mvbits.c"
92181254a7Smrg #  undef SUB_NAME
93181254a7Smrg #  undef TYPE
94181254a7Smrg #  undef UTYPE
95181254a7Smrg #endif
96181254a7Smrg #endif
97