Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / config / picochip / libgccExtras / ashrsi3.c
blob9159e0f5025889b3be9b9dd5f9d3a578ea21f7ad
1 /*
3 picoChip GCC support for 32-bit arithmetic shift right.
5 Copyright (C) 2003, 2004, 2005, 2008 Free Software Foundation, Inc.
6 Contributed by picoChip Designs Ltd.
7 Maintained by Daniel Towner (daniel.towner@picochip.com)
9 This file is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
23 This file is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
33 typedef int HItype __attribute__ ((mode (HI)));
34 typedef unsigned int UHItype __attribute__ ((mode (HI)));
35 typedef unsigned int USItype __attribute__ ((mode (SI)));
37 typedef struct USIstruct {
38 UHItype low, high;
39 } USIstruct;
41 typedef union USIunion {
42 USItype l;
43 USIstruct s;
44 } USIunion;
46 USItype __ashrsi3(USIunion value, HItype count) {
47 USIunion result;
48 int temp;
49 int wordOfSignBits;
51 /* Ignore a zero count until we get into the (count < 16)
52 clause. This is slightly slower when shifting by zero, but faster
53 and smaller in all other cases (due to the better scheduling
54 opportunities available by putting the test near computational
55 instructions. */
56 /* if (count == 0) return value.l; */
58 if (count < 16) {
59 /* Shift low and high words by the count. The high word must use
60 an arithmetic shift. There is no arithmetic shift-right by
61 variable, so synthesise it. */
62 int signWord;
63 int reverseCount;
65 /* Shift low and high parts by the count. The upper word now has
66 invalid signed bits. */
67 result.s.low = value.s.low >> count;
68 result.s.high = value.s.high >> count;
70 if (count != 0) {
72 reverseCount = 16 - count;
74 /* Given a word of sign bits, shift back left to create the
75 destination sign bits. */
76 wordOfSignBits = __builtin_asri(value.s.high, 15);
77 signWord = wordOfSignBits << reverseCount;
78 result.s.high |= signWord;
80 /* There is now a hole in the upper `count' bits of the low
81 word. Shift the lower `count' bits of the upper word into the
82 low word. */
83 temp = value.s.high << reverseCount;
84 result.s.low |= temp;
87 } else {
88 int signWord;
90 /* Shift is greater than one word, so top word will always be set
91 to sign bits, and bottom word will be shifted from top word. */
92 result.s.low = value.s.high >> count;
93 result.s.high = __builtin_asri(value.s.high, 15);
95 if (count != 16) {
97 /* Shift the upper word of the source into the lower word of the
98 result. Arithmetically shift the upper word as well, to retain
99 the sign. This shift must be synthesised, as no such shift
100 exists in the instruction set. */
101 int signWord;
104 /* Given a complete word of sign-bits, shift this back left to
105 create the destination sign bits. */
106 signWord = result.s.high << (16 - count);
107 // signWord = wordOfSignBits << (16 - count);
109 /* Insert the sign bits to the result's low word. */
110 result.s.low |= signWord;
116 return result.l;