1 /* 2 * libPCM by László Szerémi. 3 * Copyright under Boost License. 4 */ 5 6 module libPCM.utility; 7 8 @nogc package: 9 /** 10 * Converts a floating point value to integer 11 */ 12 int floatToInt(float input){ 13 int result; 14 asm @nogc{ 15 fld input; 16 frndint ; 17 fist result; 18 } 19 return result; 20 } 21 /** 22 * Converts a floating point value to integer 23 */ 24 int doubleToInt(double input){ 25 int result; 26 asm @nogc{ 27 fld input; 28 frndint ; 29 fist result; 30 } 31 return result; 32 } 33 /** 34 * Copies a part of memory. 35 */ 36 void memCpy(void* src, void* dest, size_t length){ 37 if(!length) return; 38 asm @nogc{ 39 mov ECX, length; 40 mov ESI, src; 41 mov EDI, dest; 42 43 cmp ECX, 16; 44 jl copy1byte; 45 46 copy16byte: 47 movups XMM0, [ESI]; 48 movups [EDI], XMM0; 49 add ESI, 16; 50 add EDI, 16; 51 sub ECX, 16; 52 cmp ECX, 16; 53 jge copy16byte; 54 cmp ECX, 0; 55 jz endOfAlgorithm; 56 57 copy1byte: 58 mov AL, [ESI]; 59 mov [EDI], AL; 60 inc ESI; 61 inc EDI; 62 dec ECX; 63 cmp ECX, 0; 64 jnz copy1byte; 65 endOfAlgorithm: 66 ; 67 } 68 } 69 int signExtend(int val, uint bits){ 70 int workPad; 71 uint shift = int.sizeof - bits; 72 *cast(uint*)&workPad = cast(uint)(val<<shift); 73 return workPad; 74 }