Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
miniaudio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
miniaudio
Commits
95ea6c6b
Commit
95ea6c6b
authored
Jan 01, 2017
by
David Reid
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add untested conversion routines for all supported formats.
parent
a5edf715
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
715 additions
and
126 deletions
+715
-126
mini_al.h
mini_al.h
+340
-112
resources/format_conversions.txt
resources/format_conversions.txt
+125
-12
tools/malgen/source/malgen.cpp
tools/malgen/source/malgen.cpp
+250
-2
No files found.
mini_al.h
View file @
95ea6c6b
This diff is collapsed.
Click to expand it.
resources/format_conversions.txt
View file @
95ea6c6b
...
@@ -4,14 +4,15 @@
...
@@ -4,14 +4,15 @@
#
#
# Instructions
# Instructions
# ============
# ============
# add [output] [a] [b] -> output = a + b
# add [output] [a] [b] -> output = a + b
# sub [output] [a] [b] -> output = a - b
# sub [output] [a] [b] -> output = a - b
# mul [output] [a] [b] -> output = a * b
# mul [output] [a] [b] -> output = a * b
# div [output] [a] [b] -> output = a / b
# div [output] [a] [b] -> output = a / b
# shl [output] [a] [b] -> output = a << b
# shl [output] [a] [b] -> output = a << b
# shr [output] [a] [b] -> output = a >> b
# shr [output] [a] [b] -> output = a >> b
# sig [output] [b] -> output = (sign bit in "a" is set) ? 1 : 0
# sig [output] [b] -> output = (sign bit in "a" is set) ? 1 : 0
# mov [output] [a] -> output = a;
# mov [output] [a] -> output = a;
# clip [output] [a] -> output = clamp(a, -1, 1)
#
#
# int [name] -> Declare an uninitialized 32-bit integer
# int [name] -> Declare an uninitialized 32-bit integer
# flt [name] -> Declare an uninitialized 32-bit float
# flt [name] -> Declare an uninitialized 32-bit float
...
@@ -36,16 +37,128 @@ u8->s32 {
...
@@ -36,16 +37,128 @@ u8->s32 {
# r = (x / 255) * 2 - 1
# r = (x / 255) * 2 - 1
u8->f32 {
u8->f32 {
div r x 255.0;
div r x 255.0
f
;
mul r r 2;
mul r r 2;
sub r r 1;
sub r r 1;
}
}
# r = (x >> 8) + 128
s16->u8 {
shr r x 8;
add r r 128;
}
# r = x << 8
s16->s24 {
shl r x 8;
}
# r = x << 16
s16->s32 {
shl r x 16;
}
# r = ((x + 32768) / 65536) * 2 - 1
s16->f32 {
add r x 32768.0f;
div r r 65536.0f;
mul r r 2;
sub r r 1;
}
# r = (x >> 16) + 128
s24->u8 {
shr r x 16;
add r r 128;
}
# r = x >> 8
s24->s16 {
shr r x 8;
}
# r = x << 8
s24->s32 {
shl r x 8;
}
# r = ((x + 8388608) / 16777215) * 2 - 1
s24->f32 {
add r x 8388608.0f;
div r r 16777215.0f;
mul r r 2;
sub r r 1;
}
# r = (x >> 24) + 128
s32->u8 {
shr r x 24;
add r r 128;
}
# r = x >> 16
s32->s16 {
shr r x 16;
}
# r = x >> 8
s32->s24 {
shr r x 8;
}
# r = x / (2147483647 + sign(x))
# r = x / (2147483647 + sign(x))
u
32->f32 {
s
32->f32 {
int s;
int s;
sig s x;
sig s x;
add s s 2147483647;
add s s 2147483647;
div r x (flt)s;
div r x (flt)(uint)s;
}
}
\ No newline at end of file
# r = (clip(x) * (0x7F + sign(x))) + 128
f32->u8 {
flt c;
int s;
clip c x;
sig s x;
add s s 127;
mul (int)r c s;
add r r 128;
}
# r = clip(x) * (0x7FFF + sign(x))
f32->s16 {
flt c;
int s;
clip c x;
sig s x;
add s s 32767;
mul (int)r c s;
}
# r = clip(x) * (0x7FFFFF + sign(x))
f32->s24 {
flt c;
int s;
clip c x;
sig s x;
add s s 8388607;
mul (int)r c s;
}
# r = clip(x) * (0x7FFFFFFF + sign(x))
f32->s32 {
flt c;
int s;
clip c x;
sig s x;
add s s 2147483647;
mul (int)r c s;
}
tools/malgen/source/malgen.cpp
View file @
95ea6c6b
...
@@ -21,6 +21,7 @@ typedef struct
...
@@ -21,6 +21,7 @@ typedef struct
typedef
struct
typedef
struct
{
{
char
*
pFormatsFileData
;
char
*
pFormatsFileData
;
const
char
*
userNamespace
;
std
::
vector
<
malgen_conversion_desc
>
conversions
;
std
::
vector
<
malgen_conversion_desc
>
conversions
;
}
malgen_context
;
}
malgen_context
;
...
@@ -66,7 +67,7 @@ void u8_to_f32(const unsigned char* pIn, float* pOut, unsigned int count)
...
@@ -66,7 +67,7 @@ void u8_to_f32(const unsigned char* pIn, float* pOut, unsigned int count)
int
x
;
int
x
;
unsigned
int
i
=
0
;
unsigned
int
i
=
0
;
switch
((
uintptr_t
)
pIn
&
0x3
)
switch
((
(
unsigned
long
long
)
pIn
&
0x15
)
/
sizeof
(
float
)
)
{
{
case
3
:
case
3
:
x
=
pIn
[
i
];
x
=
pIn
[
i
];
...
@@ -319,6 +320,242 @@ int malgen_compile(malgen_context* pContext)
...
@@ -319,6 +320,242 @@ int malgen_compile(malgen_context* pContext)
return
0
;
return
0
;
}
}
std
::
string
malgen_get_format_c_type_string
(
const
char
*
formatStr
)
{
if
(
strcmp
(
formatStr
,
"u8"
)
==
0
)
{
return
"unsigned char"
;
}
if
(
strcmp
(
formatStr
,
"s16"
)
==
0
)
{
return
"short"
;
}
if
(
strcmp
(
formatStr
,
"s24"
)
==
0
)
{
return
"void"
;
}
if
(
strcmp
(
formatStr
,
"s32"
)
==
0
)
{
return
"int"
;
}
if
(
strcmp
(
formatStr
,
"f32"
)
==
0
)
{
return
"float"
;
}
return
""
;
}
std
::
string
malgen_get_format_impl_c_type_string
(
const
char
*
formatStr
)
{
if
(
strcmp
(
formatStr
,
"f32"
)
==
0
)
{
return
"float"
;
}
return
"int"
;
}
std
::
string
malgen_generate_code__conversion_func_params
(
malgen_context
*
pContext
,
malgen_conversion_desc
*
pFuncDesc
)
{
std
::
string
code
;
code
+=
malgen_get_format_c_type_string
(
pFuncDesc
->
formatOutStr
);
code
+=
"* pOut, "
;
code
+=
"const "
;
code
+=
malgen_get_format_c_type_string
(
pFuncDesc
->
formatInStr
);
code
+=
"* pIn, "
;
code
+=
"unsigned int count"
;
return
code
;
}
std
::
string
malgen_get_format_input_conversion_code
(
const
char
*
formatStr
)
{
if
(
strcmp
(
formatStr
,
"s24"
)
==
0
)
{
return
"((int)(((unsigned int)(((unsigned char*)pIn)[i*3+0]) << 8) | ((unsigned int)(((unsigned char*)pIn)[i*3+1]) << 16) | ((unsigned int)(((unsigned char*)pIn)[i*3+2])) << 24)) >> 8"
;
}
return
"pIn[i]"
;
}
std
::
string
malgen_get_format_output_conversion_code
(
const
char
*
formatStr
)
{
if
(
strcmp
(
formatStr
,
"s24"
)
==
0
)
{
return
"((unsigned char*)pOut)[(i*3)+0] = (unsigned char)(r & 0xFF); ((unsigned char*)pOut)[(i*3)+1] = (unsigned char)((r & 0xFF00) >> 8); ((unsigned char*)pOut)[(i*3)+2] = (unsigned char)((r & 0xFF0000) >> 16)"
;
}
std
::
string
code
;
code
=
"pOut[i] = ("
;
code
+=
malgen_get_format_c_type_string
(
formatStr
);
code
+=
")r"
;
return
code
;
}
std
::
string
malgen_format_op_param
(
const
char
*
param
)
{
std
::
string
s
(
param
);
// (flt) -> (float)
{
const
char
*
src
=
"(flt)"
;
const
char
*
dst
=
"(float)"
;
size_t
loc
=
s
.
find
(
src
);
if
(
loc
!=
std
::
string
::
npos
)
{
s
=
s
.
replace
(
loc
,
strlen
(
src
),
dst
);
}
}
// (dbl) -> (double)
{
const
char
*
src
=
"(dbl)"
;
const
char
*
dst
=
"(double)"
;
size_t
loc
=
s
.
find
(
src
);
if
(
loc
!=
std
::
string
::
npos
)
{
s
=
s
.
replace
(
loc
,
strlen
(
src
),
dst
);
}
}
// (uint) -> (unsigned int)
{
const
char
*
src
=
"(uint)"
;
const
char
*
dst
=
"(unsigned int)"
;
size_t
loc
=
s
.
find
(
src
);
if
(
loc
!=
std
::
string
::
npos
)
{
s
=
s
.
replace
(
loc
,
strlen
(
src
),
dst
);
}
}
return
s
;
}
std
::
string
malgen_generate_code__conversion_func_inst_binary_op
(
const
char
*
result
,
const
char
*
param1
,
const
char
*
param2
,
const
char
*
op
)
{
bool
requiresCast
=
false
;
const
char
*
resultVar
=
result
;
if
(
resultVar
[
0
]
==
'('
)
{
for
(;;)
{
if
(
resultVar
[
0
]
==
'\0'
||
resultVar
[
0
]
==
')'
)
{
resultVar
+=
1
;
break
;
}
resultVar
+=
1
;
}
requiresCast
=
true
;
}
std
::
string
assignmentStr
=
malgen_format_op_param
(
param1
)
+
" "
+
op
+
" "
+
malgen_format_op_param
(
param2
);
std
::
string
code
;
code
+=
resultVar
;
code
+=
" = "
;
if
(
requiresCast
)
{
char
typeStr
[
64
];
strncpy_s
(
typeStr
,
result
,
resultVar
-
result
);
code
+=
typeStr
;
code
+=
"("
;
code
+=
assignmentStr
;
code
+=
")"
;
return
code
;
}
else
{
code
+=
assignmentStr
;
return
code
;
}
}
std
::
string
malgen_generate_code__conversion_func_inst
(
malgen_context
*
pContext
,
malgen_instruction
*
pInst
)
{
std
::
string
code
;
if
(
strcmp
(
pInst
->
name
,
"int"
)
==
0
)
{
code
+=
"int "
;
code
+=
pInst
->
params
[
0
];
}
if
(
strcmp
(
pInst
->
name
,
"flt"
)
==
0
)
{
code
+=
"float "
;
code
+=
pInst
->
params
[
0
];
}
if
(
strcmp
(
pInst
->
name
,
"add"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
"+"
);
}
if
(
strcmp
(
pInst
->
name
,
"sub"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
"-"
);
}
if
(
strcmp
(
pInst
->
name
,
"mul"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
"*"
);
}
if
(
strcmp
(
pInst
->
name
,
"div"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
"/"
);
}
if
(
strcmp
(
pInst
->
name
,
"shl"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
"<<"
);
}
if
(
strcmp
(
pInst
->
name
,
"shr"
)
==
0
)
{
code
+=
malgen_generate_code__conversion_func_inst_binary_op
(
pInst
->
params
[
0
],
pInst
->
params
[
1
],
pInst
->
params
[
2
],
">>"
);
}
if
(
strcmp
(
pInst
->
name
,
"mov"
)
==
0
)
{
code
+=
pInst
->
params
[
0
];
code
+=
" = "
;
code
+=
pInst
->
params
[
1
];
}
if
(
strcmp
(
pInst
->
name
,
"sig"
)
==
0
)
{
// <-- This gets the sign of the first input parameter and moves it to the result.
code
+=
pInst
->
params
[
0
];
code
+=
" = "
;
code
+=
"((*((int*)&"
;
code
+=
pInst
->
params
[
1
];
code
+=
")) & 0x80000000) >> 31"
;
}
if
(
strcmp
(
pInst
->
name
,
"clip"
)
==
0
)
{
// clamp(a, -1, 1) -> r = ((a < -1) ? -1 : ((a > 1) ? 1 : a))
code
+=
pInst
->
params
[
0
];
code
+=
" = "
;
code
+=
"(("
;
code
+=
pInst
->
params
[
1
];
code
+=
" < -1) ? -1 : (("
;
code
+=
pInst
->
params
[
1
];
code
+=
" > 1) ? 1 : "
;
code
+=
pInst
->
params
[
1
];
code
+=
"))"
;
}
return
code
;
}
std
::
string
malgen_generate_code__conversion_func_decl
(
malgen_context
*
pContext
,
malgen_conversion_desc
*
pFuncDesc
)
{
std
::
string
code
=
"void "
;
code
+=
pContext
->
userNamespace
;
code
+=
pFuncDesc
->
formatInStr
;
code
+=
"_to_"
;
code
+=
pFuncDesc
->
formatOutStr
;
code
+=
"("
;
code
+=
malgen_generate_code__conversion_func_params
(
pContext
,
pFuncDesc
);
code
+=
")"
;
return
code
;
}
std
::
string
malgen_generate_code__conversion_func_impl
(
malgen_context
*
pContext
,
malgen_conversion_desc
*
pFuncDesc
)
{
std
::
string
code
;
code
+=
" "
;
code
+=
malgen_get_format_impl_c_type_string
(
pFuncDesc
->
formatOutStr
);
code
+=
" r;
\n
"
;
code
+=
" for (unsigned int i = 0; i < count; ++i) {
\n
"
;
code
+=
" "
;
code
+=
malgen_get_format_impl_c_type_string
(
pFuncDesc
->
formatInStr
);
code
+=
" x = "
;
code
+=
malgen_get_format_input_conversion_code
(
pFuncDesc
->
formatInStr
);
code
+=
";
\n
"
;
for
(
size_t
i
=
0
;
i
<
pFuncDesc
->
instructions
.
size
();
++
i
)
{
code
+=
" "
;
code
+=
malgen_generate_code__conversion_func_inst
(
pContext
,
&
pFuncDesc
->
instructions
[
i
]);
code
+=
";
\n
"
;
}
code
+=
" "
;
code
+=
malgen_get_format_output_conversion_code
(
pFuncDesc
->
formatOutStr
);
code
+=
";
\n
"
;
code
+=
" }"
;
return
code
;
}
std
::
string
malgen_generate_code__conversion_func
(
malgen_context
*
pContext
,
malgen_conversion_desc
*
pFuncDesc
)
{
std
::
string
code
=
malgen_generate_code__conversion_func_decl
(
pContext
,
pFuncDesc
);
code
+=
"
\n
{
\n
"
;
code
+=
malgen_generate_code__conversion_func_impl
(
pContext
,
pFuncDesc
);
code
+=
"
\n
}
\n
"
;
return
code
;
}
int
malgen_generate_code
(
malgen_context
*
pContext
,
std
::
string
*
pCodeOut
)
{
std
::
string
code
;
// Conversion functions.
// Declarations.
for
(
size_t
i
=
0
;
i
<
pContext
->
conversions
.
size
();
++
i
)
{
code
+=
malgen_generate_code__conversion_func_decl
(
pContext
,
&
pContext
->
conversions
[
i
]);
code
+=
";
\n
"
;
}
code
+=
"
\n
"
;
// Definitions.
for
(
size_t
i
=
0
;
i
<
pContext
->
conversions
.
size
();
++
i
)
{
code
+=
malgen_generate_code__conversion_func
(
pContext
,
&
pContext
->
conversions
[
i
]);
code
+=
"
\n
"
;
}
*
pCodeOut
=
code
;
return
0
;
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
(
void
)
argc
;
(
void
)
argc
;
...
@@ -330,16 +567,27 @@ int main(int argc, char** argv)
...
@@ -330,16 +567,27 @@ int main(int argc, char** argv)
return
result
;
return
result
;
}
}
context
.
userNamespace
=
"mal_pcm_"
;
// TODO: Implement the "--namespace" command line argument.
FILE
*
pOutputFile
=
dr_fopen
(
"malgen_test0.c"
,
"w"
);
FILE
*
pOutputFile
=
dr_fopen
(
"malgen_test0.c"
,
"w"
);
if
(
pOutputFile
==
NULL
)
{
if
(
pOutputFile
==
NULL
)
{
printf
(
"Failed to open output file.
\n
"
);
printf
(
"Failed to open output file.
\n
"
);
return
-
2
;
return
-
2
;
}
}
std
::
string
code
;
result
=
malgen_generate_code
(
&
context
,
&
code
);
if
(
result
!=
0
)
{
printf
(
"Code generation failed.
\n
"
);
return
result
;
}
// We need conversion routines for each different combination of formats.
fprintf
(
pOutputFile
,
"%s"
,
code
.
c_str
());
// We need conversion routines for each different combination of formats.
// TESTING
// TESTING
for
(
size_t
i
=
0
;
i
<
context
.
conversions
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
context
.
conversions
.
size
();
++
i
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment