paddlec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,503 @@
1
+ /* Copyright (C) 2019 Théotime Bollengier <theotime.bollengier@gmail.com>
2
+ *
3
+ * This file is part of PaddleC
4
+ *
5
+ * PaddleC 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 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * PaddleC 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 PaddleC. If not, see <https://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ #include <stdlib.h>
20
+ #include <math.h>
21
+ #include "libpaddlec.h"
22
+
23
+
24
+ pdlc_buffer_t* pdlc_buffer_ceil(const pdlc_buffer_t *fbuf, pdlc_buffer_t *ofbuf)
25
+ {
26
+ size_t i;
27
+
28
+ if (!ofbuf)
29
+ ofbuf = pdlc_buffer_new(fbuf->length);
30
+ else
31
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
32
+
33
+ for (i = 0; i < fbuf->length; i++)
34
+ ofbuf->data[i] = ceilf(fbuf->data[i]);
35
+
36
+ return ofbuf;
37
+ }
38
+
39
+
40
+ pdlc_buffer_t* pdlc_buffer_ceil_inplace(pdlc_buffer_t *fbuf)
41
+ {
42
+ size_t i;
43
+
44
+ for (i = 0; i < fbuf->length; i++)
45
+ fbuf->data[i] = ceilf(fbuf->data[i]);
46
+
47
+ return fbuf;
48
+ }
49
+
50
+
51
+ pdlc_complex_buffer_t* pdlc_complex_buffer_ceil(const pdlc_complex_buffer_t *cbuf, pdlc_complex_buffer_t *ocbuf)
52
+ {
53
+ size_t i;
54
+
55
+ if (!ocbuf)
56
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
57
+ else
58
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
59
+
60
+ for (i = 0; i < cbuf->length; i++) {
61
+ ocbuf->data[i].real = ceilf(cbuf->data[i].real);
62
+ ocbuf->data[i].imag = ceilf(cbuf->data[i].imag);
63
+ }
64
+
65
+ return ocbuf;
66
+ }
67
+
68
+
69
+ pdlc_complex_buffer_t* pdlc_complex_buffer_ceil_inplace(pdlc_complex_buffer_t *cbuf)
70
+ {
71
+ size_t i;
72
+
73
+ for (i = 0; i < cbuf->length; i++) {
74
+ cbuf->data[i].real = ceilf(cbuf->data[i].real);
75
+ cbuf->data[i].imag = ceilf(cbuf->data[i].imag);
76
+ }
77
+
78
+ return cbuf;
79
+ }
80
+
81
+
82
+ pdlc_buffer_t* pdlc_buffer_floor(const pdlc_buffer_t *fbuf, pdlc_buffer_t *ofbuf)
83
+ {
84
+ size_t i;
85
+
86
+ if (!ofbuf)
87
+ ofbuf = pdlc_buffer_new(fbuf->length);
88
+ else
89
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
90
+
91
+ for (i = 0; i < fbuf->length; i++)
92
+ ofbuf->data[i] = floorf(fbuf->data[i]);
93
+
94
+ return ofbuf;
95
+ }
96
+
97
+
98
+ pdlc_buffer_t* pdlc_buffer_floor_inplace(pdlc_buffer_t *fbuf)
99
+ {
100
+ size_t i;
101
+
102
+ for (i = 0; i < fbuf->length; i++)
103
+ fbuf->data[i] = floorf(fbuf->data[i]);
104
+
105
+ return fbuf;
106
+ }
107
+
108
+
109
+ pdlc_complex_buffer_t* pdlc_complex_buffer_floor(const pdlc_complex_buffer_t *cbuf, pdlc_complex_buffer_t *ocbuf)
110
+ {
111
+ size_t i;
112
+
113
+ if (!ocbuf)
114
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
115
+ else
116
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
117
+
118
+ for (i = 0; i < cbuf->length; i++) {
119
+ ocbuf->data[i].real = floorf(cbuf->data[i].real);
120
+ ocbuf->data[i].imag = floorf(cbuf->data[i].imag);
121
+ }
122
+
123
+ return ocbuf;
124
+ }
125
+
126
+
127
+ pdlc_complex_buffer_t* pdlc_complex_buffer_floor_inplace(pdlc_complex_buffer_t *cbuf)
128
+ {
129
+ size_t i;
130
+
131
+ for (i = 0; i < cbuf->length; i++) {
132
+ cbuf->data[i].real = floorf(cbuf->data[i].real);
133
+ cbuf->data[i].imag = floorf(cbuf->data[i].imag);
134
+ }
135
+
136
+ return cbuf;
137
+ }
138
+
139
+
140
+ pdlc_buffer_t* pdlc_buffer_truncate(const pdlc_buffer_t *fbuf, pdlc_buffer_t *ofbuf)
141
+ {
142
+ size_t i;
143
+
144
+ if (!ofbuf)
145
+ ofbuf = pdlc_buffer_new(fbuf->length);
146
+ else
147
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
148
+
149
+ for (i = 0; i < fbuf->length; i++)
150
+ ofbuf->data[i] = truncf(fbuf->data[i]);
151
+
152
+ return ofbuf;
153
+ }
154
+
155
+
156
+ pdlc_buffer_t* pdlc_buffer_truncate_inplace(pdlc_buffer_t *fbuf)
157
+ {
158
+ size_t i;
159
+
160
+ for (i = 0; i < fbuf->length; i++)
161
+ fbuf->data[i] = truncf(fbuf->data[i]);
162
+
163
+ return fbuf;
164
+ }
165
+
166
+
167
+ pdlc_complex_buffer_t* pdlc_complex_buffer_truncate(const pdlc_complex_buffer_t *cbuf, pdlc_complex_buffer_t *ocbuf)
168
+ {
169
+ size_t i;
170
+
171
+ if (!ocbuf)
172
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
173
+ else
174
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
175
+
176
+ for (i = 0; i < cbuf->length; i++) {
177
+ ocbuf->data[i].real = truncf(cbuf->data[i].real);
178
+ ocbuf->data[i].imag = truncf(cbuf->data[i].imag);
179
+ }
180
+
181
+ return ocbuf;
182
+ }
183
+
184
+
185
+ pdlc_complex_buffer_t* pdlc_complex_buffer_truncate_inplace(pdlc_complex_buffer_t *cbuf)
186
+ {
187
+ size_t i;
188
+
189
+ for (i = 0; i < cbuf->length; i++) {
190
+ cbuf->data[i].real = truncf(cbuf->data[i].real);
191
+ cbuf->data[i].imag = truncf(cbuf->data[i].imag);
192
+ }
193
+
194
+ return cbuf;
195
+ }
196
+
197
+
198
+ pdlc_buffer_t* pdlc_buffer_round(const pdlc_buffer_t *fbuf, pdlc_buffer_t *ofbuf)
199
+ {
200
+ size_t i;
201
+
202
+ if (!ofbuf)
203
+ ofbuf = pdlc_buffer_new(fbuf->length);
204
+ else
205
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
206
+
207
+ for (i = 0; i < fbuf->length; i++)
208
+ ofbuf->data[i] = roundf(fbuf->data[i]);
209
+
210
+ return ofbuf;
211
+ }
212
+
213
+
214
+ pdlc_buffer_t* pdlc_buffer_round_inplace(pdlc_buffer_t *fbuf)
215
+ {
216
+ size_t i;
217
+
218
+ for (i = 0; i < fbuf->length; i++)
219
+ fbuf->data[i] = roundf(fbuf->data[i]);
220
+
221
+ return fbuf;
222
+ }
223
+
224
+
225
+ pdlc_complex_buffer_t* pdlc_complex_buffer_round(const pdlc_complex_buffer_t *cbuf, pdlc_complex_buffer_t *ocbuf)
226
+ {
227
+ size_t i;
228
+
229
+ if (!ocbuf)
230
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
231
+ else
232
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
233
+
234
+ for (i = 0; i < cbuf->length; i++) {
235
+ ocbuf->data[i].real = roundf(cbuf->data[i].real);
236
+ ocbuf->data[i].imag = roundf(cbuf->data[i].imag);
237
+ }
238
+
239
+ return ocbuf;
240
+ }
241
+
242
+
243
+ pdlc_complex_buffer_t* pdlc_complex_buffer_round_inplace(pdlc_complex_buffer_t *cbuf)
244
+ {
245
+ size_t i;
246
+
247
+ for (i = 0; i < cbuf->length; i++) {
248
+ cbuf->data[i].real = roundf(cbuf->data[i].real);
249
+ cbuf->data[i].imag = roundf(cbuf->data[i].imag);
250
+ }
251
+
252
+ return cbuf;
253
+ }
254
+
255
+
256
+ pdlc_buffer_t* pdlc_buffer_ceil_digits(const pdlc_buffer_t *fbuf, int digits, pdlc_buffer_t *ofbuf)
257
+ {
258
+ const float d = powf(10.0f, (float)digits);
259
+ size_t i;
260
+
261
+ if (!ofbuf)
262
+ ofbuf = pdlc_buffer_new(fbuf->length);
263
+ else
264
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
265
+
266
+ for (i = 0; i < fbuf->length; i++)
267
+ ofbuf->data[i] = ceilf(fbuf->data[i]*d)/d;
268
+
269
+ return ofbuf;
270
+ }
271
+
272
+
273
+ pdlc_buffer_t* pdlc_buffer_ceil_digits_inplace(pdlc_buffer_t *fbuf, int digits)
274
+ {
275
+ const float d = powf(10.0f, (float)digits);
276
+ size_t i;
277
+
278
+ for (i = 0; i < fbuf->length; i++)
279
+ fbuf->data[i] = ceilf(fbuf->data[i]*d)/d;
280
+
281
+ return fbuf;
282
+ }
283
+
284
+
285
+ pdlc_complex_buffer_t* pdlc_complex_buffer_ceil_digits(const pdlc_complex_buffer_t *cbuf, int digits, pdlc_complex_buffer_t *ocbuf)
286
+ {
287
+ const float d = powf(10.0f, (float)digits);
288
+ size_t i;
289
+
290
+ if (!ocbuf)
291
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
292
+ else
293
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
294
+
295
+ for (i = 0; i < cbuf->length; i++) {
296
+ ocbuf->data[i].real = ceilf(cbuf->data[i].real*d)/d;
297
+ ocbuf->data[i].imag = ceilf(cbuf->data[i].imag*d)/d;
298
+ }
299
+
300
+ return ocbuf;
301
+ }
302
+
303
+
304
+ pdlc_complex_buffer_t* pdlc_complex_buffer_ceil_digits_inplace(pdlc_complex_buffer_t *cbuf, int digits)
305
+ {
306
+ const float d = powf(10.0f, (float)digits);
307
+ size_t i;
308
+
309
+ for (i = 0; i < cbuf->length; i++) {
310
+ cbuf->data[i].real = ceilf(cbuf->data[i].real*d)/d;
311
+ cbuf->data[i].imag = ceilf(cbuf->data[i].imag*d)/d;
312
+ }
313
+
314
+ return cbuf;
315
+ }
316
+
317
+
318
+ pdlc_buffer_t* pdlc_buffer_floor_digits(const pdlc_buffer_t *fbuf, int digits, pdlc_buffer_t *ofbuf)
319
+ {
320
+ const float d = powf(10.0f, (float)digits);
321
+ size_t i;
322
+
323
+ if (!ofbuf)
324
+ ofbuf = pdlc_buffer_new(fbuf->length);
325
+ else
326
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
327
+
328
+ for (i = 0; i < fbuf->length; i++)
329
+ ofbuf->data[i] = floorf(fbuf->data[i]*d)/d;
330
+
331
+ return ofbuf;
332
+ }
333
+
334
+
335
+ pdlc_buffer_t* pdlc_buffer_floor_digits_inplace(pdlc_buffer_t *fbuf, int digits)
336
+ {
337
+ const float d = powf(10.0f, (float)digits);
338
+ size_t i;
339
+
340
+ for (i = 0; i < fbuf->length; i++)
341
+ fbuf->data[i] = floorf(fbuf->data[i]*d)/d;
342
+
343
+ return fbuf;
344
+ }
345
+
346
+
347
+ pdlc_complex_buffer_t* pdlc_complex_buffer_floor_digits(const pdlc_complex_buffer_t *cbuf, int digits, pdlc_complex_buffer_t *ocbuf)
348
+ {
349
+ const float d = powf(10.0f, (float)digits);
350
+ size_t i;
351
+
352
+ if (!ocbuf)
353
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
354
+ else
355
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
356
+
357
+ for (i = 0; i < cbuf->length; i++) {
358
+ ocbuf->data[i].real = floorf(cbuf->data[i].real*d)/d;
359
+ ocbuf->data[i].imag = floorf(cbuf->data[i].imag*d)/d;
360
+ }
361
+
362
+ return ocbuf;
363
+ }
364
+
365
+
366
+ pdlc_complex_buffer_t* pdlc_complex_buffer_floor_digits_inplace(pdlc_complex_buffer_t *cbuf, int digits)
367
+ {
368
+ const float d = powf(10.0f, (float)digits);
369
+ size_t i;
370
+
371
+ for (i = 0; i < cbuf->length; i++) {
372
+ cbuf->data[i].real = floorf(cbuf->data[i].real*d)/d;
373
+ cbuf->data[i].imag = floorf(cbuf->data[i].imag*d)/d;
374
+ }
375
+
376
+ return cbuf;
377
+ }
378
+
379
+
380
+ pdlc_buffer_t* pdlc_buffer_truncate_digits(const pdlc_buffer_t *fbuf, int digits, pdlc_buffer_t *ofbuf)
381
+ {
382
+ const float d = powf(10.0f, (float)digits);
383
+ size_t i;
384
+
385
+ if (!ofbuf)
386
+ ofbuf = pdlc_buffer_new(fbuf->length);
387
+ else
388
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
389
+
390
+ for (i = 0; i < fbuf->length; i++)
391
+ ofbuf->data[i] = truncf(fbuf->data[i]*d)/d;
392
+
393
+ return ofbuf;
394
+ }
395
+
396
+
397
+ pdlc_buffer_t* pdlc_buffer_truncate_digits_inplace(pdlc_buffer_t *fbuf, int digits)
398
+ {
399
+ const float d = powf(10.0f, (float)digits);
400
+ size_t i;
401
+
402
+ for (i = 0; i < fbuf->length; i++)
403
+ fbuf->data[i] = truncf(fbuf->data[i]*d)/d;
404
+
405
+ return fbuf;
406
+ }
407
+
408
+
409
+ pdlc_complex_buffer_t* pdlc_complex_buffer_truncate_digits(const pdlc_complex_buffer_t *cbuf, int digits, pdlc_complex_buffer_t *ocbuf)
410
+ {
411
+ const float d = powf(10.0f, (float)digits);
412
+ size_t i;
413
+
414
+ if (!ocbuf)
415
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
416
+ else
417
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
418
+
419
+ for (i = 0; i < cbuf->length; i++) {
420
+ ocbuf->data[i].real = truncf(cbuf->data[i].real*d)/d;
421
+ ocbuf->data[i].imag = truncf(cbuf->data[i].imag*d)/d;
422
+ }
423
+
424
+ return ocbuf;
425
+ }
426
+
427
+
428
+ pdlc_complex_buffer_t* pdlc_complex_buffer_truncate_digits_inplace(pdlc_complex_buffer_t *cbuf, int digits)
429
+ {
430
+ const float d = powf(10.0f, (float)digits);
431
+ size_t i;
432
+
433
+ for (i = 0; i < cbuf->length; i++) {
434
+ cbuf->data[i].real = truncf(cbuf->data[i].real*d)/d;
435
+ cbuf->data[i].imag = truncf(cbuf->data[i].imag*d)/d;
436
+ }
437
+
438
+ return cbuf;
439
+ }
440
+
441
+
442
+ pdlc_buffer_t* pdlc_buffer_round_digits(const pdlc_buffer_t *fbuf, int digits, pdlc_buffer_t *ofbuf)
443
+ {
444
+ const float d = powf(10.0f, (float)digits);
445
+ size_t i;
446
+
447
+ if (!ofbuf)
448
+ ofbuf = pdlc_buffer_new(fbuf->length);
449
+ else
450
+ pdlc_buffer_resize(ofbuf, fbuf->length, 0);
451
+
452
+ for (i = 0; i < fbuf->length; i++)
453
+ ofbuf->data[i] = roundf(fbuf->data[i]*d)/d;
454
+
455
+ return ofbuf;
456
+ }
457
+
458
+
459
+ pdlc_buffer_t* pdlc_buffer_round_digits_inplace(pdlc_buffer_t *fbuf, int digits)
460
+ {
461
+ const float d = powf(10.0f, (float)digits);
462
+ size_t i;
463
+
464
+ for (i = 0; i < fbuf->length; i++)
465
+ fbuf->data[i] = roundf(fbuf->data[i]*d)/d;
466
+
467
+ return fbuf;
468
+ }
469
+
470
+
471
+ pdlc_complex_buffer_t* pdlc_complex_buffer_round_digits(const pdlc_complex_buffer_t *cbuf, int digits, pdlc_complex_buffer_t *ocbuf)
472
+ {
473
+ const float d = powf(10.0f, (float)digits);
474
+ size_t i;
475
+
476
+ if (!ocbuf)
477
+ ocbuf = pdlc_complex_buffer_new(cbuf->length);
478
+ else
479
+ pdlc_complex_buffer_resize(ocbuf, cbuf->length, 0);
480
+
481
+ for (i = 0; i < cbuf->length; i++) {
482
+ ocbuf->data[i].real = roundf(cbuf->data[i].real*d)/d;
483
+ ocbuf->data[i].imag = roundf(cbuf->data[i].imag*d)/d;
484
+ }
485
+
486
+ return ocbuf;
487
+ }
488
+
489
+
490
+ pdlc_complex_buffer_t* pdlc_complex_buffer_round_digits_inplace(pdlc_complex_buffer_t *cbuf, int digits)
491
+ {
492
+ const float d = powf(10.0f, (float)digits);
493
+ size_t i;
494
+
495
+ for (i = 0; i < cbuf->length; i++) {
496
+ cbuf->data[i].real = roundf(cbuf->data[i].real*d)/d;
497
+ cbuf->data[i].imag = roundf(cbuf->data[i].imag*d)/d;
498
+ }
499
+
500
+ return cbuf;
501
+ }
502
+
503
+