sdk-world
v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Toggle main menu visibility
Loading...
Searching...
No Matches
fixed.h
Go to the documentation of this file.
1
/*
2
* libmad - MPEG audio decoder library
3
* Copyright (C) 2000-2004 Underbit Technologies, Inc.
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
*
19
* $Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp $
20
*/
21
26
# ifndef LIBMAD_FIXED_H
27
# define LIBMAD_FIXED_H
28
29
# if SIZEOF_INT >= 4
30
typedef
signed
int
mad_fixed_t;
31
32
typedef
signed
int
mad_fixed64hi_t;
33
typedef
unsigned
int
mad_fixed64lo_t;
34
# else
35
typedef
signed
long
mad_fixed_t;
36
37
typedef
signed
long
mad_fixed64hi_t;
38
typedef
unsigned
long
mad_fixed64lo_t;
39
# endif
40
41
# if defined(_MSC_VER)
42
# define mad_fixed64_t signed __int64
43
# elif 1 || defined(__GNUC__)
44
# define mad_fixed64_t signed long long
45
# endif
46
47
# if defined(FPM_FLOAT)
48
typedef
double
mad_sample_t;
49
# else
50
typedef
mad_fixed_t mad_sample_t;
51
# endif
52
53
/*
54
* Fixed-point format: 0xABBBBBBB
55
* A == whole part (sign + 3 bits)
56
* B == fractional part (28 bits)
57
*
58
* Values are signed two's complement, so the effective range is:
59
* 0x80000000 to 0x7fffffff
60
* -8.0 to +7.9999999962747097015380859375
61
*
62
* The smallest representable value is:
63
* 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
64
*
65
* 28 bits of fractional accuracy represent about
66
* 8.6 digits of decimal accuracy.
67
*
68
* Fixed-point numbers can be added or subtracted as normal
69
* integers, but multiplication requires shifting the 64-bit result
70
* from 56 fractional bits back to 28 (and rounding.)
71
*
72
* Changing the definition of MAD_F_FRACBITS is only partially
73
* supported, and must be done with care.
74
*/
75
76
# define MAD_F_FRACBITS 28
77
78
# if MAD_F_FRACBITS == 28
79
# define MAD_F(x) ((mad_fixed_t) (x##L))
80
# else
81
# if MAD_F_FRACBITS < 28
82
# warning "MAD_F_FRACBITS < 28"
83
# define MAD_F(x) ((mad_fixed_t) \
84
(((x##L) + \
85
(1L << (28 - MAD_F_FRACBITS - 1))) >> \
86
(28 - MAD_F_FRACBITS)))
87
# elif MAD_F_FRACBITS > 28
88
# error "MAD_F_FRACBITS > 28 not currently supported"
89
# define MAD_F(x) ((mad_fixed_t) \
90
((x##L) << (MAD_F_FRACBITS - 28)))
91
# endif
92
# endif
93
94
# define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
95
# define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
96
97
# define MAD_F_ONE MAD_F(0x10000000)
98
99
# define mad_f_tofixed(x) ((mad_fixed_t) \
100
((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
101
# define mad_f_todouble(x) ((double) \
102
((x) / (double) (1L << MAD_F_FRACBITS)))
103
104
# define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
105
# define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
106
/* (x should be positive) */
107
108
# define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
109
110
# define mad_f_add(x, y) ((x) + (y))
111
# define mad_f_sub(x, y) ((x) - (y))
112
113
# if defined(FPM_FLOAT)
114
# error "FPM_FLOAT not yet supported"
115
116
# undef MAD_F
117
# define MAD_F(x) mad_f_todouble(x)
118
119
# define mad_f_mul(x, y) ((x) * (y))
120
# define mad_f_scale64
121
122
# undef ASO_ZEROCHECK
123
124
# elif defined(FPM_64BIT)
125
126
/*
127
* This version should be the most accurate if 64-bit types are supported by
128
* the compiler, although it may not be the most efficient.
129
*/
130
# if defined(OPT_ACCURACY)
131
# define mad_f_mul(x, y) \
132
((mad_fixed_t) \
133
((((mad_fixed64_t) (x) * (y)) + \
134
(1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
135
# else
136
# define mad_f_mul(x, y) \
137
((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
138
# endif
139
140
# define MAD_F_SCALEBITS MAD_F_FRACBITS
141
142
/* --- Intel --------------------------------------------------------------- */
143
144
# elif defined(FPM_INTEL)
145
146
# if defined(_MSC_VER)
147
# pragma warning(push)
148
# pragma warning(disable: 4035)
/* no return value */
149
static
__forceinline
150
mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
151
{
152
enum
{
153
fracbits = MAD_F_FRACBITS
154
};
155
156
__asm {
157
mov eax, x
158
imul y
159
shrd eax, edx, fracbits
160
}
161
162
/* implicit return of eax */
163
}
164
# pragma warning(pop)
165
166
# define mad_f_mul mad_f_mul_inline
167
# define mad_f_scale64
168
# else
169
/*
170
* This Intel version is fast and accurate; the disposition of the least
171
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
172
*/
173
# define MAD_F_MLX(hi, lo, x, y) \
174
asm ("imull %3" \
175
: "=a" (lo), "=d" (hi) \
176
: "%a" (x), "rm" (y) \
177
: "cc")
178
179
# if defined(OPT_ACCURACY)
180
/*
181
* This gives best accuracy but is not very fast.
182
*/
183
# define MAD_F_MLA(hi, lo, x, y) \
184
({ mad_fixed64hi_t __hi; \
185
mad_fixed64lo_t __lo; \
186
MAD_F_MLX(__hi, __lo, (x), (y)); \
187
asm ("addl %2,%0\n\t" \
188
"adcl %3,%1" \
189
: "=rm" (lo), "=rm" (hi) \
190
: "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
191
: "cc"); \
192
})
193
# endif
/* OPT_ACCURACY */
194
195
# if defined(OPT_ACCURACY)
196
/*
197
* Surprisingly, this is faster than SHRD followed by ADC.
198
*/
199
# define mad_f_scale64(hi, lo) \
200
({ mad_fixed64hi_t __hi_; \
201
mad_fixed64lo_t __lo_; \
202
mad_fixed_t __result; \
203
asm ("addl %4,%2\n\t" \
204
"adcl %5,%3" \
205
: "=rm" (__lo_), "=rm" (__hi_) \
206
: "0" (lo), "1" (hi), \
207
"ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
208
: "cc"); \
209
asm ("shrdl %3,%2,%1" \
210
: "=rm" (__result) \
211
: "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
212
: "cc"); \
213
__result; \
214
})
215
# elif defined(OPT_INTEL)
216
/*
217
* Alternate Intel scaling that may or may not perform better.
218
*/
219
# define mad_f_scale64(hi, lo) \
220
({ mad_fixed_t __result; \
221
asm ("shrl %3,%1\n\t" \
222
"shll %4,%2\n\t" \
223
"orl %2,%1" \
224
: "=rm" (__result) \
225
: "0" (lo), "r" (hi), \
226
"I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
227
: "cc"); \
228
__result; \
229
})
230
# else
231
# define mad_f_scale64(hi, lo) \
232
({ mad_fixed_t __result; \
233
asm ("shrdl %3,%2,%1" \
234
: "=rm" (__result) \
235
: "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
236
: "cc"); \
237
__result; \
238
})
239
# endif
/* OPT_ACCURACY */
240
241
# define MAD_F_SCALEBITS MAD_F_FRACBITS
242
# endif
243
244
/* --- ARM ----------------------------------------------------------------- */
245
246
# elif defined(FPM_ARM)
247
248
/*
249
* This ARM V4 version is as accurate as FPM_64BIT but much faster. The
250
* least significant bit is properly rounded at no CPU cycle cost!
251
*/
252
# if 1
253
/*
254
* This is faster than the default implementation via MAD_F_MLX() and
255
* mad_f_scale64().
256
*/
257
# define mad_f_mul(x, y) \
258
({ mad_fixed64hi_t __hi; \
259
mad_fixed64lo_t __lo; \
260
mad_fixed_t __result; \
261
asm ("smull %0, %1, %3, %4\n\t" \
262
"movs %0, %0, lsr %5\n\t" \
263
"adc %2, %0, %1, lsl %6" \
264
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
265
: "%r" (x), "r" (y), \
266
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
267
: "cc"); \
268
__result; \
269
})
270
# endif
271
272
# define MAD_F_MLX(hi, lo, x, y) \
273
asm ("smull %0, %1, %2, %3" \
274
: "=&r" (lo), "=&r" (hi) \
275
: "%r" (x), "r" (y))
276
277
# define MAD_F_MLA(hi, lo, x, y) \
278
asm ("smlal %0, %1, %2, %3" \
279
: "+r" (lo), "+r" (hi) \
280
: "%r" (x), "r" (y))
281
282
# define MAD_F_MLN(hi, lo) \
283
asm ("rsbs %0, %2, #0\n\t" \
284
"rsc %1, %3, #0" \
285
: "=r" (lo), "=r" (hi) \
286
: "0" (lo), "1" (hi) \
287
: "cc")
288
289
# define mad_f_scale64(hi, lo) \
290
({ mad_fixed_t __result; \
291
asm ("movs %0, %1, lsr %3\n\t" \
292
"adc %0, %0, %2, lsl %4" \
293
: "=&r" (__result) \
294
: "r" (lo), "r" (hi), \
295
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
296
: "cc"); \
297
__result; \
298
})
299
300
# define MAD_F_SCALEBITS MAD_F_FRACBITS
301
302
/* --- MIPS ---------------------------------------------------------------- */
303
304
# elif defined(FPM_MIPS)
305
306
/*
307
* This MIPS version is fast and accurate; the disposition of the least
308
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
309
*/
310
# define MAD_F_MLX(hi, lo, x, y) \
311
asm ("mult %2,%3" \
312
: "=l" (lo), "=h" (hi) \
313
: "%r" (x), "r" (y))
314
315
# if defined(HAVE_MADD_ASM)
316
# define MAD_F_MLA(hi, lo, x, y) \
317
asm ("madd %2,%3" \
318
: "+l" (lo), "+h" (hi) \
319
: "%r" (x), "r" (y))
320
# elif defined(HAVE_MADD16_ASM)
321
/*
322
* This loses significant accuracy due to the 16-bit integer limit in the
323
* multiply/accumulate instruction.
324
*/
325
# define MAD_F_ML0(hi, lo, x, y) \
326
asm ("mult %2,%3" \
327
: "=l" (lo), "=h" (hi) \
328
: "%r" ((x) >> 12), "r" ((y) >> 16))
329
# define MAD_F_MLA(hi, lo, x, y) \
330
asm ("madd16 %2,%3" \
331
: "+l" (lo), "+h" (hi) \
332
: "%r" ((x) >> 12), "r" ((y) >> 16))
333
# define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
334
# endif
335
336
# if defined(OPT_SPEED)
337
# define mad_f_scale64(hi, lo) \
338
((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
339
# define MAD_F_SCALEBITS MAD_F_FRACBITS
340
# endif
341
342
/* --- SPARC --------------------------------------------------------------- */
343
344
# elif defined(FPM_SPARC)
345
346
/*
347
* This SPARC V8 version is fast and accurate; the disposition of the least
348
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
349
*/
350
# define MAD_F_MLX(hi, lo, x, y) \
351
asm ("smul %2, %3, %0\n\t" \
352
"rd %%y, %1" \
353
: "=r" (lo), "=r" (hi) \
354
: "%r" (x), "rI" (y))
355
356
/* --- PowerPC ------------------------------------------------------------- */
357
358
# elif defined(FPM_PPC)
359
360
/*
361
* This PowerPC version is fast and accurate; the disposition of the least
362
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
363
*/
364
# define MAD_F_MLX(hi, lo, x, y) \
365
do { \
366
asm ("mullw %0,%1,%2" \
367
: "=r" (lo) \
368
: "%r" (x), "r" (y)); \
369
asm ("mulhw %0,%1,%2" \
370
: "=r" (hi) \
371
: "%r" (x), "r" (y)); \
372
} \
373
while (0)
374
375
# if defined(OPT_ACCURACY)
376
/*
377
* This gives best accuracy but is not very fast.
378
*/
379
# define MAD_F_MLA(hi, lo, x, y) \
380
({ mad_fixed64hi_t __hi; \
381
mad_fixed64lo_t __lo; \
382
MAD_F_MLX(__hi, __lo, (x), (y)); \
383
asm ("addc %0,%2,%3\n\t" \
384
"adde %1,%4,%5" \
385
: "=r" (lo), "=r" (hi) \
386
: "%r" (lo), "r" (__lo), \
387
"%r" (hi), "r" (__hi) \
388
: "xer"); \
389
})
390
# endif
391
392
# if defined(OPT_ACCURACY)
393
/*
394
* This is slower than the truncating version below it.
395
*/
396
# define mad_f_scale64(hi, lo) \
397
({ mad_fixed_t __result, __round; \
398
asm ("rotrwi %0,%1,%2" \
399
: "=r" (__result) \
400
: "r" (lo), "i" (MAD_F_SCALEBITS)); \
401
asm ("extrwi %0,%1,1,0" \
402
: "=r" (__round) \
403
: "r" (__result)); \
404
asm ("insrwi %0,%1,%2,0" \
405
: "+r" (__result) \
406
: "r" (hi), "i" (MAD_F_SCALEBITS)); \
407
asm ("add %0,%1,%2" \
408
: "=r" (__result) \
409
: "%r" (__result), "r" (__round)); \
410
__result; \
411
})
412
# else
413
# define mad_f_scale64(hi, lo) \
414
({ mad_fixed_t __result; \
415
asm ("rotrwi %0,%1,%2" \
416
: "=r" (__result) \
417
: "r" (lo), "i" (MAD_F_SCALEBITS)); \
418
asm ("insrwi %0,%1,%2,0" \
419
: "+r" (__result) \
420
: "r" (hi), "i" (MAD_F_SCALEBITS)); \
421
__result; \
422
})
423
# endif
424
425
# define MAD_F_SCALEBITS MAD_F_FRACBITS
426
427
/* --- Default ------------------------------------------------------------- */
428
429
# elif defined(FPM_DEFAULT)
430
431
/*
432
* This version is the most portable but it loses significant accuracy.
433
* Furthermore, accuracy is biased against the second argument, so care
434
* should be taken when ordering operands.
435
*
436
* The scale factors are constant as this is not used with SSO.
437
*
438
* Pre-rounding is required to stay within the limits of compliance.
439
*/
440
# if defined(OPT_SPEED)
441
# define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
442
# else
443
# define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
444
(((y) + (1L << 15)) >> 16))
445
# endif
446
447
/* ------------------------------------------------------------------------- */
448
449
# else
450
# error "no FPM selected"
451
# endif
452
453
/* default implementations */
454
455
# if !defined(mad_f_mul)
456
# define mad_f_mul(x, y) \
457
({ register mad_fixed64hi_t __hi; \
458
register mad_fixed64lo_t __lo; \
459
MAD_F_MLX(__hi, __lo, (x), (y)); \
460
mad_f_scale64(__hi, __lo); \
461
})
462
# endif
463
464
# if !defined(MAD_F_MLA)
465
# define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
466
# define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
467
# define MAD_F_MLN(hi, lo) ((lo) = -(lo))
468
# define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
469
# endif
470
471
# if !defined(MAD_F_ML0)
472
# define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
473
# endif
474
475
# if !defined(MAD_F_MLN)
476
# define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
477
# endif
478
479
# if !defined(MAD_F_MLZ)
480
# define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
481
# endif
482
483
# if !defined(mad_f_scale64)
484
# if defined(OPT_ACCURACY)
485
# define mad_f_scale64(hi, lo) \
486
((((mad_fixed_t) \
487
(((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
488
((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
489
# else
490
# define mad_f_scale64(hi, lo) \
491
((mad_fixed_t) \
492
(((hi) << (32 - MAD_F_SCALEBITS)) | \
493
((lo) >> MAD_F_SCALEBITS)))
494
# endif
495
# define MAD_F_SCALEBITS MAD_F_FRACBITS
496
# endif
497
498
/* C routines */
499
500
mad_fixed_t mad_f_abs(mad_fixed_t);
501
mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);
502
503
# endif
libmad
src
fixed.h
Generated by
1.18.0