minor cleanup for smaller insert function
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 567f463..23e9037 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -262,11 +262,11 @@
function is called the float or double must be copied into a
uint64_t. That is how they are passed in. They are then converted to
network byte order correctly. The uMinLen param makes sure that even
- if all the digits of a halft, float or double are 0 it is still correctly
+ if all the digits of a half, float or double are 0 it is still correctly
encoded in 2, 4 or 8 bytes.
*/
-#ifndef FORMALLY_CORRECT_INSERTENCODEDTYPEANDNUMBER
+#ifndef FORMAL_UNOPTIMIZED_INSERTENCODEDTYPEANDNUMBER
/* This code does endian conversion without hton or knowing the
endianness of the machine with masks and shifts. It avoids the
dependency on hton and any mess of figuring out how to find
@@ -277,19 +277,21 @@
because CBOR/network byte order is big endian. However big
endian machines are uncommon.
- It is about 200 bytes instead of 500 bytes of code on x86.
+ On x86, it is about 200 bytes instead of 500 bytes for the
+ more formal unoptimized code.
- This also does the preferred shortest encoding for integers.
+ This also does the CBOR preferred shortest encoding for integers
+ and is called to do endian conversion for floats.
It works backwards from the LSB to the MSB as needed.
Code Reviewers: THIS FUNCTION DOES POINTER MATH
*/
-
+#include <stdio.h>
static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me, uint8_t uMajorType, int nMinLen, uint64_t uNumber, size_t uPos)
{
uint8_t bytes[sizeof(uint64_t)+2];
- uint8_t *pByte = &bytes[sizeof(bytes)];
+ uint8_t *pByte = &bytes[sizeof(bytes)-1];
uint8_t ib;
if(uNumber < CBOR_TWENTY_FOUR && nMinLen == 0) {
@@ -298,9 +300,9 @@
static const uint8_t aIterate[] = {1,1,2,4};
ib = 0;
while(uNumber || nMinLen > 0) {
- const uint8_t x = aIterate[ib];
- nMinLen -= x;
- for (int i = 0; i < x; i++) {
+ const uint8_t uIterations = aIterate[ib];
+ nMinLen -= uIterations;
+ for (int i = 0; i < uIterations; i++) {
*--pByte = uNumber & 0xff;
uNumber = uNumber >> 8;
}
@@ -309,8 +311,8 @@
ib += LEN_IS_ONE_BYTE-1;
}
*--pByte = (uMajorType << 5) + ib;
-
- UsefulOutBuf_InsertData(&(me->OutBuf), pByte, &bytes[sizeof(bytes)] - pByte, uPos);
+
+ UsefulOutBuf_InsertData(&(me->OutBuf), pByte, &bytes[sizeof(bytes)-1] - pByte, uPos);
}
#else
@@ -623,20 +625,23 @@
6 QCBOREncode_AddTag
7 QCBOREncode_AddDouble, QCBOREncode_AddSimple
- Object code sizes on X86 with LLVM compiler and -Os (Nov 27, 2018)
+ Object code sizes on X86 with LLVM compiler and -Os (Dec 13, 2018)
_QCBOREncode_Init 84
_QCBOREncode_AddUInt64 76
_QCBOREncode_AddInt64 87
_QCBOREncode_AddBuffer 131
+ _QCBOREncode_AddTag 27
_QCBOREncode_AddSimple 30
_AppendType7 83
+ _QCBOREncode_AddDouble 36
_QCBOREncode_OpenMapOrArray 89
_QCBOREncode_CloseMapOrArray 181
- _InsertEncodedTypeAndNumber 480
+ _InsertEncodedTypeAndNumber 190
_QCBOREncode_Finish 72
+ _QCBOREncode_FinishGetSize 70
- Total is about 1.4KB (including FinishGetSize and AddTag and AddDouble)
+ Total is about 1.1KB
_QCBOREncode_CloseMapOrArray is larger because it has a lot
of nesting tracking to do and much of Nesting_ inlines
@@ -650,225 +655,5 @@
*/
-/*
- _InsertEncodedTypeAndNumber: ## @InsertEncodedTypeAndNumber
-Lfunc_begin10:
- .loc 7 307 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:307:0
- .cfi_startproc
-## BB#0:
- pushq %rbp
-Lcfi30:
- .cfi_def_cfa_offset 16
-Lcfi31:
- .cfi_offset %rbp, -16
- movq %rsp, %rbp
-Lcfi32:
- subq $80, %rsp
- movb %sil, %al
- movl $4294967295, %esi ## imm = 0xFFFFFFFF
- movl %esi, %r9d
- movq (%r10), %r10
- movq %r10, -8(%rbp)
- movq %rdi, -32(%rbp)
- movb %al, -33(%rbp)
- movq %rdx, -48(%rbp)
- movq %rcx, -56(%rbp)
- movq %r8, -64(%rbp)
-Ltmp50:
- .loc 7 314 15 prologue_end ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:314:15
- movzbl -33(%rbp), %esi
- shll $5, %esi
- movb %sil, %al
- movb %al, -33(%rbp)
-Ltmp51:
- .loc 7 316 15 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:15
- cmpq %r9, -56(%rbp)
- .loc 7 316 28 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:28
- ja LBB10_2
-## BB#1:
- .loc 7 316 39 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:39
- cmpq $8, -48(%rbp)
-Ltmp52:
- .loc 7 316 7 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:7
- jb LBB10_3
-LBB10_2:
-Ltmp53:
- .loc 7 317 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:19
- movzbl -33(%rbp), %eax
- .loc 7 317 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:30
- addl $27, %eax
- .loc 7 317 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:19
- movb %al, %cl
- .loc 7 317 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:16
- movb %cl, -17(%rbp)
- .loc 7 318 17 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:318:17
- movq -56(%rbp), %rdi
- callq __OSSwapInt64
- .loc 7 318 15 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:318:15
- movq %rax, -56(%rbp)
- .loc 7 319 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:319:7
- movq -56(%rbp), %rax
- movq %rax, -16(%rbp)
- .loc 7 320 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:320:16
- movq $9, -72(%rbp)
- .loc 7 322 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:4
- jmp LBB10_15
-Ltmp54:
-LBB10_3:
- .loc 7 322 22 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:22
- cmpq $65535, -56(%rbp) ## imm = 0xFFFF
- .loc 7 322 31 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:31
- ja LBB10_5
-## BB#4:
- .loc 7 322 42 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:42
- cmpq $4, -48(%rbp)
-Ltmp55:
- .loc 7 322 14 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:14
- jb LBB10_6
-LBB10_5:
-Ltmp56:
- .loc 7 323 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:19
- movzbl -33(%rbp), %eax
- .loc 7 323 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:30
- addl $26, %eax
- .loc 7 323 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:19
- movb %al, %cl
- .loc 7 323 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:16
- movb %cl, -17(%rbp)
- .loc 7 324 28 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:324:28
- movq -56(%rbp), %rdx
- movl %edx, %eax
- movl %eax, %edi
- callq __OSSwapInt32
- .loc 7 324 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:324:16
- movl %eax, -76(%rbp)
- .loc 7 325 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:325:7
- movl -76(%rbp), %eax
- movl %eax, -16(%rbp)
- .loc 7 326 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:326:16
- movq $5, -72(%rbp)
- .loc 7 328 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:4
- jmp LBB10_14
-Ltmp57:
-LBB10_6:
- .loc 7 328 23 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:23
- cmpq $255, -56(%rbp)
- .loc 7 328 30 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:30
- ja LBB10_8
-## BB#7:
- .loc 7 328 40 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:40
- cmpq $2, -48(%rbp)
-Ltmp58:
- .loc 7 328 15 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:15
- jb LBB10_9
-LBB10_8:
-Ltmp59:
- .loc 7 330 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:19
- movzbl -33(%rbp), %eax
- .loc 7 330 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:30
- addl $25, %eax
- .loc 7 330 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:19
- movb %al, %cl
- .loc 7 330 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:16
- movb %cl, -17(%rbp)
- .loc 7 331 28 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:331:28
- movq -56(%rbp), %rdx
- movw %dx, %si
- movzwl %si, %edi
- callq __OSSwapInt16
- movzwl %ax, %edi
- movw %di, %ax
- .loc 7 331 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:331:16
- movw %ax, -78(%rbp)
- .loc 7 332 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:332:7
- movw -78(%rbp), %ax
- movw %ax, -16(%rbp)
- .loc 7 333 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:333:16
- movq $3, -72(%rbp)
- .loc 7 335 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:4
- jmp LBB10_13
-Ltmp60:
-LBB10_9:
- .loc 7 335 22 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:22
- cmpq $24, -56(%rbp)
-Ltmp61:
- .loc 7 335 14 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:14
- jb LBB10_11
-## BB#10:
-Ltmp62:
- .loc 7 337 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:19
- movzbl -33(%rbp), %eax
- .loc 7 337 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:30
- addl $24, %eax
- .loc 7 337 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:19
- movb %al, %cl
- .loc 7 337 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:16
- movb %cl, -17(%rbp)
- .loc 7 338 26 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:338:26
- movq -56(%rbp), %rdx
- movb %dl, %cl
- .loc 7 338 15 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:338:15
- movb %cl, -79(%rbp)
- .loc 7 339 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:339:7
- movb -79(%rbp), %cl
- movb %cl, -16(%rbp)
- .loc 7 340 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:340:16
- movq $2, -72(%rbp)
- .loc 7 342 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:342:4
- jmp LBB10_12
-Ltmp63:
-LBB10_11:
- .loc 7 344 18 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:18
- movzbl -33(%rbp), %eax
- .loc 7 344 40 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:40
- movq -56(%rbp), %rcx
- .loc 7 344 31 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:31
- movb %cl, %dl
- movzbl %dl, %esi
- .loc 7 344 29 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:29
- addl %esi, %eax
- .loc 7 344 18 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:18
- movb %al, %dl
- .loc 7 344 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:16
- movb %dl, -17(%rbp)
- .loc 7 345 16 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:345:16
- movq $1, -72(%rbp)
-Ltmp64:
-LBB10_12:
- .loc 7 0 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:0:16
- jmp LBB10_13
-LBB10_13:
- jmp LBB10_14
-LBB10_14:
- jmp LBB10_15
-LBB10_15:
- leaq -17(%rbp), %rsi
- .loc 7 347 30 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:30
- movq -32(%rbp), %rdi
- .loc 7 347 50 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:50
- movq -72(%rbp), %rdx
- .loc 7 347 60 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:60
- movq -64(%rbp), %rcx
- .loc 7 347 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:4
- callq _UsefulOutBuf_InsertData
- movq ___stack_chk_guard@GOTPCREL(%rip), %rcx
- movq (%rcx), %rcx
- movq -8(%rbp), %rdx
- cmpq %rdx, %rcx
- jne LBB10_17
-## BB#16:
- .loc 7 348 1 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:348:1
- addq $80, %rsp
- popq %rbp
- retq
-LBB10_17:
- .loc 7 0 0 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:0:0
- callq ___stack_chk_fail
-Ltmp65:
-Lfunc_end10:
- .cfi_endproc
-
-*/
-