Debian Codegraph @

code/client/snd_altivec.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /* This file is only compiled for PowerPC builds with Altivec support.
24 Altivec intrinsics need to be in a separate file, so GCC's -maltivec
25 command line can enable them, but give us the option to _not_ use that
26 on other files, where the compiler might then generate Altivec
27 instructions for normal floating point, crashing on G3 (etc) processors. */
28
29 #include "client.h"
30 #include "snd_local.h"
31
32 #if idppc_altivec
33
34 #if !defined(__APPLE__)
35 #include <altivec.h>
36 #endif
37
38 void S_PaintChannelFrom16_altivec( portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE], int snd_vol, channel_t *ch, const sfx_t *sc, int count, int sampleOffset, int bufferOffset ) {
39 int data, aoff, boff;
40 int leftvol, rightvol;
41 int i, j;
42 portable_samplepair_t *samp;
43 sndBuffer *chunk;
44 short *samples;
45 float ooff, fdata[2], fdiv, fleftvol, frightvol;
46
47 if (sc->soundChannels <= 0) {
48 return;
49 }
50
51 samp = &paintbuffer[ bufferOffset ];
52
53 if (ch->doppler) {
54 sampleOffset = sampleOffset*ch->oldDopplerScale;
55 }
56
57 if ( sc->soundChannels == 2 ) {
58 sampleOffset *= sc->soundChannels;
59
60 if ( sampleOffset & 1 ) {
61 sampleOffset &= ~1;
62 }
63 }
64
65 chunk = sc->soundData;
66 while (sampleOffset>=SND_CHUNK_SIZE) {
67 chunk = chunk->next;
68 sampleOffset -= SND_CHUNK_SIZE;
69 if (!chunk) {
70 chunk = sc->soundData;
71 }
72 }
73
74 if (!ch->doppler || ch->dopplerScale==1.0f) {
75 vector signed short volume_vec;
76 vector unsigned int volume_shift;
77 int vectorCount, samplesLeft, chunkSamplesLeft;
78 leftvol = ch->leftvol*snd_vol;
79 rightvol = ch->rightvol*snd_vol;
80 samples = chunk->sndChunk;
81 ((short *)&volume_vec)[0] = leftvol;
82 ((short *)&volume_vec)[1] = leftvol;
83 ((short *)&volume_vec)[4] = leftvol;
84 ((short *)&volume_vec)[5] = leftvol;
85 ((short *)&volume_vec)[2] = rightvol;
86 ((short *)&volume_vec)[3] = rightvol;
87 ((short *)&volume_vec)[6] = rightvol;
88 ((short *)&volume_vec)[7] = rightvol;
89 volume_shift = vec_splat_u32(8);
90 i = 0;
91
92 while(i < count) {
93 /* Try to align destination to 16-byte boundary */
94 while(i < count && (((unsigned long)&samp[i] & 0x1f) || ((count-i) < 8) || ((SND_CHUNK_SIZE - sampleOffset) < 8))) {
95 data = samples[sampleOffset++];
96 samp[i].left += (data * leftvol)>>8;
97
98 if ( sc->soundChannels == 2 ) {
99 data = samples[sampleOffset++];
100 }
101 samp[i].right += (data * rightvol)>>8;
102
103 if (sampleOffset == SND_CHUNK_SIZE) {
104 chunk = chunk->next;
105 samples = chunk->sndChunk;
106 sampleOffset = 0;
107 }
108 i++;
109 }
110 /* Destination is now aligned. Process as many 8-sample
111 chunks as we can before we run out of room from the current
112 sound chunk. We do 8 per loop to avoid extra source data reads. */
113 samplesLeft = count - i;
114 chunkSamplesLeft = SND_CHUNK_SIZE - sampleOffset;
115 if(samplesLeft > chunkSamplesLeft)
116 samplesLeft = chunkSamplesLeft;
117
118 vectorCount = samplesLeft / 8;
119
120 if(vectorCount)
121 {
122 vector unsigned char tmp;
123 vector short s0, s1, sampleData0, sampleData1;
124 vector signed int merge0, merge1;
125 vector signed int d0, d1, d2, d3;
126 vector unsigned char samplePermute0 =
127 VECCONST_UINT8(0, 1, 4, 5, 0, 1, 4, 5, 2, 3, 6, 7, 2, 3, 6, 7);
128 vector unsigned char samplePermute1 =
129 VECCONST_UINT8(8, 9, 12, 13, 8, 9, 12, 13, 10, 11, 14, 15, 10, 11, 14, 15);
130 vector unsigned char loadPermute0, loadPermute1;
131
132 // Rather than permute the vectors after we load them to do the sample
133 // replication and rearrangement, we permute the alignment vector so
134 // we do everything in one step below and avoid data shuffling.
135 tmp = vec_lvsl(0,&samples[sampleOffset]);
136 loadPermute0 = vec_perm(tmp,tmp,samplePermute0);
137 loadPermute1 = vec_perm(tmp,tmp,samplePermute1);
138
139 s0 = *(vector short *)&samples[sampleOffset];
140 while(vectorCount)
141 {
142 /* Load up source (16-bit) sample data */
143 s1 = *(vector short *)&samples[sampleOffset+7];
144
145 /* Load up destination sample data */
146 d0 = *(vector signed int *)&samp[i];
147 d1 = *(vector signed int *)&samp[i+2];
148 d2 = *(vector signed int *)&samp[i+4];
149 d3 = *(vector signed int *)&samp[i+6];
150
151 sampleData0 = vec_perm(s0,s1,loadPermute0);
152 sampleData1 = vec_perm(s0,s1,loadPermute1);
153
154 merge0 = vec_mule(sampleData0,volume_vec);
155 merge0 = vec_sra(merge0,volume_shift); /* Shift down to proper range */
156
157 merge1 = vec_mulo(sampleData0,volume_vec);
158 merge1 = vec_sra(merge1,volume_shift);
159
160 d0 = vec_add(merge0,d0);
161 d1 = vec_add(merge1,d1);
162
163 merge0 = vec_mule(sampleData1,volume_vec);
164 merge0 = vec_sra(merge0,volume_shift); /* Shift down to proper range */
165
166 merge1 = vec_mulo(sampleData1,volume_vec);
167 merge1 = vec_sra(merge1,volume_shift);
168
169 d2 = vec_add(merge0,d2);
170 d3 = vec_add(merge1,d3);
171
172 /* Store destination sample data */
173 *(vector signed int *)&samp[i] = d0;
174 *(vector signed int *)&samp[i+2] = d1;
175 *(vector signed int *)&samp[i+4] = d2;
176 *(vector signed int *)&samp[i+6] = d3;
177
178 i += 8;
179 vectorCount--;
180 s0 = s1;
181 sampleOffset += 8;
182 }
183 if (sampleOffset == SND_CHUNK_SIZE) {
184 chunk = chunk->next;
185 samples = chunk->sndChunk;
186 sampleOffset = 0;
187 }
188 }
189 }
190 } else {
191 fleftvol = ch->leftvol*snd_vol;
192 frightvol = ch->rightvol*snd_vol;
193
194 ooff = sampleOffset;
195 samples = chunk->sndChunk;
196
197 for ( i=0 ; i<count ; i++ ) {
198
199 aoff = ooff;
200 ooff = ooff + ch->dopplerScale * sc->soundChannels;
201 boff = ooff;
202 fdata[0] = fdata[1] = 0;
203 for (j=aoff; j<boff; j += sc->soundChannels) {
204 if (j == SND_CHUNK_SIZE) {
205 chunk = chunk->next;
206 if (!chunk) {
207 chunk = sc->soundData;
208 }
209 samples = chunk->sndChunk;
210 ooff -= SND_CHUNK_SIZE;
211 }
212 if ( sc->soundChannels == 2 ) {
213 fdata[0] += samples[j&(SND_CHUNK_SIZE-1)];
214 fdata[1] += samples[(j+1)&(SND_CHUNK_SIZE-1)];
215 } else {
216 fdata[0] += samples[j&(SND_CHUNK_SIZE-1)];
217 fdata[1] += samples[j&(SND_CHUNK_SIZE-1)];
218 }
219 }
220 fdiv = 256 * (boff-aoff) / sc->soundChannels;
221 samp[i].left += (fdata[0] * fleftvol)/fdiv;
222 samp[i].right += (fdata[1] * frightvol)/fdiv;
223 }
224 }
225 }
226
227
228 #endif
229